Skip to content

Commit

Permalink
Refactor setup to take extra setup functions ⛽
Browse files Browse the repository at this point in the history
And use this to extract the "local" registry part of TestKanikoTaskRun
into a separate function, reusable in other tests.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Nov 5, 2019
1 parent 7d06b97 commit 74bff77
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 73 deletions.
7 changes: 6 additions & 1 deletion test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
}

Expand Down
74 changes: 2 additions & 72 deletions test/kaniko_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -45,36 +43,14 @@ 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)

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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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...)
Expand Down
102 changes: 102 additions & 0 deletions test/registry_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
},
}
}

0 comments on commit 74bff77

Please sign in to comment.