forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_fetcher_test.go
104 lines (89 loc) · 2.95 KB
/
image_fetcher_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
package pack_test
import (
"context"
"io/ioutil"
"testing"
"github.com/fatih/color"
"github.com/golang/mock/gomock"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpack/pack"
"github.com/buildpack/pack/mocks"
h "github.com/buildpack/pack/testhelpers"
)
func TestImageFetcher(t *testing.T) {
color.NoColor = true
spec.Run(t, "ImageFetcher", testImageFetcher, spec.Report(report.Terminal{}))
}
func testImageFetcher(t *testing.T, when spec.G, it spec.S) {
var (
fetcher pack.ImageFetcher
mockController *gomock.Controller
mockImageFactory *mocks.MockImageFactory
mockRemoteImage *mocks.MockImage
mockLocalImage *mocks.MockImage
mockDocker *mocks.MockDocker
)
it.Before(func() {
mockController = gomock.NewController(t)
mockImageFactory = mocks.NewMockImageFactory(mockController)
mockRemoteImage = mocks.NewMockImage(mockController)
mockLocalImage = mocks.NewMockImage(mockController)
mockDocker = mocks.NewMockDocker(mockController)
fetcher = pack.ImageFetcher{
Docker: mockDocker,
Factory: mockImageFactory,
}
})
it.After(func() {
mockController.Finish()
})
when("#FetchUpdatedLocalImage", func() {
when("remote image exists", func() {
it.Before(func() {
mockRemoteImage.EXPECT().Found().Return(true, nil)
mockImageFactory.EXPECT().NewRemote("some/image").Return(mockRemoteImage, nil)
mockImageFactory.EXPECT().NewLocal("some/image").Return(mockLocalImage, nil)
})
it("pulls remote image", func() {
mockDocker.EXPECT().PullImage(gomock.Any(), "some/image", gomock.Any())
img, err := fetcher.FetchUpdatedLocalImage(context.TODO(), "some/image", ioutil.Discard)
h.AssertNil(t, err)
h.AssertSameInstance(t, img, mockLocalImage)
})
})
when("remote image does not exist", func() {
it.Before(func() {
mockRemoteImage.EXPECT().Found().Return(false, nil)
mockImageFactory.EXPECT().NewRemote("some/image").Return(mockRemoteImage, nil)
mockImageFactory.EXPECT().NewLocal("some/image").Return(mockLocalImage, nil)
})
it("skips pulling image", func() {
mockDocker.EXPECT().PullImage(gomock.Any(), gomock.Any(), gomock.Any()).Times(0)
img, err := fetcher.FetchUpdatedLocalImage(context.TODO(), "some/image", ioutil.Discard)
h.AssertNil(t, err)
h.AssertSameInstance(t, img, mockLocalImage)
})
})
})
when("#FetchLocalImage", func() {
it.Before(func() {
mockImageFactory.EXPECT().NewLocal("some/image").Return(mockLocalImage, nil)
})
it("returns local image", func() {
img, err := fetcher.FetchLocalImage("some/image")
h.AssertNil(t, err)
h.AssertSameInstance(t, img, mockLocalImage)
})
})
when("#FetchRemoteImage", func() {
it.Before(func() {
mockImageFactory.EXPECT().NewRemote("some/image").Return(mockRemoteImage, nil)
})
it("returns remote image", func() {
img, err := fetcher.FetchRemoteImage("some/image")
h.AssertNil(t, err)
h.AssertSameInstance(t, img, mockRemoteImage)
})
})
}