Skip to content

Commit 32bc517

Browse files
authored
Merge pull request #3959 from camilamacedo86/cleanup-deploy-image
🌱 Cleanup e2e tests for deploy image
2 parents 576e29d + 022aaab commit 32bc517

File tree

2 files changed

+85
-101
lines changed

2 files changed

+85
-101
lines changed

test/e2e/deployimage/generate_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2020 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 deployimage
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
23+
//nolint:golint
24+
// nolint:revive
25+
//nolint:golint
26+
// nolint:revive
27+
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
28+
)
29+
30+
// GenerateDeployImageWithOptions implements a go/v4 plugin project and scaffold an API using the image options
31+
func GenerateDeployImageWithOptions(kbc *utils.TestContext) {
32+
initTheProject(kbc)
33+
creatingAPIWithOptions(kbc)
34+
}
35+
36+
// GenerateDeployImage implements a go/v4 plugin project and scaffold an API using the deploy image plugin
37+
func GenerateDeployImage(kbc *utils.TestContext) {
38+
initTheProject(kbc)
39+
creatingAPI(kbc)
40+
}
41+
42+
func creatingAPI(kbc *utils.TestContext) {
43+
By("creating API definition with deploy-image/v1-alpha plugin")
44+
err := kbc.CreateAPI(
45+
"--group", kbc.Group,
46+
"--version", kbc.Version,
47+
"--kind", kbc.Kind,
48+
"--plugins", "deploy-image/v1-alpha",
49+
"--image", "busybox:1.36.1",
50+
"--make=false",
51+
"--manifests=false",
52+
)
53+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
54+
}
55+
56+
func creatingAPIWithOptions(kbc *utils.TestContext) {
57+
var err error
58+
By("creating API definition with deploy-image/v1-alpha plugin")
59+
err = kbc.CreateAPI(
60+
"--group", kbc.Group,
61+
"--version", kbc.Version,
62+
"--kind", kbc.Kind,
63+
"--plugins", "deploy-image/v1-alpha",
64+
"--image", "memcached:1.6.26-alpine3.19",
65+
"--image-container-port", "11211",
66+
"--image-container-command", "memcached,-m=64,-o,modern,-v",
67+
"--run-as-user", "1001",
68+
"--make=false",
69+
"--manifests=false",
70+
)
71+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
72+
}
73+
74+
func initTheProject(kbc *utils.TestContext) {
75+
By("initializing a project")
76+
err := kbc.Init(
77+
"--plugins", "go/v4",
78+
"--project-version", "3",
79+
"--domain", kbc.Domain,
80+
)
81+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
82+
}

test/e2e/deployimage/plugin_cluster_test.go

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -41,105 +41,23 @@ var _ = Describe("kubebuilder", func() {
4141
kbc, err = utils.NewTestContext(util.KubebuilderBinName, "GO111MODULE=on")
4242
Expect(err).NotTo(HaveOccurred())
4343
Expect(kbc.Prepare()).To(Succeed())
44-
45-
By("installing prometheus operator")
46-
Expect(kbc.InstallPrometheusOperManager()).To(Succeed())
47-
48-
By("creating manager namespace")
49-
err = kbc.CreateManagerNamespace()
50-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
51-
52-
By("labeling all namespaces to warn about restricted")
53-
err = kbc.LabelAllNamespacesToWarnAboutRestricted()
54-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
55-
56-
By("enforce that namespace where the sample will be applied can only run restricted containers")
57-
_, err = kbc.Kubectl.Command("label", "--overwrite", "ns", kbc.Kubectl.Namespace,
58-
"pod-security.kubernetes.io/audit=restricted",
59-
"pod-security.kubernetes.io/enforce-version=v1.24",
60-
"pod-security.kubernetes.io/enforce=restricted")
61-
Expect(err).To(Not(HaveOccurred()))
6244
})
6345

6446
AfterEach(func() {
6547
By("clean up API objects created during the test")
6648
kbc.CleanupManifests(filepath.Join("config", "default"))
6749

68-
By("uninstalling the Prometheus manager bundle")
69-
kbc.UninstallPrometheusOperManager()
70-
7150
By("removing controller image and working dir")
7251
kbc.Destroy()
7352
})
7453

7554
It("should generate a runnable project with deploy-image/v1-alpha options ", func() {
76-
var err error
77-
78-
By("initializing a project with go/v4")
79-
err = kbc.Init(
80-
"--plugins", "go/v4",
81-
"--project-version", "3",
82-
"--domain", kbc.Domain,
83-
)
84-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
85-
86-
By("creating API definition with deploy-image/v1-alpha plugin")
87-
err = kbc.CreateAPI(
88-
"--group", kbc.Group,
89-
"--version", kbc.Version,
90-
"--kind", kbc.Kind,
91-
"--plugins", "deploy-image/v1-alpha",
92-
"--image", "memcached:1.6.26-alpine3.19",
93-
"--image-container-port", "11211",
94-
"--image-container-command", "memcached,-m=64,-o,modern,-v",
95-
"--run-as-user", "1001",
96-
"--make=false",
97-
"--manifests=false",
98-
)
99-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
100-
101-
By("uncomment kustomization.yaml to enable prometheus")
102-
ExpectWithOffset(1, util.UncommentCode(
103-
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
104-
"#- ../prometheus", "#")).To(Succeed())
105-
106-
By("uncomment kustomize files to ensure that pods are restricted")
107-
uncommentPodStandards(kbc)
108-
55+
GenerateDeployImageWithOptions(kbc)
10956
Run(kbc)
11057
})
11158

11259
It("should generate a runnable project with deploy-image/v1-alpha without options ", func() {
113-
var err error
114-
115-
By("initializing a project with go/v4")
116-
err = kbc.Init(
117-
"--plugins", "go/v4",
118-
"--project-version", "3",
119-
"--domain", kbc.Domain,
120-
)
121-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
122-
123-
By("creating API definition with deploy-image/v1-alpha plugin")
124-
err = kbc.CreateAPI(
125-
"--group", kbc.Group,
126-
"--version", kbc.Version,
127-
"--kind", kbc.Kind,
128-
"--plugins", "deploy-image/v1-alpha",
129-
"--image", "busybox:1.36.1",
130-
"--make=false",
131-
"--manifests=false",
132-
)
133-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
134-
135-
By("uncomment kustomization.yaml to enable prometheus")
136-
ExpectWithOffset(1, util.UncommentCode(
137-
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
138-
"#- ../prometheus", "#")).To(Succeed())
139-
140-
By("uncomment kustomize files to ensure that pods are restricted")
141-
uncommentPodStandards(kbc)
142-
60+
GenerateDeployImage(kbc)
14361
Run(kbc)
14462
})
14563
})
@@ -258,7 +176,7 @@ func Run(kbc *utils.TestContext) {
258176
}
259177
Eventually(getStatus, time.Minute, time.Second).Should(Succeed())
260178

261-
// Testing the finalizer
179+
By("validating the finalizer")
262180
EventuallyWithOffset(1, func() error {
263181
_, err = kbc.Kubectl.Delete(true, "-f", sampleFilePath)
264182
return err
@@ -275,19 +193,3 @@ func Run(kbc *utils.TestContext) {
275193
return nil
276194
}, time.Minute, time.Second).Should(Succeed())
277195
}
278-
279-
func uncommentPodStandards(kbc *utils.TestContext) {
280-
configManager := filepath.Join(kbc.Dir, "config", "manager", "manager.yaml")
281-
282-
//nolint:lll
283-
if err := util.ReplaceInFile(configManager, `# TODO(user): For common cases that do not require escalating privileges
284-
# it is recommended to ensure that all your Pods/Containers are restrictive.
285-
# More info: https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted
286-
# Please uncomment the following code if your project does NOT have to work on old Kubernetes
287-
# versions < 1.19 or on vendors versions which do NOT support this field by default (i.e. Openshift < 4.11 ).
288-
# seccompProfile:
289-
# type: RuntimeDefault`, `seccompProfile:
290-
type: RuntimeDefault`); err == nil {
291-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
292-
}
293-
}

0 commit comments

Comments
 (0)