...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gvks/supportedgvks/supportedgvks_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gvks/supportedgvks

     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 supportedgvks_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/iam/v1beta1"
    21  	dclmetadata "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gvks/supportedgvks"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
    24  	testservicemappingloader "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/servicemappingloader"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  )
    27  
    28  func TestAllIncludesIAMResource(t *testing.T) {
    29  	allResources := supportedgvks.All(testservicemappingloader.New(t), dclmetadata.New())
    30  	iamResources := []schema.GroupVersionKind{
    31  		v1beta1.IAMAuditConfigGVK,
    32  		v1beta1.IAMPolicyGVK,
    33  		v1beta1.IAMPolicyMemberGVK,
    34  	}
    35  	for _, iamResource := range iamResources {
    36  		assertIncludesResource(t, allResources, iamResource)
    37  	}
    38  }
    39  
    40  func assertIncludesResource(t *testing.T, resources []schema.GroupVersionKind, resource schema.GroupVersionKind) {
    41  	for _, r := range resources {
    42  		if r == resource {
    43  			return
    44  		}
    45  	}
    46  	t.Errorf("expected list of resources to contain resource '%v', but it did not", resource)
    47  }
    48  
    49  func TestNoResourceIsDoubleDeclared(t *testing.T) {
    50  	smLoader, err := servicemappingloader.New()
    51  	if err != nil {
    52  		t.Fatalf("error creating new service mapping loader: %v", err)
    53  	}
    54  	serviceMetadataLoader := dclmetadata.New()
    55  	tfBasedResources := supportedgvks.BasedOnAllServiceMappings(smLoader)
    56  	dclBasedResources := supportedgvks.BasedOnDCL(serviceMetadataLoader)
    57  	var u []string
    58  	resourceMap := make(map[string]bool)
    59  	for _, gvk := range tfBasedResources {
    60  		resourceMap[gvk.Kind] = true
    61  	}
    62  	for _, gvk := range dclBasedResources {
    63  		if _, ok := resourceMap[gvk.Kind]; ok {
    64  			u = append(u, gvk.Kind)
    65  		}
    66  	}
    67  	if len(u) != 0 {
    68  		t.Fatalf("resources %v have been declared in both ServiceMapping(TF/KCC bridge) and ServiceMetadata(DCL/KCC bridge)", u)
    69  	}
    70  }
    71  

View as plain text