...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/preflight/configconnectorcontextchecker_test.go

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

     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 preflight
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	corev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/util/asserts"
    25  
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  )
    28  
    29  func TestConfigConnectorContextChecker(t *testing.T) {
    30  	t.Parallel()
    31  	tests := []struct {
    32  		name string
    33  		ccc  *corev1beta1.ConfigConnectorContext
    34  		err  error
    35  	}{
    36  		{
    37  			name: "CCC has spec.billingProject set and spec.requestProjectPolicy set to BILLING_PROJECT",
    38  			ccc: &corev1beta1.ConfigConnectorContext{
    39  				ObjectMeta: metav1.ObjectMeta{
    40  					Name:      k8s.ConfigConnectorContextAllowedName,
    41  					Namespace: "foo-ns",
    42  				},
    43  				Spec: corev1beta1.ConfigConnectorContextSpec{
    44  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
    45  					RequestProjectPolicy: "BILLING_PROJECT",
    46  					BillingProject:       "BILL_ME",
    47  				},
    48  			},
    49  			err: nil,
    50  		},
    51  
    52  		{
    53  			name: "CCC has spec.billingProject omitted and spec.requestProjectPolicy set to RESOURCE_PROJECT",
    54  			ccc: &corev1beta1.ConfigConnectorContext{
    55  				ObjectMeta: metav1.ObjectMeta{
    56  					Name:      k8s.ConfigConnectorContextAllowedName,
    57  					Namespace: "foo-ns",
    58  				},
    59  				Spec: corev1beta1.ConfigConnectorContextSpec{
    60  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
    61  					RequestProjectPolicy: "RESOURCE_PROJECT",
    62  				},
    63  			},
    64  			err: nil,
    65  		},
    66  
    67  		{
    68  			name: "CCC has spec.billingProject set to empty and spec.requestProjectPolicy set to RESOURCE_PROJECT",
    69  			ccc: &corev1beta1.ConfigConnectorContext{
    70  				ObjectMeta: metav1.ObjectMeta{
    71  					Name:      k8s.ConfigConnectorContextAllowedName,
    72  					Namespace: "foo-ns",
    73  				},
    74  				Spec: corev1beta1.ConfigConnectorContextSpec{
    75  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
    76  					RequestProjectPolicy: "RESOURCE_PROJECT",
    77  					BillingProject:       "",
    78  				},
    79  			},
    80  			err: nil,
    81  		},
    82  
    83  		{
    84  			name: "CCC has spec.billingProject omitted and spec.requestProjectPolicy set to SERVICE_ACCOUNT_PROJECT",
    85  			ccc: &corev1beta1.ConfigConnectorContext{
    86  				ObjectMeta: metav1.ObjectMeta{
    87  					Name:      k8s.ConfigConnectorContextAllowedName,
    88  					Namespace: "foo-ns",
    89  				},
    90  				Spec: corev1beta1.ConfigConnectorContextSpec{
    91  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
    92  					RequestProjectPolicy: "SERVICE_ACCOUNT_PROJECT",
    93  				},
    94  			},
    95  			err: nil,
    96  		},
    97  
    98  		{
    99  			name: "CCC has spec.billingProject unset and requestProjectPolicy set to BILLING_PROJECT",
   100  			ccc: &corev1beta1.ConfigConnectorContext{
   101  				ObjectMeta: metav1.ObjectMeta{
   102  					Name:      k8s.ConfigConnectorContextAllowedName,
   103  					Namespace: "foo-ns",
   104  				},
   105  				Spec: corev1beta1.ConfigConnectorContextSpec{
   106  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
   107  					RequestProjectPolicy: "BILLING_PROJECT",
   108  				},
   109  			},
   110  			err: fmt.Errorf("spec.billingProject must be set if spec.requestProjectPolicy is set to %v", k8s.BillingProjectPolicy),
   111  		},
   112  
   113  		{
   114  			name: "CCC has spec.billingProject set to empty and requestProjectPolicy set to BILLING_PROJECT",
   115  			ccc: &corev1beta1.ConfigConnectorContext{
   116  				ObjectMeta: metav1.ObjectMeta{
   117  					Name:      k8s.ConfigConnectorContextAllowedName,
   118  					Namespace: "foo-ns",
   119  				},
   120  				Spec: corev1beta1.ConfigConnectorContextSpec{
   121  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
   122  					RequestProjectPolicy: "BILLING_PROJECT",
   123  					BillingProject:       "",
   124  				},
   125  			},
   126  			err: fmt.Errorf("spec.billingProject must be set if spec.requestProjectPolicy is set to %v", k8s.BillingProjectPolicy),
   127  		},
   128  
   129  		{
   130  			name: "CCC has spec.billingProject unset and requestProjectPolicy set to RESOURCE_PROJECT",
   131  			ccc: &corev1beta1.ConfigConnectorContext{
   132  				ObjectMeta: metav1.ObjectMeta{
   133  					Name:      k8s.ConfigConnectorContextAllowedName,
   134  					Namespace: "foo-ns",
   135  				},
   136  				Spec: corev1beta1.ConfigConnectorContextSpec{
   137  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
   138  					RequestProjectPolicy: "RESOURCE_PROJECT",
   139  					BillingProject:       "BILL_ME",
   140  				},
   141  			},
   142  			err: fmt.Errorf("spec.billingProject cannot be set if spec.requestProjectPolicy is not set to %v", k8s.BillingProjectPolicy),
   143  		},
   144  
   145  		{
   146  			name: "CCC has spec.billingProject unset and requestProjectPolicy set to SERVICE_ACCOUNT_PROJECT",
   147  			ccc: &corev1beta1.ConfigConnectorContext{
   148  				ObjectMeta: metav1.ObjectMeta{
   149  					Name:      k8s.ConfigConnectorContextAllowedName,
   150  					Namespace: "foo-ns",
   151  				},
   152  				Spec: corev1beta1.ConfigConnectorContextSpec{
   153  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
   154  					RequestProjectPolicy: "SERVICE_ACCOUNT_PROJECT",
   155  					BillingProject:       "BILL_ME",
   156  				},
   157  			},
   158  			err: fmt.Errorf("spec.billingProject cannot be set if spec.requestProjectPolicy is not set to %v", k8s.BillingProjectPolicy),
   159  		},
   160  	}
   161  
   162  	checker := NewConfigConnectorContextChecker()
   163  	for _, tc := range tests {
   164  		tc := tc
   165  		t.Run(tc.name, func(t *testing.T) {
   166  			t.Parallel()
   167  			err := checker.Preflight(context.TODO(), tc.ccc)
   168  			asserts.AssertErrorIsExpected(t, err, tc.err)
   169  		})
   170  	}
   171  }
   172  

View as plain text