github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/crd/crd_test.go (about)

     1  package crd
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func Test_List(t *testing.T) {
     8  	crdV1, err := ListV1()
     9  	if err != nil {
    10  		t.Fatalf("expected err to be nil: %s", err)
    11  	}
    12  
    13  	if crdV1 == nil {
    14  		t.Fatal("expected crd slice to not be nil")
    15  	}
    16  
    17  	if len(crdV1) == 0 {
    18  		t.Fatal("expected crd slice to contain at least one item")
    19  	}
    20  }
    21  
    22  func Test_Load(t *testing.T) {
    23  	nilPanicMatcher := func(ret interface{}) bool {
    24  		return ret == nil
    25  	}
    26  	notFoundPanicMatcher := func(ret interface{}) bool {
    27  		err, ok := ret.(error)
    28  		return ok && IsNotFound(err)
    29  	}
    30  	testCases := []struct {
    31  		name            string
    32  		inputGroup      string
    33  		inputKind       string
    34  		inputCRDVersion string
    35  		panicMatcher    func(ret interface{}) bool
    36  	}{
    37  		{
    38  			name:            "case 0: v1beta1 CRD loads normally",
    39  			inputGroup:      "application.giantswarm.io",
    40  			inputKind:       "App",
    41  			inputCRDVersion: "v1beta1",
    42  			panicMatcher:    nilPanicMatcher,
    43  		},
    44  		{
    45  			name:            "case 1: non-existent v1beta1 CRD panics with notFoundError",
    46  			inputGroup:      "application.giantswarm.io",
    47  			inputKind:       "Bad",
    48  			inputCRDVersion: "v1beta1",
    49  			panicMatcher:    notFoundPanicMatcher,
    50  		},
    51  		{
    52  			name:            "case 2: v1 CRD loads normally",
    53  			inputGroup:      "infrastructure.giantswarm.io",
    54  			inputKind:       "AWSCluster",
    55  			inputCRDVersion: "v1",
    56  			panicMatcher:    nilPanicMatcher,
    57  		},
    58  		{
    59  			name:            "case 3: non-existent v1 CRD panics with notFoundError",
    60  			inputGroup:      "application.giantswarm.io",
    61  			inputKind:       "Bad",
    62  			inputCRDVersion: "v1",
    63  			panicMatcher:    notFoundPanicMatcher,
    64  		},
    65  	}
    66  
    67  	for _, tc := range testCases {
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			defer func() {
    70  				err := recover()
    71  				if !tc.panicMatcher(err) {
    72  					t.Errorf("unexpected panic: %#v", err)
    73  				}
    74  			}()
    75  			var crd interface{}
    76  			switch tc.inputCRDVersion {
    77  			case "v1beta1":
    78  				crd = LoadV1Beta1(tc.inputGroup, tc.inputKind)
    79  			case "v1":
    80  				crd = LoadV1(tc.inputGroup, tc.inputKind)
    81  			}
    82  			if crd == nil {
    83  				t.Errorf("nil crd")
    84  			}
    85  		})
    86  	}
    87  }