generic cmd parser and unit tests
This commit is contained in:
Родитель
9cf2a50ac9
Коммит
80ff959133
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# import utils
|
||||
source utils.sh
|
||||
|
||||
|
@ -27,7 +29,34 @@ chmod +x install-container-management.sh
|
|||
chmod +x install-edge-runtime.sh
|
||||
chmod +x validate-post-install.sh
|
||||
|
||||
# run scripts in order
|
||||
# create flag:variable_name dictionary
|
||||
declare -A flag_to_variable_dict
|
||||
|
||||
# add flag:variable name dictionary entries
|
||||
flag_to_variable_dict[-v]="VERBOSE_LOGGING"
|
||||
flag_to_variable_dict[-dp]="DEVICE_PROVISIONING"
|
||||
flag_to_variable_dict[-ap]="AZURE_CLOUD_IDENTITY_PROVIDER"
|
||||
flag_to_variable_dict[-s]="SCOPE_ID"
|
||||
flag_to_variable_dict[-r]="REGISTRATION_ID"
|
||||
flag_to_variable_dict[-k]="SYMMETRIC_KEY"
|
||||
|
||||
# create flag:variable_name dictionary
|
||||
declare -A parsed_cmd
|
||||
|
||||
# parse command line inputs
|
||||
cmd_parser $@
|
||||
|
||||
# fetch output from parser
|
||||
parsed_cmd="$(cmd_parser)"
|
||||
|
||||
# sample usage
|
||||
echo ""
|
||||
echo "Verbose Logging: ${parsed_cmd[VERBOSE_LOGGING]}"
|
||||
echo "Device provisioning: ${parsed_cmd[DEVICE_PROVISIONING]}"
|
||||
echo "Azure Cloud Identity Provider: ${parsed_cmd[AZURE_CLOUD_IDENTITY_PROVIDER]}"
|
||||
echo ""
|
||||
|
||||
# run scripts in order, can take parsed input from above
|
||||
./validate-tier1-os.sh
|
||||
./install-container-management.sh
|
||||
./install-edge-runtime.sh
|
||||
|
|
28
src/utils.sh
28
src/utils.sh
|
@ -1 +1,27 @@
|
|||
VERSION_TAG="v0.0.0-rc0"
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# generic command line parser
|
||||
function cmd_parser() {
|
||||
for arg in "$@"
|
||||
do
|
||||
if [[ $arg == -* ]]; then
|
||||
# iterate over all the keys in dictionary
|
||||
for k in ${!flag_to_variable_dict[*]} ; do
|
||||
# if arg==key, then we store into flag_to_val_dict
|
||||
if [ "$arg" == "$k" ]; then
|
||||
# set parsed_cmd, which is varname:value
|
||||
parsed_cmd[${flag_to_variable_dict[$k]}]=$2
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
shift # Remove argument name from processing
|
||||
done
|
||||
|
||||
# view content of entire dictionary
|
||||
# echo '('
|
||||
# for key in "${!parsed_cmd[@]}" ; do
|
||||
# echo "[$key]=${parsed_cmd[$key]}"
|
||||
# done
|
||||
# echo ')'
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# import utils.sh
|
||||
source ../../src/utils.sh
|
||||
|
||||
# create flag:variable_name dictionary
|
||||
declare -A flag_to_variable_dict
|
||||
|
||||
# add flag:variable name dictionary entries
|
||||
flag_to_variable_dict[-v]="VERBOSE_LOGGING"
|
||||
flag_to_variable_dict[-dp]="DEVICE_PROVISIONING"
|
||||
flag_to_variable_dict[-ap]="AZURE_CLOUD_IDENTITY_PROVIDER"
|
||||
flag_to_variable_dict[-s]="SCOPE_ID"
|
||||
flag_to_variable_dict[-r]="REGISTRATION_ID"
|
||||
flag_to_variable_dict[-k]="SYMMETRIC_KEY"
|
||||
|
||||
# unit test to test all illegal flags, output should be empty
|
||||
test_illegal_flags() {
|
||||
# create flag:variable_name dictionary
|
||||
declare -A parsed_cmd
|
||||
|
||||
# parse command line inputs
|
||||
cmd_parser -illegal val -t anotherillegalval -z yetanotherillegalval
|
||||
|
||||
# fetch output from parser
|
||||
parsed_cmd="$(cmd_parser)"
|
||||
|
||||
# compare output, should be empty
|
||||
if [ "${parsed_cmd[*]}" != "" ]; then
|
||||
echo "Failed to pass unit test 'test_illegal_flags' in test-cmd-parser.sh. Non-empty result: ${parsed_cmd[*]}"
|
||||
fi
|
||||
}
|
||||
|
||||
test_all_legal_flags() {
|
||||
# create flag:variable_name dictionary
|
||||
declare -A parsed_cmd
|
||||
|
||||
# parse command line inputs
|
||||
cmd_parser -dp 1 -ap 5 -s 2 -r 4 -k 3 -v 6
|
||||
|
||||
# fetch output from parser
|
||||
parsed_cmd="$(cmd_parser)"
|
||||
|
||||
# compare output, should be 123456
|
||||
if [ "${parsed_cmd[*]}" != "1 2 3 4 5 6" ]; then
|
||||
echo "Failed to pass unit test 'test_all_legal_flags' in test-cmd-parser.sh. Non-empty result: ${parsed_cmd[*]}"
|
||||
fi
|
||||
}
|
||||
|
||||
test_extra_arguments() {
|
||||
# create flag:variable_name dictionary
|
||||
declare -A parsed_cmd
|
||||
|
||||
# parse command line inputs
|
||||
cmd_parser illegalinput 0 -ap 4 -s 1 -r 3 more -extra -k 2
|
||||
|
||||
# fetch output from parser
|
||||
parsed_cmd="$(cmd_parser)"
|
||||
|
||||
# compare output, should be 1 2 3 4
|
||||
if [ "${parsed_cmd[*]}" != "1 2 3 4" ]; then
|
||||
echo "Failed to pass unit test 'test_extra_arguments' in test-cmd-parser.sh. Non-empty result: ${parsed_cmd[*]}"
|
||||
fi
|
||||
}
|
||||
|
||||
# run all tests
|
||||
test_illegal_flags
|
||||
test_all_legal_flags
|
||||
test_extra_arguments
|
Загрузка…
Ссылка в новой задаче