|
| 1 | +/* |
| 2 | +Copyright 2024 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 v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1" |
| 24 | +) |
| 25 | + |
| 26 | +func TestGCPManagedClusterValidatingWebhookUpdate(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + expectError bool |
| 30 | + spec GCPManagedClusterSpec |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "request to change mutable field additional labels", |
| 34 | + expectError: false, |
| 35 | + spec: GCPManagedClusterSpec{ |
| 36 | + Project: "old-project", |
| 37 | + Region: "us-west1", |
| 38 | + CredentialsRef: &infrav1.ObjectReference{ |
| 39 | + Namespace: "default", |
| 40 | + Name: "credsref", |
| 41 | + }, |
| 42 | + AdditionalLabels: map[string]string{ |
| 43 | + "testKey": "testVal", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "request to change immutable field project", |
| 49 | + expectError: true, |
| 50 | + spec: GCPManagedClusterSpec{ |
| 51 | + Project: "new-project", |
| 52 | + Region: "us-west1", |
| 53 | + CredentialsRef: &infrav1.ObjectReference{ |
| 54 | + Namespace: "default", |
| 55 | + Name: "credsref", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "request to change immutable field region", |
| 61 | + expectError: true, |
| 62 | + spec: GCPManagedClusterSpec{ |
| 63 | + Project: "old-project", |
| 64 | + Region: "us-central1", |
| 65 | + CredentialsRef: &infrav1.ObjectReference{ |
| 66 | + Namespace: "default", |
| 67 | + Name: "credsref", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "request to change immutable field credentials ref", |
| 73 | + expectError: true, |
| 74 | + spec: GCPManagedClusterSpec{ |
| 75 | + Project: "old-project", |
| 76 | + Region: "us-central1", |
| 77 | + CredentialsRef: &infrav1.ObjectReference{ |
| 78 | + Namespace: "new-ns", |
| 79 | + Name: "new-name", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tc := range tests { |
| 86 | + t.Run(tc.name, func(t *testing.T) { |
| 87 | + g := NewWithT(t) |
| 88 | + |
| 89 | + newMC := &GCPManagedCluster{ |
| 90 | + Spec: tc.spec, |
| 91 | + } |
| 92 | + oldMC := &GCPManagedCluster{ |
| 93 | + Spec: GCPManagedClusterSpec{ |
| 94 | + Project: "old-project", |
| 95 | + Region: "us-west1", |
| 96 | + CredentialsRef: &infrav1.ObjectReference{ |
| 97 | + Namespace: "default", |
| 98 | + Name: "credsref", |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + warn, err := newMC.ValidateUpdate(oldMC) |
| 104 | + |
| 105 | + if tc.expectError { |
| 106 | + g.Expect(err).To(HaveOccurred()) |
| 107 | + } else { |
| 108 | + g.Expect(err).ToNot(HaveOccurred()) |
| 109 | + } |
| 110 | + // Nothing emits warnings yet |
| 111 | + g.Expect(warn).To(BeEmpty()) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
0 commit comments