Skip to content

Commit

Permalink
Cleanup e2e tests for deploy image
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed May 25, 2024
1 parent 576e29d commit 022aaab
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 101 deletions.
82 changes: 82 additions & 0 deletions test/e2e/deployimage/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2020 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 deployimage

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

//nolint:golint
// nolint:revive
//nolint:golint
// nolint:revive
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
)

// GenerateDeployImageWithOptions implements a go/v4 plugin project and scaffold an API using the image options
func GenerateDeployImageWithOptions(kbc *utils.TestContext) {
initTheProject(kbc)
creatingAPIWithOptions(kbc)
}

// GenerateDeployImage implements a go/v4 plugin project and scaffold an API using the deploy image plugin
func GenerateDeployImage(kbc *utils.TestContext) {
initTheProject(kbc)
creatingAPI(kbc)
}

func creatingAPI(kbc *utils.TestContext) {
By("creating API definition with deploy-image/v1-alpha plugin")
err := kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--plugins", "deploy-image/v1-alpha",
"--image", "busybox:1.36.1",
"--make=false",
"--manifests=false",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

func creatingAPIWithOptions(kbc *utils.TestContext) {
var err error
By("creating API definition with deploy-image/v1-alpha plugin")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--plugins", "deploy-image/v1-alpha",
"--image", "memcached:1.6.26-alpine3.19",
"--image-container-port", "11211",
"--image-container-command", "memcached,-m=64,-o,modern,-v",
"--run-as-user", "1001",
"--make=false",
"--manifests=false",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

func initTheProject(kbc *utils.TestContext) {
By("initializing a project")
err := kbc.Init(
"--plugins", "go/v4",
"--project-version", "3",
"--domain", kbc.Domain,
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
104 changes: 3 additions & 101 deletions test/e2e/deployimage/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,105 +41,23 @@ var _ = Describe("kubebuilder", func() {
kbc, err = utils.NewTestContext(util.KubebuilderBinName, "GO111MODULE=on")
Expect(err).NotTo(HaveOccurred())
Expect(kbc.Prepare()).To(Succeed())

By("installing prometheus operator")
Expect(kbc.InstallPrometheusOperManager()).To(Succeed())

By("creating manager namespace")
err = kbc.CreateManagerNamespace()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("labeling all namespaces to warn about restricted")
err = kbc.LabelAllNamespacesToWarnAboutRestricted()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("enforce that namespace where the sample will be applied can only run restricted containers")
_, err = kbc.Kubectl.Command("label", "--overwrite", "ns", kbc.Kubectl.Namespace,
"pod-security.kubernetes.io/audit=restricted",
"pod-security.kubernetes.io/enforce-version=v1.24",
"pod-security.kubernetes.io/enforce=restricted")
Expect(err).To(Not(HaveOccurred()))
})

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

By("uninstalling the Prometheus manager bundle")
kbc.UninstallPrometheusOperManager()

By("removing controller image and working dir")
kbc.Destroy()
})

It("should generate a runnable project with deploy-image/v1-alpha options ", func() {
var err error

By("initializing a project with go/v4")
err = kbc.Init(
"--plugins", "go/v4",
"--project-version", "3",
"--domain", kbc.Domain,
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("creating API definition with deploy-image/v1-alpha plugin")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--plugins", "deploy-image/v1-alpha",
"--image", "memcached:1.6.26-alpine3.19",
"--image-container-port", "11211",
"--image-container-command", "memcached,-m=64,-o,modern,-v",
"--run-as-user", "1001",
"--make=false",
"--manifests=false",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("uncomment kustomization.yaml to enable prometheus")
ExpectWithOffset(1, util.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../prometheus", "#")).To(Succeed())

By("uncomment kustomize files to ensure that pods are restricted")
uncommentPodStandards(kbc)

GenerateDeployImageWithOptions(kbc)
Run(kbc)
})

It("should generate a runnable project with deploy-image/v1-alpha without options ", func() {
var err error

By("initializing a project with go/v4")
err = kbc.Init(
"--plugins", "go/v4",
"--project-version", "3",
"--domain", kbc.Domain,
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("creating API definition with deploy-image/v1-alpha plugin")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--plugins", "deploy-image/v1-alpha",
"--image", "busybox:1.36.1",
"--make=false",
"--manifests=false",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("uncomment kustomization.yaml to enable prometheus")
ExpectWithOffset(1, util.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../prometheus", "#")).To(Succeed())

By("uncomment kustomize files to ensure that pods are restricted")
uncommentPodStandards(kbc)

GenerateDeployImage(kbc)
Run(kbc)
})
})
Expand Down Expand Up @@ -258,7 +176,7 @@ func Run(kbc *utils.TestContext) {
}
Eventually(getStatus, time.Minute, time.Second).Should(Succeed())

// Testing the finalizer
By("validating the finalizer")
EventuallyWithOffset(1, func() error {
_, err = kbc.Kubectl.Delete(true, "-f", sampleFilePath)
return err
Expand All @@ -275,19 +193,3 @@ func Run(kbc *utils.TestContext) {
return nil
}, time.Minute, time.Second).Should(Succeed())
}

func uncommentPodStandards(kbc *utils.TestContext) {
configManager := filepath.Join(kbc.Dir, "config", "manager", "manager.yaml")

//nolint:lll
if err := util.ReplaceInFile(configManager, `# TODO(user): For common cases that do not require escalating privileges
# it is recommended to ensure that all your Pods/Containers are restrictive.
# More info: https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted
# Please uncomment the following code if your project does NOT have to work on old Kubernetes
# versions < 1.19 or on vendors versions which do NOT support this field by default (i.e. Openshift < 4.11 ).
# seccompProfile:
# type: RuntimeDefault`, `seccompProfile:
type: RuntimeDefault`); err == nil {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
}

0 comments on commit 022aaab

Please sign in to comment.