diff --git a/test/init_test.go b/test/init_test.go index b5db87fe93e..c8c08cda228 100644 --- a/test/init_test.go +++ b/test/init_test.go @@ -44,7 +44,7 @@ import ( var initMetrics sync.Once -func setup(t *testing.T) (*clients, string) { +func setup(t *testing.T, fn ...func(*testing.T, *clients, string)) (*clients, string) { t.Helper() namespace := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("arendelle") @@ -53,6 +53,11 @@ func setup(t *testing.T) (*clients, string) { c := newClients(t, knativetest.Flags.Kubeconfig, knativetest.Flags.Cluster, namespace) createNamespace(t, namespace, c.KubeClient) verifyServiceAccountExistence(t, namespace, c.KubeClient) + + for _, f := range fn { + f(t, c, namespace) + } + return c, namespace } diff --git a/test/kaniko_task_test.go b/test/kaniko_task_test.go index 18399a8e14f..49ef88494fc 100644 --- a/test/kaniko_task_test.go +++ b/test/kaniko_task_test.go @@ -27,10 +27,8 @@ import ( "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" tb "github.com/tektoncd/pipeline/test/builder" - appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" knativetest "knative.dev/pkg/test" ) @@ -45,7 +43,7 @@ const ( // TestTaskRun is an integration test that will verify a TaskRun using kaniko func TestKanikoTaskRun(t *testing.T) { - c, namespace := setup(t) + c, namespace := setup(t, withRegistry) t.Parallel() repo := fmt.Sprintf("registry.%s:5000/kanikotasktest", namespace) @@ -53,28 +51,6 @@ func TestKanikoTaskRun(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf) defer tearDown(t, c, namespace) - if _, err := c.KubeClient.Kube.AppsV1().Deployments(namespace).Create(getRegistryDeployment(namespace)); err != nil { - t.Fatalf("Failed to create the local registry deployment: %v", err) - } - service := getRegistryService(namespace) - if _, err := c.KubeClient.Kube.CoreV1().Services(namespace).Create(service); err != nil { - t.Fatalf("Failed to create the local registry service: %v", err) - } - set := labels.Set(service.Spec.Selector) - if pods, err := c.KubeClient.Kube.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: set.AsSelector().String()}); err != nil { - t.Fatalf("Failed to list Pods of service[%s] error:%v", service.GetName(), err) - } else { - if len(pods.Items) != 1 { - t.Fatalf("Only 1 pod for service %s should be running: %v", service, pods.Items) - } - - if err := WaitForPodState(c, pods.Items[0].Name, namespace, func(pod *corev1.Pod) (bool, error) { - return pod.Status.Phase == "Running", nil - }, "PodContainersRunning"); err != nil { - t.Fatalf("Error waiting for Pod %q to run: %v", pods.Items[0].Name, err) - } - } - t.Logf("Creating Git PipelineResource %s", kanikoGitResourceName) if _, err := c.PipelineResourceClient.Create(getGitResource(namespace)); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", kanikoGitResourceName, err) @@ -138,52 +114,6 @@ func TestKanikoTaskRun(t *testing.T) { } } -func getRegistryDeployment(namespace string) *appsv1.Deployment { - return &appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - Name: "registry", - }, - Spec: appsv1.DeploymentSpec{ - Selector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - "app": "registry", - }, - }, - Template: corev1.PodTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - "app": "registry", - }, - }, - Spec: corev1.PodSpec{ - Containers: []corev1.Container{{ - Name: "registry", - Image: "registry", - }}, - }, - }, - }, - } -} - -func getRegistryService(namespace string) *corev1.Service { - return &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - Name: "registry", - }, - Spec: corev1.ServiceSpec{ - Ports: []corev1.ServicePort{{ - Port: 5000, - }}, - Selector: map[string]string{ - "app": "registry", - }, - }, - } -} - func getGitResource(namespace string) *v1alpha1.PipelineResource { return tb.PipelineResource(kanikoGitResourceName, namespace, tb.PipelineResourceSpec( v1alpha1.PipelineResourceTypeGit, @@ -212,7 +142,7 @@ func getTask(repo, namespace string) *v1alpha1.Task { "--oci-layout-path=/workspace/output/builtImage", "--insecure", "--insecure-pull", - "--insecure-registry=registry"+namespace+":5000/", + "--insecure-registry=registry."+namespace+":5000/", ), } step := tb.Step("kaniko", "gcr.io/kaniko-project/executor:v0.13.0", stepOps...) diff --git a/test/registry_test.go b/test/registry_test.go new file mode 100644 index 00000000000..58eb638643a --- /dev/null +++ b/test/registry_test.go @@ -0,0 +1,102 @@ +// +build e2e + +/* +Copyright 2019 The Tekton 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 test + +import ( + "testing" + "time" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" +) + +func withRegistry(t *testing.T, c *clients, namespace string) { + if _, err := c.KubeClient.Kube.AppsV1().Deployments(namespace).Create(getRegistryDeployment(namespace)); err != nil { + t.Fatalf("Failed to create the local registry deployment: %v", err) + } + service := getRegistryService(namespace) + if _, err := c.KubeClient.Kube.CoreV1().Services(namespace).Create(service); err != nil { + t.Fatalf("Failed to create the local registry service: %v", err) + } + set := labels.Set(service.Spec.Selector) + + // Give it a little bit of time to at least create the pod + time.Sleep(5 * time.Second) + + if pods, err := c.KubeClient.Kube.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: set.AsSelector().String()}); err != nil { + t.Fatalf("Failed to list Pods of service[%s] error:%v", service.GetName(), err) + } else { + if len(pods.Items) != 1 { + t.Fatalf("Only 1 pod for service %s should be running: %v", service, pods.Items) + } + + if err := WaitForPodState(c, pods.Items[0].Name, namespace, func(pod *corev1.Pod) (bool, error) { + return pod.Status.Phase == "Running", nil + }, "PodContainersRunning"); err != nil { + t.Fatalf("Error waiting for Pod %q to run: %v", pods.Items[0].Name, err) + } + } +} + +func getRegistryDeployment(namespace string) *appsv1.Deployment { + return &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: namespace, + Name: "registry", + }, + Spec: appsv1.DeploymentSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "registry", + }, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + "app": "registry", + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Name: "registry", + Image: "registry", + }}, + }, + }, + }, + } +} + +func getRegistryService(namespace string) *corev1.Service { + return &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: namespace, + Name: "registry", + }, + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{ + Port: 5000, + }}, + Selector: map[string]string{ + "app": "registry", + }, + }, + } +}