Skip to content

Commit 2411239

Browse files
committed
Add tests for functions copied over from zookeeper and kafka
1 parent f5109d9 commit 2411239

File tree

5 files changed

+181
-2
lines changed

5 files changed

+181
-2
lines changed

test/array.bats

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,36 @@ load "test-helper"
3838
assert_failure
3939
}
4040

41+
@test "array_split on empty string" {
42+
local ary=( $(array_split "" "") )
43+
status="$?"
44+
assert_success
45+
assert_equal "" "${ary[0]}"
46+
}
47+
48+
@test "array_split on single element string" {
49+
local ary=( $(array_split "," "test") )
50+
status="$?"
51+
assert_success
52+
assert_equal "test" "${ary[0]}"
53+
}
54+
55+
@test "array_split on multi element string" {
56+
local ary=( $(array_split "," "test0,test1") )
57+
status="$?"
58+
assert_success
59+
assert_equal "test0" "${ary[0]}"
60+
assert_equal "test1" "${ary[1]}"
61+
}
62+
63+
@test "array_split on multi element string with multi character separator" {
64+
local ary=( $(array_split "==" "test0==test1") )
65+
status="$?"
66+
assert_success
67+
assert_equal "test0" "${ary[0]}"
68+
assert_equal "test1" "${ary[1]}"
69+
}
70+
4171
@test "array_join on empty array" {
4272
run array_join ","
4373
assert_success
@@ -60,4 +90,35 @@ load "test-helper"
6090
run array_join " == " "foo" "bar" "baz"
6191
assert_success
6292
assert_output "foo == bar == baz"
63-
}
93+
}
94+
95+
@test "array_prepend on empty string" {
96+
local ary=( $(array_prepend "test" "") )
97+
status="$?"
98+
assert_success
99+
assert_equal "" "${ary[0]}"
100+
}
101+
102+
@test "array_prepend on empty prepend string" {
103+
local ary=( $(array_prepend "" "test0" "test1") )
104+
status="$?"
105+
assert_success
106+
assert_equal "test0" "${ary[0]}"
107+
assert_equal "test1" "${ary[1]}"
108+
}
109+
110+
@test "array_prepend on single element string" {
111+
local ary=( $(array_prepend "test" "0") )
112+
status="$?"
113+
assert_success
114+
assert_equal "test0" "${ary[0]}"
115+
}
116+
117+
@test "array_prepend on multi element string" {
118+
local ary=( $(array_prepend "test" "0" "1") )
119+
status="$?"
120+
assert_success
121+
assert_equal "test0" "${ary[0]}"
122+
assert_equal "test1" "${ary[1]}"
123+
}
124+

test/assert.bats

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,51 @@ load "test-helper"
124124
assert_failure
125125
}
126126

127+
@test "assert_exactly_one_of list of length 2, with value in pos 1" {
128+
run assert_exactly_one_of "--foo" "foo" "--bar" ""
129+
assert_success
130+
}
131+
132+
@test "assert_exactly_one_of list of length 2, with value in pos 2" {
133+
run assert_exactly_one_of "--foo" "" "--bar" "bar"
134+
assert_success
135+
}
136+
137+
@test "assert_exactly_one_of list of length 3, with value in pos 1" {
138+
run assert_exactly_one_of "--foo" "foo" "--bar" "" "--baz" ""
139+
assert_success
140+
}
141+
142+
@test "assert_exactly_one_of list of length 3, with value in pos 2" {
143+
run assert_exactly_one_of "--foo" "" "--bar" "bar" "--baz" ""
144+
assert_success
145+
}
146+
147+
@test "assert_exactly_one_of list of length 3, with value in pos 3" {
148+
run assert_exactly_one_of "--foo" "" "--bar" "" "--baz" "baz"
149+
assert_success
150+
}
151+
152+
@test "assert_exactly_one_of list of length 3, with value in pos 1 and 2" {
153+
run assert_exactly_one_of "--foo" "foo" "--bar" "bar" "--baz" ""
154+
assert_failure
155+
}
156+
157+
@test "assert_exactly_one_of list of length 3, with value in pos 1 and 3" {
158+
run assert_exactly_one_of "--foo" "foo" "--bar" "" "--baz" "baz"
159+
assert_failure
160+
}
161+
162+
@test "assert_exactly_one_of list of length 3, with value in pos 2 and 3" {
163+
run assert_exactly_one_of "--foo" "" "--bar" "bar" "--baz" "baz"
164+
assert_failure
165+
}
166+
167+
@test "assert_exactly_one_of list of length 3, with value in pos 1, 2 and 3" {
168+
run assert_exactly_one_of "--foo" "foo" "--bar" "bar" "--baz" "baz"
169+
assert_failure
170+
}
171+
127172
@test "assert_uid_is_root_or_sudo as root" {
128173
run assert_uid_is_root_or_sudo
129174
assert_success

test/aws-cli.bats

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ END_HEREDOC
5353
assert_output_json "$expected"
5454
}
5555

56+
@test "aws_get_instance_tag_val non-empty" {
57+
local -r tag_key="foo"
58+
local -r tag_value="bar"
59+
60+
local instance_id
61+
instance_id=$(create_mock_instance_with_tags "$tag_key" "$tag_value")
62+
63+
run aws_get_instance_tag_val "$tag_key" "$instance_id" "us-east-1"
64+
assert_success
65+
66+
local -r expected="$tag_value"
67+
68+
assert_output "$expected"
69+
}
70+
5671
@test "aws_describe_asg empty" {
5772
run aws_describe_asg "fake-asg-name" "us-east-1"
5873
assert_success

test/file.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ load "test-helper"
136136
rm -f "$tmp_file"
137137
}
138138

139+
@test "file_replace_text_in_files non empty files, regex match" {
140+
local tmp_file1=$(mktemp)
141+
local tmp_file2=$(mktemp)
142+
local -r original_regex=".*foo.*"
143+
local -r replacement="bar"
144+
local -r file_contents1="abc foo def"
145+
local -r file_contents2="baz foo fuzz"
146+
147+
echo "$file_contents1" > "$tmp_file1"
148+
echo "$file_contents2" > "$tmp_file2"
149+
150+
run file_replace_text_in_files "$original_regex" "$replacement" "$tmp_file1" "$tmp_file2"
151+
assert_success
152+
153+
local actual1=$(cat "$tmp_file1")
154+
local actual2=$(cat "$tmp_file2")
155+
assert_equal "$replacement" "$actual1"
156+
assert_equal "$replacement" "$actual2"
157+
158+
159+
rm -f "$tmp_file1" "$tmp_file2"
160+
}
161+
139162
@test "file_replace_or_append_text empty file" {
140163
local readonly tmp_file=$(mktemp)
141164
local readonly original_regex="foo"

test/os.bats

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,39 @@ load "test-helper"
7474
@test "os_user_is_root_or_sudo for root user" {
7575
run os_user_is_root_or_sudo
7676
assert_success
77-
}
77+
}
78+
79+
@test "os_user_exists for root user" {
80+
run os_user_exists root
81+
assert_success
82+
}
83+
84+
@test "os_user_exists for missing user" {
85+
run os_user_exists missing
86+
assert_failure
87+
}
88+
89+
@test "os_create_user for new_user user" {
90+
local suffix
91+
suffix=$(date +%s)
92+
local -r username="new_user$suffix"
93+
run os_user_exists "$username"
94+
assert_failure
95+
run os_create_user "$username"
96+
assert_success
97+
run os_user_exists "$username"
98+
assert_success
99+
}
100+
101+
102+
@test "os_change_dir_owner for new_user user" {
103+
local suffix
104+
suffix=$(date +%s)
105+
local -r username="new_user$suffix"
106+
run os_create_user "$username"
107+
assert_success
108+
mkdir -p "/tmp/$username"
109+
os_change_dir_owner "/tmp/$username" "$username"
110+
local owner=$(stat -c '%U' "/tmp/$username")
111+
assert_equal "$username" "$owner"
112+
}

0 commit comments

Comments
 (0)