...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/resource_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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  func TestResource_IsResourceIDConfigured(t *testing.T) {
    25  	tests := []struct {
    26  		name           string
    27  		resource       *k8s.Resource
    28  		expectedResult bool
    29  		hasError       bool
    30  	}{
    31  		{
    32  			name: "resource ID unspecified",
    33  			resource: &k8s.Resource{
    34  				ObjectMeta: metav1.ObjectMeta{
    35  					Name: "test-resource",
    36  				},
    37  			},
    38  			expectedResult: false,
    39  		},
    40  		{
    41  			name: "resource ID empty",
    42  			resource: &k8s.Resource{
    43  				ObjectMeta: metav1.ObjectMeta{
    44  					Name: "test-resource",
    45  				},
    46  				Spec: map[string]interface{}{
    47  					"resourceID": "",
    48  				},
    49  			},
    50  			hasError: true,
    51  		},
    52  		{
    53  			name: "resource ID non-empty",
    54  			resource: &k8s.Resource{
    55  				ObjectMeta: metav1.ObjectMeta{
    56  					Name: "test-resource",
    57  				},
    58  				Spec: map[string]interface{}{
    59  					"resourceID": "test-id",
    60  				},
    61  			},
    62  			expectedResult: true,
    63  		},
    64  	}
    65  
    66  	for _, tc := range tests {
    67  		tc := tc
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			t.Parallel()
    70  			result, err := tc.resource.IsResourceIDConfigured()
    71  			if tc.hasError {
    72  				if err == nil {
    73  					t.Fatalf("got nil but want an error")
    74  				}
    75  			} else if err != nil {
    76  				t.Fatalf("error setting metadata name as resource ID: "+
    77  					"%v", err)
    78  			}
    79  			if got, want := result, tc.expectedResult; got != want {
    80  				t.Fatalf("got: %v, want: %v", got, want)
    81  			}
    82  		})
    83  	}
    84  }
    85  
    86  func TestResource_IsSpecOrStatusUpdateRequired(t *testing.T) {
    87  	t.Parallel()
    88  	tests := []struct {
    89  		name           string
    90  		resource       *k8s.Resource
    91  		original       *k8s.Resource
    92  		expectedResult bool
    93  	}{
    94  		{
    95  			name: "spec needs to update",
    96  			resource: &k8s.Resource{
    97  				ObjectMeta: metav1.ObjectMeta{
    98  					Generation: 1,
    99  				},
   100  				Spec: map[string]interface{}{
   101  					"foo": "someValue",
   102  				},
   103  				Status: map[string]interface{}{
   104  					"observedGeneration": float64(1),
   105  				},
   106  			},
   107  			original: &k8s.Resource{
   108  				ObjectMeta: metav1.ObjectMeta{
   109  					Generation: 1,
   110  				},
   111  				Spec: map[string]interface{}{},
   112  				Status: map[string]interface{}{
   113  					"observedGeneration": float64(1),
   114  				},
   115  			},
   116  			expectedResult: true,
   117  		},
   118  		{
   119  			name: "status needs to update",
   120  			resource: &k8s.Resource{
   121  				ObjectMeta: metav1.ObjectMeta{
   122  					Generation: 1,
   123  				},
   124  				Spec: map[string]interface{}{
   125  					"foo": "someValue",
   126  				},
   127  				Status: map[string]interface{}{
   128  					"bar": "someValue",
   129  				},
   130  			},
   131  			original: &k8s.Resource{
   132  				ObjectMeta: metav1.ObjectMeta{
   133  					Generation: 1,
   134  				},
   135  				Spec: map[string]interface{}{
   136  					"foo": "someValue",
   137  				},
   138  				Status: map[string]interface{}{},
   139  			},
   140  			expectedResult: true,
   141  		},
   142  		{
   143  			name: "observed generation needs to update",
   144  			resource: &k8s.Resource{
   145  				ObjectMeta: metav1.ObjectMeta{
   146  					Generation: 2,
   147  				},
   148  				Spec: map[string]interface{}{
   149  					"foo": "someValue",
   150  				},
   151  				Status: map[string]interface{}{
   152  					"bar":                "someValue",
   153  					"observedGeneration": float64(2),
   154  				},
   155  			},
   156  			original: &k8s.Resource{
   157  				ObjectMeta: metav1.ObjectMeta{
   158  					Generation: 2,
   159  				},
   160  				Spec: map[string]interface{}{
   161  					"foo": "someValue",
   162  				},
   163  				Status: map[string]interface{}{
   164  					"bar":                "someValue",
   165  					"observedGeneration": float64(1),
   166  				},
   167  			},
   168  			expectedResult: true,
   169  		},
   170  		{
   171  			name: "no update is needed",
   172  			resource: &k8s.Resource{
   173  				ObjectMeta: metav1.ObjectMeta{
   174  					Generation: 2,
   175  				},
   176  				Spec: map[string]interface{}{
   177  					"foo": "someValue",
   178  				},
   179  				Status: map[string]interface{}{
   180  					"bar":                "someValue",
   181  					"observedGeneration": float64(2),
   182  				},
   183  			},
   184  			original: &k8s.Resource{
   185  				ObjectMeta: metav1.ObjectMeta{
   186  					Generation: 2,
   187  				},
   188  				Spec: map[string]interface{}{
   189  					"foo": "someValue",
   190  				},
   191  				Status: map[string]interface{}{
   192  					"bar":                "someValue",
   193  					"observedGeneration": float64(2),
   194  				},
   195  			},
   196  			expectedResult: false,
   197  		},
   198  		{
   199  			name: "status is nil",
   200  			resource: &k8s.Resource{
   201  				ObjectMeta: metav1.ObjectMeta{
   202  					Generation: 1,
   203  				},
   204  				Spec: map[string]interface{}{
   205  					"foo": "someValue",
   206  				},
   207  			},
   208  			original: &k8s.Resource{
   209  				ObjectMeta: metav1.ObjectMeta{
   210  					Generation: 1,
   211  				},
   212  				Spec: map[string]interface{}{
   213  					"foo": "someValue",
   214  				},
   215  			},
   216  			expectedResult: true,
   217  		},
   218  	}
   219  	for _, tc := range tests {
   220  		tc := tc
   221  		t.Run(tc.name, func(t *testing.T) {
   222  			t.Parallel()
   223  			actual := k8s.IsSpecOrStatusUpdateRequired(tc.resource, tc.original)
   224  			if actual != tc.expectedResult {
   225  				t.Fatalf("got %v, want %v", actual, tc.expectedResult)
   226  			}
   227  		})
   228  	}
   229  }
   230  

View as plain text