...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/state_into_spec_annotation_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
    16  
    17  import (
    18  	"testing"
    19  
    20  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    21  )
    22  
    23  func TestValidateOrDefaultStateIntoSpecAnnotation(t *testing.T) {
    24  	tests := []struct {
    25  		name        string
    26  		obj         *unstructured.Unstructured
    27  		hasError    bool
    28  		expectedVal string
    29  	}{
    30  		{
    31  			name: "user specifies an accepted value",
    32  			obj: &unstructured.Unstructured{
    33  				Object: map[string]interface{}{
    34  					"apiVersion": "test1.cnrm.cloud.google.com/v1alpha1",
    35  					"kind":       "Test1Bar",
    36  					"metadata": map[string]interface{}{
    37  						"annotations": map[string]interface{}{
    38  							StateIntoSpecAnnotation: "merge",
    39  						},
    40  					},
    41  				},
    42  			},
    43  			expectedVal: "merge",
    44  		},
    45  		{
    46  			name: "user specifies an unacceptable value",
    47  			obj: &unstructured.Unstructured{
    48  				Object: map[string]interface{}{
    49  					"apiVersion": "test1.cnrm.cloud.google.com/v1alpha1",
    50  					"kind":       "Test1Bar",
    51  					"metadata": map[string]interface{}{
    52  						"annotations": map[string]interface{}{
    53  							StateIntoSpecAnnotation: "not_accepted_value",
    54  						},
    55  					},
    56  				},
    57  			},
    58  			hasError: true,
    59  		},
    60  		{
    61  			name: "user specifies an empty string",
    62  			obj: &unstructured.Unstructured{
    63  				Object: map[string]interface{}{
    64  					"apiVersion": "test1.cnrm.cloud.google.com/v1alpha1",
    65  					"kind":       "Test1Bar",
    66  					"metadata": map[string]interface{}{
    67  						"annotations": map[string]interface{}{
    68  							StateIntoSpecAnnotation: "",
    69  						},
    70  					},
    71  				},
    72  			},
    73  			hasError: true,
    74  		},
    75  		{
    76  			name: "defaulting if absent",
    77  			obj: &unstructured.Unstructured{
    78  				Object: map[string]interface{}{
    79  					"apiVersion": "test1.cnrm.cloud.google.com/v1alpha1",
    80  					"kind":       "Test1Bar",
    81  				},
    82  			},
    83  			expectedVal: "merge",
    84  		},
    85  		{
    86  			name: "kind that does not support 'absent' value cannot use 'absent' value",
    87  			obj: &unstructured.Unstructured{
    88  				Object: map[string]interface{}{
    89  					"apiVersion": "test1.cnrm.cloud.google.com/v1alpha1",
    90  					"kind":       "ComputeAddress",
    91  					"metadata": map[string]interface{}{
    92  						"annotations": map[string]interface{}{
    93  							StateIntoSpecAnnotation: "absent",
    94  						},
    95  					},
    96  				},
    97  			},
    98  			hasError: true,
    99  		},
   100  		{
   101  			name: "BigQueryDataset kind can use 'absent' value",
   102  			obj: &unstructured.Unstructured{
   103  				Object: map[string]interface{}{
   104  					"apiVersion": "bigquery.cnrm.cloud.google.com/v1beta1",
   105  					"kind":       "BigQueryDataset",
   106  					"metadata": map[string]interface{}{
   107  						"annotations": map[string]interface{}{
   108  							StateIntoSpecAnnotation: "absent",
   109  						},
   110  					},
   111  				},
   112  			},
   113  			expectedVal: "absent",
   114  		},
   115  		{
   116  			name: "BigQueryDataset kind can use 'merge' value",
   117  			obj: &unstructured.Unstructured{
   118  				Object: map[string]interface{}{
   119  					"apiVersion": "bigquery.cnrm.cloud.google.com/v1beta1",
   120  					"kind":       "BigQueryDataset",
   121  					"metadata": map[string]interface{}{
   122  						"annotations": map[string]interface{}{
   123  							StateIntoSpecAnnotation: "merge",
   124  						},
   125  					},
   126  				},
   127  			},
   128  			expectedVal: "merge",
   129  		},
   130  	}
   131  
   132  	for _, tc := range tests {
   133  		tc := tc
   134  		t.Run(tc.name, func(t *testing.T) {
   135  			err := ValidateOrDefaultStateIntoSpecAnnotation(tc.obj)
   136  			if tc.hasError {
   137  				if err == nil {
   138  					t.Fatalf("got nil, but expect an error")
   139  				}
   140  				return
   141  			}
   142  			if err != nil {
   143  				t.Fatalf("unexpected error: %v", err)
   144  			}
   145  			actualVal, found := GetAnnotation(StateIntoSpecAnnotation, tc.obj)
   146  			if !found {
   147  				t.Fatalf("'%v' annotation is not found", StateIntoSpecAnnotation)
   148  			}
   149  			if actualVal != tc.expectedVal {
   150  				t.Fatalf("got %v, want %v", actualVal, tc.expectedVal)
   151  			}
   152  		})
   153  	}
   154  }
   155  

View as plain text