Skip to content

Commit

Permalink
Bash CLI: Enable Super linter linting (#5647)
Browse files Browse the repository at this point in the history
* enabled validate bash for super linter.
  • Loading branch information
meyertst-aws authored and ford-at-aws committed Dec 15, 2023
1 parent 9bab11e commit 3c87958
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 89 deletions.
1 change: 1 addition & 0 deletions .github/workflows/super-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ jobs:
VALIDATE_RUBY: true
VALIDATE_KOTLIN: true
VALIDATE_EDITORCONFIG: true
VALIDATE_BASH: true
4 changes: 4 additions & 0 deletions aws-cli/bash-linux/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Look for 'source'd files relative to the checked script.
source-path=SCRIPTDIR
source-path=SCRIPTDIR/..
source-path=aws-cli/bash-linux/s3/bucket-lifecycle-operations
1 change: 1 addition & 0 deletions aws-cli/bash-linux/dynamodb/dynamodb_operations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
###############################################################################


source ./awsdocs_general.sh

# snippet-start:[aws-cli.bash-linux.dynamodb.CreateTable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ function echo_repeat() {
# 1 - If an error occurred.
###############################################################################
function dynamodb_getting_started_movies() {

source ./dynamodb_operations.sh

key_schema_json_file="dynamodb_key_schema.json"
Expand Down
26 changes: 14 additions & 12 deletions aws-cli/bash-linux/dynamodb/tests/test_dynamodb_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function main() {
local current_directory
current_directory=$(pwd)
cd ..

source ./dynamodb_operations.sh

source ./scenario_getting_started_movies.sh
# shellcheck disable=SC2164
cd "$current_directory"
Expand Down Expand Up @@ -127,7 +129,7 @@ function main() {
"dynamodb_describe_table -n $test_table_name " \
0

if [[ "$test_command_response" != "ACTIVE" ]]; then
if [[ "$test_command_response" != "ACTIVE" ]]; then
test_failed "Table is not active."
return 1
fi
Expand Down Expand Up @@ -216,8 +218,8 @@ function main() {

test_count=$((test_count + 1))
echo -n "Running test $test_count: Querying table without projection expression..."
local response
response=$(dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file")

dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file"
local error_code=${?}

if [[ $error_code -ne 0 ]]; then
Expand All @@ -229,9 +231,8 @@ function main() {

test_count=$((test_count + 1))
echo -n "Running test $test_count: Querying table with projection expression..."
local response
response=$(dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" \
-v "$test_attributes_values_json_file" -p "title,info.plot")
dynamodb_query -n "$test_table_name" -k "#n=:v" -a "$test_attribute_names_json_file" \
-v "$test_attributes_values_json_file" -p "title,info.plot"
local error_code=${?}

if [[ $error_code -ne 0 ]]; then
Expand All @@ -251,8 +252,8 @@ function main() {

test_count=$((test_count + 1))
echo -n "Running test $test_count: Scanning table without projection expression..."
local response
response=$(dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file")

dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" -v "$test_attributes_values_json_file"
local error_code=${?}

if [[ $error_code -ne 0 ]]; then
Expand All @@ -264,9 +265,9 @@ function main() {

test_count=$((test_count + 1))
echo -n "Running test $test_count: Scanning table with projection expression..."
local response
response=$(dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" \
-v "$test_attributes_values_json_file" -p "title,info.plot")

dynamodb_scan -n "$test_table_name" -f "contains(#n,:v1)" -a "$test_attribute_names_json_file" \
-v "$test_attributes_values_json_file" -p "title,info.plot"
local error_code=${?}

if [[ $error_code -ne 0 ]]; then
Expand Down Expand Up @@ -300,7 +301,7 @@ function main() {
"dynamodb_batch_get_item -i $test_requested_values_json_file " \
0

skip_tests=false
export skip_tests=false
run_test " deleting table" \
"dynamodb_delete_table -n $test_table_name " \
0
Expand Down Expand Up @@ -364,6 +365,7 @@ function main() {
rm "$test_requested_values_json_file"

echo "$test_succeeded_count tests completed successfully."

echo "$test_failed_count tests failed."
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function sts_assume_role() {
function iam_create_user_assume_role() {
{
if [ "$IAM_OPERATIONS_SOURCED" != "True" ]; then
# shellcheck disable=SC1091

source ./iam_operations.sh
fi
}
Expand Down
2 changes: 1 addition & 1 deletion aws-cli/bash-linux/iam/iam_operations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function iam_user_exists() {
###############################################################################
function iam_list_users() {
local option OPTARG # Required to use getopts command in a function.
local all_users error_code
local error_code
# bashsupport disable=BP5008
function usage() {
echo "function iam_list_users"
Expand Down
2 changes: 2 additions & 0 deletions aws-cli/bash-linux/iam/tests/test_iam_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function main() {
local current_directory
current_directory=$(pwd)
cd ..

source ./iam_operations.sh

source ./iam_create_user_assume_role_scenario.sh
# shellcheck disable=SC2164
cd "$current_directory"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@
# This function runs the IAM examples' tests.
###############################################################################
function main() {
# Set default values.
# bashsupport disable=BP2001
export INTERACTIVE=false
# bashsupport disable=BP2001
export VERBOSE=false
# Set default values.
# bashsupport disable=BP2001
export INTERACTIVE=false
# bashsupport disable=BP2001
export VERBOSE=false

source ./include_tests.sh
{
local current_directory
current_directory=$(pwd)
cd ..

source ./medical_imaging_operations.sh
# shellcheck disable=SC2164
cd "$current_directory"
Expand Down Expand Up @@ -106,7 +107,7 @@ export VERBOSE=false
local data_store_found=false
local line
while IFS=$'\n' read -r line; do
IFS=$'\t' read -ra entries <<<"$line"
IFS=$'\t' read -ra entries <<<"$line"
if [ "${entries[1]}" == "$datastore_id" ]; then
data_store_found=true
fi
Expand Down
9 changes: 3 additions & 6 deletions aws-cli/bash-linux/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
base_dir=$(pwd)

echo "Running tests for S3 Bucket Lifecycle Operations"
cd s3/bucket-lifecycle-operations/
cd "$base_dir/s3/bucket-lifecycle-operations/" || exit
./test_bucket_operations.sh
cd $base_dir

echo "Running IAM tests"
cd iam/tests
cd "$base_dir/iam/tests" || exit
./test_iam_examples.sh
cd $base_dir

echo "Running medical-imaging tests"
cd medical-imaging/tests
cd "$base_dir/medical-imaging/tests" || exit
./test_medical_imaging_examples.sh
cd $base_dir

echo "Done"
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function run_test() {

# Check to see if we got the expected error code.
if [[ "$expected_err_code" -ne "$err" ]]; then
# shellcheck disable=SC2154
test_failed "The test \"$description\" returned an unexpected error code: $err. $test_command_response"
fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
###############################################################################

# snippet-start:[s3.bash.bucket-operations.complete]

source ./awsdocs_general.sh

# snippet-start:[aws-cli.bash-linux.s3.HeadBucket]
Expand All @@ -41,12 +42,9 @@ function bucket_exists() {
# Check whether the bucket already exists.
# We suppress all output - we're interested only in the return code.

aws s3api head-bucket \
if aws s3api head-bucket \
--bucket "$bucket_name" \
>/dev/null 2>&1

# shellcheck disable=SC2181
if [[ ${?} -eq 0 ]]; then
>/dev/null 2>&1; then
return 0 # 0 in Bash script means true.
else
return 1 # 1 in Bash script means false.
Expand Down Expand Up @@ -378,4 +376,4 @@ function delete_bucket() {
# snippet-end:[s3.bash.bucket-operations.complete]

# shellcheck disable=SC2034
declare -r BUCKET_OPERATIONS_SOURCED="True"
declare -r BUCKET_OPERATIONS_SOURCED="True"
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,56 @@
#
###############################################################################


source ./awsdocs_general.sh

source ./bucket_operations.sh
{
current_directory=$(pwd)
cd ..
cd .. || exit

source ./s3_getting_started.sh
cd $current_directory
cd "$current_directory" || exit
}
source ../s3_getting_started.sh

function usage() {
echo "This script tests Amazon S3 bucket operations in the AWS CLI."
echo "It creates a randomly named bucket, copies files to it, then"
echo "deletes the files and the bucket."
echo ""
echo "To pause the script between steps so you can see the results in the"
echo "AWS Management Console, include the parameter -i."
echo ""
echo "IMPORTANT: Running this script creates resources in your Amazon"
echo " account that can incur charges. It is your responsibility to"
echo " ensure that no resources are left in your account after the script"
echo " completes. If an error occurs during the operation of the script,"
echo " then resources can remain that you might need to delete manually."
echo "This script tests Amazon S3 bucket operations in the AWS CLI."
echo "It creates a randomly named bucket, copies files to it, then"
echo "deletes the files and the bucket."
echo ""
echo "To pause the script between steps so you can see the results in the"
echo "AWS Management Console, include the parameter -i."
echo ""
echo "IMPORTANT: Running this script creates resources in your Amazon"
echo " account that can incur charges. It is your responsibility to"
echo " ensure that no resources are left in your account after the script"
echo " completes. If an error occurs during the operation of the script,"
echo " then resources can remain that you might need to delete manually."
}

# Set default values.
INTERACTIVE=false
VERBOSE=false

# Retrieve the calling parameters
while getopts "ivh" OPTION; do
case "${OPTION}"
in
i) INTERACTIVE=true;VERBOSE=true; iecho;;
v) VERBOSE=true;;
h) usage; return 0;;
\?) echo "Invalid parameter"; usage; return 1;;
esac
done

# Set default values.
INTERACTIVE=false
VERBOSE=false

# Retrieve the calling parameters
while getopts "ivh" OPTION; do
case "${OPTION}" in
i)
INTERACTIVE=true
VERBOSE=true
iecho
;;
v) VERBOSE=true ;;
h)
usage
return 0
;;
\?)
echo "Invalid parameter"
usage
return 1
;;
esac
done

if [ "$INTERACTIVE" == "true" ]; then iecho "Tests running in interactive mode."; fi
if [ "$VERBOSE" == "true" ]; then iecho "Tests running in verbose mode."; fi
Expand All @@ -76,49 +85,49 @@ iecho "FILENAME2=$FILENAME2"
iecho "**************END OF STEPS******************"

run_test "1. Creating bucket with missing bucket_name" \
"create_bucket -r $REGION" \
1 \
"ERROR: You must provide a bucket name" \
"create_bucket -r $REGION" \
1 \
"ERROR: You must provide a bucket name"

run_test "3. Creating bucket with valid parameters" \
"create_bucket -r $REGION -b $BUCKETNAME" \
0
"create_bucket -r $REGION -b $BUCKETNAME" \
0

run_test "4. Creating bucket with duplicate name and region" \
"create_bucket -r $REGION -b $BUCKETNAME" \
1 \
"ERROR: A bucket with that name already exists"
"create_bucket -r $REGION -b $BUCKETNAME" \
1 \
"ERROR: A bucket with that name already exists"

run_test "5. Copying local file (copy of this script) to bucket" \
"copy_file_to_bucket $BUCKETNAME ./$0 $FILENAME1" \
0
"copy_file_to_bucket $BUCKETNAME $0 $FILENAME1" \
0

run_test "6. Duplicating existing file in bucket" \
"copy_item_in_bucket $BUCKETNAME $FILENAME1 $FILENAME2" \
0
"copy_item_in_bucket $BUCKETNAME $FILENAME1 $FILENAME2" \
0

run_test "7. Listing contents of bucket" \
"list_items_in_bucket $BUCKETNAME" \
0
"list_items_in_bucket $BUCKETNAME" \
0

run_test "8. Deleting first file from bucket" \
"delete_item_in_bucket $BUCKETNAME $FILENAME1" \
0
"delete_item_in_bucket $BUCKETNAME $FILENAME1" \
0

run_test "9. Deleting second file from bucket" \
"delete_item_in_bucket $BUCKETNAME $FILENAME2" \
0
"delete_item_in_bucket $BUCKETNAME $FILENAME2" \
0

run_test "10. Deleting bucket" \
"delete_bucket $BUCKETNAME" \
0
"delete_bucket $BUCKETNAME" \
0

mock_input="True"
mock_input_array=("README.md" "y" "y" "y")
export mock_input="True"
export mock_input_array=("README.md" "y" "y" "y")

run_test "11. s3 getting started scenario" \
s3_getting_started \
0
s3_getting_started \
0

unset mock_input

Expand Down
Loading

0 comments on commit 3c87958

Please sign in to comment.