-
Notifications
You must be signed in to change notification settings - Fork 25
/
test.functions.os.sh
executable file
·387 lines (309 loc) · 11.1 KB
/
test.functions.os.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
function testcase_athena.os.is_command_set()
{
local old_command="test_command"
athena.os.set_command "SOMETHING"
bashunit.test.assert_exit_code "athena.os.is_command_set"
ATHENA_COMMAND=
bashunit.test.assert_exit_code.expects_fail "athena.os.is_command_set"
athena.os.set_command "$old_command"
}
function testcase_athena.os.set_command()
{
bashunit.test.assert_return "athena.os.set_command" "test"
bashunit.test.assert_exit_code.expects_fail "athena.os.set_command" ""
}
function testcase_athena.os.get_command()
{
athena.os.set_command "test"
bashunit.test.assert_output "athena.os.get_command" "test"
bashunit.test.assert_return "athena.os.get_command"
bashunit.test.mock.exits "athena.os.is_command_set" 1
bashunit.test.assert_exit_code.expects_fail "athena.os.get_command"
}
function testcase_athena.os.enable_error_mode()
{
curr_error_mode=$ATHENA_OUTPUT_MODE
athena.os.enable_error_mode
bashunit.test.assert_value "$ATHENA_OUTPUT_MODE" "1"
ATHENA_OUTPUT_MODE=$curr_error_mode
}
function testcase_athena.os.enable_quiet_mode()
{
curr_error_mode=$ATHENA_OUTPUT_MODE
athena.os.enable_quiet_mode
bashunit.test.assert_value "$ATHENA_OUTPUT_MODE" "2"
ATHENA_OUTPUT_MODE=$curr_error_mode
}
function testcase_athena.os.enable_verbose_mode()
{
curr_error_mode=$ATHENA_OUTPUT_MODE
athena.os.enable_verbose_mode
bashunit.test.assert_value "$ATHENA_OUTPUT_MODE" "0"
ATHENA_OUTPUT_MODE=$curr_error_mode
}
function testcase_athena.os.exec()
{
curr_error_mode=$ATHENA_OUTPUT_MODE
athena.os.enable_verbose_mode
output=$(athena.os.exec "echo" "hello")
bashunit.test.assert_value "hello" "$output"
athena.os.enable_quiet_mode
output=$(athena.os.exec "echo" "hello")
bashunit.test.assert_value "" "$output"
ATHENA_OUTPUT_MODE=$curr_error_mode
}
function testcase_athena.os.is_mac()
{
curr_is_mac=$ATHENA_IS_MAC
ATHENA_IS_MAC=1
bashunit.test.assert_return "athena.os.is_mac"
ATHENA_IS_MAC=0
bashunit.test.assert_return.expects_fail "athena.os.is_mac"
ATHENA_IS_MAC=$curr_is_mac
}
function testcase_athena.os.is_linux()
{
curr_is_linux=$ATHENA_IS_LINUX
ATHENA_IS_LINUX=1
bashunit.test.assert_return "athena.os.is_linux"
ATHENA_IS_LINUX=0
bashunit.test.assert_return.expects_fail "athena.os.is_linux"
ATHENA_IS_LINUX=$curr_is_linux
}
function testcase_athena.os.is_sudo()
{
curr_is_sudo=$ATHENA_SUDO
ATHENA_SUDO="sudo"
bashunit.test.assert_return "athena.os.is_sudo"
ATHENA_SUDO=""
bashunit.test.assert_return.expects_fail "athena.os.is_sudo"
ATHENA_SUDO=curr_is_sudo
}
function testcase_athena.os.get_host_ip()
{
curr_is_mac=$ATHENA_IS_MAC
bashunit.test.mock.outputs "athena.os._get_host_ip_for_mac" "IP from MAC"
bashunit.test.mock.outputs "athena.os._get_host_ip_for_linux" "IP from LINUX"
ATHENA_IS_MAC=1
bashunit.test.assert_output "athena.os.get_host_ip" "IP from MAC"
ATHENA_IS_MAC=0
bashunit.test.assert_output "athena.os.get_host_ip" "IP from LINUX"
ATHENA_IS_MAC=$curr_is_mac
}
function testcase_athena.os.get_instance()
{
curr_athena_instance=$ATHENA_INSTANCE
ATHENA_INSTANCE="myinstance"
bashunit.test.assert_output "athena.os.get_instance" "myinstance"
ATHENA_INSTANCE=$curr_athena_instance
}
function testcase_athena.os.set_instance()
{
curr_athena_instance=$ATHENA_INSTANCE
bashunit.test.assert_exit_code.expects_fail "athena.os.set_instance"
athena.os.set_instance "myinstance"
bashunit.test.assert_output "athena.os.get_instance" "myinstance"
ATHENA_INSTANCE=$curr_athena_instance
}
function testcase_athena.os.function_exists()
{
bashunit.test.assert_exit_code.expects_fail "athena.os.function_exists" ""
bashunit.test.assert_return.expects_fail "athena.os.function_exists" "my-non-existing-function"
bashunit.test.mock.exits "_my_os_function" 1
bashunit.test.assert_return "athena.os.function_exists" "_my_os_function"
}
function testcase_athena.os.function_exists_or_fail()
{
bashunit.test.assert_exit_code.expects_fail "athena.os.function_exists_or_fail" ""
bashunit.test.assert_exit_code.expects_fail "athena.os.function_exists_or_fail" "my-non-existing-function"
bashunit.test.mock.exits "_my_os_function" 1
bashunit.test.assert_return "athena.os.function_exists_or_fail" "_my_os_function"
}
function testcase_athena.os.getenv_or_fail()
{
bashunit.test.assert_exit_code.expects_fail "athena.os.getenv_or_fail" ""
bashunit.test.assert_exit_code.expects_fail "athena.os.getenv_or_fail" "AWS_PROFILE"
export AWS_PROFILE="abc"
bashunit.test.assert_return "athena.os.getenv_or_fail" "AWS_PROFILE"
}
function testcase_athena.os.return()
{
athena.os.return "one" "var1"
bashunit.test.assert_value "one" "$var1"
bashunit.test.mock "athena.get_value" "_my_os_mock"
athena.get_value
bashunit.test.assert_value "teste" "$value"
bashunit.test.assert_exit_code.expects_fail "_my_os_spinpans"
bashunit.test.assert_value "hello" "$(_my_os_athena.get_val)"
_my_os_athena.mynamespace.get_valtwo
bashunit.test.assert_value "hellofromtheoutside" "$valtwo"
}
function testcase_athena.os.include_once()
{
bashunit.test.assert_exit_code.expects_fail "athena.os.include_once" ""
bashunit.test.assert_exit_code.expects_fail "athena.os.include_once" "/non/existing/file"
tmpfile=$(bashunit.test.create_tempfile)
bashunit.test.assert_exit_code "athena.os.include_once" "$tmpfile"
rm "$tmpfile"
tmpfile=/tmp/bashunit.test.file.$RANDOM.$$@$RANDOM
touch "$tmpfile"
bashunit.test.assert_exit_code "athena.os.include_once" "$tmpfile"
rm "$tmpfile"
}
function testcase_athena.os.exit()
{
bashunit.test.assert_exit_code "athena.os.exit" 0
bashunit.test.assert_exit_code.expects_fail "athena.os.exit" 1
bashunit.test.assert_exit_code.expects_fail "athena.os.exit"
}
function testcase_athena.os.exit_with_msg()
{
bashunit.test.assert_exit_code.expects_fail "athena.os.exit_with_msg" "my msg"
}
function testcase_athena.os.handle_exit()
{
curr_container_started=$ATHENA_CONTAINER_STARTED
bashunit.test.assert_exit_code.expects_fail "athena.os.handle_exit" ABRT
bashunit.test.mock.outputs "athena.docker.cleanup" "cleanup"
bashunit.test.mock.returns "athena._print_time" 0
ATHENA_CONTAINER_STARTED=1
ATHENA_DOCKER_AUTO_CLEANUP=1
bashunit.test.assert_output "athena.os.handle_exit" "cleanup" EXIT
athena.docker.disable_auto_cleanup
bashunit.test.assert_output "athena.os.handle_exit" "" EXIT
ATHENA_CONTAINER_STARTED=0
bashunit.test.assert_output "athena.os.handle_exit" "" EXIT
bashunit.test.mock.outputs "athena.docker.stop_container" "stop_container"
bashunit.test.mock.returns "athena.color.print_debug" 0
ATHENA_CONTAINER_STARTED=1
bashunit.test.assert_output "athena.os.handle_exit" "stop_container" INT
ATHENA_CONTAINER_STARTED=0
bashunit.test.assert_output "athena.os.handle_exit" "" INT
bashunit.test.mock.outputs "athena.color.print_debug" "other"
bashunit.test.assert_output "athena.os.handle_exit" "other" OTHER
bashunit.test.assert_output "athena.os.handle_exit" "" ERR
ATHENA_CONTAINER_STARTED=$curr_container_started
}
function testcase_athena.os.register_exit_handler()
{
bashunit.test.mock "athena.os._trap" "_my_os_trap"
bashunit.test.assert_output "athena.os.register_exit_handler" "myfunc sig1 sig1" "myfunc" "sig1"
bashunit.test.assert_exit_code.expects_fail "athena.os.register_exit_handler"
}
function testcase_athena.os.override_exit_handler()
{
bashunit.test.mock.outputs "athena.color.print_debug" "overriden"
bashunit.test.mock.returns "athena.os._trap" 0
bashunit.test.mock.returns "athena.os.register_exit_handler" 0
bashunit.test.assert_output "athena.os.override_exit_handler" "overriden"
}
function testcase_athena.os.set_exit_handler()
{
bashunit.test.mock.outputs "athena.os.register_exit_handler" "setted"
bashunit.test.assert_output "athena.os.set_exit_handler" "setted"
}
function testcase_athena.os.set_debug()
{
curr_is_debug_mode=$ATHENA_IS_DEBUG
athena.os.set_debug 1
bashunit.test.assert_value "$ATHENA_IS_DEBUG" "1"
athena.os.set_debug 0
bashunit.test.assert_value "$ATHENA_IS_DEBUG" "0"
ATHENA_IS_DEBUG=$curr_is_debug_mode
}
function testcase_athena.os.is_debug_active()
{
curr_is_debug_mode=$ATHENA_IS_DEBUG
athena.os.set_debug 1
bashunit.test.assert_return "athena.os.is_debug_active"
athena.os.set_debug 0
bashunit.test.assert_return.expects_fail "athena.os.is_debug_active"
ATHENA_IS_DEBUG=$curr_is_debug_mode
}
function testcase_athena.os._process_flags()
{
athena.argument.set_arguments "--athena-env=xpto"
athena.os._process_flags
bashunit.test.assert_return.expects_fail "athena.argument.string_contains" "$ATHENA_ARGS" "--athena-env"
athena.argument.set_arguments "--athena-dbg"
athena.os._process_flags
bashunit.test.assert_return "athena.os.is_debug_active"
bashunit.test.assert_return.expects_fail "athena.argument.string_contains" "$ATHENA_ARGS" "--athena-dbg"
}
function testcase_athena.os.get_base_dir()
{
curr_base_dir=$ATHENA_BASE_DIR
ATHENA_BASE_DIR="/my/path/to/somewhere/"
bashunit.test.assert_output "athena.os.get_base_dir" "$ATHENA_BASE_DIR"
ATHENA_BASE_DIR=$curr_base_dir
}
function testcase_athena.os.get_base_lib_dir()
{
curr_base_lib_dir=$ATHENA_BASE_LIB_DIR
ATHENA_BASE_LIB_DIR="/my/path/to/somewhere/"
bashunit.test.assert_output "athena.os.get_base_lib_dir" "$ATHENA_BASE_LIB_DIR"
ATHENA_BASE_LIB_DIR=$curr_base_lib_dir
}
function testcase_athena.os.get_prefix()
{
curr_prefix=$ATHENA_PREFIX
ATHENA_PREFIX="myprefix"
bashunit.test.assert_output "athena.os.get_prefix" "$ATHENA_PREFIX"
ATHENA_PREFIX=$curr_prefix
}
function testcase_athena.os._set_no_logo()
{
curr_no_logo=$ATHENA_NO_LOGO
athena.os._set_no_logo 1
bashunit.test.assert_value "$ATHENA_NO_LOGO" 1
athena.os._set_no_logo 0
bashunit.test.assert_value "$ATHENA_NO_LOGO" 0
ATHENA_NO_LOGO=$curr_no_logo
}
function testcase_athena.os.split_string()
{
local test_string="something:with:colons: as separator"
local -a test_string_as_array=(something with colons " as separator")
local -a test_result
athena.os.split_string "$test_string" ":" test_result
bashunit.test.assert_array test_result test_string_as_array
test_string_as_array=("$test_string")
athena.os.split_string "$test_string" "/" test_result
bashunit.test.assert_array test_result test_string_as_array
}
function testcase_athena.os.call_with_args()
{
athena.argument.set_arguments one two three
bashunit.test.assert_exit_code.expects_fail "athena.os.call_with_args" 1
bashunit.test.assert_exit_code.expects_fail "athena.os.call_with_args" 1 'this wont exist as command or function'
bashunit.test.assert_exit_code "athena.os.call_with_args" 'echo'
bashunit.test.assert_output "athena.os.call_with_args" "one two three" "echo"
bashunit.test.assert_output "athena.os.call_with_args" "two" "_my_os_call_with_args_and_return_second_element"
athena.argument.set_arguments one "who let the dogs out" two three
bashunit.test.assert_output "athena.os.call_with_args" "who let the dogs out" "_my_os_call_with_args_and_return_second_element"
}
#### aux functions
function _my_os_call_with_args_and_return_second_element()
{
echo -n "${2}"
}
function _my_os_athena.mynamespace.get_valtwo()
{
athena.os.return "hellofromtheoutside"
}
function _my_os_mock()
{
athena.os.return "teste"
}
function _my_os_trap()
{
echo "$@"
}
function _my_os_spinpans()
{
athena.os.return "good morning"
}
function _my_os_athena.get_val()
{
athena.os.return "hello"
}