Skip to content

Commit

Permalink
Add an e2e test for knative#138
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jcrossley3 committed Sep 11, 2019
1 parent 916f514 commit 19c2d5d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ package test
type ResourceNames struct {
KnativeServing string
Namespace string
LoggingConfig string
}
45 changes: 45 additions & 0 deletions test/e2e/knativeservingdeployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) })
Expand All @@ -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)
Expand All @@ -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{})
Expand Down
2 changes: 2 additions & 0 deletions test/e2e_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
11 changes: 11 additions & 0 deletions test/resources/knativeserving.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 19c2d5d

Please sign in to comment.