...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/template/template_test.go

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

     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 crdtemplate_test
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdgeneration"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdloader"
    24  	crdtemplate "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/template"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/testutils"
    26  
    27  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    28  )
    29  
    30  func TestAllCRDsShouldConvertToYAML(t *testing.T) {
    31  	crds, err := crdloader.LoadCRDs()
    32  	if err != nil {
    33  		t.Fatalf("error loading crds: %v", err)
    34  	}
    35  	for _, crd := range crds {
    36  		specToYAML(t, &crd)
    37  		statusToYAML(t, &crd)
    38  	}
    39  }
    40  
    41  func TestSpecAndStatusToYAML(t *testing.T) {
    42  	// when adding a new type or updating the test data file run this test with the '-update' parameter to update the 'golden files'
    43  	testToYAML(t, "ComputeInstance")
    44  	testToYAML(t, "ComputeBackendService")
    45  	testToYAML(t, "PubSubTopic")
    46  	testToYAML(t, "PubSubSubscription")
    47  	testToYAML(t, "SpannerDatabase")
    48  }
    49  
    50  func TestAllLoadedCRDHaveManagedByKCCLabel(t *testing.T) {
    51  	crds, err := crdloader.LoadCRDs()
    52  	if err != nil {
    53  		t.Fatalf("error loading crds: %v", err)
    54  	}
    55  
    56  	for _, crd := range crds {
    57  		if _, ok := crd.Labels[crdgeneration.ManagedByKCCLabel]; !ok {
    58  			t.Errorf("%v CRD missing %v label", crd.Kind, crdgeneration.ManagedByKCCLabel)
    59  		}
    60  	}
    61  }
    62  
    63  func testToYAML(t *testing.T, resourceKind string) {
    64  	crd := getCRDForKind(t, resourceKind)
    65  	bytes := specToYAML(t, crd)
    66  	testutils.VerifyContentsMatch(t, bytes, fmt.Sprintf("testdata/%v-spec.yaml.golden", strings.ToLower(resourceKind)))
    67  	bytes = statusToYAML(t, crd)
    68  	testutils.VerifyContentsMatch(t, bytes, fmt.Sprintf("testdata/%v-status.yaml.golden", strings.ToLower(resourceKind)))
    69  }
    70  
    71  func getCRDForKind(t *testing.T, kind string) *apiextensions.CustomResourceDefinition {
    72  	crd, err := crdloader.GetCRDForKind(kind)
    73  	if err != nil {
    74  		t.Fatalf("error getting crd: %v", err)
    75  	}
    76  	return crd
    77  }
    78  
    79  func specToYAML(t *testing.T, crd *apiextensions.CustomResourceDefinition) []byte {
    80  	bytes, err := crdtemplate.SpecToYAML(crd)
    81  	if err != nil {
    82  		t.Fatalf("error converting crd spec to YAML: %v", err)
    83  	}
    84  	return bytes
    85  }
    86  
    87  func statusToYAML(t *testing.T, crd *apiextensions.CustomResourceDefinition) []byte {
    88  	bytes, err := crdtemplate.StatusToYAML(crd)
    89  	if err != nil {
    90  		t.Fatalf("error converting crd spec to YAML: %v", err)
    91  	}
    92  	return bytes
    93  }
    94  

View as plain text