Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃尡 Cleanup e2e tests for deploy image #3959

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
}
Loading