|
| 1 | +package canary |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 7 | +) |
| 8 | + |
| 9 | +func TestConfigTracker_ConfigMaps(t *testing.T) { |
| 10 | + mocks := newFixture() |
| 11 | + configMap := newTestConfigMap() |
| 12 | + configMapProjected := newTestConfigProjected() |
| 13 | + |
| 14 | + err := mocks.deployer.Initialize(mocks.canary, true) |
| 15 | + if err != nil { |
| 16 | + t.Fatal(err.Error()) |
| 17 | + } |
| 18 | + |
| 19 | + depPrimary, err := mocks.kubeClient.AppsV1().Deployments("default").Get("podinfo-primary", metav1.GetOptions{}) |
| 20 | + if err != nil { |
| 21 | + t.Fatal(err.Error()) |
| 22 | + } |
| 23 | + |
| 24 | + configPrimaryVolName := depPrimary.Spec.Template.Spec.Volumes[0].VolumeSource.ConfigMap.LocalObjectReference.Name |
| 25 | + if configPrimaryVolName != "podinfo-config-vol-primary" { |
| 26 | + t.Errorf("Got config name %v wanted %v", configPrimaryVolName, "podinfo-config-vol-primary") |
| 27 | + } |
| 28 | + |
| 29 | + configPrimary, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get("podinfo-config-env-primary", metav1.GetOptions{}) |
| 30 | + if err != nil { |
| 31 | + t.Fatal(err.Error()) |
| 32 | + } |
| 33 | + |
| 34 | + if configPrimary.Data["color"] != configMap.Data["color"] { |
| 35 | + t.Errorf("Got ConfigMap color %s wanted %s", configPrimary.Data["color"], configMap.Data["color"]) |
| 36 | + } |
| 37 | + |
| 38 | + configPrimaryEnv, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get("podinfo-config-all-env-primary", metav1.GetOptions{}) |
| 39 | + if err != nil { |
| 40 | + t.Fatal(err.Error()) |
| 41 | + } |
| 42 | + |
| 43 | + if configPrimaryEnv.Data["color"] != configMap.Data["color"] { |
| 44 | + t.Errorf("Got ConfigMap %s wanted %s", configPrimaryEnv.Data["a"], configMap.Data["color"]) |
| 45 | + } |
| 46 | + |
| 47 | + configPrimaryVol, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get("podinfo-config-vol-primary", metav1.GetOptions{}) |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err.Error()) |
| 50 | + } |
| 51 | + |
| 52 | + if configPrimaryVol.Data["color"] != configMap.Data["color"] { |
| 53 | + t.Errorf("Got ConfigMap color %s wanted %s", configPrimary.Data["color"], configMap.Data["color"]) |
| 54 | + } |
| 55 | + |
| 56 | + configProjectedName := depPrimary.Spec.Template.Spec.Volumes[2].VolumeSource.Projected.Sources[0].ConfigMap.Name |
| 57 | + if configProjectedName != "podinfo-config-projected-primary" { |
| 58 | + t.Errorf("Got config name %v wanted %v", configProjectedName, "podinfo-config-projected-primary") |
| 59 | + } |
| 60 | + |
| 61 | + configPrimaryProjected, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get("podinfo-config-vol-primary", metav1.GetOptions{}) |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err.Error()) |
| 64 | + } |
| 65 | + |
| 66 | + if configPrimaryProjected.Data["color"] != configMapProjected.Data["color"] { |
| 67 | + t.Errorf("Got ConfigMap color %s wanted %s", configPrimaryProjected.Data["color"], configMapProjected.Data["color"]) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestConfigTracker_Secrets(t *testing.T) { |
| 72 | + mocks := newFixture() |
| 73 | + secret := newTestSecret() |
| 74 | + secretProjected := newTestSecretProjected() |
| 75 | + |
| 76 | + err := mocks.deployer.Initialize(mocks.canary, true) |
| 77 | + if err != nil { |
| 78 | + t.Fatal(err.Error()) |
| 79 | + } |
| 80 | + |
| 81 | + depPrimary, err := mocks.kubeClient.AppsV1().Deployments("default").Get("podinfo-primary", metav1.GetOptions{}) |
| 82 | + if err != nil { |
| 83 | + t.Fatal(err.Error()) |
| 84 | + } |
| 85 | + |
| 86 | + secretPrimaryVolName := depPrimary.Spec.Template.Spec.Volumes[1].VolumeSource.Secret.SecretName |
| 87 | + if secretPrimaryVolName != "podinfo-secret-vol-primary" { |
| 88 | + t.Errorf("Got config name %v wanted %v", secretPrimaryVolName, "podinfo-secret-vol-primary") |
| 89 | + } |
| 90 | + |
| 91 | + secretPrimary, err := mocks.kubeClient.CoreV1().Secrets("default").Get("podinfo-secret-env-primary", metav1.GetOptions{}) |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err.Error()) |
| 94 | + } |
| 95 | + |
| 96 | + if string(secretPrimary.Data["apiKey"]) != string(secret.Data["apiKey"]) { |
| 97 | + t.Errorf("Got primary secret %s wanted %s", secretPrimary.Data["apiKey"], secret.Data["apiKey"]) |
| 98 | + } |
| 99 | + |
| 100 | + secretPrimaryEnv, err := mocks.kubeClient.CoreV1().Secrets("default").Get("podinfo-secret-all-env-primary", metav1.GetOptions{}) |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err.Error()) |
| 103 | + } |
| 104 | + |
| 105 | + if string(secretPrimaryEnv.Data["apiKey"]) != string(secret.Data["apiKey"]) { |
| 106 | + t.Errorf("Got primary secret %s wanted %s", secretPrimary.Data["apiKey"], secret.Data["apiKey"]) |
| 107 | + } |
| 108 | + |
| 109 | + secretPrimaryVol, err := mocks.kubeClient.CoreV1().Secrets("default").Get("podinfo-secret-vol-primary", metav1.GetOptions{}) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err.Error()) |
| 112 | + } |
| 113 | + |
| 114 | + if string(secretPrimaryVol.Data["apiKey"]) != string(secret.Data["apiKey"]) { |
| 115 | + t.Errorf("Got primary secret %s wanted %s", secretPrimary.Data["apiKey"], secret.Data["apiKey"]) |
| 116 | + } |
| 117 | + |
| 118 | + secretProjectedName := depPrimary.Spec.Template.Spec.Volumes[2].VolumeSource.Projected.Sources[1].Secret.Name |
| 119 | + if secretProjectedName != "podinfo-secret-projected-primary" { |
| 120 | + t.Errorf("Got config name %v wanted %v", secretProjectedName, "podinfo-secret-projected-primary") |
| 121 | + } |
| 122 | + |
| 123 | + secretPrimaryProjected, err := mocks.kubeClient.CoreV1().Secrets("default").Get("podinfo-secret-projected-primary", metav1.GetOptions{}) |
| 124 | + if err != nil { |
| 125 | + t.Fatal(err.Error()) |
| 126 | + } |
| 127 | + |
| 128 | + if string(secretPrimaryProjected.Data["apiKey"]) != string(secretProjected.Data["apiKey"]) { |
| 129 | + t.Errorf("Got primary secret %s wanted %s", secretPrimaryProjected.Data["apiKey"], secretProjected.Data["apiKey"]) |
| 130 | + } |
| 131 | +} |
0 commit comments