-
Notifications
You must be signed in to change notification settings - Fork 18
/
docker_test.go
105 lines (97 loc) · 2.71 KB
/
docker_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
//go:build !windows
// +build !windows
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGetImage(t *testing.T) {
os.Setenv("DOCKER_BUILDKIT", "0") // For docker tests to pass on Mac, docker buildkit must be disabled
testImageName := "test-image" + strconv.Itoa(randInt())
testTag := "test" + strconv.Itoa(randInt())
testImageNameTagged := testImageName + ":" + testTag
// build test image
defer func() { assert.NoError(t, exec.Command("docker", "rmi", testImageNameTagged).Run()) }()
fmt.Println("Building and tagging", testImageNameTagged)
c2 := exec.Command("docker", "build", "-", "-t", testImageNameTagged)
c2.Stdin, c2.Stdout, c2.Stderr = bytes.NewBufferString("FROM scratch\nLABEL name="+testTag), os.Stdout, os.Stderr
assert.NoError(t, c2.Run())
tests := []struct {
name string
config *TGFConfig
result string
dockerBuild bool
refresh bool
useLocalImage bool
}{
{
name: "Without build configs and tag",
config: &TGFConfig{Image: testImageName},
result: testImageName + ":latest",
dockerBuild: true,
},
{
name: "Without build configs but with a tag",
config: &TGFConfig{
Image: testImageName,
ImageTag: &testTag,
},
result: testImageNameTagged,
dockerBuild: true,
},
{
name: "With build config",
config: &TGFConfig{
ImageTag: &testTag,
Image: testImageName,
imageBuildConfigs: []TGFConfigBuild{{
Instructions: "LABEL another=test",
Tag: "buildtag",
}},
},
useLocalImage: true,
dockerBuild: true,
result: testImageNameTagged + "-" + "buildtag",
},
{
name: "With build config and no build flag",
config: &TGFConfig{
ImageTag: &testTag,
Image: testImageName,
imageBuildConfigs: []TGFConfigBuild{{
Instructions: "LABEL another=test",
Tag: "buildtag",
}},
},
useLocalImage: true,
result: testImageNameTagged,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert.NotPanics(t, func() {
app := NewTestApplication(nil, true)
tt.config.tgf = app
app.DockerBuild = tt.dockerBuild
app.Refresh = tt.refresh
app.UseLocalImage = tt.useLocalImage
docker := dockerConfig{tt.config}
assert.Equal(t, tt.result, docker.getImage(), "The result image tag is not correct")
if tt.result != testImageName+":latest" && tt.result != testImageNameTagged {
time.Sleep(1 * time.Second)
command := exec.Command("docker", "rmi", tt.result)
t.Log("Running:", strings.Join(command.Args, " "))
assert.NoError(t, command.Run())
}
})
})
}
}