...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/preflight/namecheck_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  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/util/asserts"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/mocks"
    26  
    27  	corev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
    28  )
    29  
    30  func TestNameChecker_ConfigConnector(t *testing.T) {
    31  	t.Parallel()
    32  	tests := []struct {
    33  		name string
    34  		cc   *corev1beta1.ConfigConnector
    35  		err  error
    36  	}{
    37  		{
    38  			name: "predefined name",
    39  			cc: &corev1beta1.ConfigConnector{
    40  				ObjectMeta: metav1.ObjectMeta{
    41  					Name: k8s.ConfigConnectorAllowedName,
    42  				},
    43  			},
    44  			err: nil,
    45  		},
    46  		{
    47  			name: "random name",
    48  			cc: &corev1beta1.ConfigConnector{
    49  				ObjectMeta: metav1.ObjectMeta{
    50  					Name: "test-configconnector",
    51  				},
    52  			},
    53  			err: fmt.Errorf("the only allowed name for ConfigConnector object is '%v'", k8s.ConfigConnectorAllowedName),
    54  		},
    55  	}
    56  	mgr := mocks.Manager{}
    57  
    58  	client := mgr.GetClient()
    59  	checker := NewNameChecker(client, k8s.ConfigConnectorAllowedName)
    60  	for _, tc := range tests {
    61  		tc := tc
    62  		t.Run(tc.name, func(t *testing.T) {
    63  			t.Parallel()
    64  			err := checker.Preflight(context.TODO(), tc.cc)
    65  			asserts.AssertErrorIsExpected(t, err, tc.err)
    66  		})
    67  	}
    68  }
    69  
    70  func TestNameChecker_ConfigConnectorContext(t *testing.T) {
    71  	t.Parallel()
    72  	tests := []struct {
    73  		name string
    74  		ccc  *corev1beta1.ConfigConnectorContext
    75  		err  error
    76  	}{
    77  		{
    78  			name: "valid ConfigConnectorContext name",
    79  			ccc: &corev1beta1.ConfigConnectorContext{
    80  				ObjectMeta: metav1.ObjectMeta{
    81  					Name: k8s.ConfigConnectorContextAllowedName,
    82  				},
    83  			},
    84  			err: nil,
    85  		},
    86  		{
    87  			name: "random name",
    88  			ccc: &corev1beta1.ConfigConnectorContext{
    89  				ObjectMeta: metav1.ObjectMeta{
    90  					Name: "test-ccc",
    91  				},
    92  			},
    93  			err: fmt.Errorf("the only allowed name for ConfigConnectorContext object is '%v'", k8s.ConfigConnectorContextAllowedName),
    94  		},
    95  	}
    96  	mgr := mocks.Manager{}
    97  
    98  	client := mgr.GetClient()
    99  	checker := NewNameChecker(client, k8s.ConfigConnectorContextAllowedName)
   100  	for _, tc := range tests {
   101  		tc := tc
   102  		t.Run(tc.name, func(t *testing.T) {
   103  			t.Parallel()
   104  			err := checker.Preflight(context.TODO(), tc.ccc)
   105  			asserts.AssertErrorIsExpected(t, err, tc.err)
   106  		})
   107  	}
   108  }
   109  

View as plain text