This is an automated email from the ASF dual-hosted git repository. pcongiusti pushed a commit to branch release-2.0.x in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit fae38f81f714b2f740928a92510c00b8513966fc Author: Gaelle Fournier <gaelle.fournier.w...@gmail.com> AuthorDate: Tue Dec 5 16:55:03 2023 +0100 fix(e2e): Upgrade helm procedure test --- e2e/install/upgrade/helm_upgrade_test.go | 148 +++++++++++++++++++++++++++++++ e2e/support/test_util.go | 60 +++++++++++++ 2 files changed, 208 insertions(+) diff --git a/e2e/install/upgrade/helm_upgrade_test.go b/e2e/install/upgrade/helm_upgrade_test.go new file mode 100644 index 000000000..768da149c --- /dev/null +++ b/e2e/install/upgrade/helm_upgrade_test.go @@ -0,0 +1,148 @@ +//go:build integration +// +build integration + +// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration" + +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package upgrade + +import ( + "fmt" + "os" + "os/exec" + "testing" + + . "github.com/apache/camel-k/v2/e2e/support" + "github.com/apache/camel-k/v2/pkg/util/defaults" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" +) + +// WARNING: this test is not OLM specific but needs certain setting we provide in OLM installation scenario +func TestHelmOperatorUpgrade(t *testing.T) { + RegisterTestingT(t) + + KAMEL_INSTALL_REGISTRY := os.Getenv("KAMEL_INSTALL_REGISTRY") + // need to add last release version + releaseVersion := os.Getenv("KAMEL_K_TEST_RELEASE_VERSION") + customImage := fmt.Sprintf("%s/apache/camel-k", KAMEL_INSTALL_REGISTRY) + + os.Setenv("CAMEL_K_TEST_MAKE_DIR", "../../../") + + // Ensure no CRDs are already installed + UninstallAll() + Eventually(CRDs()).Should(HaveLen(0)) + + WithNewTestNamespace(t, func(ns string) { + + // Install operator in last released version + ExpectExecSucceed(t, + exec.Command( + "helm", + "install", + "camel-k", + fmt.Sprintf("../../../docs/charts/camel-k-%s.tgz", releaseVersion), + "--set", + fmt.Sprintf("platform.build.registry.address=%s", KAMEL_INSTALL_REGISTRY), + "--set", + "platform.build.registry.insecure=true", + "-n", + ns, + ), + ) + + Eventually(OperatorPod(ns)).ShouldNot(BeNil()) + Eventually(OperatorImage(ns)).Should(ContainSubstring(releaseVersion)) + Eventually(CRDs()).Should(HaveLen(8)) + + //Test a simple route + t.Run("simple route", func(t *testing.T) { + name := "simpleyaml" + Expect(KamelRun(ns, "files/yaml.yaml", "--name", name).Execute()).To(Succeed()) + Eventually(IntegrationPodPhase(ns, name), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) + Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) + }) + + // Upgrade CRDs with kustomize + ExpectExecSucceed(t, + exec.Command( + "kubectl", + "replace", + "-f", + "../../../helm/camel-k/crds/", + "-n", + ns, + ), + ) + + // Upgrade operator to current version + ExpectExecSucceed(t, Make(fmt.Sprintf("CUSTOM_IMAGE=%s", customImage), "set-version")) + ExpectExecSucceed(t, Make("release-helm")) + ExpectExecSucceed(t, + exec.Command( + "helm", + "upgrade", + "camel-k", + fmt.Sprintf("../../../docs/charts/camel-k-%s.tgz", defaults.Version), + "--set", + fmt.Sprintf("platform.build.registry.address=%s", KAMEL_INSTALL_REGISTRY), + "--set", + "platform.build.registry.insecure=true", + "-n", + ns, + "--force", + ), + ) + + Eventually(OperatorPod(ns)).ShouldNot(BeNil()) + Eventually(OperatorImage(ns)).Should(ContainSubstring(defaults.Version)) + + //Test again a simple route + t.Run("simple route upgraded", func(t *testing.T) { + name := "upgradedyaml" + Expect(KamelRun(ns, "files/yaml.yaml", "--name", name).Execute()).To(Succeed()) + Eventually(IntegrationPodPhase(ns, name), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) + Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) + }) + + // Uninstall with helm + ExpectExecSucceed(t, + exec.Command( + "helm", + "uninstall", + "camel-k", + "-n", + ns, + ), + ) + Eventually(OperatorPod(ns)).Should(BeNil()) + + // helm does not remove the CRDs + Eventually(CRDs()).Should(HaveLen(8)) + ExpectExecSucceed(t, + exec.Command( + "kubectl", + "delete", + "-k", + "../../../config/crd/", + "-n", + ns, + ), + ) + Eventually(CRDs()).Should(HaveLen(0)) + }) +} diff --git a/e2e/support/test_util.go b/e2e/support/test_util.go index b85e93060..61fbc3f2f 100644 --- a/e2e/support/test_util.go +++ b/e2e/support/test_util.go @@ -24,10 +24,15 @@ package support import ( "os" + "os/exec" + "strings" + "testing" . "github.com/onsi/gomega" + "github.com/onsi/gomega/gexec" . "github.com/onsi/gomega/gstruct" "github.com/onsi/gomega/types" + "github.com/stretchr/testify/assert" ) func init() { @@ -50,3 +55,58 @@ func GetEnvOrDefault(key string, deflt string) string { return deflt } } + +func ExpectExecSucceed(t *testing.T, command *exec.Cmd) { + t.Helper() + + var cmdOut strings.Builder + var cmdErr strings.Builder + + defer func() { + if t.Failed() { + t.Logf("Output from exec command:\n%s\n", cmdOut.String()) + t.Logf("Error from exec command:\n%s\n", cmdErr.String()) + } + }() + + session, err := gexec.Start(command, &cmdOut, &cmdErr) + session.Wait() + Eventually(session).Should(gexec.Exit(0)) + assert.NoError(t, err) + assert.NotContains(t, strings.ToUpper(cmdErr.String()), "ERROR") +} + +// Expect a command error with an exit code of 1 +func ExpectExecError(t *testing.T, command *exec.Cmd) { + t.Helper() + + var cmdOut strings.Builder + var cmdErr strings.Builder + + defer func() { + if t.Failed() { + t.Logf("Output from exec command:\n%s\n", cmdOut.String()) + t.Logf("Error from exec command:\n%s\n", cmdErr.String()) + } + }() + + session, err := gexec.Start(command, &cmdOut, &cmdErr) + session.Wait() + Eventually(session).ShouldNot(gexec.Exit(0)) + assert.NoError(t, err) + assert.Contains(t, strings.ToUpper(cmdErr.String()), "ERROR") +} + +// Clean up the cluster ready for the next set of tests +func Cleanup() { + // Remove the locally installed operator + UninstallAll() + + // Ensure the CRDs & ClusterRoles are reinstalled if not already + Kamel("install", "--olm=false", "--cluster-setup").Execute() +} + +// Removes all items +func UninstallAll() { + Kamel("uninstall", "--olm=false", "--all").Execute() +}