...
1
2
3
4
5
6
7
8
9
10
11
12
13
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
49
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