...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/krmtotf/mutableunreadable_test.go

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

     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 krmtotf_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/krmtotf"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util"
    24  )
    25  
    26  func TestMutableButUnreadableFieldsAnnotationFor(t *testing.T) {
    27  	tests := []struct {
    28  		name                                    string
    29  		resource                                *krmtotf.Resource
    30  		expectedMutableButUnreadableFieldsState map[string]interface{}
    31  	}{
    32  		{
    33  			name: "top-level fields",
    34  			resource: &krmtotf.Resource{
    35  				Resource: k8s.Resource{
    36  					Spec: map[string]interface{}{
    37  						"fieldA": "val1",
    38  						"fieldB": "val2",
    39  						"fieldC": map[string]interface{}{
    40  							"field1": "val1",
    41  							"field2": "val2",
    42  						},
    43  						"fieldD": []interface{}{
    44  							"val1",
    45  							"val2",
    46  						},
    47  					},
    48  				},
    49  				ResourceConfig: v1alpha1.ResourceConfig{
    50  					MutableButUnreadableFields: []string{
    51  						"field_a",
    52  						"field_c",
    53  						"field_d",
    54  					},
    55  				},
    56  			},
    57  			expectedMutableButUnreadableFieldsState: map[string]interface{}{
    58  				"spec": map[string]interface{}{
    59  					"fieldA": "val1",
    60  					"fieldC": map[string]interface{}{
    61  						"field1": "val1",
    62  						"field2": "val2",
    63  					},
    64  					"fieldD": []interface{}{
    65  						"val1",
    66  						"val2",
    67  					},
    68  				},
    69  			},
    70  		},
    71  		{
    72  			name: "nested fields",
    73  			resource: &krmtotf.Resource{
    74  				Resource: k8s.Resource{
    75  					Spec: map[string]interface{}{
    76  						"parentField": map[string]interface{}{
    77  							"fieldA": "val1",
    78  							"fieldB": "val2",
    79  							"fieldC": map[string]interface{}{
    80  								"field1": "val1",
    81  								"field2": "val2",
    82  							},
    83  							"fieldD": []interface{}{
    84  								"val1",
    85  								"val2",
    86  							},
    87  						},
    88  					},
    89  				},
    90  				ResourceConfig: v1alpha1.ResourceConfig{
    91  					MutableButUnreadableFields: []string{
    92  						"parent_field.field_a",
    93  						"parent_field.field_c",
    94  						"parent_field.field_d",
    95  					},
    96  				},
    97  			},
    98  			expectedMutableButUnreadableFieldsState: map[string]interface{}{
    99  				"spec": map[string]interface{}{
   100  					"parentField": map[string]interface{}{
   101  						"fieldA": "val1",
   102  						"fieldC": map[string]interface{}{
   103  							"field1": "val1",
   104  							"field2": "val2",
   105  						},
   106  						"fieldD": []interface{}{
   107  							"val1",
   108  							"val2",
   109  						},
   110  					},
   111  				},
   112  			},
   113  		},
   114  		{
   115  			name: "no mutable-but-unreadable fields set in spec",
   116  			resource: &krmtotf.Resource{
   117  				Resource: k8s.Resource{
   118  					Spec: map[string]interface{}{
   119  						"fieldB": "val2",
   120  						"parentField": map[string]interface{}{
   121  							"fieldB": "val2",
   122  						},
   123  					},
   124  				},
   125  				ResourceConfig: v1alpha1.ResourceConfig{
   126  					MutableButUnreadableFields: []string{
   127  						"field_a",
   128  						"field_c",
   129  						"field_d",
   130  						"parent_field.field_a",
   131  						"parent_field.field_c",
   132  						"parent_field.field_d",
   133  					},
   134  				},
   135  			},
   136  			expectedMutableButUnreadableFieldsState: map[string]interface{}{},
   137  		},
   138  		{
   139  			name: "no fields marked mutable-but-unreadable",
   140  			resource: &krmtotf.Resource{
   141  				Resource: k8s.Resource{
   142  					Spec: map[string]interface{}{
   143  						"fieldA": "val1",
   144  					},
   145  				},
   146  				ResourceConfig: v1alpha1.ResourceConfig{},
   147  			},
   148  			expectedMutableButUnreadableFieldsState: map[string]interface{}{},
   149  		},
   150  		{
   151  			name: "no spec",
   152  			resource: &krmtotf.Resource{
   153  				ResourceConfig: v1alpha1.ResourceConfig{
   154  					MutableButUnreadableFields: []string{
   155  						"field_a",
   156  					},
   157  				},
   158  			},
   159  			expectedMutableButUnreadableFieldsState: map[string]interface{}{},
   160  		},
   161  	}
   162  	for _, tc := range tests {
   163  		tc := tc
   164  		t.Run(tc.name, func(t *testing.T) {
   165  			t.Parallel()
   166  			annotationInString, err := krmtotf.MutableButUnreadableFieldsAnnotationFor(tc.resource)
   167  			if err != nil {
   168  				t.Fatal(err)
   169  			}
   170  
   171  			expectedStateInString, err := util.MarshalToJSONString(tc.expectedMutableButUnreadableFieldsState)
   172  			if err != nil {
   173  				t.Fatalf("error marshaling the expected state to string: %v", err)
   174  			}
   175  			if got, want := annotationInString, expectedStateInString; got != want {
   176  				t.Fatalf("got %v, want %v", got, want)
   177  			}
   178  		})
   179  	}
   180  }
   181  

View as plain text