This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testme.sh
executable file
·1850 lines (1471 loc) · 51 KB
/
testme.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# @file testme
# @brief Setup requirements - run tests, from root folder run: ./testme.sh.
# Path to the project used as source, defaults to current path.
PROJECT_PATH=$(pwd)
# Python executable to use: python or python3. Empty by default.
PYTHON_EXEC=''
# Install requirements or not.
INSTALL_REQUIREMENT=false
# Run inside a Docker container.
DOCKER=false
# Docker image to use.
DOCKER_IMAGE='debian:stable-slim'
# String indicating to run only tests of specific types.
# The allowed values are:
# - a: Bare metal Ansible tests.
# - b: Bats tests.
# - m: Molecule tests.
# - p: Pytest tests.
# - t: Tox tests.
# - y: Poetry tests.
# The full string is: abmpty.
TYPE=''
# 'Become root password' to pass to Ansible roles.
BECOME_PASS=''
# Wheter to generate or not a Python coverage report.
COVERAGE_REPORT=false
# When set to true, enter recursively on each directory on project's
# root folder and execute every testme.sh script found (add a
# .testignore file to ignore a directory).
RECURSIVE=false
# Show time report on stdout when finished.
CLOCK=false
# Internal flag to prevent executing pytest two times when
# running Ansible tests.
PYTEST_EXECUTED=false
# @description Determines if Ansible bare metal tests exists.
#
# This function tries:
# - Search .yml files on $test_path.
# - Search .yaml files on $test_path.
# - Search for the library folder.
# - Search for the test_plugins folder.
#
# @arg $1 string Optional project path. Default to current path.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
#
# @return true if ansible tests exists, false otherwise.
function ansible_exist() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
local test_path=$(get_test_path $project_path)
! [[ -d $test_path ]] && echo false && return 1
# Bare metal tests (no molecule).
# Search for yml tests.
local ls_result=$(ls $test_path)
[[ $ls_result == *'.yml'* ]] && echo true && return 0
# Search for .yaml tests.
ls_result=$(ls $test_path)
[[ $ls_result == *'.yaml'* ]] && echo true && return 0
# Check if ./library directory exists and contains py files.
if [[ -d $project_path/library ]]; then
ls_result=$(ls $project_path/library)
[[ $ls_result == *'.py'* ]] && echo true && return 0
fi
# Check if ./test_plugins directory exists and
# contains py files.
if [[ -d $project_path/test_plugins ]]; then
ls_result=$(ls $project_path/test_plugins)
[[ $ls_result == *'.py'* ]] && echo true && return 0
fi
echo false && return 0
}
# @description Execute ansible tests.
#
# @arg $1 string Optional project path. Default to current path.
# @arg $2 string Optional become password.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function ansible_run() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
if [[ $(ansible_exist $project_path) == 'true' ]]; then
ansible_setup $project_path
[ $? -eq 1 ] && return 1
local test_path=$(get_test_path $project_path)
local become_parameter=''
! [[ -z $2 ]] && become_parameter="ansible_become_pass: '$2', "
# Recolect the list of playbooks to execute.
local yml_files=''
local ls_result=$(ls $test_path)
[[ $ls_result == *'.yml'* ]] && yml_files=$(ls $test_path/*.yml)
local ls_result=$(ls $test_path)
[[ $ls_result == *'.yaml'* ]] && yml_files="yml_files $(ls $test_path/*.yaml)"
# Bare metal tests, using test playbook files.
# dependency roles must be preinstalled: ansible-galaxy install.
local python_exec=$(get_python_exec)
# Check if inventory exists, if not, create a temporary one.
local inventory_path=$test_path/inventory
if ! [[ -f $inventory_path ]]; then
echo 'localhost' > $test_path/inventory_tmp
inventory_path=$test_path/inventory_tmp
fi
for playbook in $yml_files; do
ansible-playbook -i $inventory_path $playbook -e \
"{${become_parameter}ansible_python_interpreter: '/usr/bin/$python_exec'}"
# Clear history for security.
! [[ -z $become_parameter ]] && history -c
done
fi
[[ -f $test_path/inventory_tmp ]] && rm -f $test_path/inventory_tmp
return 0
}
# @description Setup ansible tests.
#
# @arg $1 string Optional project path. Default to current_path.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function ansible_setup() {
# Ensure Python requirements are installed.
install_pip
[ $? -eq 1 ] && return 1
if [[ $(validate 'ansible') == 'false' ]]; then
install_pip 'ansible>=2.8'
[ $? -eq 1 ] && return 1
fi
if [[ $(validate_pip 'requests') == 'false' ]]; then
install_pip 'requests'
[ $? -eq 1 ] && return 1
fi
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
local test_path=$(get_test_path $project_path)
if [[ -d $project_path/library ]] && [[ -d $test_path ]]; then
ansible_setup_python $project_path/library $test_path
[ $? -eq 1 ] && return 1
fi
if [[ -d $project_path/test_plugins ]] && [[ -d $test_path ]]; then
ansible_setup_python $project_path/test_plugins $test_path
[ $? -eq 1 ] && return 1
fi
# Check if project is an ansible role.
if [[ -f $project_path/meta/main.yml ]]; then
# Get author from meta information.
local author=$(cat $project_path/meta/main.yml | grep 'author:')
if ! [[ -z $author ]]; then
# Remove the 'author:' part.
author=${author//author\: /}
# Sanitize the string.
author=$(sanitize "$author")
local role_name=$(basename $project_path)
# Copy role to ~/.ansible/roles.
! [[ -d ~/.ansible/roles ]] && mkdir -p ~/.ansible/roles
# Prevents copy a role with double author name if we are
# inside an installed role (i.e.: ~/.ansible/roles/author.role).
if [[ $role_name == *"${author}."* ]]; then
cp -rf $project_path ~/.ansible/roles &>/dev/null
else
cp -rf $project_path ~/.ansible/roles/${author}.${role_name}
fi
fi
# If ansible role.
fi
return 0
}
# @description Create symbolic links for Ansible modules and plugins.
#
# Link the directories:
# - ./library
# - ./test_plugins
#
# To the locations:
# - $test_path/library
# - $test_path/test_plugins
#
# Each .py file found under those directories will be compiled.
#
# @arg $1 Source directory (i.e.: $project_path/library).
# @arg $2 Destination directory (i.e.: $test_path).
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function ansible_setup_python() {
if [[ -z $1 ]] || ! [[ -d $1 ]]; then
error_message 'path' "$1"
error_message 'custom' 'Must specify an existent source directory.'
fi
if [[ -z $2 ]] || ! [[ -d $2 ]]; then
error_message 'path' "$2"
error_message 'custom' 'Must specify an existent destiny directory.'
fi
local source_path="$( cd "$1" ; pwd -P )"
local destiny_path="$( cd "$2" ; pwd -P )"
# Check if source directory contains python files.
local ls_result=$(ls $source_path)
if [[ $ls_result == *'.py'* ]]; then
ln -fs $source_path $destiny_path
# Once we know .py files exists, list again and store the listings.
local files_list=$(ls $source_path/*.py)
# Ensure Python requirements are installed.
install_pip
[ $? -eq 1 ] && return 1
local python_exec=$(get_python_exec)
for file_item in $file_list; do
$python_exec -m py_compile $file_item
done
fi
return 0
}
# @description Determines if Bats tests exists.
#
# This function tries:
# - Search .bats files on $test_path.
#
# @arg $1 string Optional project path. Default to current path.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
#
# @return true if ansible tests exists, false otherwise.
function bats_exist() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
local test_path=$(get_test_path $project_path)
! [[ -d $test_path ]] && echo false && return 1
local test_list=$(ls $test_path)
[[ $test_list == *'.bats'* ]] && echo true && return 0
echo false && return 0
}
# @description Execute bats tests.
#
# @arg $1 string Optional project path. Default to current path.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function bats_run() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
# Verify bats tests exists.
[[ $(bats_exist $project_path) == 'false' ]] && return 0
bats_setup
[ $? -eq 1 ] && return 1
# Verify test folder exists.
local test_path=$(get_test_path $project_path)
! [[ -d $test_path ]] && error_message 'path' "$test_path" && return 1
# Run bats tests.
bats $test_path
return 0
}
# @description Setup bats tests.
#
# @noargs
#
# @exitcode 0 if succesuful.
# @exitcode 1 if failure.
function bats_setup() {
install_apt 'bats'
[ $? -eq 1 ] && return 1
return 0
}
# @description Create a parameters string to pass to each
# recursively call of the testme.sh script.
#
# - *g* (coverage report).
# - *i* (install requirements).
# - *k* (clock report).
# - *K* (become password for Ansible roles).
# - *t* <type> (only tests of type).
# - *x* <python executable>.
#
# @arg $@ string Bash arguments.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
#
# @stdout Prints the created parameter string.
function create_parameter_string() {
local become_pass_regex='-K (.*)'
local python_exec_regex='-x (python[0-9]*)'
local type_regex='-t ([abmpty])+'
local parameter_string=''
[[ "$@" == *'-g'* ]] && parameter_string+='-g '
[[ "$@" == *'-i'* ]] && parameter_string+='-i '
# Type.
if [[ $@ =~ $type_regex ]]; then
parameter_string+="-t ${BASH_REMATCH[1]} "
fi
# Clock.
[[ "$@" == *'-k'* ]] && parameter_string+='-k '
if [[ $@ =~ $python_exec_regex ]]; then
parameter_string+="-x ${BASH_REMATCH[1]} "
fi
# Become password.
if [[ $@ =~ $become_pass_regex ]]; then
parameter_string+="-K $(sanitize ${BASH_REMATCH[1]})"
fi
echo "$parameter_string"
return 0
}
# @description Setups Docker.
#
# @noargs
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function docker_setup() {
# Ensure Python requirements are installed.
install_pip
[ $? -eq 1 ] && return 1
# Docker must be installed.
if [[ $(validate_apt 'docker-ce') == 'false' ]]; then
# Check if can install.
if [[ $(validate 'install') == 'true' ]]; then
# Ensure python requirements are present.
install_pip
# Official procedure:
# https://docs.docker.com/install/linux/docker-ce/debian.
# https://docs.docker.com/install/linux/docker-ce/ubuntu.
uninstall_apt 'containerd containerd.io docker docker-ce docker-engine docker.io runc'
[ $? -eq 1 ] && return 1
install_apt 'apt-transport-https ca-certificates curl gnupg2 software-properties-common'
[ $? -eq 1 ] && return 1
# Get 'debian' or 'ubuntu' name.
local os_name=$(cat /etc/os-release | grep ^ID=)
os_name="${os_name//ID=/}"
# Get codename, i.e.: 'buster'.
local os_codename=$(cat /etc/os-release | grep ^VERSION_CODENAME=)
os_codename="${os_codename//VERSION_CODENAME=/}"
# Add docker key.
echo 'Adding docker key with apt-key add ...'
curl -fsSL https://download.docker.com/linux/${os_name}/gpg | sudo apt-key add -
echo 'Adding docker fingerprint with apt-key ...'
sudo apt-key fingerprint 0EBFCD88
# Add docker repository.
echo 'Adding docker repository with add-apt-repository ...'
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$os_name \
$os_codename \
stable"
# Install all requirements.
install_apt 'docker-ce docker-ce-cli containerd.io'
[ $? -eq 1 ] && return 1
else
error_message 'docker'
return 1
fi
fi
return 0
}
# @description Shows an error message.
#
# @arg $1 string Error name: custom, execution, path, sudo, <name>.
# @arg $2 string Optional text to use on error messages.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
#
# @stdout 'Error message'
function error_message() {
[[ -z $1 ]] && return 1
case $1 in
custom)
if ! [[ -z $2 ]]; then
echo "$2"
else
echo 'Unknown error ocurred.'
fi
;;
execution)
if ! [[ -z $2 ]]; then
echo "An error occurred executing $2."
else
echo 'An error ocurred during execution.'
fi
;;
path)
if ! [[ -z $2 ]]; then
echo "$2 is not an existent directory."
else
echo 'Inexistent directory used.'
fi
;;
sudo)
echo "It's not possible to adquire administrative permissions."
echo 'It could be one of the following causes:'
echo '- The program "sudo" is not installed.'
echo '- Your user is not on the "root" or "sudo" groups.'
echo 'Run:'
echo 'su -c "apt install -y sudo" && su -c "sudo adduser $(whoami) sudo" && su'
;;
uninstall)
if ! [[ -z $2 ]]; then
echo "Cannot uninstall $2."
else
echo 'Cannot uninstall package.'
fi
;;
*)
echo ' __.....__'
echo ' ."" _ o "`.'
echo ' ." O (_) () o`.'
echo ' . O .'
echo ' . () o__...__ O .'
echo '. _.--""" """--._ .'
echo ':" ";'
echo ' `-.__ : : __.-"'
echo ' """-: :-"""'
echo ' J L'
echo ' : :'
echo ' J L'
echo ' : :'
echo ' `._____."'
echo "$1 needs to be installed ..."
echo "Run:"
echo "./$(basename $0).sh -i"
;;
esac
return 0
}
# @description Get bash parameters.
#
# Accepts:
# - *d* (docker).
# - *D* (docker image).
# - *g* (coverage report).
# - *h* (help).
# - *i* (install requirements).
# - *k* (clock).
# - *K* (become password for Ansible roles).
# - *p* <project_path>.
# - *r* (recursive).
# - *t* <types> (only tests of type).
# - *x* <python executable>.
#
# @arg '$@' string Bash arguments.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function get_parameters() {
# Obtain parameters.
while getopts 'd;D:g;h;i;k;K:p:r;t:x:' opt; do
OPTARG=$(sanitize "$OPTARG")
case "$opt" in
d) DOCKER=true;;
D) DOCKER_IMAGE="${OPTARG}";;
g) COVERAGE_REPORT=true;;
h) help && exit 0;;
i) INSTALL_REQUIREMENT=true;;
k) CLOCK=true;;
K) BECOME_PASS="${OPTARG}";;
p) PROJECT_PATH="${OPTARG}";;
r) RECURSIVE=true;;
t) TYPE="${OPTARG}";;
x) [[ "${OPTARG}" == *'python'* ]] && PYTHON_EXEC="${OPTARG}";;
esac
done
return 0
}
# @description Obtains the Python executable to use: python or python3.
#
# This function tries:
# - Use the $PYTHON_EXEC variable if not empty and like 'python'.
# - Use 'python3' is available.
# - Use 'python' if available.
#
# @args noargs
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout the python executable (python or python3).
function get_python_exec() {
# Try user passed exec.
if ! [[ -z $PYTHON_EXEC ]]; then
if [[ $(validate_apt "$PYTHON_EXEC") == 'true' ]]; then
echo "$PYTHON_EXEC" && return 0
fi
# Check if python was installed by compiling or other method.
if [[ $($PYTHON_EXEC --version) -eq 0 ]]; then
echo "$PYTHON_EXEC" && return 0
fi
fi
# Try python3.
if [[ $(validate_apt 'python3') == 'true' ]]; then
echo 'python3' && return 0
fi
python3 --version &>/dev/null
[ $? -eq 0 ] && echo 'python3' && return 0
# Try python.
if [[ $(validate_apt 'python') == 'true' ]]; then
echo 'python' && return 0
fi
python --version &>/dev/null
[ $? -eq 0 ] && echo 'python' && return 0
# Python is not installed.
echo '' && return 1
}
# @description Obtains the project's test directory.
#
# This function tries:
# - Search for the */tests* directory.
# - Default to */test* directory.
#
# @arg $1 string Optional project path. Default to current path.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
#
# @stdout Path to the test directory.
function get_test_path() {
local project_path=$(pwd)
[[ -d $1 ]] && project_path="$( cd "$1" ; pwd -P )"
# Try /tests.
[[ -d $project_path/tests ]] && echo "$project_path/tests" && return 0
# Default /test.
echo "$project_path/test" && return 0
}
# @description Shows help message.
#
# @noargs
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function help() {
echo 'Auto discover and execute project tests.'
echo 'Parameters:'
echo '-d (docker): Execute the tests inside a Docker container.'
echo '-D (docker image): Docker image to use.'
echo '-g (coverage): Generate a Python coverage report.'
echo '-h (help): Show this help message.'
echo '-i (requirements): Install requirements.'
echo '-k (clock): Show time report when finished'
echo '-K (become pass): Plain-text sudo password (for Ansible roles).'
echo '-t <type>: Optional string containing:
a (Ansible bare metal tests)
b (Bats tests)
m (Molecule tests)
p (Pytest tests)
t (Tox tests)
y (Poetry tests)'
echo "-p <project-path>: Absolute path to project's root directory."
echo "-r (recursive): Enter recursively each directory on project's
root directory and execute every testme.sh script found."
echo '-x <python-executable>: Run using python or python3.'
echo 'Example:'
echo "./testme.sh -d -D debian:stable-slim -g -i -k -K pass -t abmpty -p /project/path -r -x python3"
return 0
}
# @description Installs Apt packages.
#
# @arg $1 string List of package names to install, must be space-separated.
#
# @exitcode 0 If successful.
# @exitcode 1 On failure.
function install_apt() {
[[ -z $1 ]] && return 1
local package_array=($(echo $1 | tr " " "\n"))
# Prevents updating apt twice.
local apt_updated=false
# sudo must be installed.
if [[ $(validate_apt 'sudo') == 'false' ]]; then
# Add current user to sudoers.
local current_username=$(whoami)
echo 'Installing sudo using su -c ...'
su -c "apt update && apt install -y sudo && /usr/sbin/addgroup $current_username sudo"
apt_updated=true
fi
# build-essential must be installed.
if [[ $(validate_apt 'build-essential') == 'false' ]]; then
# Validate if can install
if [[ $(validate 'install') == 'false' ]]; then
error_message 'build-essential'
echo '░░░░░░░░▄▄▄███░░░░░░░░░░░░░░░░░░░░'
echo '░░░▄▄██████████░░░░░░░░░░░░░░░░░░░'
echo '░███████████████░░░░░░░░░░░░░░░░░░'
echo '░▀███████████████░░░░░▄▄▄░░░░░░░░░'
echo '░░░███████████████▄███▀▀▀░░░░░░░░░'
echo '░░░░███████████████▄▄░░░░░░░░░░░░░'
echo '░░░░▄████████▀▀▄▄▄▄▄░▀░░░░░░░░░░░░'
echo '▄███████▀█▄▀█▄░░█░▀▀▀░█░░▄▄░░░░░░░'
echo '▀▀░░░██▄█▄░░▀█░░▄███████▄█▀░░░▄░░░'
echo '░░░░░█░█▀▄▄▀▄▀░█▀▀▀█▀▄▄▀░░░░░░▄░▄█'
echo '░░░░░█░█░░▀▀▄▄█▀░█▀▀░░█░░░░░░░▀██░'
echo '░░░░░▀█▄░░░░░░░░░░░░░▄▀░░░░░░▄██░░'
echo '░░░░░░▀█▄▄░░░░░░░░▄▄█░░░░░░▄▀░░█░░'
echo '░░░░░░░░░▀███▀▀████▄██▄▄░░▄▀░░░░░░'
echo '░░░░░░░░░░░█▄▀██▀██▀▄█▄░▀▀░░░░░░░░'
echo '░░░░░░░░░░░██░▀█▄█░█▀░▀▄░░░░░░░░░░'
echo '░░░░░░░░░░█░█▄░░▀█▄▄▄░░█░░░░░░░░░░'
echo '░░░░░░░░░░█▀██▀▀▀▀░█▄░░░░░░░░░░░░░'
echo '░░░░░░░░░░░░▀░░░░░░░░░░░▀░░░░░░░░░'
return 1
fi
echo 'Installing build-essential ...'
[[ $apt_updated == 'false' ]] && sudo apt update && apt_updated=true
sudo apt install --no-install-recommends -y build-essential
fi
for package_name in ${package_array[@]}; do
# Validate if the package is already installed via apt.
[[ $(validate_apt "$package_name") == 'true' ]] && continue
# Validate if can install
if [[ $(validate 'install') == 'false' ]]; then
error_message "$package_name"
return 1
fi
echo "Installing $package_name ..."
# Check if the package is pip to handle specially.
if [[ "$package_name" == 'pip' ]] || [[ "$package_name" == 'pip3' ]] ||
[[ "$package_name" == 'python-pip' ]] || [[ "$package_name" == 'python3-pip' ]]; then
local python_exec=$(get_python_exec)
if [[ -z $python_exec ]]; then
install_apt python3
[ $? -eq 1 ] && return 1
python_exec=python3
fi
if [[ $(validate_pip_installed) == 'false' ]]; then
# Install pip.
package_name="${python_exec}-pip"
fi
fi
[[ $apt_updated == 'false' ]] && sudo apt update && apt_updated=true
sudo apt install --no-install-recommends -y "$package_name"
done
return 0
}
# @description Installs Python packages via pip.
#
# This function ensures that Python, Pip and Setuptools are installed
# and then installs all required packages.
#
# You can pass to this function:
# - A filepath to a requirements*.txt file to be installed.
# - A filepath to directory containing requirements*.txt files to install.
# - A single package name.
#
# If this function is called without passing any argument to it,
# it will search for requirements*.txt files on the current directory.
#
# This function expects that each requirements filename has the text
# 'requirements' included on it and to have the .txt extension.
#
# This function will always check for Python, Pip and Setuptools to be
# installed and will try to install them if not present.
#
# Each package will be checked to see if its installed, if not installed
# then this function proceeds to install it.
#
# @arg $1 string Optional filepath, path to dir or single package name.
#
# @exitcode 0 on success.
# @exitcode 1 on failure.
function install_pip() {
local python_exec=$(get_python_exec)
# Python must be installed.
if [[ -z $python_exec ]]; then
install_apt 'python3'
[ $? -eq 1 ] && return 1
python_exec='python3'
fi
# Pip must be installed.
install_apt "${python_exec}-pip"
[ $? -eq 1 ] && return 1
# Setuptools must be installed.
if [[ $(validate_apt "${python_exec}-setuptools") == 'false' ]] &&
[[ $(validate_pip 'setuptools') == 'false' ]]; then
$python_exec -m pip install setuptools
fi
# Virtualenv must be installed.
if [[ $(validate_apt "${python_exec}-venv") == 'false' ]] &&
[[ $(validate_pip 'virtualenv') == 'false' ]]; then
install_apt "${python_exec}-venv"
fi
# Path to where requirement files resides.
local base_path=''
local file_name=''
# List of files to install.
local requirement_list=''
# Check if single file.
if ! [[ -z $1 ]] && [[ -f $1 ]]; then
requirement_list=$1
base_path=$(dirname $1)
# Check if directory.
elif ! [[ -z $1 ]] && [[ -d $1 ]]; then
requirement_list=$(ls $1)
base_path=$1
# Single package, install it.
elif ! [[ -z $1 ]]; then
if [[ $(validate_pip "$1") == 'false' ]]; then
$python_exec -m pip install $1
return 0
fi
fi
# If at least one requirement file exists.
if [[ "$requirement_list" =~ .*requirements.*.txt ]]; then
# Traverse all requirements files, i.e.:
# requirements.txt, requirements-dev.txt.
for requirement_file in $requirement_list; do
if [[ "$requirement_file" == *'requirements'* ]]; then
file_name=$(basename $requirement_file)
# Copy file and add a newline to it to overcome bash limit.
cp -f $base_path/$file_name reqs-current-tmp.txt
echo '' >> reqs-current-tmp.txt
# Traverse current requirements file checking item by
# item if is installed or not.
while read LINE
do
# If not installed, install it.
if ! [[ -z $LINE ]] &&
[[ $(validate_pip "$LINE") == 'false' ]];
then
$python_exec -m pip install $LINE
fi
# Traverse requirement items (item by item).
done < reqs-current-tmp.txt
rm -f reqs-current-tmp.txt
fi
# Traverse requirements files (file by file).
done
fi
return 0
}
# @description Setup requirements and run tests.
#
# @arg $@ string Bash arguments.
#
# @exitcode 0 if successful.
# @exitcode 1 on failure.
function main() {
get_parameters "$@"
if ! [[ -d $PROJECT_PATH ]]; then
error_message 'path' "$PROJECT_PATH"
return 1
fi
local parameter_string=''
# Run on docker.
if [[ $DOCKER == "true" ]]; then
# Handle docker setup.
docker_setup
[ $? -eq 1 ] && return 1
parameter_string=$(create_parameter_string "$@")
if [[ $RECURSIVE == 'true' ]]; then
parameter_string="{parameter_string} -r"
fi
echo " DOCKA: $DOCKER_IMAGE ..."
echo ' ------------'
echo ' / (_)_ \'
echo ' /) (_) (_) \'
echo ' | |'
echo '| _ (_) _ (_) |'
echo '|(_) _ (_) _ (_)|'
echo '|___(_)_____(_)_____|'
echo ' |||||||||||||||||||'
echo ' | |'
echo ' | |'
echo ' | |'
echo ' |___|'
echo '--------------------'
local proj_name=$(basename $PROJECT_PATH)
docker run \
--rm \
--mount type=bind,source="$PROJECT_PATH",target=/$proj_name \
$DOCKER_IMAGE \
bash -c "cd /$proj_name && ./testme.sh -i ${parameter_string}"
return 0
fi
# Track time.
local start_time=''
[[ $CLOCK == 'true' ]] && start_time=`date +%s`
# Recursive run.
if [[ $RECURSIVE == 'true' ]]; then
# Verify if project has subdirectories.
find "$PROJECT_PATH" -mindepth 1 -type d -print &>/dev/null
if [ $? -eq 1 ]; then
error_message 'custom' "$PROJECT_PATH has no subidrectories."
return 1
fi
# Create recursive parameters string from bash parameters string.
parameter_string=$(create_parameter_string "$@")
# Enter each subdirectory checking if testme.sh exists.
local directories_list=$(find $PROJECT_PATH -type d -print)
for directory in $directories_list; do
# testme.sh exists and a .testignore does not exists, execute.
if [[ -f $directory/testme.sh ]] &&
! [[ -f $directory/.testignore ]]; then
./testme.sh -p $( cd "$directory" ; pwd -P ) $parameter_string
show_time $start_time
fi
done
else
# Run ansible tests.
if [[ -z $TYPE ]] || [[ $TYPE == *'a'* ]]; then
ansible_run "$PROJECT_PATH" "$BECOME_PASS"
[ $? -eq 1 ] && error_message 'execution' 'ansible' && return 1
# Execute pytest.
pytest_run $project_path
[ $? -eq 1 ] && error_message 'execution' 'pytest' && return 1
PYTEST_EXECUTED=true
fi