...

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

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

     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 k8s_test
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    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  	"github.com/google/go-cmp/cmp"
    26  )
    27  
    28  var mutableButUnreadableFieldAnnotationTests = []struct {
    29  	name                                    string
    30  	resource                                *krmtotf.Resource
    31  	mutableButUnreadablePaths               [][]string
    32  	expectedMutableButUnreadableFieldsState map[string]interface{}
    33  }{
    34  	{
    35  		name: "top-level fields",
    36  		resource: &krmtotf.Resource{
    37  			Resource: k8s.Resource{
    38  				Spec: map[string]interface{}{
    39  					"fieldA": "val1",
    40  					"fieldB": "val2",
    41  					"fieldC": map[string]interface{}{
    42  						"field1": "val1",
    43  						"field2": "val2",
    44  					},
    45  					"fieldD": []interface{}{
    46  						"val1",
    47  						"val2",
    48  					},
    49  				},
    50  			},
    51  		},
    52  		mutableButUnreadablePaths: [][]string{
    53  			{"fieldA"},
    54  			{"fieldC"},
    55  			{"fieldD"},
    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  		},
    91  		mutableButUnreadablePaths: [][]string{
    92  			{"parentField", "fieldA"},
    93  			{"parentField", "fieldC"},
    94  			{"parentField", "fieldD"},
    95  		},
    96  		expectedMutableButUnreadableFieldsState: map[string]interface{}{
    97  			"spec": map[string]interface{}{
    98  				"parentField": map[string]interface{}{
    99  					"fieldA": "val1",
   100  					"fieldC": map[string]interface{}{
   101  						"field1": "val1",
   102  						"field2": "val2",
   103  					},
   104  					"fieldD": []interface{}{
   105  						"val1",
   106  						"val2",
   107  					},
   108  				},
   109  			},
   110  		},
   111  	},
   112  	{
   113  		name: "no mutable-but-unreadable fields set in spec",
   114  		resource: &krmtotf.Resource{
   115  			Resource: k8s.Resource{
   116  				Spec: map[string]interface{}{
   117  					"fieldB": "val2",
   118  					"parentField": map[string]interface{}{
   119  						"fieldB": "val2",
   120  					},
   121  				},
   122  			},
   123  		},
   124  		mutableButUnreadablePaths: [][]string{
   125  			{"fieldA"},
   126  			{"fieldC"},
   127  			{"fieldD"},
   128  			{"parentField", "fieldA"},
   129  			{"parentField", "fieldC"},
   130  			{"parentField", "fieldD"},
   131  		},
   132  		expectedMutableButUnreadableFieldsState: map[string]interface{}{},
   133  	},
   134  	{
   135  		name: "no fields marked mutable-but-unreadable",
   136  		resource: &krmtotf.Resource{
   137  			Resource: k8s.Resource{
   138  				Spec: map[string]interface{}{
   139  					"fieldA": "val1",
   140  				},
   141  			},
   142  		},
   143  		mutableButUnreadablePaths:               [][]string{},
   144  		expectedMutableButUnreadableFieldsState: map[string]interface{}{},
   145  	},
   146  	{
   147  		name:     "no spec",
   148  		resource: &krmtotf.Resource{},
   149  		mutableButUnreadablePaths: [][]string{
   150  			{"fieldA"},
   151  		},
   152  		expectedMutableButUnreadableFieldsState: map[string]interface{}{},
   153  	},
   154  }
   155  
   156  func TestGenerateMutableButUnreadableFieldsAnnotation(t *testing.T) {
   157  	for _, tc := range mutableButUnreadableFieldAnnotationTests {
   158  		tc := tc
   159  		t.Run(tc.name, func(t *testing.T) {
   160  			t.Parallel()
   161  			annotationInString, err := k8s.GenerateMutableButUnreadableFieldsAnnotation(&tc.resource.Resource, tc.mutableButUnreadablePaths)
   162  			if err != nil {
   163  				t.Fatal(err)
   164  			}
   165  
   166  			expectedStateInString, err := util.MarshalToJSONString(tc.expectedMutableButUnreadableFieldsState)
   167  			if err != nil {
   168  				t.Fatalf("error marshaling the expected state to string: %v", err)
   169  			}
   170  			if got, want := annotationInString, expectedStateInString; got != want {
   171  				t.Fatalf("got %v, want %v", got, want)
   172  			}
   173  		})
   174  	}
   175  }
   176  
   177  func TestGetMutableButUnreadableFieldsFromAnnotations(t *testing.T) {
   178  	for _, tc := range mutableButUnreadableFieldAnnotationTests {
   179  		tc := tc
   180  		t.Run(tc.name, func(t *testing.T) {
   181  			t.Parallel()
   182  			mutableButUnreadableFields, err := k8s.GetMutableButUnreadableFieldsFromAnnotations(&tc.resource.Resource, tc.mutableButUnreadablePaths)
   183  			if err != nil {
   184  				t.Fatal(err)
   185  			}
   186  			if got, want := mutableButUnreadableFields, tc.expectedMutableButUnreadableFieldsState; !reflect.DeepEqual(got, want) {
   187  				t.Fatalf("unexpected mutable-but-unreadable fields diff (-want +got): \n%v", cmp.Diff(want, got))
   188  			}
   189  		})
   190  	}
   191  }
   192  

View as plain text