1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package resourcefixture_test
16
17 import (
18 "strings"
19 "testing"
20
21 dclmetadata "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata"
22 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
23 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
24 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
25 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/resourcefixture"
26 testservicemapping "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/servicemapping"
27 testservicemappingloader "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/servicemappingloader"
28 testyaml "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/yaml"
29
30 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
31 )
32
33
34 func TestGetBasicTypeSetCover(t *testing.T) {
35 smLoader := testservicemappingloader.New(t)
36 serviceMetadataLoader := dclmetadata.New()
37 basicFixtures := resourcefixture.GetBasicTypeSetCover(t)
38 resourceConfigIds := getResourceConfigIdSet(t, smLoader)
39 notFoundResourceConfigIds := make(map[string]bool, len(resourceConfigIds))
40 for k, v := range resourceConfigIds {
41 notFoundResourceConfigIds[k] = v
42 }
43 for _, f := range basicFixtures {
44 verifyResourceConfigIdAndDeleteFromNotFound(t, smLoader, serviceMetadataLoader, f.Create, resourceConfigIds, notFoundResourceConfigIds)
45 for _, d := range testyaml.SplitYAML(t, f.Dependencies) {
46 verifyResourceConfigIdAndDeleteFromNotFound(t, smLoader, serviceMetadataLoader, d, resourceConfigIds, notFoundResourceConfigIds)
47 }
48 }
49 if len(notFoundResourceConfigIds) > 0 {
50 resourceConfigIds := make([]string, 0, len(notFoundResourceConfigIds))
51 for k := range notFoundResourceConfigIds {
52 resourceConfigIds = append(resourceConfigIds, k)
53 }
54 t.Fatalf("resources covering the config(s) of '%v' are missing from the basic resource set cover", strings.Join(resourceConfigIds, ", "))
55 }
56 }
57
58 func verifyResourceConfigIdAndDeleteFromNotFound(t *testing.T, smLoader *servicemappingloader.ServiceMappingLoader, serviceMetadataLoader dclmetadata.ServiceMetadataLoader, yamlBytes []byte, resourceConfigIds, notFound map[string]bool) {
59 t.Helper()
60 u := test.ToUnstruct(t, yamlBytes)
61 if !resourcefixture.ShouldHaveResourceConfig(u, serviceMetadataLoader) {
62 return
63 }
64 rc := testservicemapping.GetResourceConfig(t, smLoader, u)
65 rcId := resourcefixture.GetUniqueResourceConfigId(*rc)
66
67 if _, ok := resourceConfigIds[rcId]; !ok {
68 t.Fatalf("unexpected missing resource name: %v", rcId)
69 }
70 delete(notFound, rcId)
71 }
72
73 func getResourceConfigIdSet(t *testing.T, smLoader *servicemappingloader.ServiceMappingLoader) map[string]bool {
74 sms := smLoader.GetServiceMappings()
75 resourceConfigIds := make(map[string]bool, 0)
76 for _, sm := range sms {
77 for _, rc := range sm.Spec.Resources {
78
79
80 if sm.GetVersionFor(&rc) == k8s.KCCAPIVersionV1Alpha1 && rc.AutoGenerated {
81 continue
82 }
83 id := resourcefixture.GetUniqueResourceConfigId(rc)
84 if _, ok := resourceConfigIds[id]; ok {
85 t.Fatalf("unexpected scenario: resource config '%v' and another resource config resolve to the same id '%v'",
86 rc.Name, id)
87 }
88 resourceConfigIds[id] = true
89 }
90 }
91
92
93 delete(resourceConfigIds, "AccessContextManagerAccessPolicy")
94 return resourceConfigIds
95 }
96
97 func TestNamesAreUniqueWithinTheSameTestType(t *testing.T) {
98
99 fixtures := resourcefixture.Load(t)
100 namesPerTestType := make(map[resourcefixture.TestType]map[string]bool)
101 for _, f := range fixtures {
102 if _, ok := namesPerTestType[f.Type]; !ok {
103 namesPerTestType[f.Type] = make(map[string]bool)
104 }
105 names := namesPerTestType[f.Type]
106 if _, ok := names[f.Name]; ok {
107 t.Fatalf("cannot have two fixtures with the same type '%v' and name '%v'", f.Type, f.Name)
108 }
109 names[f.Name] = true
110 }
111 }
112
113 func TestResourceContents(t *testing.T) {
114 fixtures := resourcefixture.Load(t)
115 for _, f := range fixtures {
116 if f.Update == nil {
117 continue
118 }
119 createUnstruct := test.ToUnstruct(t, f.Create)
120 updateUnstruct := test.ToUnstruct(t, f.Update)
121 testCreateAndUpdateUnstructMatchingProperties(t, f, createUnstruct, updateUnstruct)
122 dependencies := make([]*unstructured.Unstructured, 0)
123 testAllNamesAreUnique(t, f, append(dependencies, createUnstruct)...)
124 }
125 }
126
127 func testCreateAndUpdateUnstructMatchingProperties(t *testing.T, fixture resourcefixture.ResourceFixture,
128 createUnstruct, updateUnstruct *unstructured.Unstructured) {
129 testCreateAndUpdateUnstructShouldMatchNameAndNamespace(t, fixture, createUnstruct, updateUnstruct)
130 testCreateAndUpdateUnstructShouldHaveTheSameGVK(t, fixture, createUnstruct, updateUnstruct)
131 }
132
133 func testCreateAndUpdateUnstructShouldMatchNameAndNamespace(t *testing.T, fixture resourcefixture.ResourceFixture,
134 createUnstruct, updateUnstruct *unstructured.Unstructured) {
135 if createUnstruct.GetName() != updateUnstruct.GetName() {
136 t.Errorf("name mismatch between create and update for fixture '%v': create(%v), update(%v)",
137 fixture.Name, createUnstruct.GetName(), updateUnstruct.GetName())
138 }
139 if createUnstruct.GetNamespace() != updateUnstruct.GetNamespace() {
140 t.Errorf("namespace mismatch between create and update for fixture '%v': create(%v), update(%v)",
141 fixture.Name, createUnstruct.GetNamespace(), updateUnstruct.GetNamespace())
142 }
143 }
144
145 func testCreateAndUpdateUnstructShouldHaveTheSameGVK(t *testing.T, fixture resourcefixture.ResourceFixture,
146 createUnstruct, updateUnstruct *unstructured.Unstructured) {
147 if createUnstruct.GroupVersionKind() != updateUnstruct.GroupVersionKind() {
148 t.Errorf("gvk mismatch between create and update for fixture '%v': create(%v), update(%v)",
149 fixture.Name, createUnstruct.GroupVersionKind(), updateUnstruct.GroupVersionKind())
150 }
151 }
152
153 func testAllNamesAreUnique(t *testing.T, fixture resourcefixture.ResourceFixture, resources ...*unstructured.Unstructured) {
154 names := make(map[string]*unstructured.Unstructured)
155 for _, r := range resources {
156 if _, ok := names[r.GetName()]; ok {
157 t.Errorf("fixture '%v' has more than one resource with name '%v': all resource names within a fixture must be unique",
158 fixture.Name, r.GetName())
159 }
160 names[r.GetName()] = r
161 }
162 }
163
View as plain text