From 19c2d5de45cc95168050e7daca05f048938c8f94 Mon Sep 17 00:00:00 2001 From: Jim Crossley Date: Wed, 11 Sep 2019 09:43:30 -0400 Subject: [PATCH] Add an e2e test for #138 After creating the KnativeServing instance, we then update it twice, once with an entry for the config-logging ConfigMap and then again without the key, verifying that the operator correctly syncs the ConfigMap each time. --- test/crd.go | 1 + test/e2e/knativeservingdeployment_test.go | 45 +++++++++++++++++++++++ test/e2e_flags.go | 2 + test/resources/knativeserving.go | 11 ++++++ 4 files changed, 59 insertions(+) diff --git a/test/crd.go b/test/crd.go index 227044db..7f350277 100644 --- a/test/crd.go +++ b/test/crd.go @@ -20,4 +20,5 @@ package test type ResourceNames struct { KnativeServing string Namespace string + LoggingConfig string } diff --git a/test/e2e/knativeservingdeployment_test.go b/test/e2e/knativeservingdeployment_test.go index 063c6300..8bd6c2c2 100644 --- a/test/e2e/knativeservingdeployment_test.go +++ b/test/e2e/knativeservingdeployment_test.go @@ -27,6 +27,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "knative.dev/pkg/test/logstream" + "knative.dev/serving-operator/pkg/apis/serving/v1alpha1" "knative.dev/serving-operator/test" "knative.dev/serving-operator/test/resources" ) @@ -40,6 +41,7 @@ func TestKnativeServingDeployment(t *testing.T) { names := test.ResourceNames{ KnativeServing: test.ServingOperatorName, Namespace: test.ServingOperatorNamespace, + LoggingConfig: test.LoggingConfigMapName, } test.CleanupOnInterrupt(func() { test.TearDown(clients, names) }) @@ -55,6 +57,11 @@ func TestKnativeServingDeployment(t *testing.T) { knativeServingVerify(t, clients, names) }) + t.Run("configure", func(t *testing.T) { + knativeServingVerify(t, clients, names) + knativeServingConfigure(t, clients, names) + }) + // Delete the deployments one by one to see if they will be recreated. t.Run("restore", func(t *testing.T) { knativeServingVerify(t, clients, names) @@ -78,6 +85,44 @@ func knativeServingVerify(t *testing.T, clients *test.Clients, names test.Resour } +// knativeServingConfigure verifies that KnativeServing config is set properly +func knativeServingConfigure(t *testing.T, clients *test.Clients, names test.ResourceNames) { + // Get the existing KS without any spec + ks, err := clients.KnativeServing().Get(names.KnativeServing, metav1.GetOptions{}) + // Add config to its spec + ks.Spec = v1alpha1.KnativeServingSpec{ + Config: map[string]map[string]string{ + "logging": map[string]string{ + "loglevel.controller": "debug", + }, + }, + } + // Update it + if ks, err = clients.KnativeServing().Update(ks); err != nil { + t.Fatalf("KnativeServing %q failed to update: %v", names.KnativeServing, err) + } + // Verifty the relevant configmap has been updated + err = resources.WaitForConfigMap(clients.KubeClient.Kube, names, func(m map[string]string) bool { + return m["loglevel.controller"] == "debug" + }) + if err != nil { + t.Fatal("The operator failed to update the configmap") + } + // Now remove the config from the spec and update + ks.Spec = v1alpha1.KnativeServingSpec{} + if ks, err = clients.KnativeServing().Update(ks); err != nil { + t.Fatalf("KnativeServing %q failed to update: %v", names.KnativeServing, err) + } + // And verify the configmap entry is gone + err = resources.WaitForConfigMap(clients.KubeClient.Kube, names, func(m map[string]string) bool { + _, exists := m["loglevel.controller"] + return !exists + }) + if err != nil { + t.Fatal("The operator failed to revert the configmap") + } +} + // deploymentRecreation verify whether all the deployments for knative serving are able to recreate, when they are deleted. func deploymentRecreation(t *testing.T, clients *test.Clients, names test.ResourceNames) { dpList, err := clients.KubeClient.Kube.AppsV1().Deployments(names.Namespace).List(metav1.ListOptions{}) diff --git a/test/e2e_flags.go b/test/e2e_flags.go index f7dc4f3f..45ee02e6 100644 --- a/test/e2e_flags.go +++ b/test/e2e_flags.go @@ -24,4 +24,6 @@ const ( ServingOperatorNamespace = "operator-tests" // ServingOperatorName is the default operator name for serving operator e2e tests ServingOperatorName = "knative-serving" + // Logging configmap name + LoggingConfigMapName = "config-logging" ) diff --git a/test/resources/knativeserving.go b/test/resources/knativeserving.go index 079252a1..d784989c 100644 --- a/test/resources/knativeserving.go +++ b/test/resources/knativeserving.go @@ -28,6 +28,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/kubernetes" "knative.dev/pkg/test/logging" "knative.dev/serving-operator/pkg/apis/serving/v1alpha1" servingv1alpha1 "knative.dev/serving-operator/pkg/client/clientset/versioned/typed/serving/v1alpha1" @@ -73,6 +74,16 @@ func CreateKnativeServing(clients servingv1alpha1.KnativeServingInterface, names return svc, err } +func WaitForConfigMap(client *kubernetes.Clientset, names test.ResourceNames, fn func(map[string]string) bool) error { + return wait.PollImmediate(Interval, Timeout, func() (bool, error) { + cm, err := client.CoreV1().ConfigMaps(names.Namespace).Get(names.LoggingConfig, metav1.GetOptions{}) + if err != nil { + return false, err + } + return fn(cm.Data), nil + }) +} + // IsKnativeServingReady will check the status conditions of the KnativeServing and return true if the KnativeServing is ready. func IsKnativeServingReady(s *v1alpha1.KnativeServing, err error) (bool, error) { return s.Status.IsReady(), err