...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides/overrides_test.go

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

     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 resourceoverrides_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides"
    22  
    23  	"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
    24  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    27  )
    28  
    29  var (
    30  	fooKind = "Foo"
    31  	barKind = "Bar"
    32  	bazKind = "Baz"
    33  )
    34  
    35  func TestResourceOverridesHandler(t *testing.T) {
    36  	t.Parallel()
    37  	handler := resourceoverrides.NewResourceOverridesHandler()
    38  	handler.Register(getFooOverrides())
    39  	handler.Register(getBazOverrides())
    40  
    41  	fooUnstruc := &unstructured.Unstructured{}
    42  	fooUnstruc.SetKind(fooKind)
    43  	barUnstruc := &unstructured.Unstructured{}
    44  	barUnstruc.SetKind(barKind)
    45  	bazUnstruc := &unstructured.Unstructured{}
    46  	bazUnstruc.SetKind(bazKind)
    47  
    48  	tests := []struct {
    49  		name              string
    50  		kind              string
    51  		crd               *apiextensions.CustomResourceDefinition
    52  		u                 *unstructured.Unstructured
    53  		original          *k8s.Resource
    54  		reconciled        *k8s.Resource
    55  		hasOverrides      bool
    56  		hasConfigValidate bool
    57  	}{
    58  		{
    59  			name: "resource Foo with overrides",
    60  			kind: fooKind,
    61  			crd: &apiextensions.CustomResourceDefinition{
    62  				Spec: apiextensions.CustomResourceDefinitionSpec{
    63  					Names: apiextensions.CustomResourceDefinitionNames{
    64  						Kind: fooKind,
    65  					},
    66  				},
    67  			},
    68  			u: fooUnstruc,
    69  			original: &k8s.Resource{
    70  				TypeMeta: v1.TypeMeta{
    71  					Kind: fooKind,
    72  				},
    73  			},
    74  			reconciled: &k8s.Resource{
    75  				TypeMeta: v1.TypeMeta{
    76  					Kind: fooKind,
    77  				},
    78  			},
    79  			hasConfigValidate: true,
    80  			hasOverrides:      true,
    81  		},
    82  		{
    83  			name: "resource Bar with no override",
    84  			kind: barKind,
    85  			crd: &apiextensions.CustomResourceDefinition{
    86  				Spec: apiextensions.CustomResourceDefinitionSpec{
    87  					Names: apiextensions.CustomResourceDefinitionNames{
    88  						Kind: barKind,
    89  					},
    90  				},
    91  			},
    92  			u: barUnstruc,
    93  			original: &k8s.Resource{
    94  				TypeMeta: v1.TypeMeta{
    95  					Kind: barKind,
    96  				},
    97  			},
    98  			reconciled: &k8s.Resource{
    99  				TypeMeta: v1.TypeMeta{
   100  					Kind: barKind,
   101  				},
   102  			},
   103  			hasOverrides:      false,
   104  			hasConfigValidate: false,
   105  		},
   106  		{
   107  			name: "resource baz has overrides with no ConfigValidate",
   108  			kind: bazKind,
   109  			crd: &apiextensions.CustomResourceDefinition{
   110  				Spec: apiextensions.CustomResourceDefinitionSpec{
   111  					Names: apiextensions.CustomResourceDefinitionNames{
   112  						Kind: bazKind,
   113  					},
   114  				},
   115  			},
   116  			u: bazUnstruc,
   117  			original: &k8s.Resource{
   118  				TypeMeta: v1.TypeMeta{
   119  					Kind: bazKind,
   120  				},
   121  			},
   122  			reconciled: &k8s.Resource{
   123  				TypeMeta: v1.TypeMeta{
   124  					Kind: bazKind,
   125  				},
   126  			},
   127  			hasOverrides:      true,
   128  			hasConfigValidate: false,
   129  		},
   130  	}
   131  
   132  	for _, tc := range tests {
   133  		tc := tc
   134  		t.Run(tc.name, func(t *testing.T) {
   135  			if tc.hasOverrides != handler.HasOverrides(tc.kind) {
   136  				t.Fatalf("expect HasOverrides() for kind %v to return %v", tc.kind, tc.hasOverrides)
   137  			}
   138  			if tc.hasConfigValidate != handler.HasConfigValidate(tc.kind) {
   139  				t.Fatalf("expect HasConfigValidate() for kind %v to return %v", tc.kind, tc.hasConfigValidate)
   140  			}
   141  			if err := handler.CRDDecorate(tc.crd); err != nil {
   142  				t.Fatalf("unexpected error: %v", err)
   143  			}
   144  			if err := handler.ConfigValidate(tc.u); err != nil {
   145  				t.Fatalf("unexpected error: %v", err)
   146  			}
   147  			if err := handler.PreActuationTransform(tc.original); err != nil {
   148  				t.Fatalf("unexpected error: %v", err)
   149  			}
   150  			if err := handler.PostActuationTransform(tc.original, tc.reconciled, nil, nil); err != nil {
   151  				t.Fatalf("unexpected error: %v", err)
   152  			}
   153  		})
   154  	}
   155  }
   156  
   157  func getBazOverrides() resourceoverrides.ResourceOverrides {
   158  	ro := resourceoverrides.ResourceOverrides{
   159  		Kind: bazKind,
   160  	}
   161  	o1 := resourceoverrides.ResourceOverride{}
   162  	o1.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
   163  		return nil
   164  	}
   165  	return ro
   166  }
   167  
   168  func getFooOverrides() resourceoverrides.ResourceOverrides {
   169  	ro := resourceoverrides.ResourceOverrides{
   170  		Kind: fooKind,
   171  	}
   172  	o1 := resourceoverrides.ResourceOverride{}
   173  	o1.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
   174  		return nil
   175  	}
   176  	o1.ConfigValidate = func(r *unstructured.Unstructured) error {
   177  		return nil
   178  	}
   179  	o1.PostActuationTransform = func(original, reconciled *k8s.Resource, tfState *terraform.InstanceState, dclState *unstructured.Unstructured) error {
   180  		return nil
   181  	}
   182  	o1.PreActuationTransform = func(r *k8s.Resource) error {
   183  		return nil
   184  	}
   185  	// ResourceOverride only modifies the CRD
   186  	o2 := resourceoverrides.ResourceOverride{}
   187  	o2.CRDDecorate = func(crd *apiextensions.CustomResourceDefinition) error {
   188  		return nil
   189  	}
   190  	ro.Overrides = append(ro.Overrides, o1)
   191  	ro.Overrides = append(ro.Overrides, o2)
   192  	return ro
   193  }
   194  

View as plain text