-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Increase test coverage for managed cluster webhooks #1339
Merged
k8s-ci-robot
merged 3 commits into
kubernetes-sigs:main
from
salasberryfin:gcpmanaged-improve-test-coverage
Oct 21, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1" | ||
) | ||
|
||
func TestGCPManagedClusterValidatingWebhookUpdate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expectError bool | ||
spec GCPManagedClusterSpec | ||
}{ | ||
{ | ||
name: "request to change mutable field additional labels", | ||
expectError: false, | ||
spec: GCPManagedClusterSpec{ | ||
Project: "old-project", | ||
Region: "us-west1", | ||
CredentialsRef: &infrav1.ObjectReference{ | ||
Namespace: "default", | ||
Name: "credsref", | ||
}, | ||
AdditionalLabels: map[string]string{ | ||
"testKey": "testVal", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "request to change immutable field project", | ||
expectError: true, | ||
spec: GCPManagedClusterSpec{ | ||
Project: "new-project", | ||
Region: "us-west1", | ||
CredentialsRef: &infrav1.ObjectReference{ | ||
Namespace: "default", | ||
Name: "credsref", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "request to change immutable field region", | ||
expectError: true, | ||
spec: GCPManagedClusterSpec{ | ||
Project: "old-project", | ||
Region: "us-central1", | ||
CredentialsRef: &infrav1.ObjectReference{ | ||
Namespace: "default", | ||
Name: "credsref", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "request to change immutable field credentials ref", | ||
expectError: true, | ||
spec: GCPManagedClusterSpec{ | ||
Project: "old-project", | ||
Region: "us-central1", | ||
CredentialsRef: &infrav1.ObjectReference{ | ||
Namespace: "new-ns", | ||
Name: "new-name", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
newMC := &GCPManagedCluster{ | ||
Spec: tc.spec, | ||
} | ||
oldMC := &GCPManagedCluster{ | ||
Spec: GCPManagedClusterSpec{ | ||
Project: "old-project", | ||
Region: "us-west1", | ||
CredentialsRef: &infrav1.ObjectReference{ | ||
Namespace: "default", | ||
Name: "credsref", | ||
}, | ||
}, | ||
} | ||
|
||
warn, err := newMC.ValidateUpdate(oldMC) | ||
|
||
if tc.expectError { | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
} | ||
// Nothing emits warnings yet | ||
g.Expect(warn).To(BeEmpty()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var ( | ||
vV1_27_1 = "v1.27.1" | ||
releaseChannel = Rapid | ||
) | ||
|
||
func TestGCPManagedControlPlaneDefaultingWebhook(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resourceName string | ||
resourceNS string | ||
spec GCPManagedControlPlaneSpec | ||
expectSpec GCPManagedControlPlaneSpec | ||
expetError bool | ||
expectHash bool | ||
}{ | ||
{ | ||
name: "valid cluster name", | ||
resourceName: "cluster1", | ||
resourceNS: "default", | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "default_cluster1", | ||
}, | ||
expectSpec: GCPManagedControlPlaneSpec{ClusterName: "default_cluster1"}, | ||
}, | ||
{ | ||
name: "no cluster name should generate a valid one", | ||
resourceName: "cluster1", | ||
resourceNS: "default", | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "", | ||
}, | ||
expectSpec: GCPManagedControlPlaneSpec{ClusterName: "default-cluster1"}, | ||
}, | ||
{ | ||
name: "invalid cluster name (too long)", | ||
resourceName: strings.Repeat("A", maxClusterNameLength+1), | ||
resourceNS: "default", | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "", | ||
}, | ||
expectSpec: GCPManagedControlPlaneSpec{ClusterName: "capg-"}, | ||
expectHash: true, | ||
}, | ||
{ | ||
name: "with kubernetes version", | ||
resourceName: "cluster1", | ||
resourceNS: "default", | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "cluster1_27_1", | ||
ControlPlaneVersion: &vV1_27_1, | ||
}, | ||
expectSpec: GCPManagedControlPlaneSpec{ClusterName: "cluster1_27_1", ControlPlaneVersion: &vV1_27_1}, | ||
}, | ||
{ | ||
name: "with autopilot enabled", | ||
resourceName: "cluster1", | ||
resourceNS: "default", | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "cluster1_autopilot", | ||
ControlPlaneVersion: &vV1_27_1, | ||
EnableAutopilot: true, | ||
}, | ||
expectSpec: GCPManagedControlPlaneSpec{ClusterName: "cluster1_autopilot", ControlPlaneVersion: &vV1_27_1, EnableAutopilot: true}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
mcp := &GCPManagedControlPlane{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tc.resourceName, | ||
Namespace: tc.resourceNS, | ||
}, | ||
Spec: tc.spec, | ||
} | ||
mcp.Default() | ||
|
||
g.Expect(mcp.Spec).ToNot(BeNil()) | ||
g.Expect(mcp.Spec.ClusterName).ToNot(BeEmpty()) | ||
|
||
if tc.expectHash { | ||
g.Expect(strings.HasPrefix(mcp.Spec.ClusterName, "capg-")).To(BeTrue()) | ||
// We don't care about the exact name | ||
tc.expectSpec.ClusterName = mcp.Spec.ClusterName | ||
} | ||
g.Expect(mcp.Spec).To(Equal(tc.expectSpec)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGCPManagedControlPlaneValidatingWebhookCreate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expectError bool | ||
spec GCPManagedControlPlaneSpec | ||
}{ | ||
{ | ||
name: "cluster name too long should cause an error", | ||
expectError: true, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: strings.Repeat("A", maxClusterNameLength+1), | ||
}, | ||
}, | ||
{ | ||
name: "autopilot enabled without release channel should cause an error", | ||
expectError: true, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "", | ||
EnableAutopilot: true, | ||
ReleaseChannel: nil, | ||
}, | ||
}, | ||
{ | ||
name: "autopilot enabled with release channel", | ||
expectError: false, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "", | ||
EnableAutopilot: true, | ||
ReleaseChannel: &releaseChannel, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
mcp := &GCPManagedControlPlane{ | ||
Spec: tc.spec, | ||
} | ||
warn, err := mcp.ValidateCreate() | ||
|
||
if tc.expectError { | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
} | ||
// Nothing emits warnings yet | ||
g.Expect(warn).To(BeEmpty()) | ||
}) | ||
} | ||
} | ||
|
||
func TestGCPManagedControlPlaneValidatingWebhookUpdate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expectError bool | ||
spec GCPManagedControlPlaneSpec | ||
}{ | ||
{ | ||
name: "request to change cluster name should cause an error", | ||
expectError: true, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "default_cluster2", | ||
}, | ||
}, | ||
{ | ||
name: "request to change project should cause an error", | ||
expectError: true, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "default_cluster1", | ||
Project: "new-project", | ||
}, | ||
}, | ||
{ | ||
name: "request to change location should cause an error", | ||
expectError: true, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "default_cluster1", | ||
Location: "us-west4", | ||
}, | ||
}, | ||
{ | ||
name: "request to enable/disable autopilot should cause an error", | ||
expectError: true, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "default_cluster1", | ||
EnableAutopilot: true, | ||
}, | ||
}, | ||
{ | ||
name: "request to change network should not cause an error", | ||
expectError: false, | ||
spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "default_cluster1", | ||
ClusterNetwork: &ClusterNetwork{ | ||
PrivateCluster: &PrivateCluster{ | ||
EnablePrivateEndpoint: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
newMCP := &GCPManagedControlPlane{ | ||
Spec: tc.spec, | ||
} | ||
oldMCP := &GCPManagedControlPlane{ | ||
Spec: GCPManagedControlPlaneSpec{ | ||
ClusterName: "default_cluster1", | ||
ClusterNetwork: &ClusterNetwork{ | ||
PrivateCluster: &PrivateCluster{ | ||
EnablePrivateEndpoint: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
warn, err := newMCP.ValidateUpdate(oldMCP) | ||
|
||
if tc.expectError { | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
} | ||
// Nothing emits warnings yet | ||
g.Expect(warn).To(BeEmpty()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be hardcoded? can we avoid that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is simply used as a valid version placeholder but the value itself is not relevant. I'd say we won't need to update this in the foreseeable future, so it won't hurt that it is hard-coded. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the clarification