sigs.k8s.io/cluster-api@v1.7.1/exp/addons/internal/webhooks/clusterresourcesetbinding_webhook_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package webhooks
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  
    24  	addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
    25  )
    26  
    27  func TestClusterResourceSetBindingClusterNameImmutable(t *testing.T) {
    28  	tests := []struct {
    29  		name           string
    30  		oldClusterName string
    31  		newClusterName string
    32  		expectErr      bool
    33  	}{
    34  		{
    35  			name:           "when ClusterName is empty",
    36  			oldClusterName: "",
    37  			newClusterName: "",
    38  			expectErr:      false,
    39  		},
    40  		{
    41  			name:           "add ClusterName field",
    42  			oldClusterName: "",
    43  			newClusterName: "bar",
    44  			expectErr:      false,
    45  		},
    46  		{
    47  			name:           "when the ClusterName has not changed",
    48  			oldClusterName: "bar",
    49  			newClusterName: "bar",
    50  			expectErr:      false,
    51  		},
    52  		{
    53  			name:           "when the ClusterName has changed",
    54  			oldClusterName: "bar",
    55  			newClusterName: "different",
    56  			expectErr:      true,
    57  		},
    58  		{
    59  			name:           "existing ClusterName field to be set to empty",
    60  			oldClusterName: "bar",
    61  			newClusterName: "",
    62  			expectErr:      true,
    63  		},
    64  	}
    65  
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			g := NewWithT(t)
    69  
    70  			newClusterResourceSetBinding := &addonsv1.ClusterResourceSetBinding{
    71  				Spec: addonsv1.ClusterResourceSetBindingSpec{
    72  					ClusterName: tt.newClusterName,
    73  				},
    74  			}
    75  
    76  			oldClusterResourceSetBinding := &addonsv1.ClusterResourceSetBinding{
    77  				Spec: addonsv1.ClusterResourceSetBindingSpec{
    78  					ClusterName: tt.oldClusterName,
    79  				},
    80  			}
    81  			webhook := ClusterResourceSetBinding{}
    82  
    83  			warnings, err := webhook.ValidateCreate(ctx, newClusterResourceSetBinding)
    84  			g.Expect(err).ToNot(HaveOccurred())
    85  			g.Expect(warnings).To(BeEmpty())
    86  			if tt.expectErr {
    87  				warnings, err = webhook.ValidateUpdate(ctx, oldClusterResourceSetBinding, newClusterResourceSetBinding)
    88  				g.Expect(err).To(HaveOccurred())
    89  				g.Expect(warnings).To(BeEmpty())
    90  			} else {
    91  				warnings, err = webhook.ValidateUpdate(ctx, oldClusterResourceSetBinding, newClusterResourceSetBinding)
    92  				g.Expect(err).ToNot(HaveOccurred())
    93  				g.Expect(warnings).To(BeEmpty())
    94  			}
    95  		})
    96  	}
    97  }