12
12
# The output of sloctl is sanitized, the trailing whitespaces,
13
13
# if present, are removed for easier output validation.
14
14
run_sloctl () {
15
- run bash -c " set -o pipefail && sloctl $* | sed 's/ *$//'"
15
+ run bash -c " set -o pipefail && sloctl $* | sed 's/ *$//'"
16
16
}
17
17
18
18
# read_files
@@ -31,7 +31,7 @@ run_sloctl() {
31
31
# documents style.
32
32
# yq works with json as it is only a preprocessor for jq.
33
33
read_files () {
34
- yq -sY ' [ .[] | if type == "array" then .[] else . end]' " $@ "
34
+ yq -sY ' [ .[] | if type == "array" then .[] else . end]' " $@ "
35
35
}
36
36
37
37
# assert_applied
@@ -44,7 +44,7 @@ read_files() {
44
44
# Options:
45
45
# <expected> The expected YAML string.
46
46
assert_applied () {
47
- _assert_objects_existence " apply" " $1 "
47
+ _assert_objects_existence " apply" " $1 "
48
48
}
49
49
50
50
# assert_deleted
@@ -57,7 +57,7 @@ assert_applied() {
57
57
# Options:
58
58
# <expected> The expected YAML string.
59
59
assert_deleted () {
60
- _assert_objects_existence " delete" " $1 "
60
+ _assert_objects_existence " delete" " $1 "
61
61
}
62
62
63
63
# _assert_objects_existence
@@ -80,44 +80,44 @@ assert_deleted() {
80
80
# - apply: assert that the output contains the expected object.
81
81
# - delete: assert that the output contains 'No resources found'.
82
82
_assert_objects_existence () {
83
- load_lib " bats-support"
84
-
85
- assert [ -n " $2 " ]
86
- assert [ " $( yq -r ' type' <<< " $2" ) " = " array" ]
87
-
88
- yq -c .[] <<< " $2" | while read -r object; do
89
- name=$( yq -r .metadata.name <<< " $object" )
90
- kind=$( yq -r .kind <<< " $object" )
91
- args=(" get" " ${kind,,} " " $name " ) # Converts kind to lowercase.
92
- if [[ " $kind " != " Project" ]] && [[ " $kind " != " RoleBinding" ]]; then
93
- project=$( yq -r .metadata.project <<< " $object" )
94
- args+=(-p " $project " )
95
- fi
96
-
97
- case " $1 " in
98
- apply)
99
- run_sloctl " ${args[*]} "
100
- # shellcheck disable=2154
101
- have=$( yq --sort-keys -y ' [.[] | del(.status)]' <<< " $output" )
102
- want=$( yq --sort-keys -y ' [
83
+ load_lib " bats-support"
84
+
85
+ assert [ -n " $2 " ]
86
+ assert [ " $( yq -r ' type' <<< " $2" ) " = " array" ]
87
+
88
+ yq -c .[] <<< " $2" | while read -r object; do
89
+ name=$( yq -r .metadata.name <<< " $object" )
90
+ kind=$( yq -r .kind <<< " $object" )
91
+ args=(" get" " ${kind,,} " " $name " ) # Converts kind to lowercase.
92
+ if [[ " $kind " != " Project" ]] && [[ " $kind " != " RoleBinding" ]]; then
93
+ project=$( yq -r .metadata.project <<< " $object" )
94
+ args+=(-p " $project " )
95
+ fi
96
+
97
+ case " $1 " in
98
+ apply)
99
+ run_sloctl " ${args[*]} "
100
+ # shellcheck disable=2154
101
+ have=$( yq --sort-keys -y ' [.[] | del(.status)]' <<< " $output" )
102
+ want=$( yq --sort-keys -y ' [
103
103
.[] | select(.kind == "' " $kind " ' ") |
104
104
select(.metadata.name == "' " $name " ' ") |
105
105
if .metadata.project then
106
106
select(.metadata.project == "' " $project " ' ")
107
107
else
108
108
.
109
109
end]' <<< " $2" )
110
- assert_equal " $have " " $want "
111
- ;;
112
- delete)
113
- run_sloctl " ${args[*]} "
114
- assert_output --partial " No resources found"
115
- ;;
116
- * )
117
- fail " Unknown verb '$1 '"
118
- ;;
119
- esac
120
- done
110
+ assert_equal " $have " " $want "
111
+ ;;
112
+ delete)
113
+ run_sloctl " ${args[*]} "
114
+ assert_output --partial " No resources found"
115
+ ;;
116
+ * )
117
+ fail " Unknown verb '$1 '"
118
+ ;;
119
+ esac
120
+ done
121
121
}
122
122
123
123
# generate_inputs
@@ -137,44 +137,44 @@ _assert_objects_existence() {
137
137
# them in parallel or a cleanup after the test fails for whatever reason.
138
138
# It works for both YAML and JSON files.
139
139
generate_inputs () {
140
- load_lib " bats-support"
140
+ load_lib " bats-support"
141
141
142
- directory=" $1 "
143
- test_filename=$( basename " $BATS_TEST_FILENAME " .bats)
144
- TEST_INPUTS=" $directory /$test_filename "
145
- mkdir " $TEST_INPUTS "
142
+ directory=" $1 "
143
+ test_filename=$( basename " $BATS_TEST_FILENAME " .bats)
144
+ TEST_INPUTS=" $directory /$test_filename "
145
+ mkdir " $TEST_INPUTS "
146
146
147
- test_hash=" ${BATS_TEST_NUMBER} -$( date +%s) -$SLOCTL_GIT_REVISION "
148
- TEST_PROJECT=" e2e-$test_hash "
147
+ test_hash=" ${BATS_TEST_NUMBER} -$( date +%s) -$SLOCTL_GIT_REVISION "
148
+ TEST_PROJECT=" e2e-$test_hash "
149
149
150
- files=$( find " $TEST_SUITE_INPUTS /$test_filename " -type f \( -iname \* .json -o -iname \* .yaml -o -iname \* .yml \) )
151
- for file in $files ; do
152
- pipeline='
150
+ files=$( find " $TEST_SUITE_INPUTS /$test_filename " -type f \( -iname \* .json -o -iname \* .yaml -o -iname \* .yml \) )
151
+ for file in $files ; do
152
+ pipeline='
153
153
if .kind == "Project" then
154
- .metadata.labels = {"origin": ["sloctl-e2e-tests"]}
154
+ .metadata.labels.origin = ["sloctl-e2e-tests"]
155
155
else
156
156
.
157
157
end'
158
- filter='
158
+ filter='
159
159
if type == "array" then
160
160
[.[] | ' " $pipeline " ' ]
161
161
else
162
162
' " $pipeline " '
163
163
end'
164
- new_file=" ${file/ $TEST_SUITE_INPUTS / $directory } "
165
- mkdir -p " $( dirname " $new_file " ) "
166
- sed_replace=" s/<PROJECT>/$TEST_PROJECT /g"
167
- if [[ $file =~ .* .ya? ml ]]; then
168
- yq -Y " $filter " " $file " | sed " $sed_replace " > " $new_file "
169
- elif [[ $file == * .json ]]; then
170
- jq " $filter " " $file " | sed " $sed_replace " > " $new_file "
171
- else
172
- fail " test input file: ${file} must be either YAML or JSON"
173
- fi
174
- done
175
-
176
- export TEST_INPUTS
177
- export TEST_PROJECT
164
+ new_file=" ${file/ $TEST_SUITE_INPUTS / $directory } "
165
+ mkdir -p " $( dirname " $new_file " ) "
166
+ sed_replace=" s/<PROJECT>/$TEST_PROJECT /g"
167
+ if [[ $file =~ .* .ya? ml ]]; then
168
+ yq -Y " $filter " " $file " | sed " $sed_replace " > " $new_file "
169
+ elif [[ $file == * .json ]]; then
170
+ jq " $filter " " $file " | sed " $sed_replace " > " $new_file "
171
+ else
172
+ fail " test input file: ${file} must be either YAML or JSON"
173
+ fi
174
+ done
175
+
176
+ export TEST_INPUTS
177
+ export TEST_PROJECT
178
178
}
179
179
180
180
# select_object
@@ -192,7 +192,7 @@ generate_inputs() {
192
192
# extract an object by its former name a regex match with jq 'test'
193
193
# function has to be performed.
194
194
select_object () {
195
- yq ' [if type == "array" then .[] else . end |
195
+ yq ' [if type == "array" then .[] else . end |
196
196
select(.metadata.name | test("^' " $1 " ' "))]' " $1 " " $2 "
197
197
}
198
198
@@ -208,16 +208,16 @@ select_object() {
208
208
#
209
209
# If 'yq' is provided as one of the dependencies, ensure it is coming from https://github.com/kislyuk/yq.
210
210
ensure_installed () {
211
- load_lib " bats-support"
212
-
213
- for dep in " $@ " ; do
214
- if ! command -v " $dep " > /dev/null 2>&1 ; then
215
- fail " ERROR: $dep is not installed!"
216
- fi
217
- if [ " $dep " = " yq" ] && [ " $( yq --help | grep " kislyuk/yq" ) " -eq 1 ]; then
218
- fail " ERROR: yq is not installed from https://github.com/kislyuk/yq!"
219
- fi
220
- done
211
+ load_lib " bats-support"
212
+
213
+ for dep in " $@ " ; do
214
+ if ! command -v " $dep " > /dev/null 2>&1 ; then
215
+ fail " ERROR: $dep is not installed!"
216
+ fi
217
+ if [ " $dep " = " yq" ] && [ " $( yq --help | grep " kislyuk/yq" ) " -eq 1 ]; then
218
+ fail " ERROR: yq is not installed from https://github.com/kislyuk/yq!"
219
+ fi
220
+ done
221
221
}
222
222
223
223
# load_lib
@@ -230,6 +230,6 @@ ensure_installed() {
230
230
# Options:
231
231
# <name> Name of the library to load.
232
232
load_lib () {
233
- local name=" $1 "
234
- load " /usr/lib/bats/${name} /load.bash"
233
+ local name=" $1 "
234
+ load " /usr/lib/bats/${name} /load.bash"
235
235
}
0 commit comments