...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package testiam
16
17 import (
18 "testing"
19
20 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/iam/v1beta1"
21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/resourcefixture"
22 testrunner "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/runner"
23
24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25 "sigs.k8s.io/controller-runtime/pkg/manager"
26 )
27
28 type ResourceLevelTestFunc func(t *testing.T, testId string, mgr manager.Manager, rc IAMResourceContext, refResource *unstructured.Unstructured, resourceRef v1beta1.ResourceReference)
29
30
31
32
33
34 func RunResourceLevelTest(t *testing.T, mgr manager.Manager, iamTestFunc ResourceLevelTestFunc, shouldRunFunc resourcefixture.ShouldRunFunc) {
35 t.Parallel()
36 kindToIamPolicyResourceContext := getKindToIamPolicyResourceContextMap()
37 testFunc := buildTestFunc(kindToIamPolicyResourceContext, iamTestFunc)
38 shouldRun := buildShouldRunFunc(kindToIamPolicyResourceContext, shouldRunFunc)
39 testrunner.RunAllWithObjectCreated(t, mgr, shouldRun, testFunc)
40 }
41
42
43
44
45 func RunResourceLevelTestWithExternalRef(t *testing.T, mgr manager.Manager, iamTestFunc ResourceLevelTestFunc, shouldRunFunc resourcefixture.ShouldRunFunc) {
46 t.Parallel()
47 kindToIamPolicyResourceContext := getKindToIamPolicyResourceContextMap()
48 testFunc := buildTestFuncWithExternalRef(kindToIamPolicyResourceContext, iamTestFunc)
49 shouldRun := buildShouldRunFunc(kindToIamPolicyResourceContext, shouldRunFunc)
50 testrunner.RunAllWithObjectCreated(t, mgr, shouldRun, testFunc)
51 }
52
53 func buildTestFunc(kindToIamPolicyResourceContext map[string]IAMResourceContext, testFunc ResourceLevelTestFunc) testrunner.TestCaseFunc {
54 return func(t *testing.T, testContext testrunner.TestContext, sysContext testrunner.SystemContext) {
55 rc := kindToIamPolicyResourceContext[testContext.ResourceFixture.GVK.Kind]
56 refResource := testContext.CreateUnstruct
57 resourceRef := NewResourceRef(refResource)
58 testFunc(t, testContext.UniqueId, sysContext.Manager, rc, refResource, resourceRef)
59 }
60 }
61
62 func buildTestFuncWithExternalRef(kindToIamPolicyResourceContext map[string]IAMResourceContext, testFunc ResourceLevelTestFunc) testrunner.TestCaseFunc {
63 return func(t *testing.T, testContext testrunner.TestContext, sysContext testrunner.SystemContext) {
64 rc := kindToIamPolicyResourceContext[testContext.ResourceFixture.GVK.Kind]
65 refResource := testContext.CreateUnstruct
66 resourceRef, err := NewExternalRef(refResource, sysContext.TFProvider, sysContext.SMLoader)
67 if err != nil {
68 t.Fatal(err)
69 }
70 testFunc(t, testContext.UniqueId, sysContext.Manager, rc, refResource, resourceRef)
71 }
72 }
73
74 func buildShouldRunFunc(kindToIamPolicyResourceContext map[string]IAMResourceContext, additionalShouldRunFunc resourcefixture.ShouldRunFunc) testrunner.ShouldRunFunc {
75 return func(fixture resourcefixture.ResourceFixture, mgr manager.Manager) bool {
76 if additionalShouldRunFunc != nil {
77 if !additionalShouldRunFunc(fixture) {
78 return false
79 }
80 }
81
82 if fixture.Type != resourcefixture.Basic {
83 return false
84 }
85
86 rc, ok := kindToIamPolicyResourceContext[fixture.GVK.Kind]
87 if !ok {
88 return false
89 }
90
91 if rc.Name == "" {
92 return true
93 }
94 return rc.Name == fixture.Name
95 }
96 }
97
98 func getKindToIamPolicyResourceContextMap() map[string]IAMResourceContext {
99 results := make(map[string]IAMResourceContext)
100 for _, r := range ResourceContexts {
101 results[r.Kind] = r
102 }
103 return results
104 }
105
View as plain text