Skip to content

Commit

Permalink
Merge pull request #2117 from buildpacks/enhancement/jjbustamante/iss…
Browse files Browse the repository at this point in the history
…ue-2113

Exposing the logic for processing the Docker Context to be public
  • Loading branch information
jjbustamante authored Apr 9, 2024
2 parents 969ff6c + 70d3f4d commit 5b8da44
Show file tree
Hide file tree
Showing 18 changed files with 20 additions and 21 deletions.
4 changes: 1 addition & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/docker"

"github.com/buildpacks/pack/buildpackage"
builderwriter "github.com/buildpacks/pack/internal/builder/writer"
"github.com/buildpacks/pack/internal/commands"
Expand Down Expand Up @@ -139,7 +137,7 @@ func initConfig() (config.Config, string, error) {
}

func initClient(logger logging.Logger, cfg config.Config) (*client.Client, error) {
if err := docker.ProcessDockerContext(logger); err != nil {
if err := client.ProcessDockerContext(logger); err != nil {
return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package docker
package client

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package docker_test
package client_test

import (
"bytes"
Expand All @@ -11,7 +11,7 @@ import (
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/pack/internal/docker"
"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/logging"
h "github.com/buildpacks/pack/testhelpers"
)
Expand All @@ -23,8 +23,9 @@ func TestProcessDockerContext(t *testing.T) {
}

const (
happyCase = "happy-cases"
errorCase = "error-cases"
rootFolder = "docker-context"
happyCase = "happy-cases"
errorCase = "error-cases"
)

func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
Expand All @@ -43,7 +44,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("docker context process is skipped", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, strings.TrimSpace(outBuf.String()), "'DOCKER_HOST=some-value' environment variable is being used")
})
Expand All @@ -61,7 +62,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("docker context process is skip", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, strings.TrimSpace(outBuf.String()), "docker context is default or empty, skipping it")
})
Expand All @@ -73,7 +74,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("throw an error", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNotNil(t, err)
h.AssertError(t, err, "docker context 'some-bad-context' not found")
})
Expand All @@ -86,7 +87,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("docker endpoint host is being used", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, outBuf.String(), "using docker context 'desktop-linux' with endpoint = 'unix:///Users/user/.docker/run/docker.sock'")
})
Expand All @@ -98,7 +99,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("docker endpoint host is being used", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, outBuf.String(), "using docker context 'desktop-linux' with endpoint = 'unix:///Users/user/.docker/run/docker.sock'")
})
Expand All @@ -110,7 +111,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("throw an error", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNotNil(t, err)
h.AssertError(t, err, "context 'desktop-linux' doesn't match metadata name 'bad-name'")
})
Expand All @@ -122,7 +123,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("writes a warn message into the log", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, outBuf.String(), "docker endpoint doesn't exist for context 'desktop-linux'")
})
Expand All @@ -134,7 +135,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("throw an error", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNotNil(t, err)
h.AssertError(t, err, "reading metadata for current context 'desktop-linux'")
})
Expand All @@ -148,7 +149,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("throw an error", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNotNil(t, err)
h.AssertError(t, err, "reading configuration file")
})
Expand All @@ -160,7 +161,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("docker context process is skip", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, strings.TrimSpace(outBuf.String()), "docker context is default or empty, skipping it")
})
Expand All @@ -172,7 +173,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("docker context process is skip", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, strings.TrimSpace(outBuf.String()), "docker context is default or empty, skipping it")
})
Expand All @@ -184,7 +185,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {
})

it("docker context process is skip", func() {
err := docker.ProcessDockerContext(logger)
err := client.ProcessDockerContext(logger)
h.AssertNil(t, err)
h.AssertContains(t, strings.TrimSpace(outBuf.String()), "docker context is default or empty, skipping it")
})
Expand All @@ -194,7 +195,7 @@ func testProcessDockerContext(t *testing.T, when spec.G, it spec.S) {

func setDockerConfig(t *testing.T, test, context string) {
t.Helper()
contextDir, err := filepath.Abs(filepath.Join("testdata", test, context))
contextDir, err := filepath.Abs(filepath.Join("testdata", rootFolder, test, context))
h.AssertNil(t, err)
err = os.Setenv("DOCKER_CONFIG", contextDir)
h.AssertNil(t, err)
Expand Down

0 comments on commit 5b8da44

Please sign in to comment.