|
| 1 | +package osde2etests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" //nolint:golint |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | + rbacv1 "k8s.io/api/rbac/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + apimachinerylabels "k8s.io/apimachinery/pkg/labels" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 18 | + "sigs.k8s.io/e2e-framework/klient/k8s/resources" |
| 19 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 20 | + "sigs.k8s.io/e2e-framework/klient/wait/conditions" |
| 21 | +) |
| 22 | + |
| 23 | +var _ = ginkgo.Describe("DVO", ginkgo.Ordered, func() { |
| 24 | + const ( |
| 25 | + namespaceName = "openshift-deployment-validation-operator" |
| 26 | + deploymentName = "deployment-validation-operator" |
| 27 | + ) |
| 28 | + |
| 29 | + var k8s *resources.Resources |
| 30 | + |
| 31 | + ginkgo.BeforeAll(func() { |
| 32 | + // setup the k8s client |
| 33 | + cfg, err := config.GetConfig() |
| 34 | + Expect(err).Should(BeNil(), "failed to get kubeconfig") |
| 35 | + k8s, err = resources.New(cfg) |
| 36 | + Expect(err).Should(BeNil(), "resources.New error") |
| 37 | + }) |
| 38 | + |
| 39 | + ginkgo.It("exists and is running", func(ctx context.Context) { |
| 40 | + const serviceName = "deployment-validation-operator-metrics" |
| 41 | + clusterRoles := []string{ |
| 42 | + "deployment-validation-operator-og-admin", |
| 43 | + "deployment-validation-operator-og-edit", |
| 44 | + "deployment-validation-operator-og-view", |
| 45 | + } |
| 46 | + |
| 47 | + err := k8s.Get(ctx, namespaceName, namespaceName, &v1.Namespace{}) |
| 48 | + Expect(err).Should(BeNil(), "unable to get namespace %s", namespaceName) |
| 49 | + |
| 50 | + err = k8s.Get(ctx, serviceName, namespaceName, &v1.Service{}) |
| 51 | + Expect(err).Should(BeNil(), "unable to get service %s", serviceName) |
| 52 | + |
| 53 | + for _, clusterRoleName := range clusterRoles { |
| 54 | + err = k8s.Get(ctx, clusterRoleName, "", &rbacv1.ClusterRole{}) |
| 55 | + Expect(err).Should(BeNil(), "unable to get clusterrole %s", clusterRoleName) |
| 56 | + } |
| 57 | + |
| 58 | + deployment := &appsv1.Deployment{ |
| 59 | + ObjectMeta: metav1.ObjectMeta{Name: deploymentName, Namespace: namespaceName}, |
| 60 | + } |
| 61 | + deploymentAvailable := conditions.New(k8s). |
| 62 | + DeploymentConditionMatch(deployment, appsv1.DeploymentAvailable, v1.ConditionTrue) |
| 63 | + err = wait.For(deploymentAvailable, wait.WithTimeout(10*time.Second)) |
| 64 | + Expect(err).Should(BeNil(), "deployment %s never became available", deploymentName) |
| 65 | + }) |
| 66 | + |
| 67 | + ginkgo.Context("validates", func() { |
| 68 | + ginkgo.BeforeEach(func(ctx context.Context) { |
| 69 | + labels := map[string]string{"app": "test"} |
| 70 | + deployment := &appsv1.Deployment{ |
| 71 | + ObjectMeta: metav1.ObjectMeta{GenerateName: "osde2e-", Namespace: "default"}, |
| 72 | + Spec: appsv1.DeploymentSpec{ |
| 73 | + Selector: &metav1.LabelSelector{MatchLabels: labels}, |
| 74 | + Template: v1.PodTemplateSpec{ |
| 75 | + ObjectMeta: metav1.ObjectMeta{Labels: labels}, |
| 76 | + Spec: v1.PodSpec{ |
| 77 | + Containers: []v1.Container{ |
| 78 | + {Name: "pause", Image: "registry.k8s.io/pause:latest"}, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | + err := k8s.Create(ctx, deployment) |
| 85 | + Expect(err).Should(BeNil(), "unable to create deployment %s", deployment.GetName()) |
| 86 | + deploymentAvailable := conditions.New(k8s). |
| 87 | + DeploymentConditionMatch(deployment, appsv1.DeploymentAvailable, v1.ConditionTrue) |
| 88 | + err = wait.For(deploymentAvailable) |
| 89 | + Expect(err).Should(BeNil(), "deployment %s never became available", deployment.GetName()) |
| 90 | + |
| 91 | + ginkgo.DeferCleanup(k8s.Delete, deployment) |
| 92 | + }) |
| 93 | + |
| 94 | + ginkgo.It("new deployments", func(ctx context.Context) { |
| 95 | + //nolint:lll |
| 96 | + validationMsg := fmt.Sprintf("\"msg\":\"Set memory requests and limits for your container based on its requirements. Refer to https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for details.\",\"request.namespace\":\"default\"") |
| 97 | + |
| 98 | + // Wait for the deployment logs to contain the validation message |
| 99 | + err := wait.For(func() (bool, error) { |
| 100 | + pods := &v1.PodList{} |
| 101 | + lbls := apimachinerylabels.FormatLabels(map[string]string{"app": deploymentName}) |
| 102 | + err := k8s.List(ctx, pods, resources.WithLabelSelector(lbls)) |
| 103 | + if err != nil { |
| 104 | + return false, err |
| 105 | + } |
| 106 | + |
| 107 | + if len(pods.Items) < 1 { |
| 108 | + return false, fmt.Errorf("unable to find pod for deployment %s", deploymentName) |
| 109 | + } |
| 110 | + |
| 111 | + clientset, err := kubernetes.NewForConfig(k8s.GetConfig()) |
| 112 | + if err != nil { |
| 113 | + return false, err |
| 114 | + } |
| 115 | + |
| 116 | + req := clientset.CoreV1().Pods(namespaceName). |
| 117 | + GetLogs(pods.Items[0].GetName(), &v1.PodLogOptions{}) |
| 118 | + logs, err := req.DoRaw(ctx) |
| 119 | + if err != nil { |
| 120 | + return false, err |
| 121 | + } |
| 122 | + |
| 123 | + return strings.Contains(string(logs), validationMsg), nil |
| 124 | + }, wait.WithTimeout(10*time.Minute)) |
| 125 | + Expect(err).Should(BeNil(), "failed waiting for validation message") |
| 126 | + }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments