-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
executable file
·385 lines (312 loc) · 9.38 KB
/
run.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env bash
# Copyright 2023 Nokia
# Licensed under the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
set -o errexit
set -o pipefail
# lab node name
NODE_NAME="clab-ansible-srl"
# Directory where the scripts are located.
SCRIPTS_DIR="scripts"
# Directory where the tests are located.
TESTS_DIR="$(pwd)/tests"
# Containerlab version to use in CI tests
CLAB_VERSION="0.57.5"
CHECKPOINT_NAME="clab-initial"
# -----------------------------------------------------------------------------
# Helper functions start with _ and aren't listed in this script's help menu.
# -----------------------------------------------------------------------------
# Change to the tests directory only if we're not already there.
function _cdTests() {
if [ "$(pwd)" != "${TESTS_DIR}" ]; then
cd "${TESTS_DIR}"
fi
}
# transoforms ansible core version by swapping : with /
function _transformAnsibleCoreVersion() {
echo "${1}" | sed 's/:/\//g'
}
# -----------------------------------------------------------------------------
# Install functions.
# -----------------------------------------------------------------------------
# Install containerlab.
# To install a specific version, pass a version (without v prefix).
function install-containerlab {
if [ -z "$1" ]; then
echo "Installing latest containerlab version"
bash -c "$(curl -sL https://get.containerlab.dev)"
else
echo "Installing containerlab version $1"
bash -c "$(curl -sL https://get.containerlab.dev)" -- -v "$1"
fi
}
# Install a local collection.
function install-local-collection {
ansible-galaxy collection install --force ..
}
function remove-local-collection {
rm -rf ~/.ansible/collections/ansible_collections/nokia
}
# Deploy test lab.
function deploy-lab {
cd ${SCRIPTS_DIR}
sudo -E containerlab deploy -c
}
# Prepare local dev environment by setting the symlink to the collection.
function prepare-dev-env {
# setup the symlink for ansible to resolve collection paths
rm -f /tmp/srl_ansible_dev/ansible_collections/nokia/srlinux
mkdir -p /tmp/srl_ansible_dev/ansible_collections/nokia
ln -s "$(pwd)" /tmp/srl_ansible_dev/ansible_collections/nokia/srlinux
# setup .env file for python to resolve imports
echo "PYTHONPATH=$(realpath ~)/.ansible/collections:/tmp/srl_ansible_dev" > .env
}
# revert to initial checkpoint to guarantee the node initial state
function revert-to-checkpoint {
# revert to the checkpoint named "initial" to guarantee a clean state
docker exec ${NODE_NAME} sr_cli /tools system configuration checkpoint ${CHECKPOINT_NAME} revert
}
# -----------------------------------------------------------------------------
# Test functions.
# -----------------------------------------------------------------------------
function test-auth-fail {
_cdTests
ansible-playbook playbooks/auth-fail.yml "$@"
}
function test-config-backup {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/backup-cfg.yml "$@"
}
function test-tls-fail {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/tls-missed-check-fail.yml "$@"
}
function test-tls-skip {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/tls-skipped-check.yml "$@"
}
function test-tls-custom-ca {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/tls-with-custom-ca.yml "$@"
}
function test-get-container {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/get-container.yml "$@"
}
function test-get-multiple-paths {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/get-multiple-paths.yml "$@"
}
function test-get-oc-container {
_cdTests
ansible-playbook playbooks/get-oc-container.yml "$@"
}
function test-get-wrong-path {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/get-wrong-path.yml "$@"
}
function test-backup-cfg {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/backup-cfg.yml "$@"
}
function test-cli-show-version {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/cli-show-version.yml "$@"
}
function test-cli-wrong-cmd {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/cli-wrong-cmd.yml "$@"
}
function test-cli-put-file {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/cli-put-file.yml "$@"
}
function test-set-check-mode {
_cdTests
revert-to-checkpoint
# file to store the output of the playbook so we can grep it and test diff mode
OUT_LOG=/tmp/srl-ansible-set-check-mode.log
rm -f ${OUT_LOG}
ansible-playbook playbooks/set-check.yml "$@" | tee ${OUT_LOG}
# ensure that diff was printed
grep -q "+ contact \"Some contact\"" ${OUT_LOG}
}
function test-set-leaves {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-leaves.yml "$@"
}
# test that subsequent config changes are idempotent
# and does not lead to commit being recorded since
# the diff is empty
function test-set-leaves-twice {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-leaves-twice.yml "$@"
}
function test-set-interface {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-interface.yml "$@"
}
function test-set-wrong-value {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-wrong-value.yml "$@"
}
function test-set-multiple-paths {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-multiple-paths.yml "$@"
}
function test-set-oc-leaf {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-oc-leaf.yml "$@"
}
function test-set-tools {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-tools.yml "$@"
}
function test-delete-leaves {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/delete-leaves.yml "$@"
}
function test-set-idempotent {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-idempotent.yml "$@"
}
function test-replace-full-congig {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/replace-full-cfg.yml "$@"
}
function test-validate {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/validate.yml "$@"
}
function test-oc-validate {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/oc-validate.yml "$@"
}
function test-commit-confirm {
_cdTests
revert-to-checkpoint
ansible-playbook playbooks/set-confirm-timeout.yml "$@"
}
# Shouldn't be called directly, use test or ci-test instead.
# Meant to define the collection of tests to run.
function _run-tests {
test-auth-fail "$@"
test-get-container "$@"
test-config-backup "$@"
test-get-wrong-path "$@"
test-cli-show-version "$@"
test-cli-wrong-cmd "$@"
test-tls-fail "$@"
test-tls-skip "$@"
test-tls-custom-ca "$@"
test-set-check-mode "$@"
test-set-leaves "$@"
test-set-leaves-twice "$@"
test-set-wrong-value "$@"
test-set-multiple-paths "$@"
test-set-interface "$@"
test-set-tools "$@"
test-delete-leaves "$@"
test-set-idempotent "$@"
test-replace-full-congig "$@"
test-get-multiple-paths "$@"
test-commit-confirm "$@"
# OC-related tests
if [[ " $* " == *" oc-tests "* ]]; then
# OC-related tests
test-get-oc-container "$@"
test-set-oc-leaf "$@"
test-oc-validate "$@"
fi
}
# prepare local dev environment and run tests
# to enable debug, pass -vvvv as an argument
function test {
prepare-dev-env
revert-to-checkpoint
_run-tests "$@"
}
function dump-logs {
ansible-galaxy collection list
echo
pip list
python --version
}
# ci-test is a wrapper for testing in CI which first setups the environment.
function ci-test {
install-containerlab ${CLAB_VERSION}
install-local-collection
deploy-lab
dump-logs
# at this point we are already in ./tests dir
# since we changed into it in deploy-lab
# we use ci-ansible.cfg to make sure default collections paths is used
ANSIBLE_CONFIG=ci-ansible.cfg _run-tests "$@"
}
# copy sanity ignore files from ignore-2.10.txt to all other supported ansible versions
function copy-sanity-ignore {
_cdTests
cd sanity
for version in 2.14 2.15 2.16 2.17; do
cp ignore-2.10.txt ignore-${version}.txt
done
}
# sanity-test runs ansible-test tool with sanity checks.
function sanity-test {
install-local-collection
cd ~/.ansible/collections/ansible_collections/nokia/srlinux
ansible-test sanity --docker default -v "$@"
remove-local-collection
}
# testing gh workflow
function test-act-release {
gh act release -W '.github/workflows/container-build.yml' -e .github/workflows/release-event.json -s GITHUB_TOKEN="$(gh auth token)" --matrix ansible-core-image:2.14.10:py3.11
}
# -----------------------------------------------------------------------------
# Publish functions.
# -----------------------------------------------------------------------------
function build-collection {
# cleanup
rm -f nokia-srlinux-*.tar.gz
# build the collection
ansible-galaxy collection build --force
}
function publish-collection {
# build the collection
build-collection
ansible-galaxy collection publish -v --token $(cat apikey) $(ls -1 nokia-srlinux-*.tar.gz)
}
# -----------------------------------------------------------------------------
# Bash runner functions.
# -----------------------------------------------------------------------------
function help {
printf "%s <task> [args]\n\nTasks:\n" "${0}"
compgen -A function | grep -v "^_" | cat -n
printf "\nExtended help:\n Each task has comments for general usage\n"
}
# This idea is heavily inspired by: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"