Skip to content

Commit 936fcd5

Browse files
committed
Revert "delete test file while others working on them"
This reverts commit a6e9659.
1 parent ca74261 commit 936fcd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3828
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 v1beta1_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
24+
"time"
25+
)
26+
27+
var _ = Describe("CloudStackMachine types", func() {
28+
var cloudStackMachine infrav1.CloudStackMachine
29+
30+
BeforeEach(func() { // Reset test vars to initial state.
31+
cloudStackMachine = infrav1.CloudStackMachine{}
32+
})
33+
34+
Context("When calculating time since state change", func() {
35+
It("Return the correct value when the last state update time is known", func() {
36+
delta := time.Duration(10 * time.Minute)
37+
lastUpdated := time.Now().Add(-delta)
38+
cloudStackMachine.Status.InstanceStateLastUpdated = metav1.NewTime(lastUpdated)
39+
Ω(cloudStackMachine.Status.TimeSinceLastStateChange()).Should(BeNumerically("~", delta, time.Second))
40+
})
41+
42+
It("Return a negative value when the last state update time is unknown", func() {
43+
Ω(cloudStackMachine.Status.TimeSinceLastStateChange()).Should(BeNumerically("<", 0))
44+
})
45+
})
46+
})
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 v1beta2_test
18+
19+
import (
20+
"context"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
25+
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta2"
26+
)
27+
28+
var _ = Describe("CloudStackCluster webhooks", func() {
29+
var ctx context.Context
30+
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
31+
requiredRegex := "admission webhook.*denied the request.*Required value\\: %s"
32+
33+
BeforeEach(func() { // Reset test vars to initial state.
34+
ctx = context.Background()
35+
dummies.SetDummyVars() // Reset cluster var.
36+
_ = k8sClient.Delete(ctx, dummies.CSCluster) // Delete any remnants.
37+
dummies.SetDummyVars() // Reset again since the k8s client can set this on delete.
38+
})
39+
40+
Context("When creating a CloudStackCluster", func() {
41+
It("Should accept a CloudStackCluster with all attributes present", func() {
42+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(Succeed())
43+
})
44+
45+
It("Should reject a CloudStackCluster with missing Zones.Network attribute", func() {
46+
dummies.CSCluster.Spec.FailureDomains = []infrav1.CloudStackFailureDomainSpec{{}}
47+
dummies.CSCluster.Spec.FailureDomains[0].Zone.Name = "ZoneWNoNetwork"
48+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(
49+
MatchError(MatchRegexp(requiredRegex, "each Zone requires a Network specification")))
50+
})
51+
52+
It("Should reject a CloudStackCluster with missing Zone attribute", func() {
53+
dummies.CSCluster.Spec.FailureDomains[0].Zone = infrav1.CloudStackZoneSpec{}
54+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(MatchError(MatchRegexp(requiredRegex,
55+
"each Zone requires a Network specification")))
56+
})
57+
58+
It("Should reject a CloudStackCluster with IdentityRef not of kind 'Secret'", func() {
59+
dummies.CSCluster.Spec.IdentityRef.Kind = "NewType"
60+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).
61+
Should(MatchError(MatchRegexp(forbiddenRegex, "must be a Secret")))
62+
})
63+
})
64+
65+
Context("When updating a CloudStackCluster", func() {
66+
BeforeEach(func() {
67+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(Succeed())
68+
})
69+
70+
It("Should reject updates to CloudStackCluster FailureDomains", func() {
71+
dummies.CSCluster.Spec.FailureDomains[0].Zone.Name = "SomeRandomUpdate"
72+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).Should(MatchError(MatchRegexp(forbiddenRegex, "Zones and sub")))
73+
})
74+
It("Should reject updates to Networks specified in CloudStackCluster Zones", func() {
75+
dummies.CSCluster.Spec.FailureDomains[0].Zone.Network.Name = "ArbitraryUpdateNetworkName"
76+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).Should(MatchError(MatchRegexp(forbiddenRegex, "Zones and sub")))
77+
})
78+
It("Should reject updates to CloudStackCluster controlplaneendpoint.host", func() {
79+
dummies.CSCluster.Spec.ControlPlaneEndpoint.Host = "1.1.1.1"
80+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
81+
Should(MatchError(MatchRegexp(forbiddenRegex, "controlplaneendpoint\\.host")))
82+
})
83+
84+
It("Should reject updates to CloudStackCluster controlplaneendpoint.port", func() {
85+
dummies.CSCluster.Spec.ControlPlaneEndpoint.Port = int32(1234)
86+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
87+
Should(MatchError(MatchRegexp(forbiddenRegex, "controlplaneendpoint\\.port")))
88+
})
89+
It("Should reject updates to the CloudStackCluster identity reference kind", func() {
90+
dummies.CSCluster.Spec.IdentityRef.Kind = "NewType"
91+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
92+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityref\\.kind")))
93+
})
94+
It("Should reject updates to the CloudStackCluster identity reference name", func() {
95+
dummies.CSCluster.Spec.IdentityRef.Name = "NewType"
96+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
97+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityref\\.name")))
98+
})
99+
})
100+
})
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 v1beta2_test
18+
19+
import (
20+
"context"
21+
22+
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
23+
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta2"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
var _ = Describe("CloudStackMachine webhook", func() {
30+
var ctx context.Context
31+
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
32+
requiredRegex := "admission webhook.*denied the request.*Required value\\: %s"
33+
34+
BeforeEach(func() { // Reset test vars to initial state.
35+
dummies.SetDummyVars()
36+
ctx = context.Background()
37+
_ = k8sClient.Delete(ctx, dummies.CSMachine1) // Clear out any remaining machines. Ignore errors.
38+
})
39+
40+
Context("When creating a CloudStackMachine", func() {
41+
It("should accept CloudStackMachine with all attributes", func() {
42+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed())
43+
})
44+
45+
It("should accept a CloudStackMachine with disk Offering attribute", func() {
46+
dummies.CSMachine1.Spec.DiskOffering = dummies.DiskOffering
47+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed())
48+
})
49+
50+
It("should accept a CloudStackMachine with positive disk Offering size attribute", func() {
51+
dummies.CSMachine1.Spec.DiskOffering = dummies.DiskOffering
52+
dummies.CSMachine1.Spec.DiskOffering.CustomSize = 1
53+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed())
54+
})
55+
56+
It("should not accept a CloudStackMachine with negative disk Offering size attribute", func() {
57+
dummies.CSMachine1.Spec.DiskOffering = dummies.DiskOffering
58+
dummies.CSMachine1.Spec.DiskOffering.CustomSize = -1
59+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(MatchError(MatchRegexp(forbiddenRegex, "customSizeInGB")))
60+
})
61+
62+
It("should reject a CloudStackMachine with missing Offering attribute", func() {
63+
dummies.CSMachine1.Spec.Offering = infrav1.CloudStackResourceIdentifier{ID: "", Name: ""}
64+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
65+
Should(MatchError(MatchRegexp(requiredRegex, "Offering")))
66+
})
67+
68+
It("should reject a CloudStackMachine with missing Template attribute", func() {
69+
dummies.CSMachine1.Spec.Template = infrav1.CloudStackResourceIdentifier{ID: "", Name: ""}
70+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
71+
Should(MatchError(MatchRegexp(requiredRegex, "Template")))
72+
})
73+
74+
It("should reject a CloudStackMachine with IdentityRef not of kind 'Secret'", func() {
75+
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
76+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
77+
Should(MatchError(MatchRegexp(forbiddenRegex, "must be a Secret")))
78+
})
79+
})
80+
81+
Context("When updating a CloudStackMachine", func() {
82+
BeforeEach(func() {
83+
Ω(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed())
84+
})
85+
86+
It("should reject VM offering updates to the CloudStackMachine", func() {
87+
dummies.CSMachine1.Spec.Offering = infrav1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateOffering"}
88+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
89+
Should(MatchError(MatchRegexp(forbiddenRegex, "offering")))
90+
})
91+
92+
It("should reject VM template updates to the CloudStackMachine", func() {
93+
dummies.CSMachine1.Spec.Template = infrav1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateTemplate"}
94+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
95+
Should(MatchError(MatchRegexp(forbiddenRegex, "template")))
96+
})
97+
98+
It("should reject VM disk offering updates to the CloudStackMachine", func() {
99+
dummies.CSMachine1.Spec.DiskOffering.Name = "medium"
100+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
101+
Should(MatchError(MatchRegexp(forbiddenRegex, "diskOffering")))
102+
})
103+
104+
It("should reject updates to VM details of the CloudStackMachine", func() {
105+
dummies.CSMachine1.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
106+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
107+
Should(MatchError(MatchRegexp(forbiddenRegex, "details")))
108+
})
109+
110+
It("should reject identity reference kind updates to the CloudStackMachine", func() {
111+
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
112+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
113+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Kind")))
114+
})
115+
116+
It("should reject identity reference name updates to the CloudStackMachine", func() {
117+
dummies.CSMachine1.Spec.IdentityRef.Name = "IdentityConfigMap"
118+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
119+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Name")))
120+
})
121+
122+
It("should reject updates to the list of affinty groups of the CloudStackMachine", func() {
123+
dummies.CSMachine1.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"}
124+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
125+
Should(MatchError(MatchRegexp(forbiddenRegex, "AffinityGroupIDs")))
126+
})
127+
})
128+
})
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 v1beta2_test
18+
19+
import (
20+
"context"
21+
22+
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
23+
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta2"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
var _ = Describe("CloudStackMachineTemplate webhook", func() {
30+
var ctx context.Context
31+
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
32+
requiredRegex := "admission webhook.*denied the request.*Required value\\: %s"
33+
34+
BeforeEach(func() { // Reset test vars to initial state.
35+
dummies.SetDummyVars()
36+
ctx = context.Background()
37+
_ = k8sClient.Delete(ctx, dummies.CSMachineTemplate1) // Delete any remnants.
38+
})
39+
40+
Context("When creating a CloudStackMachineTemplate", func() {
41+
It("Should accept a CloudStackMachineTemplate with all attributes present", func() {
42+
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).Should(Succeed())
43+
})
44+
45+
It("Should accept a CloudStackMachineTemplate when missing the VM Disk Offering attribute", func() {
46+
dummies.CSMachineTemplate1.Spec.Spec.Spec.DiskOffering = infrav1.CloudStackResourceDiskOffering{
47+
CloudStackResourceIdentifier: infrav1.CloudStackResourceIdentifier{Name: "", ID: ""},
48+
}
49+
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).Should(Succeed())
50+
})
51+
52+
It("Should reject a CloudStackMachineTemplate when missing the VM Offering attribute", func() {
53+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = infrav1.CloudStackResourceIdentifier{Name: "", ID: ""}
54+
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).
55+
Should(MatchError(MatchRegexp(requiredRegex, "Offering")))
56+
})
57+
58+
It("Should reject a CloudStackMachineTemplate when missing the VM Template attribute", func() {
59+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = infrav1.CloudStackResourceIdentifier{Name: "", ID: ""}
60+
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).
61+
Should(MatchError(MatchRegexp(requiredRegex, "Template")))
62+
})
63+
64+
It("should reject a CloudStackMachineTemplate with IdentityRef not of kind 'Secret'", func() {
65+
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
66+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
67+
Should(MatchError(MatchRegexp(forbiddenRegex, "must be a Secret")))
68+
})
69+
})
70+
71+
Context("When updating a CloudStackMachineTemplate", func() {
72+
BeforeEach(func() { // Reset test vars to initial state.
73+
Ω(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).Should(Succeed())
74+
})
75+
76+
It("should reject VM template updates to the CloudStackMachineTemplate", func() {
77+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = infrav1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateTemplate"}
78+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
79+
Should(MatchError(MatchRegexp(forbiddenRegex, "template")))
80+
})
81+
82+
It("should reject VM disk offering updates to the CloudStackMachineTemplate", func() {
83+
dummies.CSMachineTemplate1.Spec.Spec.Spec.DiskOffering = infrav1.CloudStackResourceDiskOffering{
84+
CloudStackResourceIdentifier: infrav1.CloudStackResourceIdentifier{Name: "DiskOffering2"}}
85+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
86+
Should(MatchError(MatchRegexp(forbiddenRegex, "diskOffering")))
87+
})
88+
89+
It("should reject VM offering updates to the CloudStackMachineTemplate", func() {
90+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = infrav1.CloudStackResourceIdentifier{Name: "Offering2"}
91+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
92+
Should(MatchError(MatchRegexp(forbiddenRegex, "offering")))
93+
})
94+
95+
It("should reject updates to VM details of the CloudStackMachineTemplate", func() {
96+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
97+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
98+
Should(MatchError(MatchRegexp(forbiddenRegex, "details")))
99+
})
100+
101+
It("should reject identity reference kind updates to the CloudStackMachineTemplate", func() {
102+
dummies.CSMachineTemplate1.Spec.Spec.Spec.IdentityRef.Kind = "configMap"
103+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
104+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Kind")))
105+
})
106+
107+
It("should reject identity reference name updates to the CloudStackMachineTemplate", func() {
108+
dummies.CSMachineTemplate1.Spec.Spec.Spec.IdentityRef.Name = "IDentityConfigMap"
109+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
110+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Name")))
111+
})
112+
113+
It("should reject updates to the list of AffinityGroupIDs of the CloudStackMachineTemplate", func() {
114+
dummies.CSMachineTemplate1.Spec.Spec.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2ag"}
115+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).ShouldNot(Succeed())
116+
})
117+
})
118+
})

0 commit comments

Comments
 (0)