...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/resourceactuation/resourceactuation_test.go

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

     1  // Copyright 2023 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 resourceactuation_test
    16  
    17  import (
    18  	"strconv"
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/resourceactuation"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    23  
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  )
    26  
    27  func TestShouldSkip(t *testing.T) {
    28  	testcases := []struct {
    29  		name               string
    30  		generation         int64
    31  		observedGeneration int64
    32  		reconcileInterval  int32
    33  		status             string
    34  		reason             string
    35  		wantSkip           bool
    36  	}{
    37  		{
    38  			name:               "Should skip the resource actuation",
    39  			generation:         1,
    40  			observedGeneration: 1,
    41  			reconcileInterval:  0,
    42  			status:             "True",
    43  			reason:             k8s.UpToDate,
    44  			wantSkip:           true,
    45  		},
    46  		{
    47  			name:               "Generation and ObservedGeneration don't match",
    48  			generation:         2,
    49  			observedGeneration: 1,
    50  			reconcileInterval:  0,
    51  			status:             "True",
    52  			reason:             k8s.UpToDate,
    53  			wantSkip:           false,
    54  		},
    55  		{
    56  			name:               "Reconcile interval is not set to 0",
    57  			generation:         1,
    58  			observedGeneration: 1,
    59  			reconcileInterval:  600,
    60  			status:             "True",
    61  			reason:             k8s.UpToDate,
    62  			wantSkip:           false,
    63  		},
    64  		{
    65  			name:               "Resource status is not true",
    66  			generation:         1,
    67  			observedGeneration: 1,
    68  			reconcileInterval:  0,
    69  			status:             "False",
    70  			reason:             k8s.UpToDate,
    71  			wantSkip:           false,
    72  		},
    73  		{
    74  			name:               "Resource reason is not UpToDate",
    75  			generation:         1,
    76  			observedGeneration: 1,
    77  			reconcileInterval:  0,
    78  			status:             "True",
    79  			reason:             k8s.Updating,
    80  			wantSkip:           false,
    81  		},
    82  	}
    83  
    84  	for _, tc := range testcases {
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			var u unstructured.Unstructured
    87  			u.SetGeneration(tc.generation)
    88  			if err := unstructured.SetNestedField(u.Object, tc.observedGeneration, "status", "observedGeneration"); err != nil {
    89  				t.Errorf("Unable to set status.observedGeneration: %v", err)
    90  			}
    91  			condition := map[string]interface{}{
    92  				"status": tc.status,
    93  				"reason": tc.reason,
    94  			}
    95  			if err := unstructured.SetNestedSlice(u.Object, []interface{}{condition}, "status", "conditions"); err != nil {
    96  				t.Errorf("Unable to set status.conditions: %v", err)
    97  			}
    98  			u.SetAnnotations(map[string]string{k8s.ReconcileIntervalInSecondsAnnotation: strconv.Itoa(int(tc.reconcileInterval))})
    99  			skip, _ := resourceactuation.ShouldSkip(&u)
   100  			if skip != tc.wantSkip {
   101  				t.Errorf("resourceactuation.ShouldSkip returns %t, want %t", skip, tc.wantSkip)
   102  			}
   103  		})
   104  	}
   105  }
   106  

View as plain text