forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_test.go
175 lines (142 loc) · 5.97 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"io"
"os"
"os/exec"
"path"
"path/filepath"
"testing"
gofrogcmd "github.com/jfrog/gofrog/io"
"github.com/jfrog/jfrog-cli-go/artifactory/commands/generic"
"github.com/jfrog/jfrog-cli-go/artifactory/spec"
"github.com/jfrog/jfrog-cli-go/inttestutils"
"github.com/jfrog/jfrog-cli-go/utils/cliutils"
"github.com/jfrog/jfrog-cli-go/utils/tests"
"github.com/jfrog/jfrog-client-go/utils/log"
)
const DockerTestImage string = "jfrog_cli_test_image"
// Image get parent image id command
type buildDockerImage struct {
dockerFilePath string
dockerTag string
}
func (image *buildDockerImage) GetCmd() *exec.Cmd {
var cmd []string
cmd = append(cmd, "docker")
cmd = append(cmd, "build")
cmd = append(cmd, image.dockerFilePath)
cmd = append(cmd, "--tag", image.dockerTag)
return exec.Command(cmd[0], cmd[1:]...)
}
func (image *buildDockerImage) GetEnv() map[string]string {
return map[string]string{}
}
func (image *buildDockerImage) GetStdWriter() io.WriteCloser {
return nil
}
func (image *buildDockerImage) GetErrWriter() io.WriteCloser {
return nil
}
func TestDockerPush(t *testing.T) {
if !*tests.TestDocker {
t.Skip("Skipping docker test. To run docker test add the '-test.docker=true' option.")
}
runDockerPushTest(DockerTestImage, DockerTestImage+":1", false, t)
}
func TestDockerPushWithModuleName(t *testing.T) {
if !*tests.TestDocker {
t.Skip("Skipping docker test. To run docker test add the '-test.docker=true' option.")
}
runDockerPushTest(DockerTestImage, ModuleNameJFrogTest, true, t)
}
func TestDockerPushWithMultipleSlash(t *testing.T) {
if !*tests.TestDocker {
t.Skip("Skipping docker test. To run docker test add the '-test.docker=true' option.")
}
runDockerPushTest(DockerTestImage+"/multiple", "multiple:1", false, t)
}
// Run docker push to Artifactory
func runDockerPushTest(imageName, module string, withModule bool, t *testing.T) {
imageTag := buildTestDockerImage(imageName)
buildName := "docker-build"
buildNumber := "1"
// Push docker image using docker client
if withModule {
artifactoryCli.Exec("docker-push", imageTag, *tests.DockerTargetRepo, "--build-name="+buildName, "--build-number="+buildNumber, "--module="+module)
} else {
artifactoryCli.Exec("docker-push", imageTag, *tests.DockerTargetRepo, "--build-name="+buildName, "--build-number="+buildNumber)
}
artifactoryCli.Exec("build-publish", buildName, buildNumber)
imagePath := path.Join(*tests.DockerTargetRepo, imageName, "1") + "/"
validateDockerBuild(buildName, buildNumber, imagePath, module, 7, 5, 7, t)
dockerTestCleanup(imageName, buildName)
}
func TestDockerPushBuildNameNumberFromEnv(t *testing.T) {
if !*tests.TestDocker {
t.Skip("Skipping docker test. To run docker test add the '-test.docker=true' option.")
}
imageName := DockerTestImage
imageTag := buildTestDockerImage(imageName)
buildName := "docker-build"
buildNumber := "1"
os.Setenv(cliutils.BuildName, buildName)
os.Setenv(cliutils.BuildNumber, buildNumber)
defer os.Unsetenv(cliutils.BuildName)
defer os.Unsetenv(cliutils.BuildNumber)
// Push docker image using docker client
artifactoryCli.Exec("docker-push", imageTag, *tests.DockerTargetRepo)
artifactoryCli.Exec("build-publish")
imagePath := path.Join(*tests.DockerTargetRepo, imageName, "1") + "/"
validateDockerBuild(buildName, buildNumber, imagePath, DockerTestImage+":1", 7, 5, 7, t)
dockerTestCleanup(imageName, buildName)
}
func TestDockerPull(t *testing.T) {
if !*tests.TestDocker {
t.Skip("Skipping docker test. To run docker test add the '-test.docker=true' option.")
}
imageName := DockerTestImage
imageTag := buildTestDockerImage(imageName)
// Push docker image using docker client
artifactoryCli.Exec("docker-push", imageTag, *tests.DockerTargetRepo)
buildName := "docker-pull"
buildNumber := "1"
// Pull docker image using docker client
artifactoryCli.Exec("docker-pull", imageTag, *tests.DockerTargetRepo, "--build-name="+buildName, "--build-number="+buildNumber)
artifactoryCli.Exec("build-publish", buildName, buildNumber)
imagePath := path.Join(*tests.DockerTargetRepo, imageName, "1") + "/"
validateDockerBuild(buildName, buildNumber, imagePath, imageName+":1", 0, 7, 7, t)
buildNumber = "2"
artifactoryCli.Exec("docker-pull", imageTag, *tests.DockerTargetRepo, "--build-name="+buildName, "--build-number="+buildNumber, "--module="+ModuleNameJFrogTest)
artifactoryCli.Exec("build-publish", buildName, buildNumber)
validateDockerBuild(buildName, buildNumber, imagePath, ModuleNameJFrogTest, 0, 7, 7, t)
dockerTestCleanup(imageName, buildName)
}
func buildTestDockerImage(imageName string) string {
imageTag := path.Join(*tests.DockerRepoDomain, imageName+":1")
dockerFilePath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "docker")
imageBuilder := &buildDockerImage{dockerTag: imageTag, dockerFilePath: dockerFilePath}
gofrogcmd.RunCmd(imageBuilder)
return imageTag
}
func validateDockerBuild(buildName, buildNumber, imagePath, module string, expectedArtifacts, expectedDependencies, expectedItemsInArtifactory int, t *testing.T) {
specFile := spec.NewBuilder().Pattern(imagePath + "*").BuildSpec()
searchCmd := generic.NewSearchCommand()
searchCmd.SetRtDetails(artifactoryDetails).SetSpec(specFile)
err := searchCmd.Search()
if err != nil {
log.Error(err)
t.Error(err)
}
if expectedItemsInArtifactory != len(searchCmd.SearchResult()) {
t.Error("Docker build info was not pushed correctly, expected:", expectedArtifacts, " Found:", len(searchCmd.SearchResult()))
}
buildInfo := inttestutils.GetBuildInfo(artifactoryDetails.Url, buildName, buildNumber, t, artHttpDetails)
validateBuildInfo(buildInfo, t, expectedDependencies, expectedArtifacts, module)
}
func dockerTestCleanup(imageName, buildName string) {
// Remove build from Artifactory
inttestutils.DeleteBuild(artifactoryDetails.Url, buildName, artHttpDetails)
// Remove image from Artifactory
deleteSpec := spec.NewBuilder().Pattern(path.Join(*tests.DockerTargetRepo, imageName)).BuildSpec()
tests.DeleteFiles(deleteSpec, artifactoryDetails)
}