...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/iam/resource_level_test_runner.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/iam

     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 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  // Runs a resource level test against all resources.
    31  // testFunc will be executed once for each resource that supports IAMPolicy.
    32  // shouldRunFunc is optional, it can be supplied to skip tests for resources that don't support a given operation
    33  // (e.g. deleting the IAMPolicy on a storage bucket)
    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  // Runs a resource level test against all resources, but creates an external
    43  // resource reference to point to the referenced resource instead of a regular
    44  // resource reference.
    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  		// If there is no specific test case name defined, run all the test cases for the kind.
    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