This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 75e85b16b826423b970ae0c14adeeccbbdd900d8 Author: Antonin Stefanutti <[email protected]> AuthorDate: Thu Feb 18 19:30:21 2021 +0100 chore(test): Add integration scaling e2e tests --- e2e/common/scale_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++++ e2e/support/test_support.go | 37 +++++++++++++-- 2 files changed, 144 insertions(+), 4 deletions(-) diff --git a/e2e/common/scale_test.go b/e2e/common/scale_test.go new file mode 100644 index 0000000..cc77db1 --- /dev/null +++ b/e2e/common/scale_test.go @@ -0,0 +1,111 @@ +// +build integration + +// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "knative" + +/* +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 common + +import ( + "testing" + + . "github.com/onsi/gomega" + "github.com/onsi/gomega/gstruct" + "github.com/stretchr/testify/assert" + + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + + "k8s.io/client-go/dynamic" + "k8s.io/client-go/restmapper" + "k8s.io/client-go/scale" + + . "github.com/apache/camel-k/e2e/support" + camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1" + "github.com/apache/camel-k/pkg/client/camel/clientset/versioned" +) + +func TestIntegrationScale(t *testing.T) { + WithNewTestNamespace(t, func(ns string) { + name := "java" + Expect(Kamel("install", "-n", ns).Execute()).Should(BeNil()) + Expect(Kamel("run", "-n", ns, "files/Java.java", "--name", name).Execute()).Should(BeNil()) + Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(v1.PodRunning)) + Eventually(IntegrationCondition(ns, name, camelv1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(v1.ConditionTrue)) + Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) + + t.Run("Scale integration with polymorphic client", func(t *testing.T) { + // Polymorphic scale client + groupResources, err := restmapper.GetAPIGroupResources(TestClient().Discovery()) + assert.Nil(t, err) + mapper := restmapper.NewDiscoveryRESTMapper(groupResources) + resolver := scale.NewDiscoveryScaleKindResolver(TestClient().Discovery()) + scaleClient, err := scale.NewForConfig(TestClient().GetConfig(), mapper, dynamic.LegacyAPIPathResolverFunc, resolver) + assert.Nil(t, err) + + // Patch the integration scale subresource + patch := "{\"spec\":{\"replicas\":2}}" + _, err = scaleClient.Scales(ns).Patch(TestContext, camelv1.SchemeGroupVersion.WithResource("integrations"), name, types.MergePatchType, []byte(patch), metav1.PatchOptions{}) + if err != nil { + t.Fatal(err) + } + + // Check the Integration scale subresource Spec field + Eventually(IntegrationSpecReplicas(ns, name), TestTimeoutShort). + Should(gstruct.PointTo(BeNumerically("==", 2))) + // Then check it cascades into the Deployment scale + Eventually(IntegrationPods(ns, name), TestTimeoutMedium).Should(HaveLen(2)) + // Finally check it cascades into the Integration scale subresource Status field + Eventually(IntegrationStatusReplicas(ns, name), TestTimeoutShort). + Should(gstruct.PointTo(BeNumerically("==", 2))) + }) + + t.Run("Scale integration with Camel K client", func(t *testing.T) { + camel, err := versioned.NewForConfig(TestClient().GetConfig()) + if err != nil { + t.Fatal(err) + } + + // Getter + integrationScale, err := camel.CamelV1().Integrations(ns).GetScale(TestContext, name, metav1.GetOptions{}) + Expect(integrationScale).ShouldNot(BeNil()) + Expect(integrationScale.Spec.Replicas).Should(BeNumerically("==", 2)) + Expect(integrationScale.Status.Replicas).Should(BeNumerically("==", 2)) + + // Setter + integrationScale.Spec.Replicas = 1 + integrationScale, err = camel.CamelV1().Integrations(ns).UpdateScale(TestContext, name, integrationScale, metav1.UpdateOptions{}) + if err != nil { + t.Fatal(err) + } + + // Check the Integration scale subresource Spec field + Eventually(IntegrationSpecReplicas(ns, name), TestTimeoutShort). + Should(gstruct.PointTo(BeNumerically("==", 1))) + // Then check it cascades into the Deployment scale + Eventually(IntegrationPods(ns, name), TestTimeoutMedium).Should(HaveLen(1)) + // Finally check it cascades into the Integration scale subresource Status field + Eventually(IntegrationStatusReplicas(ns, name), TestTimeoutShort). + Should(gstruct.PointTo(BeNumerically("==", 1))) + }) + + // Cleanup + Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil()) + }) +} diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index 21a85d8..7a5ef96 100644 --- a/e2e/support/test_support.go +++ b/e2e/support/test_support.go @@ -34,11 +34,10 @@ import ( "testing" "time" - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/google/uuid" "github.com/onsi/gomega" "github.com/spf13/cobra" + appsv1 "k8s.io/api/apps/v1" "k8s.io/api/batch/v1beta1" corev1 "k8s.io/api/core/v1" @@ -46,6 +45,7 @@ import ( k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" eventing "knative.dev/eventing/pkg/apis/eventing/v1beta1" @@ -57,10 +57,12 @@ import ( "github.com/apache/camel-k/e2e/support/util" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" "github.com/apache/camel-k/pkg/cmd" "github.com/apache/camel-k/pkg/install" "github.com/apache/camel-k/pkg/util/defaults" + "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/apache/camel-k/pkg/util/log" "github.com/apache/camel-k/pkg/util/openshift" @@ -260,6 +262,16 @@ func IntegrationPodImage(ns string, name string) func() string { func IntegrationPod(ns string, name string) func() *corev1.Pod { return func() *corev1.Pod { + pods := IntegrationPods(ns, name)() + if len(pods) == 0 { + return nil + } + return &pods[0] + } +} + +func IntegrationPods(ns string, name string) func() []corev1.Pod { + return func() []corev1.Pod { lst := corev1.PodList{ TypeMeta: metav1.TypeMeta{ Kind: "Pod", @@ -274,10 +286,27 @@ func IntegrationPod(ns string, name string) func() *corev1.Pod { if err != nil { panic(err) } - if len(lst.Items) == 0 { + return lst.Items + } +} + +func IntegrationSpecReplicas(ns string, name string) func() *int32 { + return func() *int32 { + it := Integration(ns, name)() + if it == nil { return nil } - return &lst.Items[0] + return it.Spec.Replicas + } +} + +func IntegrationStatusReplicas(ns string, name string) func() *int32 { + return func() *int32 { + it := Integration(ns, name)() + if it == nil { + return nil + } + return it.Status.Replicas } }
