...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/fielddesc/fielddesc_test.go

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

     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 fielddesc_test
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdloader"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/fielddesc"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/testutils"
    25  
    26  	"gopkg.in/yaml.v2"
    27  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    28  )
    29  
    30  func TestAllCRDsGetSpecAndStatusDescription(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  		fd := fielddesc.GetSpecDescription(&crd)
    37  		expectedType := "object"
    38  		if fd.Type != expectedType {
    39  			t.Fatalf("unexpected type: got '%v', want' %v'", fd.Type, expectedType)
    40  		}
    41  		fd = getStatusDescription(t, &crd)
    42  		if fd.Type != expectedType {
    43  			t.Fatalf("unexpected type: got '%v', want' %v'", fd.Type, expectedType)
    44  		}
    45  	}
    46  }
    47  
    48  // note: when updating the schema of the CRDs below these tests will likely fail. You can update the
    49  // expected test data by running the test with the -update flag.
    50  func TestOutputMatches(t *testing.T) {
    51  	testOutputMatches(t, "AccessContextManagerAccessLevel")
    52  	testOutputMatches(t, "BinaryAuthorizationPolicy")
    53  	testOutputMatches(t, "PubSubSubscription")
    54  }
    55  
    56  func testOutputMatches(t *testing.T, resourceKind string) {
    57  	crd, err := crdloader.GetCRDForKind(resourceKind)
    58  	if err != nil {
    59  		t.Fatalf("error getting crd '%v': %v", resourceKind, err)
    60  	}
    61  	fd := fielddesc.GetSpecDescription(crd)
    62  	bytes := fieldDescToYAML(t, fd)
    63  	testutils.VerifyContentsMatch(t, bytes, fmt.Sprintf("testdata/%v-spec.golden.yaml", strings.ToLower(resourceKind)))
    64  	fd = getStatusDescription(t, crd)
    65  	bytes = fieldDescToYAML(t, fd)
    66  	testutils.VerifyContentsMatch(t, bytes, fmt.Sprintf("testdata/%v-status.golden.yaml", strings.ToLower(resourceKind)))
    67  }
    68  
    69  func getStatusDescription(t *testing.T, crd *apiextensions.CustomResourceDefinition) fielddesc.FieldDescription {
    70  	fd, err := fielddesc.GetStatusDescription(crd)
    71  	if err != nil {
    72  		t.Fatalf("error getting status description")
    73  	}
    74  	return fd
    75  }
    76  
    77  func fieldDescToYAML(t *testing.T, fieldDesc fielddesc.FieldDescription) []byte {
    78  	bytes, err := yaml.Marshal(fieldDesc)
    79  	if err != nil {
    80  		t.Fatalf("error marshalling to yaml: %v", err)
    81  	}
    82  	return bytes
    83  }
    84  

View as plain text