diff --git a/e2e/build_push_test.go b/e2e/build_push_test.go index 4ffd28e..41ab45b 100644 --- a/e2e/build_push_test.go +++ b/e2e/build_push_test.go @@ -8,32 +8,13 @@ import ( "gotest.tools/v3/assert" ) -func TestBuildPush(t *testing.T) { - tags := []string{ - "localhost:5000/my-repository:build-push-tag1", - "localhost:5000/my-repository:build-push-test", - } - labels := map[string]string{ - "a": "a1", - } +func testBuildPush(t *testing.T, envFile string, tags []string, labels map[string]string) { err := removeImages(tags) assert.NilError(t, err) - defer removeImages(tags) - - err = setupLocalRegistry() - assert.NilError(t, err) - defer removeLocalRegistry() - - err = loginLocalRegistry() - assert.NilError(t, err) - err = runActionsCommand("build-push", "testdata/build_push_tests/build_push.env") + err = runActionsCommand("build-push", envFile) assert.NilError(t, err) - for _, tag := range tags { - assertBuildPushImages(t, tag, tags, labels) - } - err = removeImages(tags) assert.NilError(t, err) @@ -47,6 +28,47 @@ func TestBuildPush(t *testing.T) { } } +func TestBuildPush(t *testing.T) { + err := setupLocalRegistry() + assert.NilError(t, err) + defer removeLocalRegistry() + + err = ensureLocalRegistryAlive() + assert.NilError(t, err) + + // Build and push base image + baseTags := []string{ + "localhost:5000/org/base:build-push-tag1", + "localhost:5000/org/base:build-push-test", + } + defer removeImages(baseTags) + testBuildPush( + t, + "testdata/build_push_tests/build_push.env", + baseTags, + map[string]string{ + "a": "a1", + }, + ) + + err = logoutLocalRegistry() + assert.NilError(t, err) + + // Build and push image using base image from local registry + testBuildPush( + t, + "testdata/build_push_tests/build_push_from_registry.env", + []string{ + "localhost:5000/org/repo:build-push-reg-tag1", + "localhost:5000/org/repo:build-push-reg-test", + }, + map[string]string{ + "a": "a1", + "b": "b1", + }, + ) +} + func assertBuildPushImages(t *testing.T, image string, expectedTags []string, expectedLabels map[string]string) { inspect, err := inspectImage(image) assert.NilError(t, err) @@ -57,3 +79,15 @@ func assertBuildPushImages(t *testing.T, image string, expectedTags []string, ex assert.DeepEqual(t, expectedTags, repoTags) assert.DeepEqual(t, expectedLabels, inspect.Config.Labels) } + +func ensureLocalRegistryAlive() error { + if err := loginLocalRegistry(); err != nil { + return err + } + + return logoutLocalRegistry() +} + +func logoutLocalRegistry() error { + return exec.Command("docker", "logout", "localhost:5000").Run() +} diff --git a/e2e/login_test.go b/e2e/login_test.go index 8cc7bf5..05d5d43 100644 --- a/e2e/login_test.go +++ b/e2e/login_test.go @@ -18,6 +18,7 @@ func TestLogin(t *testing.T) { } func loginLocalRegistry() error { + // Polls as registry takes a moment to start up return wait.Poll(2*time.Second, 30*time.Second, func() (bool, error) { err := runActionsCommand("login", "testdata/login_test.env") return err == nil, err diff --git a/e2e/testdata/build_push_tests/build_push.env b/e2e/testdata/build_push_tests/build_push.env index e504cc7..3aa5790 100644 --- a/e2e/testdata/build_push_tests/build_push.env +++ b/e2e/testdata/build_push_tests/build_push.env @@ -6,6 +6,6 @@ INPUT_LABELS=a=a1 INPUT_REGISTRY=localhost:5000 INPUT_USERNAME=my_user INPUT_PASSWORD=my_password -INPUT_REPOSITORY=my-repository +INPUT_REPOSITORY=org/base INPUT_PUSH=true GITHUB_REF=refs/tags/build-push-tag1 diff --git a/e2e/testdata/build_push_tests/build_push_from_registry.env b/e2e/testdata/build_push_tests/build_push_from_registry.env new file mode 100644 index 0000000..1bf6cdc --- /dev/null +++ b/e2e/testdata/build_push_tests/build_push_from_registry.env @@ -0,0 +1,11 @@ +INPUT_PATH=./testdata/build_push_tests +INPUT_DOCKERFILE=./testdata/build_push_tests/fromreg.Dockerfile +INPUT_TAG_WITH_REF=true +INPUT_TAGS=build-push-reg-test +INPUT_LABELS=b=b1 +INPUT_REGISTRY=localhost:5000 +INPUT_USERNAME=my_user +INPUT_PASSWORD=my_password +INPUT_REPOSITORY=org/repo +INPUT_PUSH=true +GITHUB_REF=refs/tags/build-push-reg-tag1 diff --git a/e2e/testdata/build_push_tests/fromreg.Dockerfile b/e2e/testdata/build_push_tests/fromreg.Dockerfile new file mode 100644 index 0000000..fceb888 --- /dev/null +++ b/e2e/testdata/build_push_tests/fromreg.Dockerfile @@ -0,0 +1,3 @@ +FROM localhost:5000/org/base:build-push-test + +ENTRYPOINT ["echo", "hello-world build-push-from-registry"] \ No newline at end of file