Skip to content
This repository was archived by the owner on Feb 22, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 237 additions & 0 deletions pkg/apis/eventing/v1alpha1/knativeeventing_lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
10 changes: 9 additions & 1 deletion pkg/apis/eventing/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions pkg/apis/eventing/v1alpha1/register_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}