This repository has been archived by the owner on May 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.bats
185 lines (144 loc) · 5.91 KB
/
test.bats
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
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env bats
setup(){
cat /dev/null >| mockCalledWith
declare -A -p MOCK_RETURNS=(
['/usr/local/bin/docker']=""
) > mockReturns
export INPUT_USERNAME='USERNAME'
export INPUT_PASSWORD='PASSWORD'
export INPUT_NAME='my/repository'
}
teardown() {
unset INPUT_SEMVER
unset INPUT_DOCKERFILE
unset INPUT_REGISTRY
unset MOCK_ERROR_CONDITION
}
@test "it errors when with.name was not set" {
unset INPUT_NAME
run /entrypoint.sh
local expected="Unable to find the name. Did you set with.name?"
echo $output
[ "$status" -eq 1 ]
echo "$output" | grep "$expected"
}
@test "it errors when with.username was not set" {
unset INPUT_USERNAME
run /entrypoint.sh
local expected="Unable to find the username. Did you set with.username?"
echo $output
[ "$status" -eq 1 ]
echo "$output" | grep "$expected"
}
@test "it errors when with.password was not set" {
unset INPUT_PASSWORD
run /entrypoint.sh
local expected="Unable to find the password. Did you set with.password?"
echo $output
[ "$status" -eq 1 ]
echo "$output" | grep "$expected"
}
@test "it errors when the working directory is configured but not present" {
export INPUT_WORKDIR='mySubDir'
run /entrypoint.sh
[ "$status" -eq 2 ]
}
@test "with semver it pushes tags using the semver version" {
export INPUT_SEMVER="1.2.5"
run /entrypoint.sh
expectStdOut "
::debug file=entrypoint.sh::Starting docker build -t my/repository:latest .
::debug file=entrypoint.sh::Finished building my/repository:latest
::debug file=entrypoint.sh::Starting docker push my/repository:latest
::debug file=entrypoint.sh::Finished pushing my/repository:latest
::debug file=entrypoint.sh::Starting docker tag my/repository:latest my/repository:1.2.5
::debug file=entrypoint.sh::Finished tagging my/repository:latest my/repository:1.2.5
::debug file=entrypoint.sh::Starting docker push my/repository:1.2.5
::debug file=entrypoint.sh::Finished pushing my/repository:1.2.5
::debug file=entrypoint.sh::Starting docker tag my/repository:latest my/repository:1
::debug file=entrypoint.sh::Finished tagging my/repository:latest my/repository:1
::debug file=entrypoint.sh::Starting docker push my/repository:1
::debug file=entrypoint.sh::Finished pushing my/repository:1
::debug file=entrypoint.sh::Starting docker tag my/repository:latest my/repository:1.2
::debug file=entrypoint.sh::Finished tagging my/repository:latest my/repository:1.2
::debug file=entrypoint.sh::Starting docker push my/repository:1.2
::debug file=entrypoint.sh::Finished pushing my/repository:1.2
::set-output name=tag::1.2.5"
expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin
/usr/local/bin/docker build -t my/repository:latest .
/usr/local/bin/docker push my/repository:latest
/usr/local/bin/docker tag my/repository:latest my/repository:1.2.5
/usr/local/bin/docker push my/repository:1.2.5
/usr/local/bin/docker tag my/repository:latest my/repository:1
/usr/local/bin/docker push my/repository:1
/usr/local/bin/docker tag my/repository:latest my/repository:1.2
/usr/local/bin/docker push my/repository:1.2
/usr/local/bin/docker logout"
}
@test "without semver it pushes tags using the latest" {
export INPUT_SEMVER=""
run /entrypoint.sh
expectStdOut "
::debug file=entrypoint.sh::Starting docker build -t my/repository:latest .
::debug file=entrypoint.sh::Finished building my/repository:latest
::debug file=entrypoint.sh::Starting docker push my/repository:latest
::debug file=entrypoint.sh::Finished pushing my/repository:latest
::set-output name=tag::latest"
expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin
/usr/local/bin/docker build -t my/repository:latest .
/usr/local/bin/docker push my/repository:latest
/usr/local/bin/docker logout"
}
@test "it pushes to another registry and adds the hostname" {
export INPUT_REGISTRY='my.Registry.io'
run /entrypoint.sh
expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin my.Registry.io
/usr/local/bin/docker build -t my.Registry.io/my/repository:latest .
/usr/local/bin/docker push my.Registry.io/my/repository:latest
/usr/local/bin/docker logout"
}
@test "it pushes to another registry and is ok when the hostname is already present" {
export INPUT_REGISTRY='my.Registry.io'
export INPUT_NAME='my.Registry.io/my/repository'
run /entrypoint.sh
expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin my.Registry.io
/usr/local/bin/docker build -t my.Registry.io/my/repository:latest .
/usr/local/bin/docker push my.Registry.io/my/repository:latest
/usr/local/bin/docker logout"
}
@test "it pushes to another registry and removes the protocol from the hostname" {
export INPUT_REGISTRY='https://my.Registry.io'
export INPUT_NAME='my/repository'
run /entrypoint.sh
expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin https://my.Registry.io
/usr/local/bin/docker build -t my.Registry.io/my/repository:latest .
/usr/local/bin/docker push my.Registry.io/my/repository:latest
/usr/local/bin/docker logout"
}
@test "it uses buildargs for building, if configured" {
export INPUT_BUILDARGS='MY_FIRST,MY_SECOND'
run /entrypoint.sh
expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin
/usr/local/bin/docker build --build-arg MY_FIRST --build-arg MY_SECOND -t my/repository:latest .
/usr/local/bin/docker push my/repository:latest
/usr/local/bin/docker logout"
}
@test "it can set a custom context" {
export INPUT_CONTEXT='/myContextFolder'
run /entrypoint.sh
expectMockCalled "/usr/local/bin/docker login -u USERNAME --password-stdin
/usr/local/bin/docker build -t my/repository:latest /myContextFolder
/usr/local/bin/docker push my/repository:latest
/usr/local/bin/docker logout"
}
function expectStdOut() {
echo "Expected: |$1|
Got: |$output|"
[ "$output" = "$1" ]
}
function expectMockCalled() {
local mockCalledWith=$(cat mockCalledWith)
echo "Expected: |$1|
Got: |$mockCalledWith|"
[ "$mockCalledWith" = "$1" ]
}