forked from gruntwork-io/bash-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.bats
135 lines (114 loc) · 3.22 KB
/
os.bats
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
#!/usr/bin/env bats
source "$BATS_TEST_DIRNAME/../modules/bash-commons/src/os.sh"
load "test-helper"
@test "os_get_available_memory_mb" {
run os_get_available_memory_mb
assert_success
assert_output_regex "[0-9]+"
}
@test "os_is_amazon_linux" {
run os_is_amazon_linux
assert_failure
}
@test "os_is_ubuntu" {
run os_is_ubuntu
assert_success
}
@test "os_is_darwin" {
run os_is_darwin
assert_failure
}
@test "os_validate_checksum valid sha256" {
run os_validate_checksum "$BATS_TEST_DIRNAME/fixtures/checksum/foo.txt" "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" "sha256"
assert_success
}
@test "os_validate_checksum invalid sha256" {
run os_validate_checksum "$BATS_TEST_DIRNAME/fixtures/checksum/foo.txt" "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7af" "sha256"
assert_failure
}
@test "os_validate_checksum valid md5" {
run os_validate_checksum "$BATS_TEST_DIRNAME/fixtures/checksum/foo.txt" "acbd18db4cc2f85cedef654fccc4a4d8" "md5"
assert_success
}
@test "os_validate_checksum invalid md5" {
run os_validate_checksum "$BATS_TEST_DIRNAME/fixtures/checksum/foo.txt" "bcbd18db4cc2f85cedef654fccc4a4d8" "md5"
assert_failure
}
@test "os_command_is_installed bash built in" {
run os_command_is_installed "echo"
assert_success
}
@test "os_command_is_installed installed app" {
run os_command_is_installed "bats"
assert_success
}
@test "os_command_is_installed non-existent app" {
run os_command_is_installed "not-a-real-app"
assert_failure
}
@test "os_get_current_users_name" {
run os_get_current_users_name
assert_success
assert_output "root"
}
@test "os_get_current_users_group" {
run os_get_current_users_group
assert_success
assert_output "root"
}
@test "os_user_is_root_or_sudo for root user" {
run os_user_is_root_or_sudo
assert_success
}
@test "os_user_exists for root user" {
run os_user_exists root
assert_success
}
@test "os_user_exists for missing user" {
run os_user_exists missing
assert_failure
}
@test "os_create_user for new_user user" {
local suffix
suffix=$(date +%s)
local -r username="new_user$suffix"
run os_user_exists "$username"
assert_failure
run os_create_user "$username"
assert_success
run os_user_exists "$username"
assert_success
}
@test "os_create_user with sudo for new_user_sudo user" {
local suffix
suffix=$(date +%s)
local -r username="new_user_sudo$suffix"
run os_user_exists "$username"
assert_failure
run os_create_user "$username" 'true'
assert_success
run os_user_exists "$username"
assert_success
}
@test "os_change_dir_owner for new_user user" {
local suffix
suffix=$(date +%s)
local -r username="new_user$suffix"
run os_create_user "$username"
assert_success
mkdir -p "/tmp/$username"
os_change_dir_owner "/tmp/$username" "$username"
local owner=$(stat -c '%U' "/tmp/$username")
assert_equal "$username" "$owner"
}
@test "os_change_dir_owner with sudo for new_user_sudo user" {
local suffix
suffix=$(date +%s)
local -r username="new_user_sudo$suffix"
run os_create_user "$username"
assert_success
mkdir -p "/tmp/$username"
os_change_dir_owner "/tmp/$username" "$username" 'true'
local owner=$(stat -c '%U' "/tmp/$username")
assert_equal "$username" "$owner"
}