diff --git a/test/components/cert-manager/cert-manager_test.go b/test/components/cert-manager/cert-manager_test.go index 2d7b69a96..3e84a5579 100644 --- a/test/components/cert-manager/cert-manager_test.go +++ b/test/components/cert-manager/cert-manager_test.go @@ -51,3 +51,13 @@ func TestCertManagerDeployments(t *testing.T) { }) } } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/contour/contour_test.go b/test/components/contour/contour_test.go index 9bf8f4ccf..eaf6d3bc9 100644 --- a/test/components/contour/contour_test.go +++ b/test/components/contour/contour_test.go @@ -24,9 +24,12 @@ import ( testutil "github.com/kinvolk/lokomotive/test/components/util" ) +const ( + namespace = "projectcontour" +) + func TestEnvoyDaemonset(t *testing.T) { t.Parallel() - namespace := "projectcontour" daemonset := "envoy" client := testutil.CreateKubeClient(t) @@ -43,3 +46,13 @@ func TestContourDeployment(t *testing.T) { testutil.WaitForDeployment(t, client, namespace, deployment, time.Second*5, time.Minute*5) } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/dex/dex_test.go b/test/components/dex/dex_test.go index 4a02c91c3..231433896 100644 --- a/test/components/dex/dex_test.go +++ b/test/components/dex/dex_test.go @@ -24,9 +24,11 @@ import ( testutil "github.com/kinvolk/lokomotive/test/components/util" ) -func TestDexDeployment(t *testing.T) { - namespace := "dex" +const ( + namespace = "dex" +) +func TestDexDeployment(t *testing.T) { client := testutil.CreateKubeClient(t) t.Run("deployment", func(t *testing.T) { @@ -36,3 +38,13 @@ func TestDexDeployment(t *testing.T) { testutil.WaitForDeployment(t, client, namespace, deployment, time.Second*5, time.Minute*5) }) } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/external-dns/external-dns_test.go b/test/components/external-dns/external-dns_test.go index da76bfef8..c28947878 100644 --- a/test/components/external-dns/external-dns_test.go +++ b/test/components/external-dns/external-dns_test.go @@ -35,3 +35,13 @@ func TestExternalDNSDeployments(t *testing.T) { testutil.WaitForDeployment(t, client, namespace, deployment, time.Second*5, time.Minute*5) }) } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/flatcar-linux-update-operator/fluo_test.go b/test/components/flatcar-linux-update-operator/fluo_test.go index e02be5b9b..a8be20b69 100644 --- a/test/components/flatcar-linux-update-operator/fluo_test.go +++ b/test/components/flatcar-linux-update-operator/fluo_test.go @@ -24,9 +24,12 @@ import ( testutil "github.com/kinvolk/lokomotive/test/components/util" ) +const ( + namespace = "reboot-coordinator" +) + func TestUpdateAgentDaemonset(t *testing.T) { t.Parallel() - namespace := "reboot-coordinator" daemonset := "flatcar-linux-update-agent" client := testutil.CreateKubeClient(t) @@ -43,3 +46,13 @@ func TestUpdateOperatorDeployment(t *testing.T) { testutil.WaitForDeployment(t, client, namespace, deployment, time.Second*5, time.Minute*5) } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/gangway/gangway_test.go b/test/components/gangway/gangway_test.go index 030174a12..4ddea1374 100644 --- a/test/components/gangway/gangway_test.go +++ b/test/components/gangway/gangway_test.go @@ -24,9 +24,11 @@ import ( testutil "github.com/kinvolk/lokomotive/test/components/util" ) -func TestGangwayDeployment(t *testing.T) { - namespace := "gangway" +const ( + namespace = "gangway" +) +func TestGangwayDeployment(t *testing.T) { client := testutil.CreateKubeClient(t) t.Run("deployment", func(t *testing.T) { @@ -36,3 +38,13 @@ func TestGangwayDeployment(t *testing.T) { testutil.WaitForDeployment(t, client, namespace, deployment, time.Second*5, time.Minute*5) }) } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/kubernetes/kube_system_labels_test.go b/test/components/kubernetes/kube_system_labels_test.go new file mode 100644 index 000000000..9977a1f85 --- /dev/null +++ b/test/components/kubernetes/kube_system_labels_test.go @@ -0,0 +1,37 @@ +// Copyright 2020 The Lokomotive 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. + +// +build aws aws_edge packet aks +// +build e2e + +package kubernetes //nolint:testpackage + +import ( + "testing" + "time" + + testutil "github.com/kinvolk/lokomotive/test/components/util" +) + +const namespace = "kube-system" + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/metallb/metallb_test.go b/test/components/metallb/metallb_test.go index db91219e1..54288bd22 100644 --- a/test/components/metallb/metallb_test.go +++ b/test/components/metallb/metallb_test.go @@ -24,9 +24,11 @@ import ( testutil "github.com/kinvolk/lokomotive/test/components/util" ) -func TestMetalLBDeployment(t *testing.T) { - namespace := "metallb-system" +const ( + namespace = "metallb-system" +) +func TestMetalLBDeployment(t *testing.T) { client := testutil.CreateKubeClient(t) t.Run("speaker daemonset", func(t *testing.T) { @@ -43,3 +45,13 @@ func TestMetalLBDeployment(t *testing.T) { testutil.WaitForDeployment(t, client, namespace, deployment, time.Second*5, time.Minute*5) }) } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/openebs-operator/openebs_operator_test.go b/test/components/openebs-operator/openebs_operator_test.go index eea9f8a54..54b3a2f5f 100644 --- a/test/components/openebs-operator/openebs_operator_test.go +++ b/test/components/openebs-operator/openebs_operator_test.go @@ -24,9 +24,11 @@ import ( testutil "github.com/kinvolk/lokomotive/test/components/util" ) -func TestOpenEBSOperatorDeployment(t *testing.T) { - namespace := "openebs" +const ( + namespace = "openebs" +) +func TestOpenEBSOperatorDeployment(t *testing.T) { client := testutil.CreateKubeClient(t) deployments := []string{ @@ -46,3 +48,13 @@ func TestOpenEBSOperatorDeployment(t *testing.T) { }) } } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/prometheus-operator/prometheus_operator_test.go b/test/components/prometheus-operator/prometheus_operator_test.go index e0bfdef95..3e74378ef 100644 --- a/test/components/prometheus-operator/prometheus_operator_test.go +++ b/test/components/prometheus-operator/prometheus_operator_test.go @@ -163,3 +163,13 @@ func TestGrafanaLoadsEnvVars(t *testing.T) { t.Fatalf("required env var %q not found in following env vars:\n\n%s\n", searchEnvVar, containerOutput) } } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/rook/rook_test.go b/test/components/rook/rook_test.go index 339d43ab7..70a37fe09 100644 --- a/test/components/rook/rook_test.go +++ b/test/components/rook/rook_test.go @@ -24,6 +24,10 @@ import ( testutil "github.com/kinvolk/lokomotive/test/components/util" ) +const ( + namespace = "rook" +) + func TestRookDeployment(t *testing.T) { namespace := "rook" @@ -36,3 +40,13 @@ func TestRookDeployment(t *testing.T) { testutil.WaitForDeployment(t, client, namespace, deployment, time.Second*5, time.Minute*5) }) } + +func TestNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +} diff --git a/test/components/util/util.go b/test/components/util/util.go index 49adc7fc2..0a53b9776 100644 --- a/test/components/util/util.go +++ b/test/components/util/util.go @@ -126,6 +126,49 @@ func WaitForDaemonSet(t *testing.T, client kubernetes.Interface, ns, name string } } +// IsLabelPresentInNamespace tests whether the given labels are present in the namespace. +func IsLabelPresentInNamespace( + t *testing.T, + client kubernetes.Interface, + ns string, + labels map[string]string, + retryInterval, + timeout time.Duration, +) { + var err error + + var namespace *corev1.Namespace + + if err = wait.PollImmediate(retryInterval, timeout, func() (done bool, err error) { + namespace, err = client.CoreV1().Namespaces().Get(context.TODO(), ns, metav1.GetOptions{}) + if err != nil { + if k8serrors.IsNotFound(err) { + t.Logf("waiting for namespace %s to be available", ns) + + return false, nil + } + + return false, err + } + + for expectedKey, expectedValue := range labels { + value, ok := namespace.ObjectMeta.Labels[expectedKey] + if !ok { + t.Errorf("expected key '%s' not found", expectedKey) + } + + if expectedValue != value { + t.Errorf("expected value '%s' for key '%s', got '%s'", expectedValue, expectedKey, value) + } + } + + return true, nil + }); err != nil { + t.Errorf("error while waiting for namespace: %v", err) + return + } +} + func WaitForDeployment(t *testing.T, client kubernetes.Interface, ns, name string, retryInterval, timeout time.Duration) { var err error var deploy *appsv1.Deployment diff --git a/test/system/kube_system_namespace_labels_test.go b/test/system/kube_system_namespace_labels_test.go new file mode 100644 index 000000000..def32595b --- /dev/null +++ b/test/system/kube_system_namespace_labels_test.go @@ -0,0 +1,39 @@ +// Copyright 2020 The Lokomotive 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. + +// +build aws aws_edge packet baremetal +// +build e2e + +package system // nolint:testpackage + +import ( + "testing" + "time" + + testutil "github.com/kinvolk/lokomotive/test/components/util" +) + +const ( + namespace = "kube-system" +) + +func TestKubeSystemNamespaceHasLabels(t *testing.T) { + client := testutil.CreateKubeClient(t) + + labels := map[string]string{ + "lokomotive.kinvolk.io/name": namespace, + } + + testutil.IsLabelPresentInNamespace(t, client, namespace, labels, time.Second*5, time.Minute*5) +}