From 9771d689ca4f2d12a2c551a8c3b52433398e4c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Mert=20Y=C4=B1ld=C4=B1ran?= Date: Sun, 9 Jan 2022 17:38:41 +0300 Subject: [PATCH 1/2] Fix the acceptance tests and a typo in `CONFIGURATION.md` (#610) * Enable acceptance tests * Fix the acceptance tests and a typo in `CONFIGURATION.md` * Include the container name into the log fetching function * Duplicate the fix for the logs test * Revert "Enable acceptance tests" This reverts commit c10a67c29305468d9022404722a416a6c88ddcea. --- acceptanceTests/logs_test.go | 14 ++++++++++++-- acceptanceTests/tap_test.go | 7 ++++++- acceptanceTests/testsUtils.go | 2 +- cli/mizu/fsUtils/mizuLogsUtils.go | 24 +++++++++++++----------- docs/CONFIGURATION.md | 2 +- shared/kubernetes/provider.go | 4 ++-- 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/acceptanceTests/logs_test.go b/acceptanceTests/logs_test.go index 5508fdb11..d2808da97 100644 --- a/acceptanceTests/logs_test.go +++ b/acceptanceTests/logs_test.go @@ -81,11 +81,16 @@ func TestLogs(t *testing.T) { logsFileNames = append(logsFileNames, file.Name) } - if !Contains(logsFileNames, "mizu.mizu-api-server.log") { + if !Contains(logsFileNames, "mizu.mizu-api-server.mizu-api-server.log") { t.Errorf("api server logs not found") return } + if !Contains(logsFileNames, "mizu.mizu-api-server.basenine.log") { + t.Errorf("basenine logs not found") + return + } + if !Contains(logsFileNames, "mizu_cli.log") { t.Errorf("cli logs not found") return @@ -174,11 +179,16 @@ func TestLogsPath(t *testing.T) { logsFileNames = append(logsFileNames, file.Name) } - if !Contains(logsFileNames, "mizu.mizu-api-server.log") { + if !Contains(logsFileNames, "mizu.mizu-api-server.mizu-api-server.log") { t.Errorf("api server logs not found") return } + if !Contains(logsFileNames, "mizu.mizu-api-server.basenine.log") { + t.Errorf("basenine logs not found") + return + } + if !Contains(logsFileNames, "mizu_cli.log") { t.Errorf("cli logs not found") return diff --git a/acceptanceTests/tap_test.go b/acceptanceTests/tap_test.go index 52a9410fa..4a4606046 100644 --- a/acceptanceTests/tap_test.go +++ b/acceptanceTests/tap_test.go @@ -862,11 +862,16 @@ func TestTapDumpLogs(t *testing.T) { logsFileNames = append(logsFileNames, file.Name) } - if !Contains(logsFileNames, "mizu.mizu-api-server.log") { + if !Contains(logsFileNames, "mizu.mizu-api-server.mizu-api-server.log") { t.Errorf("api server logs not found") return } + if !Contains(logsFileNames, "mizu.mizu-api-server.basenine.log") { + t.Errorf("basenine logs not found") + return + } + if !Contains(logsFileNames, "mizu_cli.log") { t.Errorf("cli logs not found") return diff --git a/acceptanceTests/testsUtils.go b/acceptanceTests/testsUtils.go index 2a2701d28..58f4d69bd 100644 --- a/acceptanceTests/testsUtils.go +++ b/acceptanceTests/testsUtils.go @@ -92,7 +92,7 @@ func getDefaultCommandArgs() []string { setFlag := "--set" telemetry := "telemetry=false" agentImage := "agent-image=gcr.io/up9-docker-hub/mizu/ci:0.0.0" - imagePullPolicy := "image-pull-policy=Never" + imagePullPolicy := "image-pull-policy=IfNotPresent" headless := "headless=true" return []string{setFlag, telemetry, setFlag, agentImage, setFlag, imagePullPolicy, setFlag, headless} diff --git a/cli/mizu/fsUtils/mizuLogsUtils.go b/cli/mizu/fsUtils/mizuLogsUtils.go index fe79936cc..d43cfe5b6 100644 --- a/cli/mizu/fsUtils/mizuLogsUtils.go +++ b/cli/mizu/fsUtils/mizuLogsUtils.go @@ -38,18 +38,20 @@ func DumpLogs(ctx context.Context, provider *kubernetes.Provider, filePath strin defer zipWriter.Close() for _, pod := range pods { - logs, err := provider.GetPodLogs(ctx, pod.Namespace, pod.Name) - if err != nil { - logger.Log.Errorf("Failed to get logs, %v", err) - continue - } else { - logger.Log.Debugf("Successfully read log length %d for pod: %s.%s", len(logs), pod.Namespace, pod.Name) - } + for _, container := range pod.Spec.Containers { + logs, err := provider.GetPodLogs(ctx, pod.Namespace, pod.Name, container.Name) + if err != nil { + logger.Log.Errorf("Failed to get logs, %v", err) + continue + } else { + logger.Log.Debugf("Successfully read log length %d for pod: %s.%s.%s", len(logs), pod.Namespace, pod.Name, container.Name) + } - if err := AddStrToZip(zipWriter, logs, fmt.Sprintf("%s.%s.log", pod.Namespace, pod.Name)); err != nil { - logger.Log.Errorf("Failed write logs, %v", err) - } else { - logger.Log.Debugf("Successfully added log length %d from pod: %s.%s", len(logs), pod.Namespace, pod.Name) + if err := AddStrToZip(zipWriter, logs, fmt.Sprintf("%s.%s.%s.log", pod.Namespace, pod.Name, container.Name)); err != nil { + logger.Log.Errorf("Failed write logs, %v", err) + } else { + logger.Log.Debugf("Successfully added log length %d from pod: %s.%s.%s", len(logs), pod.Namespace, pod.Name, container.Name) + } } } diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 50609f4ab..7282bc141 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -25,7 +25,7 @@ Please make sure to use full option name (`tap.dry-run` as opposed to `dry-run` * `dump-logs` - if set to `true`, saves log files for all Mizu components (tapper, api-server, CLI) in a zip file under `$HOME/.mizu`. Default value is `false` -* `image-pull-policy` - container image pull policy for Kubernetes, default value `Always`. Other accepted values are `Never` or `IfNotExist`. Please mind the implications when changing this. +* `image-pull-policy` - container image pull policy for Kubernetes, default value `Always`. Other accepted values are `Never` or `IfNotPresent`. Please mind the implications when changing this. * `kube-config-path` - path to alternative kubeconfig file to use for all interactions with Kubernetes cluster. By default - `$HOME/.kubeconfig` diff --git a/shared/kubernetes/provider.go b/shared/kubernetes/provider.go index ffdcc6e4f..1d5615b6c 100644 --- a/shared/kubernetes/provider.go +++ b/shared/kubernetes/provider.go @@ -943,8 +943,8 @@ func (provider *Provider) ListAllNamespaces(ctx context.Context) ([]core.Namespa return namespaces.Items, err } -func (provider *Provider) GetPodLogs(ctx context.Context, namespace string, podName string) (string, error) { - podLogOpts := core.PodLogOptions{} +func (provider *Provider) GetPodLogs(ctx context.Context, namespace string, podName string, containerName string) (string, error) { + podLogOpts := core.PodLogOptions{Container: containerName} req := provider.clientSet.CoreV1().Pods(namespace).GetLogs(podName, &podLogOpts) podLogs, err := req.Stream(ctx) if err != nil { From a2eff2654d4fb2ae578a0086c17f6c1cb82540cf Mon Sep 17 00:00:00 2001 From: Adam Kol <93466081+AdamKol-up9@users.noreply.github.com> Date: Sun, 9 Jan 2022 16:47:35 +0200 Subject: [PATCH 2/2] Cypress: new 'Multiple Namespaces' test (#600) --- acceptanceTests/cypress.json | 7 +- .../integration/tests/MultipleNamespaces.js | 67 +++++++++++++++++++ acceptanceTests/tap_test.go | 48 ++----------- 3 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 acceptanceTests/cypress/integration/tests/MultipleNamespaces.js diff --git a/acceptanceTests/cypress.json b/acceptanceTests/cypress.json index 21abdfe9d..214245c52 100644 --- a/acceptanceTests/cypress.json +++ b/acceptanceTests/cypress.json @@ -4,5 +4,10 @@ "viewportHeight": 1080, "video": false, "screenshotOnRunFailure": false, - "testFiles": ["tests/GuiPort.js"] + "testFiles": + ["tests/GuiPort.js", + "tests/MultipleNamespaces.js"], + "env": { + "testUrl": "http://localhost:8899/" + } } diff --git a/acceptanceTests/cypress/integration/tests/MultipleNamespaces.js b/acceptanceTests/cypress/integration/tests/MultipleNamespaces.js new file mode 100644 index 000000000..ea67b428f --- /dev/null +++ b/acceptanceTests/cypress/integration/tests/MultipleNamespaces.js @@ -0,0 +1,67 @@ +const columns = {"podName" : 1, "namespace" : 2, "tapping" : 3} +const greenStatusImageSrc = "/static/media/success.662997eb.svg" + +it('opening', function () { + cy.visit(Cypress.env('testUrl')) + cy.get('.podsCount').trigger('mouseover') +}); + +[1, 2, 3].map(doItFunc) + +function doItFunc(number) { + const podName = Cypress.env(`name${number}`) + const namespace = Cypress.env(`namespace${number}`) + + it(`verifying the pod (${podName}, ${namespace})`, function () { + findLineAndCheck({"podName" : podName, "namespace" : namespace}) + }) +} + +function getDomPathInStatusBar(line, column) { + return `.expandedStatusBar > :nth-child(2) > > :nth-child(2) > :nth-child(${line}) > :nth-child(${column})` +} + +function checkLine(line, expectedValues) { + cy.get(getDomPathInStatusBar(line, columns.podName)).invoke('text').then(podValue => { + const podName = podValue.substring(0, podValue.indexOf('-')) + expect(podName).to.equal(expectedValues.podName) + + cy.get(getDomPathInStatusBar(line, columns.namespace)).invoke('text').then(namespaceValue => { + expect(namespaceValue).to.equal(expectedValues.namespace) + cy.get(getDomPathInStatusBar(line, columns.tapping)).children().should('have.attr', 'src', greenStatusImageSrc) + }) + }) +} + +function findLineAndCheck(expectedValues) { + cy.get('.expandedStatusBar > :nth-child(2) > > :nth-child(2) > > :nth-child(1)').then(pods => { + cy.get('.expandedStatusBar > :nth-child(2) > > :nth-child(2) > > :nth-child(2)').then(namespaces => { + + // organizing namespaces array + const namespacesObjectsArray = Object.values(namespaces) + let namespacesArray = [] + namespacesObjectsArray.forEach(line => { + line.getAttribute ? namespacesArray.push(line.innerHTML) : null + }) + + // organizing pods array + const podObjectsArray = Object.values(pods) + let podsArray = [] + podObjectsArray.forEach(line => { + line.getAttribute ? podsArray.push(line.innerHTML.substring(0, line.innerHTML.indexOf('-'))) : null + }) + + let rightIndex = -1 + podsArray.forEach((element, index) => { + if (element === expectedValues.podName && namespacesArray[index] === expectedValues.namespace) { + rightIndex = index + 1 + } + }) + rightIndex === -1 ? throwError(expectedValues.podName, expectedValues.namespace) : checkLine(rightIndex, expectedValues) + }) + }) +} + +function throwError(pod, namespace) { + throw new Error(`The pod named ${pod} doesn't match any namespace named ${namespace}`) +} diff --git a/acceptanceTests/tap_test.go b/acceptanceTests/tap_test.go index 4a4606046..2415fee78 100644 --- a/acceptanceTests/tap_test.go +++ b/acceptanceTests/tap_test.go @@ -151,6 +151,7 @@ func TestTapAllNamespaces(t *testing.T) { expectedPods := []PodDescriptor{ {Name: "httpbin", Namespace: "mizu-tests"}, + {Name: "httpbin2", Namespace: "mizu-tests"}, {Name: "httpbin", Namespace: "mizu-tests2"}, } @@ -184,25 +185,8 @@ func TestTapAllNamespaces(t *testing.T) { return } - podsUrl := fmt.Sprintf("%v/status/tap", apiServerUrl) - requestResult, requestErr := executeHttpGetRequest(podsUrl) - if requestErr != nil { - t.Errorf("failed to get tap status, err: %v", requestErr) - return - } - - pods, err := getPods(requestResult) - if err != nil { - t.Errorf("failed to get pods, err: %v", err) - return - } - - for _, expectedPod := range expectedPods { - if !isPodDescriptorInPodArray(pods, expectedPod) { - t.Errorf("unexpected result - expected pod not found, pod namespace: %v, pod name: %v", expectedPod.Namespace, expectedPod.Name) - return - } - } + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/MultipleNamespaces.js\" --env name1=%v,name2=%v,name3=%v,namespace1=%v,namespace2=%v,namespace3=%v", + expectedPods[0].Name, expectedPods[1].Name, expectedPods[2].Name, expectedPods[0].Namespace, expectedPods[1].Namespace, expectedPods[2].Namespace)) } func TestTapMultipleNamespaces(t *testing.T) { @@ -250,30 +234,8 @@ func TestTapMultipleNamespaces(t *testing.T) { return } - podsUrl := fmt.Sprintf("%v/status/tap", apiServerUrl) - requestResult, requestErr := executeHttpGetRequest(podsUrl) - if requestErr != nil { - t.Errorf("failed to get tap status, err: %v", requestErr) - return - } - - pods, err := getPods(requestResult) - if err != nil { - t.Errorf("failed to get pods, err: %v", err) - return - } - - if len(expectedPods) != len(pods) { - t.Errorf("unexpected result - expected pods length: %v, actual pods length: %v", len(expectedPods), len(pods)) - return - } - - for _, expectedPod := range expectedPods { - if !isPodDescriptorInPodArray(pods, expectedPod) { - t.Errorf("unexpected result - expected pod not found, pod namespace: %v, pod name: %v", expectedPod.Namespace, expectedPod.Name) - return - } - } + runCypressTests(t, fmt.Sprintf("npx cypress run --spec \"cypress/integration/tests/MultipleNamespaces.js\" --env name1=%v,name2=%v,name3=%v,namespace1=%v,namespace2=%v,namespace3=%v", + expectedPods[0].Name, expectedPods[1].Name, expectedPods[2].Name, expectedPods[0].Namespace, expectedPods[1].Namespace, expectedPods[2].Namespace)) } func TestTapRegex(t *testing.T) {