From bf6bc861727832680c039c4dac8009df816432a9 Mon Sep 17 00:00:00 2001 From: Soumik Majumder Date: Mon, 5 Aug 2024 15:28:12 +0530 Subject: [PATCH] Add tests for selective ownership override Signed-off-by: Soumik Majumder --- test/e2e/selective_ownership_override_test.go | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/e2e/selective_ownership_override_test.go diff --git a/test/e2e/selective_ownership_override_test.go b/test/e2e/selective_ownership_override_test.go new file mode 100644 index 000000000..3242541b9 --- /dev/null +++ b/test/e2e/selective_ownership_override_test.go @@ -0,0 +1,72 @@ +// Copyright 2024 The Carvel Authors. +// SPDX-License-Identifier: Apache-2.0 + +package e2e + +import ( + "fmt" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSelectiveOwnershipOverride(t *testing.T) { + env := BuildEnv(t) + logger := Logger{} + kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger} + + const existingAppName1 = "existing-app-1" + const existingAppName2 = "existing-app-2" + const newAppName = "new-app" + + resourceYAML := ` +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: cm-%s +data: + foo: bar +` + + cleanUp := func() { + kapp.Run([]string{"delete", "-a", existingAppName1}) + kapp.Run([]string{"delete", "-a", existingAppName2}) + kapp.Run([]string{"delete", "-a", newAppName}) + } + cleanUp() + defer cleanUp() + + logger.Section("deploy existing apps", func() { + kapp.RunWithOpts([]string{"deploy", "-a", existingAppName1, "-f", "-"}, RunOpts{StdinReader: strings.NewReader(fmt.Sprintf(resourceYAML, "1"))}) + kapp.RunWithOpts([]string{"deploy", "-a", existingAppName2, "-f", "-"}, RunOpts{StdinReader: strings.NewReader(fmt.Sprintf(resourceYAML, "2"))}) + }) + + logger.Section("deploy new app with selective overrides", func() { + resourcesString := fmt.Sprintf("%s%s", fmt.Sprintf(resourceYAML, "1"), fmt.Sprintf(resourceYAML, "2")) + // Overrides disallowed + _, err := kapp.RunWithOpts([]string{"deploy", "-a", newAppName, "-f", "-"}, RunOpts{StdinReader: strings.NewReader(resourcesString), AllowError: true}) + require.Error(t, err) + require.Contains(t, err.Error(), existingAppName1) + require.Contains(t, err.Error(), existingAppName2) + + // Test with override scoped while override is disallowed + _, err = kapp.RunWithOpts([]string{"deploy", "-a", newAppName, "-f", "-", "--ownership-override-app-filter", existingAppName1}, + RunOpts{StdinReader: strings.NewReader(resourcesString), AllowError: true}) + require.Error(t, err) + require.Contains(t, err.Error(), existingAppName1) + require.Contains(t, err.Error(), existingAppName2) + + // Test with override scoped to single existing app + _, err = kapp.RunWithOpts([]string{"deploy", "-a", newAppName, "-f", "-", "--dangerous-override-ownership-of-existing-resources", "--ownership-override-app-filter", existingAppName1}, + RunOpts{StdinReader: strings.NewReader(resourcesString), AllowError: true}) + require.Error(t, err) + require.NotContains(t, err.Error(), existingAppName1) + require.Contains(t, err.Error(), existingAppName2) + + // Test with override scoped to both existing app + kapp.RunWithOpts([]string{"deploy", "-a", newAppName, "-f", "-", "--dangerous-override-ownership-of-existing-resources", "--ownership-override-app-filter", fmt.Sprintf("%s,%s", existingAppName1, existingAppName2)}, + RunOpts{StdinReader: strings.NewReader(resourcesString)}) + }) +}