...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/policymember/iampolicymember_controller_test.go

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

     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 policymember
    16  
    17  import (
    18  	"testing"
    19  
    20  	iamv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/iam/v1beta1"
    21  	condition "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    23  
    24  	corev1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  )
    27  
    28  func TestIsAPIServerUpdateRequired(t *testing.T) {
    29  	t.Parallel()
    30  	tests := []struct {
    31  		name           string
    32  		policy         *iamv1beta1.IAMPolicyMember
    33  		expectedResult bool
    34  	}{
    35  		{
    36  			name: "no previous conditions and observed generation",
    37  			policy: &iamv1beta1.IAMPolicyMember{
    38  				ObjectMeta: metav1.ObjectMeta{
    39  					Generation: 1,
    40  				},
    41  				Status: iamv1beta1.IAMPolicyMemberStatus{},
    42  			},
    43  			expectedResult: true,
    44  		},
    45  		{
    46  			name: "conditions are update to date, no observed generation",
    47  			policy: &iamv1beta1.IAMPolicyMember{
    48  				ObjectMeta: metav1.ObjectMeta{
    49  					Generation: 1,
    50  				},
    51  				Status: iamv1beta1.IAMPolicyMemberStatus{
    52  					Conditions: []condition.Condition{
    53  						k8s.NewCustomReadyCondition(corev1.ConditionTrue, k8s.UpToDate, k8s.UpToDateMessage),
    54  					},
    55  				},
    56  			},
    57  			expectedResult: true,
    58  		},
    59  		{
    60  			name: "conditions are update to date, observed generation is stale",
    61  			policy: &iamv1beta1.IAMPolicyMember{
    62  				ObjectMeta: metav1.ObjectMeta{
    63  					Generation: 2,
    64  				},
    65  				Status: iamv1beta1.IAMPolicyMemberStatus{
    66  					Conditions: []condition.Condition{
    67  						k8s.NewCustomReadyCondition(corev1.ConditionTrue, k8s.UpToDate, k8s.UpToDateMessage),
    68  					},
    69  					ObservedGeneration: 1,
    70  				},
    71  			},
    72  			expectedResult: true,
    73  		},
    74  		{
    75  			name: "conditions are update to date, observed generation matches with the generation",
    76  			policy: &iamv1beta1.IAMPolicyMember{
    77  				ObjectMeta: metav1.ObjectMeta{
    78  					Generation: 2,
    79  				},
    80  				Status: iamv1beta1.IAMPolicyMemberStatus{
    81  					Conditions: []condition.Condition{
    82  						k8s.NewCustomReadyCondition(corev1.ConditionTrue, k8s.UpToDate, k8s.UpToDateMessage),
    83  					},
    84  					ObservedGeneration: 2,
    85  				},
    86  			},
    87  			expectedResult: false,
    88  		},
    89  	}
    90  	for _, tc := range tests {
    91  		tc := tc
    92  		t.Run(tc.name, func(t *testing.T) {
    93  			t.Parallel()
    94  			actual := isAPIServerUpdateRequired(tc.policy)
    95  			if actual != tc.expectedResult {
    96  				t.Fatalf("got %v, want %v", actual, tc.expectedResult)
    97  			}
    98  		})
    99  	}
   100  }
   101  

View as plain text