-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake-test-resources.sh
executable file
·229 lines (192 loc) · 9.76 KB
/
make-test-resources.sh
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env bash
FAKE_ID_LONG=c0ffeec0ffeec0ffeec0ffeec0ffeec0ffeec0ffeec0ffeec0ffeec0ffeec0ff
FAKE_ID_SHORT=c0ffeec0ffee
FAKE_DT="2024-01-01T00:00:00.000000000Z"
FAKE_MAC="x6:de:ad:c0:ff:ee"
# shellcheck disable=SC2016
JQ_FILTERS=(
'.Id = $fakeidlong'
'.Config.Hostname = $fakeidshort'
'.State.Pid = 1337'
'(.Created, .State.StartedAt) = $fakedt'
'(.ResolvConfPath, .HostnamePath, .HostsPath, .LogPath) |= (sub("[0-9a-f]{64}";$fakeidlong;"g"))'
'(.GraphDriver.Data.LowerDir, .GraphDriver.Data.MergedDir, .GraphDriver.Data.UpperDir, .GraphDriver.Data.WorkDir) |= (sub("\/[0-9a-f]{64}(?<init>-init)?\/";"/X\(if .init then .init else "" end)/";"g"))'
'(.NetworkSettings.SandboxID, .NetworkSettings.EndpointID, (.NetworkSettings.Networks[]|.NetworkID), (.NetworkSettings.Networks[]|.EndpointID)) |= (sub("[0-9a-f]{64}";"X"))'
'.NetworkSettings.SandboxKey |= (sub("\/[0-9a-f]{12}";"/X"))'
'(.NetworkSettings.Networks[]|.MacAddress) = $fakemac'
'(.NetworkSettings.MacAddress|select(. == null or . == "")) = $fakemac'
'.HostConfig.NetworkMode |= (sub(":[0-9a-f]{64}";":" + $fakeidlong))'
)
stop_and_rm_container () {
local CONTAINER_ID="$1"
docker stop -t 0 "${CONTAINER_ID}" > /dev/null 2>&1
docker rm "${CONTAINER_ID}" > /dev/null 2>&1
}
join_by_char() {
local IFS="$1"
shift
echo "$*"
}
JQ_FILTER=$(join_by_char "|" "${JQ_FILTERS[@]}")
#
# Get version info
#
VERSION_JSON=$(curl -s --unix-socket /var/run/docker.sock --proto '=http' './version')
VERSION_API_VERSION=$(echo "$VERSION_JSON" | jq -r .ApiVersion)
mkdir -p "tests/resources/v${VERSION_API_VERSION}/"
echo "${VERSION_JSON}" | jq -r > "tests/resources/v${VERSION_API_VERSION}/version.body"
#
# Get sunny-default-bridge
#
stop_and_rm_container "sunny-default-bridge"
CONTAINER_ID=$(docker run --name sunny-default-bridge -d hashicorp/http-echo:1.0.0 -listen=:80 -text="✅ it works!")
INSPECT_JSON=$(curl -s --unix-socket /var/run/docker.sock --proto '=http' "./v${VERSION_API_VERSION}/containers/sunny-default-bridge/json")
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge"
echo "${INSPECT_JSON}" | \
jq \
--arg fakeidlong "$FAKE_ID_LONG" \
--arg fakeidshort "$FAKE_ID_SHORT" \
--arg fakedt "$FAKE_DT" \
--arg fakemac "$FAKE_MAC" \
"${JQ_FILTER}" \
> "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body"
stop_and_rm_container "sunny-default-bridge"
#
# Get sunny-default-bridge-container-subdomains-allowed
#
stop_and_rm_container "sunny-default-bridge-container-subdomains-allowed"
CONTAINER_ID=$(docker run --name sunny-default-bridge-container-subdomains-allowed -d --label=".com.github.petski.nss-docker-ng.container-subdomains-allowed=true" hashicorp/http-echo:1.0.0 -listen=:80 -text="✅ it works!")
INSPECT_JSON=$(curl -s --unix-socket /var/run/docker.sock --proto '=http' "./v${VERSION_API_VERSION}/containers/sunny-default-bridge-container-subdomains-allowed/json")
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge-container-subdomains-allowed"
echo "${INSPECT_JSON}" | \
jq \
--arg fakeidlong "$FAKE_ID_LONG" \
--arg fakeidshort "$FAKE_ID_SHORT" \
--arg fakedt "$FAKE_DT" \
--arg fakemac "$FAKE_MAC" \
"${JQ_FILTER}" \
> "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge-container-subdomains-allowed/json.body"
stop_and_rm_container "sunny-default-bridge-container-subdomains-allowed"
#
# Get rainy-404
#
stop_and_rm_container "rainy-404"
INSPECT_JSON=$(curl -s --unix-socket /var/run/docker.sock --proto '=http' "./v${VERSION_API_VERSION}/containers/rainy-404/json")
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-404"
echo "${INSPECT_JSON}" | \
jq \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-404/json.body"
#
# Get rainy-no-name
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-name"
jq 'del(.Name)' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-name/json.body"
#
# Get rainy-no-network-mode
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-network-mode"
jq 'del(.HostConfig.NetworkMode)' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-network-mode/json.body"
#
# Get rainy-network-mode-none
#
stop_and_rm_container "rainy-network-mode-none"
CONTAINER_ID=$(docker run --name rainy-network-mode-none --network none -d hashicorp/http-echo:1.0.0 -listen=:80 -text="✅ it works!")
INSPECT_JSON=$(curl -s --unix-socket /var/run/docker.sock --proto '=http' "./v${VERSION_API_VERSION}/containers/rainy-network-mode-none/json")
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-mode-none"
echo "${INSPECT_JSON}" | \
jq \
--arg fakeidlong "$FAKE_ID_LONG" \
--arg fakeidshort "$FAKE_ID_SHORT" \
--arg fakedt "$FAKE_DT" \
--arg fakemac "$FAKE_MAC" \
"${JQ_FILTER}" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-mode-none/json.body"
stop_and_rm_container "rainy-network-mode-none"
#
# Get rainy-network-mode-host
#
stop_and_rm_container "rainy-network-mode-host"
CONTAINER_ID=$(docker run --name rainy-network-mode-host --network host -d hashicorp/http-echo:1.0.0 -text="✅ it works!")
INSPECT_JSON=$(curl -s --unix-socket /var/run/docker.sock --proto '=http' "./v${VERSION_API_VERSION}/containers/rainy-network-mode-host/json")
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-mode-host"
echo "${INSPECT_JSON}" | \
jq \
--arg fakeidlong "$FAKE_ID_LONG" \
--arg fakeidshort "$FAKE_ID_SHORT" \
--arg fakedt "$FAKE_DT" \
--arg fakemac "$FAKE_MAC" \
"${JQ_FILTER}" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-mode-host/json.body"
stop_and_rm_container "rainy-network-mode-host"
#
# Get rainy-network-mode-container
#
stop_and_rm_container "rainy-network-mode-container-helper"
stop_and_rm_container "rainy-network-mode-container"
CONTAINER_ID=$(docker run --name rainy-network-mode-container-helper -d hashicorp/http-echo:1.0.0 -listen=:81 -text="✅ it works!")
CONTAINER_ID=$(docker run --name rainy-network-mode-container --network container:rainy-network-mode-container-helper -d hashicorp/http-echo:1.0.0 -listen=:80 -text="✅ it works!")
INSPECT_JSON=$(curl -s --unix-socket /var/run/docker.sock --proto '=http' "./v${VERSION_API_VERSION}/containers/rainy-network-mode-container/json")
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-mode-container"
echo "${INSPECT_JSON}" | \
jq \
--arg fakeidlong "$FAKE_ID_LONG" \
--arg fakeidshort "$FAKE_ID_SHORT" \
--arg fakedt "$FAKE_DT" \
--arg fakemac "$FAKE_MAC" \
"${JQ_FILTER}" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-mode-container/json.body"
stop_and_rm_container "rainy-network-mode-container-helper"
stop_and_rm_container "rainy-network-mode-container"
#
# Get rainy-zero-networks
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-zero-networks"
jq '.NetworkSettings.Networks = {}' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-zero-networks/json.body"
#
# Get rainy-no-networks
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-networks"
jq 'del(.NetworkSettings.Networks)' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-networks/json.body"
#
# Get rainy-network-not-exists
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-not-exists"
jq '.NetworkSettings.Networks.foo = .NetworkSettings.Networks.bridge | del(.NetworkSettings.Networks.bridge) | .HostConfig.NetworkMode = "bar"' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-network-not-exists/json.body"
#
# Get rainy-ip-address-empty
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-ip-address-empty"
jq '.NetworkSettings.Networks.bridge.IPAddress = ""' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-ip-address-empty/json.body"
#
# Get rainy-no-ip-address
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-ip-address"
jq 'del(.NetworkSettings.Networks.bridge.IPAddress)' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-ip-address/json.body"
#
# Get rainy-unparseable-ip-address
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-unparseable-ip-address"
jq '.NetworkSettings.Networks.bridge.IPAddress = "x.x.x.x"' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-unparseable-ip-address/json.body"
#
# Get rainy-no-id
#
mkdir -p "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-id"
jq 'del(.Id)' \
< "tests/resources/v${VERSION_API_VERSION}/containers/sunny-default-bridge/json.body" \
> "tests/resources/v${VERSION_API_VERSION}/containers/rainy-no-id/json.body"