forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
113 lines (98 loc) · 2.89 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package pack
import (
"bytes"
"os"
"testing"
dockerClient "github.com/docker/docker/client"
"github.com/golang/mock/gomock"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/pack/logging"
h "github.com/buildpacks/pack/testhelpers"
"github.com/buildpacks/pack/testmocks"
)
func TestClient(t *testing.T) {
spec.Run(t, "Client", testClient, spec.Report(report.Terminal{}))
}
func testClient(t *testing.T, when spec.G, it spec.S) {
when("#NewClient", func() {
it("default works", func() {
_, err := NewClient()
h.AssertNil(t, err)
})
when("docker env is messed up", func() {
var dockerHost string
var dockerHostKey = "DOCKER_HOST"
it.Before(func() {
dockerHost = os.Getenv(dockerHostKey)
h.AssertNil(t, os.Setenv(dockerHostKey, "fake-value"))
})
it.After(func() {
h.AssertNil(t, os.Setenv(dockerHostKey, dockerHost))
})
it("returns errors", func() {
_, err := NewClient()
h.AssertError(t, err, "docker client")
})
})
})
when("#WithLogger", func() {
it("uses logger provided", func() {
var w bytes.Buffer
logger := logging.New(&w)
cl, err := NewClient(WithLogger(logger))
h.AssertNil(t, err)
h.AssertSameInstance(t, cl.logger, logger)
})
})
when("#WithImageFactory", func() {
it("uses image factory provided", func() {
mockController := gomock.NewController(t)
mockImageFactory := testmocks.NewMockImageFactory(mockController)
cl, err := NewClient(WithImageFactory(mockImageFactory))
h.AssertNil(t, err)
h.AssertSameInstance(t, cl.imageFactory, mockImageFactory)
})
})
when("#WithFetcher", func() {
it("uses image factory provided", func() {
mockController := gomock.NewController(t)
mockFetcher := testmocks.NewMockImageFetcher(mockController)
cl, err := NewClient(WithFetcher(mockFetcher))
h.AssertNil(t, err)
h.AssertSameInstance(t, cl.imageFetcher, mockFetcher)
})
})
when("#WithDownloader", func() {
it("uses image factory provided", func() {
mockController := gomock.NewController(t)
mockDownloader := testmocks.NewMockDownloader(mockController)
cl, err := NewClient(WithDownloader(mockDownloader))
h.AssertNil(t, err)
h.AssertSameInstance(t, cl.downloader, mockDownloader)
})
})
when("#WithDockerClient", func() {
it("uses docker client provided", func() {
docker, err := dockerClient.NewClientWithOpts(
dockerClient.FromEnv,
)
h.AssertNil(t, err)
cl, err := NewClient(WithDockerClient(docker))
h.AssertNil(t, err)
h.AssertSameInstance(t, cl.docker, docker)
})
})
when("#WithExperimental", func() {
it("sets experimental = true", func() {
cl, err := NewClient(WithExperimental(true))
h.AssertNil(t, err)
h.AssertEq(t, cl.experimental, true)
})
it("sets experimental = false", func() {
cl, err := NewClient(WithExperimental(true))
h.AssertNil(t, err)
h.AssertEq(t, cl.experimental, true)
})
})
}