Skip to content

Commit

Permalink
Let Kaniko E2E test work with a KO_DOCKER_REPO env
Browse files Browse the repository at this point in the history
If test runner set a KO_DOCKER_REPO variable, use it, so we run the tests
against an external container registry. If not set, the kaniko tests
will spin up its own local registry.

Since the local registry runs on HTTP (not HTTPS), the local registry
approach does not work for the helm test as the kubelet tries to use
HTTPS. If KO_DOCKER_REPO is not specified skip the helm test and do not
fail.

This setup makes it easier to run E2E tests in different environments,
where a registry may or may not be available.

Signed-off-by: Andrea Frittoli <andrea.frittoli@uk.ibm.com>
  • Loading branch information
afrittoli committed Dec 1, 2021
1 parent df5cc01 commit 1a64c95
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
11 changes: 10 additions & 1 deletion test/helm_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ var (
// TestHelmDeployPipelineRun is an integration test that will verify a pipeline build an image
// and then using helm to deploy it
func TestHelmDeployPipelineRun(t *testing.T) {
repo := ensureDockerRepo(t)

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

repo, err := getDockerRepo("helmtasktest")
if err != nil {
// No KO_DOCKER_REPO set, this test cannot run, let's skip it
// We cannot use the sidecar registry (as it is today) because
// the kubelet won't pull images from an insecure registry
t.Skip("KO_DOCKER_REPO env variable is required")
}

c, namespace := setup(ctx, t)
setupClusterBindingForHelm(ctx, c, t, namespace)

Expand Down
26 changes: 17 additions & 9 deletions test/kaniko_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const (

// TestTaskRun is an integration test that will verify a TaskRun using kaniko
func TestKanikoTaskRun(t *testing.T) {
var (
namespace, repo string
c *clients
)

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand All @@ -53,11 +58,16 @@ func TestKanikoTaskRun(t *testing.T) {
t.Skip("Skip test as skipRootUserTests set to true")
}

c, namespace := setup(ctx, t, withRegistry)
repo, err := getDockerRepo("kanikotasktest")
if err != nil {
// No KO_DOCKER_REPO set, use a sidecar registry instead
c, namespace = setup(ctx, t, withRegistry)
repo = fmt.Sprintf("registry.%s.svc.cluster.local:5000/kanikotasktest", namespace)
} else {
c, namespace = setup(ctx, t)
}
t.Parallel()

repo := fmt.Sprintf("registry.%s:5000/kanikotasktest", namespace)

knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

Expand Down Expand Up @@ -159,7 +169,7 @@ spec:
}

func getTask(t *testing.T, repo, namespace string) *v1beta1.Task {
return parse.MustParseTask(t, fmt.Sprintf(`
task := parse.MustParseTask(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
Expand All @@ -177,16 +187,14 @@ spec:
args: ['--dockerfile=/workspace/gitsource/integration/dockerfiles/Dockerfile_test_label',
'--destination=%s',
'--context=/workspace/gitsource',
'--oci-layout-path=/workspace/output/builtImage',
'--insecure',
'--insecure-pull',
'--insecure-registry=registry.%s:5000/']
'--oci-layout-path=/workspace/output/builtImage']
securityContext:
runAsUser: 0
sidecars:
- name: registry
image: %s
`, kanikoTaskName, namespace, getTestImage(kanikoImage), repo, namespace, getTestImage(registryImage)))
`, kanikoTaskName, namespace, getTestImage(kanikoImage), repo, getTestImage(registryImage)))
return task
}

func getTaskRun(t *testing.T, namespace string) *v1beta1.TaskRun {
Expand Down
21 changes: 2 additions & 19 deletions test/ko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,15 @@ import (
"errors"
"fmt"
"os"
"testing"
)

var (
// Wether missing KO_DOCKER_REPO environment variable should be fatal or not
missingKoFatal = "true"
)

func ensureDockerRepo(t *testing.T) string {
repo, err := getDockerRepo()
if err != nil {
if missingKoFatal == "false" {
t.Skip("KO_DOCKER_REPO env variable is required")
}
t.Fatal("KO_DOCKER_REPO env variable is required")
}
return repo
}

func getDockerRepo() (string, error) {
func getDockerRepo(name string) (string, error) {
// according to knative/test-infra readme (https://github.com/knative/test-infra/blob/13055d769cc5e1756e605fcb3bcc1c25376699f1/scripts/README.md)
// the KO_DOCKER_REPO will be set with according to the project where the cluster is created
// it is used here to dynamically get the docker registry to push the image to
dockerRepo := os.Getenv("KO_DOCKER_REPO")
if dockerRepo == "" {
return "", errors.New("KO_DOCKER_REPO env variable is required")
}
return fmt.Sprintf("%s/kanikotasktest", dockerRepo), nil
return fmt.Sprintf("%s/%s", dockerRepo, name), nil
}

0 comments on commit 1a64c95

Please sign in to comment.