From 042de8236ae0618a2a62cf26cd18c611a29e122e Mon Sep 17 00:00:00 2001 From: Vincent Hou Date: Mon, 28 Oct 2019 21:14:27 -0400 Subject: [PATCH] Add first group of unit tests to test the eveting lifecycle This PR adds only one unit test, but it will make sure the continuous build won't fail. --- .../knativeeventing_lifecycle_test.go | 237 ++++++++++++++++++ pkg/apis/eventing/v1alpha1/register.go | 10 +- pkg/apis/eventing/v1alpha1/register_test.go | 37 +++ 3 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 pkg/apis/eventing/v1alpha1/knativeeventing_lifecycle_test.go create mode 100644 pkg/apis/eventing/v1alpha1/register_test.go diff --git a/pkg/apis/eventing/v1alpha1/knativeeventing_lifecycle_test.go b/pkg/apis/eventing/v1alpha1/knativeeventing_lifecycle_test.go new file mode 100644 index 00000000..a84bf44e --- /dev/null +++ b/pkg/apis/eventing/v1alpha1/knativeeventing_lifecycle_test.go @@ -0,0 +1,237 @@ +/* +Copyright 2019 The Knative Authors + +Licensed 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 v1alpha1 + +import ( + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "testing" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + "knative.dev/pkg/apis" + duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" +) + +var ignoreAllButTypeAndStatus = cmpopts.IgnoreFields( + apis.Condition{}, + "LastTransitionTime", "Message", "Reason", "Severity") + +func TestKnativeEventingGroupVersionKind(t *testing.T) { + r := &Eventing{} + want := schema.GroupVersionKind{ + Group: GroupName, + Version: SchemaVersion, + Kind: "Eventing", + } + if got := r.GroupVersionKind(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +func TestKnativeEventingStatusGetCondition(t *testing.T) { + ke := &EventingStatus{} + if a := ke.GetCondition(InstallSucceeded); a != nil { + t.Errorf("empty EventingStatus returned %v when expected nil", a) + } + mc := &apis.Condition{ + Type: InstallSucceeded, + Status: corev1.ConditionTrue, + } + ke.MarkEventingInstalled() + if diff := cmp.Diff(mc, ke.GetCondition(InstallSucceeded), cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime")); diff != "" { + t.Errorf("GetCondition refs diff (-want +got): %v", diff) + } +} + +func TestKnativeEventingStatusEventingInstalled(t *testing.T) { + ke := &EventingStatus{} + mc := &apis.Condition{ + Type: InstallSucceeded, + Status: corev1.ConditionTrue, + } + ke.MarkEventingInstalled() + if diff := cmp.Diff(mc, ke.GetCondition(InstallSucceeded), cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime")); diff != "" { + t.Errorf("GetCondition refs diff (-want +got): %v", diff) + } +} + +func TestKnativeEventingStatusEventingFailed(t *testing.T) { + reason := "NotReady" + message := "Waiting on deployments" + ke := &EventingStatus{} + mc := &apis.Condition{ + Type: EventingConditionReady, + Status: corev1.ConditionFalse, + Reason: reason, + Message: message, + } + ke.MarkEventingFailed(reason, message) + if diff := cmp.Diff(mc, ke.GetCondition(EventingConditionReady), cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime")); diff != "" { + t.Errorf("GetCondition refs diff (-want +got): %v", diff) + } +} + +func TestKnativeEventingStatusNotReady(t *testing.T) { + reason := "NotReady" + message := "Waiting on deployments" + ke := &EventingStatus{} + mc := &apis.Condition{ + Type: EventingConditionReady, + Status: corev1.ConditionUnknown, + Reason: reason, + Message: message, + } + ke.MarkEventingNotReady(reason, message) + if diff := cmp.Diff(mc, ke.GetCondition(EventingConditionReady), cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime")); diff != "" { + t.Errorf("GetCondition refs diff (-want +got): %v", diff) + } +} + +func TestKnativeEventingStatusReady(t *testing.T) { + ke := &EventingStatus{} + mc := &apis.Condition{ + Type: EventingConditionReady, + Status: corev1.ConditionTrue, + } + ke.MarkEventingReady() + if diff := cmp.Diff(mc, ke.GetCondition(EventingConditionReady), cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime")); diff != "" { + t.Errorf("GetCondition refs diff (-want +got): %v", diff) + } +} + +func TestKnativeEventingStatusIsReady(t *testing.T) { + ke := &EventingStatus{} + ke.MarkEventingReady() + if diff := cmp.Diff(true, ke.IsReady()); diff != "" { + t.Errorf("IsReady refs diff (-want +got): %v", diff) + } +} + +func TestKnativeEventingInitializeConditions(t *testing.T) { + tests := []struct { + name string + ke *EventingStatus + want *EventingStatus + }{{ + name: "empty", + ke: &EventingStatus{}, + want: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: InstallSucceeded, + Status: corev1.ConditionUnknown, + }, { + Type: EventingConditionReady, + Status: corev1.ConditionUnknown, + }}, + }, + }, + }, { + name: "eventingConditionNotReady", + ke: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: EventingConditionReady, + Status: corev1.ConditionFalse, + }}, + }, + }, + want: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: InstallSucceeded, + Status: corev1.ConditionUnknown, + }, { + Type: EventingConditionReady, + Status: corev1.ConditionFalse, + }}, + }, + }, + }, { + name: "eventingConditionReady", + ke: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: EventingConditionReady, + Status: corev1.ConditionTrue, + }}, + }, + }, + want: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: InstallSucceeded, + Status: corev1.ConditionTrue, + }, { + Type: EventingConditionReady, + Status: corev1.ConditionTrue, + }}, + }, + }, + }, { + name: "installSucceeded", + ke: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: InstallSucceeded, + Status: corev1.ConditionTrue, + }}, + }, + }, + want: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: InstallSucceeded, + Status: corev1.ConditionTrue, + }, { + Type: EventingConditionReady, + Status: corev1.ConditionUnknown, + }}, + }, + }, + }, { + name: "installNotSucceeded", + ke: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: InstallSucceeded, + Status: corev1.ConditionFalse, + }}, + }, + }, + want: &EventingStatus{ + Status: duckv1beta1.Status{ + Conditions: []apis.Condition{{ + Type: InstallSucceeded, + Status: corev1.ConditionFalse, + }, { + Type: EventingConditionReady, + Status: corev1.ConditionUnknown, + }}, + }, + }, + }} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + test.ke.InitializeConditions() + if diff := cmp.Diff(test.want, test.ke, ignoreAllButTypeAndStatus); diff != "" { + t.Errorf("unexpected conditions (-want, +got) = %v", diff) + } + }) + } +} diff --git a/pkg/apis/eventing/v1alpha1/register.go b/pkg/apis/eventing/v1alpha1/register.go index c68a20ef..2adf8c19 100644 --- a/pkg/apis/eventing/v1alpha1/register.go +++ b/pkg/apis/eventing/v1alpha1/register.go @@ -22,6 +22,14 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" ) +const ( + // The group name. This is used for CRDs. + GroupName = "operator.knative.dev" + + // The Version of the schema. This is used for CRDs. + SchemaVersion = "v1alpha1" +) + // Resource takes an unqualified resource and returns a Group qualified GroupResource func Resource(resource string) schema.GroupResource { return SchemeGroupVersion.WithResource(resource).GroupResource() @@ -39,7 +47,7 @@ func addKnownTypes(s *runtime.Scheme) error { var ( // SchemeGroupVersion is group version used to register these objects - SchemeGroupVersion = schema.GroupVersion{Group: "operator.knative.dev", Version: "v1alpha1"} + SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: SchemaVersion} // SchemeBuilder is used to add go types to the GroupVersionKind scheme SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) diff --git a/pkg/apis/eventing/v1alpha1/register_test.go b/pkg/apis/eventing/v1alpha1/register_test.go new file mode 100644 index 00000000..0cbaff8c --- /dev/null +++ b/pkg/apis/eventing/v1alpha1/register_test.go @@ -0,0 +1,37 @@ +/* +Copyright 2019 The Knative Authors + +Licensed 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 v1alpha1 + +import ( + "testing" + + "k8s.io/apimachinery/pkg/runtime" +) + +func TestRegisterHelpers(t *testing.T) { + if got, want := Resource("Eventing"), "Eventing."+GroupName; got.String() != want { + t.Errorf("Resource(PodAutoscaler) = %v, want %v", got.String(), want) + } + + if got, want := SchemeGroupVersion.String(), GroupName+"/v1alpha1"; got != want { + t.Errorf("SchemeGroupVersion() = %v, want %v", got, want) + } + + scheme := runtime.NewScheme() + if err := addKnownTypes(scheme); err != nil { + t.Errorf("addKnownTypes() = %v", err) + } +}