...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/iam_utils_test.go

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

     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 webhook
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/iam/v1beta1"
    21  
    22  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  func TestIsIAMPolicyMember(t *testing.T) {
    28  	testCases := []struct {
    29  		Name                            string
    30  		Meta                            metav1.TypeMeta
    31  		IsIAMPolicyMemberExpectedResult bool
    32  		IsIAMPolicyExpectedResult       bool
    33  	}{
    34  		{
    35  			Name: "IAMPolicyMemberVersionAndKind",
    36  			Meta: metav1.TypeMeta{
    37  				Kind:       v1beta1.IAMPolicyMemberGVK.Kind,
    38  				APIVersion: v1beta1.IAMPolicyMemberGVK.GroupVersion().String(),
    39  			},
    40  			IsIAMPolicyMemberExpectedResult: true,
    41  			IsIAMPolicyExpectedResult:       false,
    42  		},
    43  		{
    44  			Name: "IAMPolicyVersionAndKind",
    45  			Meta: metav1.TypeMeta{
    46  				Kind:       v1beta1.IAMPolicyGVK.Kind,
    47  				APIVersion: v1beta1.IAMPolicyGVK.GroupVersion().String(),
    48  			},
    49  			IsIAMPolicyMemberExpectedResult: false,
    50  			IsIAMPolicyExpectedResult:       true,
    51  		},
    52  		{
    53  			Name: "PubSubVersionAndKind",
    54  			Meta: metav1.TypeMeta{
    55  				Kind:       "PubSubTopic",
    56  				APIVersion: "pubsub.cnrm.cloud.google.com/v1alpha1",
    57  			},
    58  			IsIAMPolicyMemberExpectedResult: false,
    59  			IsIAMPolicyExpectedResult:       false,
    60  		},
    61  	}
    62  	for _, tc := range testCases {
    63  		t.Run(tc.Name, func(t *testing.T) {
    64  			u := newUnstructuredFromObject(t, tc.Meta)
    65  			assertIsIAMPolicyMember(t, u, tc.IsIAMPolicyMemberExpectedResult)
    66  			assertIsIAMPolicy(t, u, tc.IsIAMPolicyExpectedResult)
    67  			assertIsIAMResource(t, u, tc.IsIAMPolicyExpectedResult || tc.IsIAMPolicyMemberExpectedResult)
    68  		})
    69  	}
    70  }
    71  
    72  func assertIsIAMPolicyMember(t *testing.T, u *unstructured.Unstructured, expectedResult bool) {
    73  	t.Helper()
    74  	result := isIAMPolicyMember(u)
    75  	if result != expectedResult {
    76  		t.Fatalf("unexpected result: got '%v', want '%v'", result, expectedResult)
    77  	}
    78  }
    79  
    80  func assertIsIAMPolicy(t *testing.T, u *unstructured.Unstructured, expectedResult bool) {
    81  	t.Helper()
    82  	result := isIAMPolicy(u)
    83  	if result != expectedResult {
    84  		t.Fatalf("unexpected result: got '%v', want '%v'", result, expectedResult)
    85  	}
    86  }
    87  
    88  func assertIsIAMResource(t *testing.T, u *unstructured.Unstructured, expectedResult bool) {
    89  	t.Helper()
    90  	result := isIAMResource(u)
    91  	if result != expectedResult {
    92  		t.Fatalf("unexpected result: got '%v', want '%v'", result, expectedResult)
    93  	}
    94  }
    95  

View as plain text