From 2af682bc3b5d4d6d03b9234ba38812560650927b Mon Sep 17 00:00:00 2001 From: hangaoshuai Date: Mon, 5 Mar 2018 16:17:04 +0800 Subject: [PATCH] add some uts in helpers for CRD --- .../pkg/apis/apiextensions/helpers_test.go | 331 ++++++++++++++++++ 1 file changed, 331 insertions(+) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/helpers_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/helpers_test.go index 1abb4981558..df58a7cbf8e 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/helpers_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/helpers_test.go @@ -19,6 +19,7 @@ package apiextensions import ( "reflect" "testing" + "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -88,3 +89,333 @@ func TestCRDRemoveFinalizer(t *testing.T) { } } } + +func TestSetCRDCondition(t *testing.T) { + tests := []struct { + name string + crdCondition []CustomResourceDefinitionCondition + newCondition CustomResourceDefinitionCondition + expectedcrdCondition []CustomResourceDefinitionCondition + }{ + { + name: "test setCRDcondition when one condition", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + newCondition: CustomResourceDefinitionCondition{ + Type: Established, + Status: ConditionFalse, + Reason: "NotAccepted", + Message: "Not accepted", + LastTransitionTime: metav1.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC), + }, + expectedcrdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionFalse, + Reason: "NotAccepted", + Message: "Not accepted", + LastTransitionTime: metav1.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC), + }, + }, + }, + { + name: "test setCRDcondition when two condition", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionTrue, + Reason: "NoConflicts", + Message: "no conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + newCondition: CustomResourceDefinitionCondition{ + Type: NamesAccepted, + Status: ConditionFalse, + Reason: "Conflicts", + Message: "conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC), + }, + expectedcrdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionFalse, + Reason: "Conflicts", + Message: "conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC), + }, + }, + }, + { + name: "test setCRDcondition when condition needs to be appended", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + newCondition: CustomResourceDefinitionCondition{ + Type: Terminating, + Status: ConditionFalse, + Reason: "NeverEstablished", + Message: "resource was never established", + LastTransitionTime: metav1.Date(2018, 2, 1, 0, 0, 0, 0, time.UTC), + }, + expectedcrdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: Terminating, + Status: ConditionFalse, + Reason: "NeverEstablished", + Message: "resource was never established", + LastTransitionTime: metav1.Date(2018, 2, 1, 0, 0, 0, 0, time.UTC), + }, + }, + }, + } + for _, tc := range tests { + crd := generateCRDwithCondition(tc.crdCondition) + SetCRDCondition(crd, tc.newCondition) + if len(tc.expectedcrdCondition) != len(crd.Status.Conditions) { + t.Errorf("%v expected %v, got %v", tc.name, tc.expectedcrdCondition, crd.Status.Conditions) + } + for i := range tc.expectedcrdCondition { + if !IsCRDConditionEquivalent(&tc.expectedcrdCondition[i], &crd.Status.Conditions[i]) { + t.Errorf("%v expected %v, got %v", tc.name, tc.expectedcrdCondition, crd.Status.Conditions) + } + } + } +} + +func TestRemoveCRDCondition(t *testing.T) { + tests := []struct { + name string + crdCondition []CustomResourceDefinitionCondition + conditionType CustomResourceDefinitionConditionType + expectedcrdCondition []CustomResourceDefinitionCondition + }{ + { + name: "test remove CRDCondition when the conditionType meets", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionTrue, + Reason: "NoConflicts", + Message: "no conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + conditionType: NamesAccepted, + expectedcrdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2011, 1, 2, 0, 0, 0, 0, time.UTC), + }, + }, + }, + { + name: "test remove CRDCondition when the conditionType not meets", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionTrue, + Reason: "NoConflicts", + Message: "no conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + conditionType: Terminating, + expectedcrdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionTrue, + Reason: "NoConflicts", + Message: "no conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + }, + } + for _, tc := range tests { + crd := generateCRDwithCondition(tc.crdCondition) + RemoveCRDCondition(crd, tc.conditionType) + if len(tc.expectedcrdCondition) != len(crd.Status.Conditions) { + t.Errorf("%v expected %v, got %v", tc.name, tc.expectedcrdCondition, crd.Status.Conditions) + } + for i := range tc.expectedcrdCondition { + if !IsCRDConditionEquivalent(&tc.expectedcrdCondition[i], &crd.Status.Conditions[i]) { + t.Errorf("%v expected %v, got %v", tc.name, tc.expectedcrdCondition, crd.Status.Conditions) + } + } + } +} + +func TestIsCRDConditionPresentAndEqual(t *testing.T) { + tests := []struct { + name string + crdCondition []CustomResourceDefinitionCondition + conditionType CustomResourceDefinitionConditionType + status ConditionStatus + expectresult bool + }{ + { + name: "test CRDCondition is not Present", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionTrue, + Reason: "NoConflicts", + Message: "no conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + conditionType: Terminating, + status: ConditionTrue, + expectresult: false, + }, + { + name: "test CRDCondition is Present but not Equal", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionTrue, + Reason: "NoConflicts", + Message: "no conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + conditionType: Established, + status: ConditionFalse, + expectresult: false, + }, + { + name: "test CRDCondition is Present and Equal", + crdCondition: []CustomResourceDefinitionCondition{ + { + Type: Established, + Status: ConditionTrue, + Reason: "Accepted", + Message: "the initial names have been accepted", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + Type: NamesAccepted, + Status: ConditionTrue, + Reason: "NoConflicts", + Message: "no conflicts found", + LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + conditionType: NamesAccepted, + status: ConditionTrue, + expectresult: true, + }, + } + for _, tc := range tests { + crd := generateCRDwithCondition(tc.crdCondition) + res := IsCRDConditionPresentAndEqual(crd, tc.conditionType, tc.status) + if res != tc.expectresult { + t.Errorf("%v expected %t, got %t", tc.name, tc.expectresult, res) + } + } +} + +func generateCRDwithCondition(conditions []CustomResourceDefinitionCondition) *CustomResourceDefinition { + testCRDObjectMeta := metav1.ObjectMeta{ + Name: "plural.group.com", + ResourceVersion: "12", + } + testCRDSpec := CustomResourceDefinitionSpec{ + Group: "group.com", + Version: "version", + Scope: ResourceScope("Cluster"), + Names: CustomResourceDefinitionNames{ + Plural: "plural", + Singular: "singular", + Kind: "kind", + ListKind: "listkind", + }, + } + testCRDAcceptedNames := CustomResourceDefinitionNames{ + Plural: "plural", + Singular: "singular", + Kind: "kind", + ListKind: "listkind", + } + return &CustomResourceDefinition{ + ObjectMeta: testCRDObjectMeta, + Spec: testCRDSpec, + Status: CustomResourceDefinitionStatus{ + AcceptedNames: testCRDAcceptedNames, + Conditions: conditions, + }, + } +}