From fd6e0cc3cbd4fdaf5a8e35e3ac78dd06239c42c8 Mon Sep 17 00:00:00 2001 From: Pasquale Congiusti Date: Thu, 25 Nov 2021 13:36:44 +0100 Subject: [PATCH] feat(e2e): adding generated configmap checks Ref #2320 --- e2e/common/cli/dev_mode_test.go | 45 ++++++++++++++++++++++++++++++++ e2e/common/config/config_test.go | 23 ++++++++++++++++ e2e/support/test_support.go | 20 ++++++++++++++ pkg/cmd/run_help.go | 2 +- pkg/util/kubernetes/factory.go | 2 +- 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/e2e/common/cli/dev_mode_test.go b/e2e/common/cli/dev_mode_test.go index 480204e368..251d83838b 100644 --- a/e2e/common/cli/dev_mode_test.go +++ b/e2e/common/cli/dev_mode_test.go @@ -24,11 +24,14 @@ package common import ( "context" + "fmt" "io" + "io/ioutil" "os" "testing" . "github.com/onsi/gomega" + "github.com/stretchr/testify/assert" . "github.com/apache/camel-k/e2e/support" "github.com/apache/camel-k/e2e/support/util" @@ -87,5 +90,47 @@ func TestRunDevMode(t *testing.T) { Eventually(logScanner.IsFound("Magicstring!"), TestTimeoutMedium).Should(BeTrue()) }) + + t.Run("Dev mode resource file generated configmap", func(t *testing.T) { + var tmpFile *os.File + var err error + if tmpFile, err = ioutil.TempFile("", "camel-k-"); err != nil { + t.Error(err) + } + assert.Nil(t, tmpFile.Close()) + assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte("Hello from test!"), 0o644)) + + RegisterTestingT(t) + ctx, cancel := context.WithCancel(TestContext) + defer cancel() + piper, pipew := io.Pipe() + defer pipew.Close() + defer piper.Close() + + file := util.MakeTempCopy(t, "../config/files/resource-file-location-route.groovy") + + kamelRun := KamelWithContext(ctx, "run", "-n", ns, file, "--dev", "--resource", fmt.Sprintf("file:%s@/tmp/file.txt", tmpFile.Name())) + kamelRun.SetOut(pipew) + + logScanner := util.NewLogScanner(ctx, piper, `integration "resource-file-location-route" in phase Running`, + "Hello from test!", "Goodbye from test!") + + args := os.Args + defer func() { os.Args = args }() + os.Args = []string{"kamel", "run", "-n", ns, file, "--dev", "--resource", fmt.Sprintf("file:%s@/tmp/file.txt", tmpFile.Name())} + go kamelRun.Execute() + + Eventually(logScanner.IsFound(`integration "resource-file-location-route" in phase Running`), TestTimeoutMedium).Should(BeTrue()) + Eventually(logScanner.IsFound("Hello from test!"), TestTimeoutMedium).Should(BeTrue()) + Expect(logScanner.IsFound("Goodbye from test!")()).To(BeFalse()) + + // cool, now let's change the file to confirm the sync take place + assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte("Goodbye from test!"), 0o644)) + Eventually(logScanner.IsFound("Goodbye from test!"), TestTimeoutMedium).Should(BeTrue()) + + Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed()) + // When the integration is deleted, then, also the autogenerated configmaps must be cleaned + Eventually(AutogeneratedConfigmapsCount(ns), TestTimeoutShort).Should(Equal(0)) + }) }) } diff --git a/e2e/common/config/config_test.go b/e2e/common/config/config_test.go index d9b16f953c..e5697a1f4b 100644 --- a/e2e/common/config/config_test.go +++ b/e2e/common/config/config_test.go @@ -23,7 +23,9 @@ limitations under the License. package resources import ( + "fmt" "io/ioutil" + "os" "testing" . "github.com/onsi/gomega" @@ -242,5 +244,26 @@ func TestRunConfigExamples(t *testing.T) { Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed()) }) + // Resource File: generated configmap must be deleted when Integration is deleted + + t.Run("Plain text sync resource file generated configmap", func(t *testing.T) { + var tmpFile *os.File + var err error + if tmpFile, err = ioutil.TempFile("", "camel-k-"); err != nil { + t.Error(err) + } + assert.Nil(t, tmpFile.Close()) + assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte("Hello from test!"), 0o644)) + + Expect(Kamel("run", "-n", ns, "./files/resource-file-location-route.groovy", "--resource", fmt.Sprintf("file:%s@/tmp/file.txt", tmpFile.Name())).Execute()).To(Succeed()) + Eventually(IntegrationPodPhase(ns, "resource-file-location-route"), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) + Eventually(IntegrationConditionStatus(ns, "resource-file-location-route", v1.IntegrationConditionReady), TestTimeoutMedium).Should(Equal(corev1.ConditionTrue)) + Eventually(IntegrationLogs(ns, "resource-file-location-route"), TestTimeoutMedium).Should(ContainSubstring("Hello from test!")) + + Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed()) + // When the integration is deleted, then, also the autogenerated configmaps must be cleaned + Eventually(AutogeneratedConfigmapsCount(ns), TestTimeoutShort).Should(Equal(0)) + }) + }) } diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index 8d3ab965b4..8d75eb68fe 100644 --- a/e2e/support/test_support.go +++ b/e2e/support/test_support.go @@ -918,6 +918,26 @@ func Configmap(ns string, name string) func() *corev1.ConfigMap { } } +func AutogeneratedConfigmapsCount(ns string) func() int { + return func() int { + lst := corev1.ConfigMapList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMap", + APIVersion: corev1.SchemeGroupVersion.String(), + }, + } + err := TestClient().List(TestContext, &lst, + ctrl.InNamespace(ns), + ctrl.MatchingLabels{ + "camel.apache.org/autogenerated": "true", + }) + if err != nil { + panic(err) + } + return len(lst.Items) + } +} + func NewPlainTextConfigmap(ns string, name string, data map[string]string) error { cm := corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ diff --git a/pkg/cmd/run_help.go b/pkg/cmd/run_help.go index 5ccbe146c8..1970da0d6d 100644 --- a/pkg/cmd/run_help.go +++ b/pkg/cmd/run_help.go @@ -249,7 +249,7 @@ func convertFileToConfigmap(ctx context.Context, c client.Client, resourceSpec v config.destinationPath = filepath.Dir(config.DestinationPath()) } genCmName := fmt.Sprintf("cm-%s", hashFrom([]byte(resourceSpec.Content), resourceSpec.RawContent)) - cm := kubernetes.NewConfigmap(namespace, genCmName, config.Name(), config.Key(), resourceSpec.Content, resourceSpec.RawContent) + cm := kubernetes.NewConfigmap(namespace, genCmName, filepath.Base(config.Name()), config.Key(), resourceSpec.Content, resourceSpec.RawContent) err := c.Create(ctx, cm) if err != nil { if k8serrors.IsAlreadyExists(err) { diff --git a/pkg/util/kubernetes/factory.go b/pkg/util/kubernetes/factory.go index 6fa42ecda5..6b0e8923ee 100644 --- a/pkg/util/kubernetes/factory.go +++ b/pkg/util/kubernetes/factory.go @@ -129,7 +129,7 @@ func NewConfigmap(namespace, cmName, originalFilename string, generatedKey strin Name: cmName, Namespace: namespace, Labels: map[string]string{ - "camel.apache.org/original-path": originalFilename, + "camel.apache.org/filename": originalFilename, }, }, Immutable: &immutable,