...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/status_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  	"testing"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    22  	"github.com/google/go-cmp/cmp"
    23  
    24  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  )
    26  
    27  func TestRenameStatusFieldsWithReservedNames(t *testing.T) {
    28  	tests := []struct {
    29  		name           string
    30  		resourceName   string
    31  		originalStatus *apiextensions.JSONSchemaProps
    32  		expectedStatus *apiextensions.JSONSchemaProps
    33  		hasError       bool
    34  	}{
    35  		{
    36  			name: "fields that don't collide with reserved names are left alone",
    37  			originalStatus: &apiextensions.JSONSchemaProps{
    38  				Type: "object",
    39  				Properties: map[string]apiextensions.JSONSchemaProps{
    40  					"randomField": {
    41  						Type: "string",
    42  					},
    43  				},
    44  			},
    45  			expectedStatus: &apiextensions.JSONSchemaProps{
    46  				Type: "object",
    47  				Properties: map[string]apiextensions.JSONSchemaProps{
    48  					"randomField": {
    49  						Type: "string",
    50  					},
    51  				},
    52  			},
    53  		},
    54  		{
    55  			name: "fields that collide with reserved names are renamed",
    56  			originalStatus: &apiextensions.JSONSchemaProps{
    57  				Type: "object",
    58  				Properties: map[string]apiextensions.JSONSchemaProps{
    59  					"conditions": {
    60  						Type: "string",
    61  					},
    62  					"observedGeneration": {
    63  						Type: "string",
    64  					},
    65  				},
    66  			},
    67  			expectedStatus: &apiextensions.JSONSchemaProps{
    68  				Type: "object",
    69  				Properties: map[string]apiextensions.JSONSchemaProps{
    70  					"resourceConditions": {
    71  						Type: "string",
    72  					},
    73  					"resourceObservedGeneration": {
    74  						Type: "string",
    75  					},
    76  				},
    77  			},
    78  		},
    79  		{
    80  			name:         "fields that collide with reserved names but resource is in exclude list",
    81  			resourceName: "google_storage_default_object_access_control",
    82  			originalStatus: &apiextensions.JSONSchemaProps{
    83  				Type: "object",
    84  				Properties: map[string]apiextensions.JSONSchemaProps{
    85  					"conditions": {
    86  						Type: "string",
    87  					},
    88  					"observedGeneration": {
    89  						Type: "string",
    90  					},
    91  				},
    92  			},
    93  			expectedStatus: &apiextensions.JSONSchemaProps{
    94  				Type: "object",
    95  				Properties: map[string]apiextensions.JSONSchemaProps{
    96  					"conditions": {
    97  						Type: "string",
    98  					},
    99  					"observedGeneration": {
   100  						Type: "string",
   101  					},
   102  				},
   103  			},
   104  		},
   105  		{
   106  			name: "error if status has fields that collide with the renames of the reserved names",
   107  			originalStatus: &apiextensions.JSONSchemaProps{
   108  				Type: "object",
   109  				Properties: map[string]apiextensions.JSONSchemaProps{
   110  					"resourceConditions": {
   111  						Type: "string",
   112  					},
   113  					"resourceObservedGeneration": {
   114  						Type: "string",
   115  					},
   116  				},
   117  			},
   118  			hasError: true,
   119  		},
   120  	}
   121  
   122  	for _, tc := range tests {
   123  		tc := tc
   124  		t.Run(tc.name, func(t *testing.T) {
   125  			actualStatus, err := k8s.RenameStatusFieldsWithReservedNamesIfResourceNotExcluded(tc.resourceName, tc.originalStatus)
   126  			if tc.hasError {
   127  				if err == nil {
   128  					t.Fatalf("got nil error, want an error")
   129  				}
   130  				return
   131  			}
   132  			if !test.Equals(t, tc.expectedStatus, actualStatus) {
   133  				t.Fatalf("unexpected diff in returned status (-want +got): \n%v", cmp.Diff(tc.expectedStatus, actualStatus))
   134  			}
   135  		})
   136  	}
   137  }
   138  

View as plain text