...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdloader/crdloader_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdloader

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package crdloader_test
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdloader"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo"
    24  
    25  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    26  	"k8s.io/client-go/kubernetes/scheme"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    29  )
    30  
    31  const (
    32  	PubSubGroup     = "pubsub.cnrm.cloud.google.com"
    33  	PubSubTopicKind = "PubSubTopic"
    34  	PubSubVersion   = "v1beta1"
    35  )
    36  
    37  type CRDTestCase struct {
    38  	Name          string
    39  	ShouldSucceed bool
    40  	Group         string
    41  	Version       string
    42  	Kind          string
    43  }
    44  
    45  var crdTestCases = []CRDTestCase{
    46  	{"AllNil", false, "", "", ""},
    47  	{"UnknownKind", false, "", "", "MyNewKind"},
    48  	{"NoKind", false, PubSubGroup, PubSubVersion, ""},
    49  	{"JustKind", true, "", "", PubSubTopicKind},
    50  	{"InvalidVersion", false, "", "v1invalid1", PubSubTopicKind},
    51  	{"KindAndVersion", true, "", PubSubVersion, PubSubTopicKind},
    52  	{"InvalidGroup", false, "invalidgroup.google.com", "", PubSubTopicKind},
    53  	{"GroupAndKind", true, PubSubGroup, "", PubSubTopicKind},
    54  	{"GroupKindAndVersion", true, PubSubGroup, PubSubVersion, PubSubTopicKind},
    55  }
    56  
    57  func TestGetCRD(t *testing.T) {
    58  	for _, tc := range crdTestCases {
    59  		t.Run(tc.Name, func(t *testing.T) {
    60  			getCRDAssertResult(t, tc, crdloader.GetCRD)
    61  		})
    62  	}
    63  }
    64  
    65  func TestCrdLoader_GetCRD(t *testing.T) {
    66  	env := &envtest.Environment{
    67  		CRDDirectoryPaths:        []string{repo.GetCRDsPath()},
    68  		ControlPlaneStartTimeout: time.Minute,
    69  	}
    70  	cfg, err := env.Start()
    71  	if err != nil {
    72  		t.Fatalf("error starting test environment: %v", err)
    73  	}
    74  	if err := apiextensions.SchemeBuilder.AddToScheme(scheme.Scheme); err != nil {
    75  		t.Fatalf("error adding to scheme: %v", err)
    76  	}
    77  	clientOptions := client.Options{
    78  		Scheme: scheme.Scheme,
    79  	}
    80  	kubeClient, err := client.New(cfg, clientOptions)
    81  	if err != nil {
    82  		t.Fatalf("error creating k8s client: %v", err)
    83  	}
    84  	for _, tc := range crdTestCases {
    85  		t.Run(tc.Name, func(t *testing.T) {
    86  			loader := crdloader.New(kubeClient)
    87  			getCRDAssertResult(t, tc, loader.GetCRD)
    88  		})
    89  	}
    90  }
    91  
    92  func getCRDAssertResult(t *testing.T, tc CRDTestCase, getCRDFunc func(string, string, string) (*apiextensions.CustomResourceDefinition, error)) {
    93  	crd, err := getCRDFunc(tc.Group, tc.Version, tc.Kind)
    94  	if tc.ShouldSucceed {
    95  		if err != nil {
    96  			t.Fatalf("unexpected error: %v", err)
    97  		}
    98  	} else {
    99  		if err == nil {
   100  			t.Fatalf("expected error, instead got 'nil'")
   101  		}
   102  		return
   103  	}
   104  	if crd.Spec.Names.Kind != tc.Kind {
   105  		t.Errorf("mismatched value for 'kind': got '%v', want '%v'", crd.Spec.Names.Kind, tc.Kind)
   106  	}
   107  	if tc.Group != "" && crd.Spec.Group != tc.Group {
   108  		t.Errorf("mismatched value for 'group': got '%v', want '%v'", crd.Spec.Group, tc.Group)
   109  	}
   110  	version := k8s.GetVersionFromCRD(crd)
   111  	if tc.Version != "" && version != tc.Version {
   112  		t.Errorf("mismatched value for 'version': got '%v', want '%v'", version, tc.Version)
   113  	}
   114  }
   115  

View as plain text