...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension/container/container_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension/container

     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 container_test
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	corekccv1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension/container"
    23  	testdclschemaloader "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/dclschemaloader"
    24  	testservicemetadataloader "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/servicemetadataloader"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	"github.com/nasa9084/go-openapi"
    28  	k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
    29  )
    30  
    31  func TestGetContainersForGVK(t *testing.T) {
    32  	tests := []struct {
    33  		name      string
    34  		gvk       k8sschema.GroupVersionKind
    35  		expected  []corekccv1alpha1.Container
    36  		shouldErr bool
    37  	}{
    38  		{
    39  			name: "resource with project container",
    40  			gvk: k8sschema.GroupVersionKind{
    41  				Group:   "test4.cnrm.cloud.google.com",
    42  				Version: "v1alpha1",
    43  				Kind:    "Test4ProjectContainer",
    44  			},
    45  			expected: []corekccv1alpha1.Container{
    46  				{
    47  					TFField: "project",
    48  					Type:    corekccv1alpha1.ContainerTypeProject,
    49  				},
    50  			},
    51  		},
    52  		{
    53  			name: "resource with organization container",
    54  			gvk: k8sschema.GroupVersionKind{
    55  				Group:   "test4.cnrm.cloud.google.com",
    56  				Version: "v1alpha1",
    57  				Kind:    "Test4OrganizationContainer",
    58  			},
    59  			expected: []corekccv1alpha1.Container{
    60  				{
    61  					TFField: "organization",
    62  					Type:    corekccv1alpha1.ContainerTypeOrganization,
    63  				},
    64  			},
    65  		},
    66  		{
    67  			name: "resource with no x-dcl-parent-container extension",
    68  			gvk: k8sschema.GroupVersionKind{
    69  				Group:   "test4.cnrm.cloud.google.com",
    70  				Version: "v1alpha1",
    71  				Kind:    "Test4NoContainer",
    72  			},
    73  			expected: nil,
    74  		},
    75  		{
    76  			name: "resource with no x-dcl-parent-container extension but should support container annotations",
    77  			gvk: k8sschema.GroupVersionKind{
    78  				Group:   "test4.cnrm.cloud.google.com",
    79  				Version: "v1alpha1",
    80  				Kind:    "Test4NoContainerExtensionButSupportsContainers",
    81  			},
    82  			expected: []corekccv1alpha1.Container{
    83  				{
    84  					TFField: "project",
    85  					Type:    corekccv1alpha1.ContainerTypeProject,
    86  				},
    87  			},
    88  		},
    89  		{
    90  			name: "resource with no x-dcl-parent-container extension, and should support container annotations, but has no hierarchical references",
    91  			gvk: k8sschema.GroupVersionKind{
    92  				Group:   "test4.cnrm.cloud.google.com",
    93  				Version: "v1alpha1",
    94  				Kind:    "Test4NoContainerExtensionAndSupportsContainersButHasNoHierarchicalRefs",
    95  			},
    96  			shouldErr: true,
    97  		},
    98  	}
    99  
   100  	schemaLoader := testdclschemaloader.New(dclSchemaMap)
   101  	smLoader := testservicemetadataloader.NewForUnitTest()
   102  	for _, tc := range tests {
   103  		tc := tc
   104  		t.Run(tc.name, func(t *testing.T) {
   105  			t.Parallel()
   106  			actual, err := container.GetContainersForGVK(tc.gvk, smLoader, schemaLoader)
   107  			if tc.shouldErr {
   108  				if err == nil {
   109  					t.Fatalf("expected error but got none")
   110  				}
   111  				return
   112  			}
   113  			if err != nil {
   114  				t.Fatalf("got unexpected error: %v", err)
   115  			}
   116  			if !reflect.DeepEqual(tc.expected, actual) {
   117  				t.Fatalf("got unexpected containers (-want +got): \n%v", cmp.Diff(tc.expected, actual))
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  var dclSchemaMap = map[string]*openapi.Schema{
   124  	"test4_beta_projectcontainer": &openapi.Schema{
   125  		Type: "object",
   126  		Properties: map[string]*openapi.Schema{
   127  			"project": &openapi.Schema{
   128  				Type: "string",
   129  			},
   130  			"name": &openapi.Schema{
   131  				Type: "string",
   132  			},
   133  		},
   134  		Extension: map[string]interface{}{
   135  			"x-dcl-parent-container": "project",
   136  		},
   137  	},
   138  	"test4_beta_organizationcontainer": &openapi.Schema{
   139  		Type: "object",
   140  		Properties: map[string]*openapi.Schema{
   141  			"organization": &openapi.Schema{
   142  				Type: "string",
   143  			},
   144  			"name": &openapi.Schema{
   145  				Type: "string",
   146  			},
   147  		},
   148  		Extension: map[string]interface{}{
   149  			"x-dcl-parent-container": "organization",
   150  		},
   151  	},
   152  	"test4_beta_nocontainer": &openapi.Schema{
   153  		Type: "object",
   154  		Properties: map[string]*openapi.Schema{
   155  			"project": &openapi.Schema{
   156  				Type: "string",
   157  			},
   158  			"name": &openapi.Schema{
   159  				Type: "string",
   160  			},
   161  		},
   162  	},
   163  	"test4_beta_nocontainerextensionbutsupportscontainers": &openapi.Schema{
   164  		Type: "object",
   165  		Properties: map[string]*openapi.Schema{
   166  			"project": &openapi.Schema{
   167  				Type: "string",
   168  			},
   169  			"name": &openapi.Schema{
   170  				Type: "string",
   171  			},
   172  		},
   173  	},
   174  	"test4_beta_nocontainerextensionandsupportscontainersbuthasnohierarchicalrefs": &openapi.Schema{
   175  		Type: "object",
   176  		Properties: map[string]*openapi.Schema{
   177  			"name": &openapi.Schema{
   178  				Type: "string",
   179  			},
   180  		},
   181  	},
   182  }
   183  

View as plain text