|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/gruntwork-io/terratest/modules/helm" |
| 14 | + "github.com/gruntwork-io/terratest/modules/k8s" |
| 15 | + "github.com/gruntwork-io/terratest/modules/random" |
| 16 | + "github.com/imroc/req/v3" |
| 17 | + "github.com/marklogic/marklogic-kubernetes/test/testUtil" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/tidwall/gjson" |
| 20 | + digestAuth "github.com/xinsnake/go-http-digest-auth-client" |
| 21 | +) |
| 22 | + |
| 23 | +func VerifyGrpNameChng(t *testing.T, groupEndpoint string, newGroupName string) (int, error) { |
| 24 | + client := req.C(). |
| 25 | + SetCommonDigestAuth("admin", "admin"). |
| 26 | + SetCommonRetryCount(10). |
| 27 | + SetCommonRetryFixedInterval(10 * time.Second) |
| 28 | + |
| 29 | + t.Logf(`Endpoint: %s`, groupEndpoint) |
| 30 | + strJSONData := fmt.Sprintf(`{"group-name":"%s"}`, newGroupName) |
| 31 | + |
| 32 | + resp, err := client.R(). |
| 33 | + SetContentType("application/json"). |
| 34 | + SetBodyJsonString(strJSONData). |
| 35 | + Put(groupEndpoint) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err.Error()) |
| 38 | + return (resp.GetStatusCode()), err |
| 39 | + } |
| 40 | + return resp.GetStatusCode(), resp.Err |
| 41 | +} |
| 42 | + |
| 43 | +func TestSingleGrpCfgChng(t *testing.T) { |
| 44 | + // Path to the helm chart we will test |
| 45 | + helmChartPath, e := filepath.Abs("../../charts") |
| 46 | + if e != nil { |
| 47 | + t.Fatalf(e.Error()) |
| 48 | + } |
| 49 | + username := "admin" |
| 50 | + password := "admin" |
| 51 | + releaseName := "test-grp" |
| 52 | + groupName := "Default" |
| 53 | + imageRepo, repoPres := os.LookupEnv("dockerRepository") |
| 54 | + imageTag, tagPres := os.LookupEnv("dockerVersion") |
| 55 | + |
| 56 | + if !repoPres { |
| 57 | + imageRepo = "progressofficial/marklogic-db" |
| 58 | + t.Logf("No imageRepo variable present, setting to default value: " + imageRepo) |
| 59 | + } |
| 60 | + |
| 61 | + if !tagPres { |
| 62 | + imageTag = "latest-11" |
| 63 | + t.Logf("No imageTag variable present, setting to default value: " + imageTag) |
| 64 | + } |
| 65 | + |
| 66 | + namespaceName := "ml-" + strings.ToLower(random.UniqueId()) |
| 67 | + kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName) |
| 68 | + options := &helm.Options{ |
| 69 | + KubectlOptions: kubectlOptions, |
| 70 | + SetValues: map[string]string{ |
| 71 | + "persistence.enabled": "true", |
| 72 | + "replicaCount": "1", |
| 73 | + "image.repository": imageRepo, |
| 74 | + "image.tag": imageTag, |
| 75 | + "auth.adminUsername": username, |
| 76 | + "auth.adminPassword": password, |
| 77 | + "logCollection.enabled": "false", |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + t.Logf("====Creating namespace: " + namespaceName) |
| 82 | + k8s.CreateNamespace(t, kubectlOptions, namespaceName) |
| 83 | + |
| 84 | + defer t.Logf("====Deleting namespace: " + namespaceName) |
| 85 | + defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName) |
| 86 | + |
| 87 | + t.Logf("====Setting helm chart path to %s", helmChartPath) |
| 88 | + t.Logf("====Installing Helm Chart") |
| 89 | + podZeroName := testUtil.HelmInstall(t, options, releaseName, kubectlOptions, helmChartPath) |
| 90 | + |
| 91 | + // wait until the pod is in Ready status |
| 92 | + k8s.WaitUntilPodAvailable(t, kubectlOptions, podZeroName, 15, 20*time.Second) |
| 93 | + tunnel := k8s.NewTunnel( |
| 94 | + kubectlOptions, k8s.ResourceTypePod, podZeroName, 8002, 8002) |
| 95 | + defer tunnel.Close() |
| 96 | + tunnel.ForwardPort(t) |
| 97 | + |
| 98 | + // change the group name for dnode and verify it passes |
| 99 | + newgroupName := "newDefault" |
| 100 | + t.Logf("====Test updating group name for %s to %s", groupName, newgroupName) |
| 101 | + groupEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), groupName) |
| 102 | + responseCode, err := VerifyGrpNameChng(t, groupEndpoint, newgroupName) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf(err.Error()) |
| 105 | + } |
| 106 | + if responseCode != 204 { |
| 107 | + t.Fatal("Failed to change group name") |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestMultiGroupCfgChng(t *testing.T) { |
| 112 | + username := "admin" |
| 113 | + password := "admin" |
| 114 | + imageRepo, repoPres := os.LookupEnv("dockerRepository") |
| 115 | + imageTag, tagPres := os.LookupEnv("dockerVersion") |
| 116 | + var initialChartVersion string |
| 117 | + upgradeHelm, _ := os.LookupEnv("upgradeTest") |
| 118 | + runUpgradeTest, _ := strconv.ParseBool(upgradeHelm) |
| 119 | + if runUpgradeTest { |
| 120 | + initialChartVersion, _ = os.LookupEnv("initialChartVersion") |
| 121 | + t.Logf("====Setting initial Helm chart version: %s", initialChartVersion) |
| 122 | + } |
| 123 | + namespaceName := "ml-" + strings.ToLower(random.UniqueId()) |
| 124 | + kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName) |
| 125 | + dnodeGrpName := "dnode" |
| 126 | + enodeGrpName := "enode" |
| 127 | + dnodeReleaseName := "dnode" |
| 128 | + enodeReleaseName := "enode" |
| 129 | + dnodePodName := dnodeReleaseName + "-0" |
| 130 | + enodePodName0 := enodeReleaseName + "-0" |
| 131 | + |
| 132 | + // Path to the helm chart we will test |
| 133 | + helmChartPath, e := filepath.Abs("../../charts") |
| 134 | + if e != nil { |
| 135 | + t.Fatalf(e.Error()) |
| 136 | + } |
| 137 | + |
| 138 | + if !repoPres { |
| 139 | + imageRepo = "progressofficial/marklogic-db" |
| 140 | + t.Logf("No imageRepo variable present, setting to default value: " + imageRepo) |
| 141 | + } |
| 142 | + |
| 143 | + if !tagPres { |
| 144 | + imageTag = "latest-11" |
| 145 | + t.Logf("No imageTag variable present, setting to default value: " + imageTag) |
| 146 | + } |
| 147 | + |
| 148 | + options := &helm.Options{ |
| 149 | + KubectlOptions: kubectlOptions, |
| 150 | + SetValues: map[string]string{ |
| 151 | + "persistence.enabled": "true", |
| 152 | + "replicaCount": "1", |
| 153 | + "image.repository": imageRepo, |
| 154 | + "image.tag": imageTag, |
| 155 | + "auth.adminUsername": username, |
| 156 | + "auth.adminPassword": password, |
| 157 | + "group.name": dnodeGrpName, |
| 158 | + "logCollection.enabled": "false", |
| 159 | + }, |
| 160 | + Version: initialChartVersion, |
| 161 | + } |
| 162 | + |
| 163 | + t.Logf("====Creating namespace: " + namespaceName) |
| 164 | + k8s.CreateNamespace(t, kubectlOptions, namespaceName) |
| 165 | + |
| 166 | + defer t.Logf("====Deleting namespace: " + namespaceName) |
| 167 | + defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName) |
| 168 | + |
| 169 | + t.Logf("====Setting helm chart path to %s", helmChartPath) |
| 170 | + t.Logf("====Installing Helm Chart " + dnodeReleaseName) |
| 171 | + dnodePodName = testUtil.HelmInstall(t, options, dnodeReleaseName, kubectlOptions, helmChartPath) |
| 172 | + |
| 173 | + // wait until the pod is in ready status |
| 174 | + k8s.WaitUntilPodAvailable(t, kubectlOptions, dnodePodName, 15, 20*time.Second) |
| 175 | + tunnel := k8s.NewTunnel( |
| 176 | + kubectlOptions, k8s.ResourceTypePod, dnodePodName, 8002, 8002) |
| 177 | + defer tunnel.Close() |
| 178 | + tunnel.ForwardPort(t) |
| 179 | + |
| 180 | + // change the group name for dnode and verify it passes |
| 181 | + newDnodeGrpName := "newDnode" |
| 182 | + t.Logf("====Test updating group name for %s to %s", dnodeGrpName, newDnodeGrpName) |
| 183 | + groupEndpoint := fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), dnodeGrpName) |
| 184 | + responseCode, err := VerifyGrpNameChng(t, groupEndpoint, newDnodeGrpName) |
| 185 | + if err != nil { |
| 186 | + t.Fatalf(err.Error()) |
| 187 | + } |
| 188 | + if responseCode != 204 { |
| 189 | + t.Fatal("Failed to change group name") |
| 190 | + } |
| 191 | + |
| 192 | + hostsEndpoint := fmt.Sprintf("http://%s/manage/v2/hosts?format=json", tunnel.Endpoint()) |
| 193 | + t.Logf(`Endpoint: %s`, hostsEndpoint) |
| 194 | + |
| 195 | + getHostsDR := digestAuth.NewRequest(username, password, "GET", hostsEndpoint, "") |
| 196 | + |
| 197 | + resp, err := getHostsDR.Execute() |
| 198 | + if err != nil { |
| 199 | + t.Fatalf(err.Error()) |
| 200 | + } |
| 201 | + defer resp.Body.Close() |
| 202 | + body, err := io.ReadAll(resp.Body) |
| 203 | + if err != nil { |
| 204 | + t.Fatalf(err.Error()) |
| 205 | + } |
| 206 | + bootstrapHost := gjson.Get(string(body), `host-default-list.list-items.list-item.#(roleref="bootstrap").nameref`) |
| 207 | + t.Logf("bootstrapHost: %s", bootstrapHost) |
| 208 | + |
| 209 | + enodeOptions := &helm.Options{ |
| 210 | + KubectlOptions: kubectlOptions, |
| 211 | + SetValues: map[string]string{ |
| 212 | + "persistence.enabled": "true", |
| 213 | + "replicaCount": "2", |
| 214 | + "image.repository": imageRepo, |
| 215 | + "image.tag": imageTag, |
| 216 | + "auth.adminUsername": username, |
| 217 | + "auth.adminPassword": password, |
| 218 | + "group.name": enodeGrpName, |
| 219 | + "bootstrapHostName": bootstrapHost.Str, |
| 220 | + "group.enableXdqpSsl": "false", |
| 221 | + "logCollection.enabled": "false", |
| 222 | + }, |
| 223 | + } |
| 224 | + t.Logf("====Installing Helm Chart " + enodeReleaseName) |
| 225 | + enodePodName0 = testUtil.HelmInstall(t, enodeOptions, enodeReleaseName, kubectlOptions, helmChartPath) |
| 226 | + |
| 227 | + // wait until the first enode pod is in Ready status |
| 228 | + k8s.WaitUntilPodAvailable(t, kubectlOptions, enodePodName0, 15, 20*time.Second) |
| 229 | + |
| 230 | + // change the enode group name to a existing group name in the cluster and verify it fails |
| 231 | + t.Logf("====Test updating group name for %s to an existing group name(%s) should fail", enodeGrpName, newDnodeGrpName) |
| 232 | + groupEndpoint = fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), enodeGrpName) |
| 233 | + responseCode, err = VerifyGrpNameChng(t, groupEndpoint, newDnodeGrpName) |
| 234 | + if err != nil { |
| 235 | + t.Fatalf(err.Error()) |
| 236 | + } |
| 237 | + assert.Equal(t, 400, responseCode) |
| 238 | + |
| 239 | + // change the enode group name to a new group name and verify it passes |
| 240 | + newEnodeGrpName := "newEnode" |
| 241 | + t.Logf("====Test updating group name for %s to %s", enodeGrpName, newEnodeGrpName) |
| 242 | + groupEndpoint = fmt.Sprintf("http://%s/manage/v2/groups/%s/properties", tunnel.Endpoint(), enodeGrpName) |
| 243 | + responseCode, err = VerifyGrpNameChng(t, groupEndpoint, newEnodeGrpName) |
| 244 | + if err != nil { |
| 245 | + t.Fatalf(err.Error()) |
| 246 | + } |
| 247 | + if responseCode != 204 { |
| 248 | + t.Fatal("Failed to change group name") |
| 249 | + } |
| 250 | +} |
0 commit comments