From 0c0915f896584f01ff608ddca028d9b0ba2138d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 29 Aug 2024 14:17:05 +0300 Subject: [PATCH 1/2] Add new cli options: `disable-import` and `disable-update` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- cmd/uptest/main.go | 4 ++++ internal/config/config.go | 2 ++ internal/tester.go | 28 +++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/cmd/uptest/main.go b/cmd/uptest/main.go index 508592d..a3d2547 100644 --- a/cmd/uptest/main.go +++ b/cmd/uptest/main.go @@ -45,6 +45,8 @@ var ( renderOnly = e2e.Flag("render-only", "Only render test files. Do not run the tests.").Default("false").Bool() logCollectInterval = e2e.Flag("log-collect-interval", "Specifies the interval duration for collecting logs. "+ "The duration should be provided in a format understood by the tool, such as seconds (s), minutes (m), or hours (h). For example, '30s' for 30 seconds, '5m' for 5 minutes, or '1h' for one hour.").Default("30s").Duration() + skipUpdate = e2e.Flag("skip-update", "Skip the update step of the test.").Default("false").Bool() + skipImport = e2e.Flag("skip-import", "Skip the import step of the test.").Default("false").Bool() ) func main() { @@ -95,6 +97,8 @@ func e2eTests() { DefaultTimeout: *defaultTimeout, Directory: *testDir, SkipDelete: *skipDelete, + SkipUpdate: *skipUpdate, + SkipImport: *skipImport, OnlyCleanUptestResources: *onlyCleanUptestResources, RenderOnly: *renderOnly, LogCollectionInterval: *logCollectInterval, diff --git a/internal/config/config.go b/internal/config/config.go index 15e5424..ff774b2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -55,6 +55,8 @@ type AutomatedTest struct { DefaultConditions []string SkipDelete bool + SkipUpdate bool + SkipImport bool OnlyCleanUptestResources bool diff --git a/internal/tester.go b/internal/tester.go index 88d9bd4..e03a314 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -32,6 +32,8 @@ var testFiles = []string{ "03-delete.yaml", } +const crossplaneTempError = "crossplane: error: cannot get requested resource" + func newTester(ms []config.Manifest, opts *config.AutomatedTest) *tester { return &tester{ options: opts, @@ -122,14 +124,19 @@ func logCollector(done chan bool, ticker *time.Ticker, mutex sync.Locker, resour return case <-ticker.C: mutex.Lock() - log.Printf("crossplane trace logs %s\n", time.Now()) for _, r := range resources { traceCmd := exec.Command("bash", "-c", fmt.Sprintf(`"${CROSSPLANE_CLI}" beta trace %s %s -o wide`, r.KindGroup, r.Name)) //nolint:gosec // Disabling gosec to allow dynamic shell command execution output, err := traceCmd.CombinedOutput() if err != nil { - log.Println("Error executing crossplane:", err) + // During the setup script is running, the crossplane command + // is failing because of the resource not found error. + // We do not want to show this error to the user because it + // is a noise and temporary one. + if !strings.Contains(string(output), crossplaneTempError) { + log.Printf("crossplane trace logs %s\n%s: %s: %s\n", time.Now(), "Error executing crossplane", err, string(output)) + } } else { - log.Println(string(output)) + log.Printf("crossplane trace logs %s\n%s\n", time.Now(), string(output)) } } mutex.Unlock() @@ -148,6 +155,7 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) { } examples := make([]config.Resource, 0, len(t.manifests)) + rootFound := false for _, m := range t.manifests { obj := m.Object groupVersionKind := obj.GroupVersionKind() @@ -238,12 +246,26 @@ func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) { tc.SkipUpdate = true } example.Root = true + rootFound = true } } examples = append(examples, example) } + if !rootFound { + log.Println("Skipping update step because the root resource does not exist") + tc.SkipUpdate = true + } + if t.options.SkipUpdate { + log.Println("Skipping update step because the skip-delete option is set to true") + tc.SkipUpdate = true + } + if t.options.SkipImport { + log.Println("Skipping import step because the skip-import option is set to true") + tc.SkipImport = true + } + return tc, examples, nil } From d4abb6aee63eec57da1bd3dd7d24aee60410e34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 29 Aug 2024 19:42:54 +0300 Subject: [PATCH 2/2] Add assertion for managed resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- internal/templates/03-delete.yaml.tmpl | 5 +++++ internal/templates/renderer_test.go | 6 ++++++ internal/tester.go | 20 +++++++------------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/internal/templates/03-delete.yaml.tmpl b/internal/templates/03-delete.yaml.tmpl index dd08913..d10fdc0 100644 --- a/internal/templates/03-delete.yaml.tmpl +++ b/internal/templates/03-delete.yaml.tmpl @@ -45,6 +45,11 @@ spec: for: deletion: {} {{- end }} + {{- if not .TestCase.OnlyCleanUptestResources }} + - script: + content: | + ${KUBECTL} wait managed --all --for=delete --timeout -1s + {{- end }} {{- if .TestCase.TeardownScriptPath }} - command: entrypoint: {{ .TestCase.TeardownScriptPath }} diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index ad9cf85..64a1354 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -222,6 +222,9 @@ spec: name: example-bucket for: deletion: {} + - script: + content: | + ${KUBECTL} wait managed --all --for=delete --timeout -1s `, }, }, @@ -435,6 +438,9 @@ spec: namespace: upbound-system for: deletion: {} + - script: + content: | + ${KUBECTL} wait managed --all --for=delete --timeout -1s - command: entrypoint: /tmp/teardown.sh `, diff --git a/internal/tester.go b/internal/tester.go index e03a314..676edd5 100644 --- a/internal/tester.go +++ b/internal/tester.go @@ -32,8 +32,6 @@ var testFiles = []string{ "03-delete.yaml", } -const crossplaneTempError = "crossplane: error: cannot get requested resource" - func newTester(ms []config.Manifest, opts *config.AutomatedTest) *tester { return &tester{ options: opts, @@ -125,24 +123,20 @@ func logCollector(done chan bool, ticker *time.Ticker, mutex sync.Locker, resour case <-ticker.C: mutex.Lock() for _, r := range resources { - traceCmd := exec.Command("bash", "-c", fmt.Sprintf(`"${CROSSPLANE_CLI}" beta trace %s %s -o wide`, r.KindGroup, r.Name)) //nolint:gosec // Disabling gosec to allow dynamic shell command execution + // During the setup script is running, the crossplane command + // is failing because of the resource not found error. + // We do not want to show this error to the user because it + // is a noise and temporary one. + // The error output was redirected to a file. + traceCmd := exec.Command("bash", "-c", fmt.Sprintf(`"${CROSSPLANE_CLI}" beta trace %s %s -o wide 2>>/tmp/uptest_crossplane_temp_errors.log`, r.KindGroup, r.Name)) //nolint:gosec // Disabling gosec to allow dynamic shell command execution output, err := traceCmd.CombinedOutput() - if err != nil { - // During the setup script is running, the crossplane command - // is failing because of the resource not found error. - // We do not want to show this error to the user because it - // is a noise and temporary one. - if !strings.Contains(string(output), crossplaneTempError) { - log.Printf("crossplane trace logs %s\n%s: %s: %s\n", time.Now(), "Error executing crossplane", err, string(output)) - } - } else { + if err == nil { log.Printf("crossplane trace logs %s\n%s\n", time.Now(), string(output)) } } mutex.Unlock() } } - } func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) { //nolint:gocyclo // TODO: can we break this?