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