|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gruntwork-io/terratest/modules/helm" |
| 13 | + "github.com/gruntwork-io/terratest/modules/k8s" |
| 14 | + "github.com/gruntwork-io/terratest/modules/random" |
| 15 | + "github.com/tidwall/gjson" |
| 16 | + digestAuth "github.com/xinsnake/go-http-digest-auth-client" |
| 17 | +) |
| 18 | + |
| 19 | +func TestHelmScaleUp(t *testing.T) { |
| 20 | + // Path to the helm chart we will test |
| 21 | + helmChartPath, e := filepath.Abs("../../charts") |
| 22 | + if e != nil { |
| 23 | + t.Fatalf(e.Error()) |
| 24 | + } |
| 25 | + imageRepo, repoPres := os.LookupEnv("dockerRepository") |
| 26 | + imageTag, tagPres := os.LookupEnv("dockerVersion") |
| 27 | + username := "admin" |
| 28 | + password := "admin" |
| 29 | + |
| 30 | + if !repoPres { |
| 31 | + imageRepo = "marklogicdb/marklogic-db" |
| 32 | + t.Logf("No imageRepo variable present, setting to default value: " + imageRepo) |
| 33 | + } |
| 34 | + |
| 35 | + if !tagPres { |
| 36 | + imageTag = "latest" |
| 37 | + t.Logf("No imageTag variable present, setting to default value: " + imageTag) |
| 38 | + } |
| 39 | + |
| 40 | + namespaceName := "marklogic-" + strings.ToLower(random.UniqueId()) |
| 41 | + kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName) |
| 42 | + options := &helm.Options{ |
| 43 | + KubectlOptions: kubectlOptions, |
| 44 | + SetValues: map[string]string{ |
| 45 | + "persistence.enabled": "false", |
| 46 | + "replicaCount": "1", |
| 47 | + "image.repository": imageRepo, |
| 48 | + "image.tag": imageTag, |
| 49 | + "auth.adminUsername": username, |
| 50 | + "auth.adminPassword": password, |
| 51 | + "logCollection.enabled": "false", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + t.Logf("====Creating namespace: " + namespaceName) |
| 56 | + k8s.CreateNamespace(t, kubectlOptions, namespaceName) |
| 57 | + defer t.Logf("====Deleting namespace: " + namespaceName) |
| 58 | + defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName) |
| 59 | + |
| 60 | + t.Logf("====Installing Helm Chart") |
| 61 | + releaseName := "test-scale-up" |
| 62 | + helm.Install(t, options, helmChartPath, releaseName) |
| 63 | + |
| 64 | + newOptions := &helm.Options{ |
| 65 | + KubectlOptions: kubectlOptions, |
| 66 | + SetValues: map[string]string{ |
| 67 | + "persistence.enabled": "false", |
| 68 | + "replicaCount": "2", |
| 69 | + "image.repository": imageRepo, |
| 70 | + "image.tag": imageTag, |
| 71 | + "logCollection.enabled": "false", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + t.Logf("====Upgrading Helm Chart") |
| 76 | + helm.Upgrade(t, newOptions, helmChartPath, releaseName) |
| 77 | + |
| 78 | + podName := releaseName + "-marklogic-1" |
| 79 | + |
| 80 | + // wait until second pod is in Ready status |
| 81 | + k8s.WaitUntilPodAvailable(t, kubectlOptions, podName, 10, 20*time.Second) |
| 82 | + |
| 83 | + tunnel := k8s.NewTunnel( |
| 84 | + kubectlOptions, k8s.ResourceTypePod, podName, 8002, 8002) |
| 85 | + defer tunnel.Close() |
| 86 | + tunnel.ForwardPort(t) |
| 87 | + |
| 88 | + hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?view=status&format=json", tunnel.Endpoint()) |
| 89 | + t.Logf(`Endpoint: %s`, hostsEndpoint) |
| 90 | + |
| 91 | + getHostsDR := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "") |
| 92 | + |
| 93 | + resp, err := getHostsDR.Execute() |
| 94 | + if err != nil { |
| 95 | + t.Fatalf(err.Error()) |
| 96 | + } |
| 97 | + defer resp.Body.Close() |
| 98 | + body, err := ioutil.ReadAll(resp.Body) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf(err.Error()) |
| 101 | + } |
| 102 | + totalHosts := gjson.Get(string(body), `host-status-list.status-list-summary.total-hosts.value`) |
| 103 | + |
| 104 | + // verify total number of hosts on the clsuter after scaling up |
| 105 | + if totalHosts.Num != 2 { |
| 106 | + t.Errorf("Incorrect number of MarkLogic hosts") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestHelmScaleDown(t *testing.T) { |
| 111 | + // Path to the helm chart we will test |
| 112 | + helmChartPath, e := filepath.Abs("../../charts") |
| 113 | + if e != nil { |
| 114 | + t.Fatalf(e.Error()) |
| 115 | + } |
| 116 | + imageRepo, repoPres := os.LookupEnv("dockerRepository") |
| 117 | + imageTag, tagPres := os.LookupEnv("dockerVersion") |
| 118 | + username := "admin" |
| 119 | + password := "admin" |
| 120 | + |
| 121 | + if !repoPres { |
| 122 | + imageRepo = "marklogicdb/marklogic-db" |
| 123 | + t.Logf("No imageRepo variable present, setting to default value: " + imageRepo) |
| 124 | + } |
| 125 | + |
| 126 | + if !tagPres { |
| 127 | + imageTag = "latest" |
| 128 | + t.Logf("No imageTag variable present, setting to default value: " + imageTag) |
| 129 | + } |
| 130 | + |
| 131 | + namespaceName := "marklogic-" + strings.ToLower(random.UniqueId()) |
| 132 | + kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName) |
| 133 | + options := &helm.Options{ |
| 134 | + KubectlOptions: kubectlOptions, |
| 135 | + SetValues: map[string]string{ |
| 136 | + "persistence.enabled": "false", |
| 137 | + "replicaCount": "2", |
| 138 | + "image.repository": imageRepo, |
| 139 | + "image.tag": imageTag, |
| 140 | + "auth.adminUsername": username, |
| 141 | + "auth.adminPassword": password, |
| 142 | + "logCollection.enabled": "false", |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + t.Logf("====Creating namespace: " + namespaceName) |
| 147 | + k8s.CreateNamespace(t, kubectlOptions, namespaceName) |
| 148 | + defer t.Logf("====Deleting namespace: " + namespaceName) |
| 149 | + defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName) |
| 150 | + |
| 151 | + t.Logf("====Installing Helm Chart") |
| 152 | + releaseName := "test-scale-down" |
| 153 | + helm.Install(t, options, helmChartPath, releaseName) |
| 154 | + |
| 155 | + podName1 := releaseName + "-marklogic-1" |
| 156 | + |
| 157 | + // wait until the pod is in Ready status |
| 158 | + k8s.WaitUntilPodAvailable(t, kubectlOptions, podName1, 10, 20*time.Second) |
| 159 | + |
| 160 | + newOptions := &helm.Options{ |
| 161 | + KubectlOptions: kubectlOptions, |
| 162 | + SetValues: map[string]string{ |
| 163 | + "persistence.enabled": "false", |
| 164 | + "replicaCount": "1", |
| 165 | + "image.repository": imageRepo, |
| 166 | + "image.tag": imageTag, |
| 167 | + "logCollection.enabled": "false", |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + t.Logf("====Upgrading Helm Chart") |
| 172 | + helm.Upgrade(t, newOptions, helmChartPath, releaseName) |
| 173 | + |
| 174 | + time.Sleep(20 * time.Second) |
| 175 | + |
| 176 | + podName0 := releaseName + "-marklogic-0" |
| 177 | + |
| 178 | + tunnel := k8s.NewTunnel( |
| 179 | + kubectlOptions, k8s.ResourceTypePod, podName0, 8002, 8002) |
| 180 | + defer tunnel.Close() |
| 181 | + tunnel.ForwardPort(t) |
| 182 | + |
| 183 | + hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?view=status&format=json", tunnel.Endpoint()) |
| 184 | + t.Logf(`Endpoint: %s`, hostsEndpoint) |
| 185 | + |
| 186 | + getHostsDR := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "") |
| 187 | + |
| 188 | + resp, err := getHostsDR.Execute() |
| 189 | + if err != nil { |
| 190 | + t.Fatalf(err.Error()) |
| 191 | + } |
| 192 | + defer resp.Body.Close() |
| 193 | + body, err := ioutil.ReadAll(resp.Body) |
| 194 | + if err != nil { |
| 195 | + t.Fatalf(err.Error()) |
| 196 | + } |
| 197 | + |
| 198 | + totalHostsOffline := gjson.Get(string(body), `host-status-list.status-list-summary.total-hosts-offline.value`) |
| 199 | + |
| 200 | + //verify there is a offline host after scaling down |
| 201 | + if totalHostsOffline.Num != 1 { |
| 202 | + t.Errorf("Incorrect number of offline hosts") |
| 203 | + } |
| 204 | +} |
0 commit comments