From 006b0c80cf7ee80d3565e4713d2e017d640f9445 Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Sat, 12 Jun 2021 09:38:19 +0200 Subject: [PATCH] test(e2e): ImagePullPolicy for v1alpha using annotations --- e2e/imagepullpolicy_multiple_test.go | 122 +++++++++++++++++++++++++++ e2e/imagepullpolicy_single_test.go | 122 +++++++++++++++++++++++++++ 2 files changed, 244 insertions(+) create mode 100644 e2e/imagepullpolicy_multiple_test.go create mode 100644 e2e/imagepullpolicy_single_test.go diff --git a/e2e/imagepullpolicy_multiple_test.go b/e2e/imagepullpolicy_multiple_test.go new file mode 100644 index 00000000..5a644390 --- /dev/null +++ b/e2e/imagepullpolicy_multiple_test.go @@ -0,0 +1,122 @@ +//+build e2e + +// Copyright 2020-2021 Clastix Labs +// SPDX-License-Identifier: Apache-2.0 + +package e2e + +import ( + "context" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/clastix/capsule/api/v1alpha1" +) + +var _ = Describe("enforcing some defined ImagePullPolicy", func() { + tnt := &v1alpha1.Tenant{ + ObjectMeta: metav1.ObjectMeta{ + Name: "image-pull-policies", + Annotations: map[string]string{ + "capsule.clastix.io/allowed-image-pull-policy": "Always,IfNotPresent", + }, + }, + Spec: v1alpha1.TenantSpec{ + Owner: v1alpha1.OwnerSpec{ + Name: "alex", + Kind: "User", + }, + }, + } + + JustBeforeEach(func() { + EventuallyCreation(func() error { + tnt.ResourceVersion = "" + return k8sClient.Create(context.TODO(), tnt) + }).Should(Succeed()) + }) + + JustAfterEach(func() { + Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed()) + }) + + It("should just allow the defined policies", func() { + ns := NewNamespace("allow-policy") + NamespaceCreation(ns, tnt, defaultTimeoutInterval).Should(Succeed()) + + cs := ownerClient(tnt) + + By("allowing Always", func() { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pull-always", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "container", + Image: "gcr.io/google_containers/pause-amd64:3.0", + ImagePullPolicy: corev1.PullAlways, + }, + }, + }, + } + + EventuallyCreation(func() (err error) { + _, err = cs.CoreV1().Pods(ns.Name).Create(context.Background(), pod, metav1.CreateOptions{}) + + return + }).Should(Succeed()) + }) + + + By("allowing IfNotPresent", func() { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "if-not-present", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "container", + Image: "gcr.io/google_containers/pause-amd64:3.0", + ImagePullPolicy: corev1.PullIfNotPresent, + }, + }, + }, + } + + EventuallyCreation(func() (err error) { + _, err = cs.CoreV1().Pods(ns.Name).Create(context.Background(), pod, metav1.CreateOptions{}) + + return + }).Should(Succeed()) + }) + + By("blocking Never", func() { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "never", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "container", + Image: "gcr.io/google_containers/pause-amd64:3.0", + ImagePullPolicy: corev1.PullNever, + }, + }, + }, + } + + EventuallyCreation(func() (err error) { + _, err = cs.CoreV1().Pods(ns.Name).Create(context.Background(), pod, metav1.CreateOptions{}) + + return + }).ShouldNot(Succeed()) + }) + }) +}) diff --git a/e2e/imagepullpolicy_single_test.go b/e2e/imagepullpolicy_single_test.go new file mode 100644 index 00000000..b3a782a5 --- /dev/null +++ b/e2e/imagepullpolicy_single_test.go @@ -0,0 +1,122 @@ +//+build e2e + +// Copyright 2020-2021 Clastix Labs +// SPDX-License-Identifier: Apache-2.0 + +package e2e + +import ( + "context" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/clastix/capsule/api/v1alpha1" +) + +var _ = Describe("enforcing a defined ImagePullPolicy", func() { + tnt := &v1alpha1.Tenant{ + ObjectMeta: metav1.ObjectMeta{ + Name: "image-pull-policy", + Annotations: map[string]string{ + "capsule.clastix.io/allowed-image-pull-policy": "Always", + }, + }, + Spec: v1alpha1.TenantSpec{ + Owner: v1alpha1.OwnerSpec{ + Name: "axel", + Kind: "User", + }, + }, + } + + JustBeforeEach(func() { + EventuallyCreation(func() error { + tnt.ResourceVersion = "" + return k8sClient.Create(context.TODO(), tnt) + }).Should(Succeed()) + }) + + JustAfterEach(func() { + Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed()) + }) + + It("should just allow the defined policy", func() { + ns := NewNamespace("allow-policies") + NamespaceCreation(ns, tnt, defaultTimeoutInterval).Should(Succeed()) + + cs := ownerClient(tnt) + + By("allowing Always", func() { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pull-always", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "container", + Image: "gcr.io/google_containers/pause-amd64:3.0", + ImagePullPolicy: corev1.PullAlways, + }, + }, + }, + } + + EventuallyCreation(func() (err error) { + _, err = cs.CoreV1().Pods(ns.Name).Create(context.Background(), pod, metav1.CreateOptions{}) + + return + }).Should(Succeed()) + }) + + + By("blocking IfNotPresent", func() { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "if-not-present", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "container", + Image: "gcr.io/google_containers/pause-amd64:3.0", + ImagePullPolicy: corev1.PullIfNotPresent, + }, + }, + }, + } + + EventuallyCreation(func() (err error) { + _, err = cs.CoreV1().Pods(ns.Name).Create(context.Background(), pod, metav1.CreateOptions{}) + + return + }).ShouldNot(Succeed()) + }) + + By("blocking Never", func() { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "never", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "container", + Image: "gcr.io/google_containers/pause-amd64:3.0", + ImagePullPolicy: corev1.PullNever, + }, + }, + }, + } + + EventuallyCreation(func() (err error) { + _, err = cs.CoreV1().Pods(ns.Name).Create(context.Background(), pod, metav1.CreateOptions{}) + + return + }).ShouldNot(Succeed()) + }) + }) +})