-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.sh
156 lines (121 loc) · 4.09 KB
/
tests.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
#!/bin/bash
# Copyright (C) 2022 Daniel Vidal de la Rubia WTFPL
#
# This library is free software; you can redistribute it and/or modify it
# under any version of the Do What The Fuck You Want To Public License;
# see the file LICENSE if the license name isn't clear enough.
set -euo pipefail
source "./awsprofile.sh"
tmp_dir="$(mktemp -d)"
trap "rm -r ${tmp_dir}" INT TERM EXIT
mocked_aws_output="default\nprod_env\npreprod_env"
test_awsprofile_without_arguments_lists_profiles() {
echo -n ":: ${FUNCNAME[0]}"
# Mocked awscli
aws() {
echo "${mocked_aws_output}"
}
set +e
awsprofile > "${tmp_dir}/output.txt" 2>&1
local return_code=$?
local output="$(cat "${tmp_dir}/output.txt")"
set -e
local expected_return_code=0
if [[ "${expected_return_code}" -ne "${return_code}" ]]; then
echo " ❌"
echo -e ":::: Expected return code: ${expected_return_code}\n:::: Actual return code: ${return_code}"
return 1
fi
local expected_output="default\nprod_env\npreprod_env"
if [[ "${expected_output}" != "${output}" ]]; then
echo " ❌"
echo -e ":::: Expected output: ${expected_output}\n:::: Actual output: ${output}"
return 1
fi
echo " ✅"
}
test_awsprofile_with_nonexisting_profile_argument_returns_error() {
echo -n ":: ${FUNCNAME[0]}"
# Mocked awscli
aws() {
echo -e "${mocked_aws_output}"
}
local profile_name="nonexisting_profile"
set +e
awsprofile ${profile_name} > "${tmp_dir}/output.txt" 2>&1
local return_code=$?
local output="$(cat "${tmp_dir}/output.txt")"
set -e
local expected_output="Profile ${profile_name} doesn't exist."
if [[ "${expected_output}" != "${output}" ]]; then
echo " ❌"
echo -e ":::: Expected output:\n${expected_output}\n:::: Actual output:\n${output}"
return 1
fi
local expected_return_code=1
if [[ "${expected_return_code}" -ne "${return_code}" ]]; then
echo " ❌"
echo -e ":::: Expected return code: ${expected_return_code}\n:::: Actual return code: ${return_code}"
return 1
fi
echo " ✅"
}
test_awsprofile_with_existing_profile_argument_changes_env_variable() {
echo -n ":: ${FUNCNAME[0]}"
# Mocked awscli
aws() {
echo -e "${mocked_aws_output}"
}
local profile_name="prod_env"
set +e
awsprofile ${profile_name} > "${tmp_dir}/output.txt" 2>&1
local return_code=$?
local output="$(cat "${tmp_dir}/output.txt")"
set -e
local expected_return_code=0
if [[ "${expected_return_code}" -ne "${return_code}" ]]; then
echo " ❌"
echo -e ":::: Expected return code: ${expected_return_code}\n:::: Actual return code: ${return_code}"
return 1
fi
local expected_output="AWS profile changed to ${profile_name}."
if [[ "${expected_output}" != "${output}" ]]; then
echo " ❌"
echo -e ":::: Expected output:\n${expected_output}\n:::: Actual output:\n${output}"
return 1
fi
if [[ "${AWS_PROFILE}" != "${profile_name}" ]]; then
echo " ❌"
echo -e ":::: Expected AWS_PROFILE: ${AWS_PROFILE}\n:::: Actual AWS_PROFILE: ${profile_name}"
return 1
fi
echo " ✅"
}
test_awsprompt_prints_profile_name() {
echo -n ":: ${FUNCNAME[0]}"
local AWS_PROFILE='test_profile'
output="$(_awsprompt)"
local expected_output="[AWS->${AWS_PROFILE}]"
if [[ "${expected_output}" != "${output}" ]]; then
echo " ❌"
echo -e ":::: Expected output: ${expected_output}\n:::: Actual output: ${output}"
return 1
fi
echo " ✅"
}
test_awsprompt_prints_defaults_when_env_var_not_defined() {
echo -n ":: ${FUNCNAME[0]}"
local AWS_PROFILE=''
output="$(_awsprompt)"
local expected_output="[AWS->default]"
if [[ "${expected_output}" != "${output}" ]]; then
echo " ❌"
echo -e ":::: Expected output: ${expected_output}\n:::: Actual output: ${output}"
return 1
fi
echo " ✅"
}
for test in $(declare -F | awk '{$0=$3} /^test_/{print}')
do
${test}
done