forked from cloudfoundry/log-cache-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest
executable file
·253 lines (215 loc) · 6.4 KB
/
test
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
#!/bin/bash
trap "echo Exited!; exit 1;" SIGINT SIGTERM
PROJECT_DIR="$(cd "$(dirname "$0")/.."; pwd)"
function print_usage {
echo "usage: test [subcommand] [go test args]"
echo
echo -e "\033[1mSubcommands:\033[0m"
echo " all Run all the tests, excluding linters (default)"
echo " cleaners Run tools that clean the code base"
echo " unit Run the unit tests"
echo " linters Run common linters against the project"
echo " ops_files Run tests for operation files"
echo " install_tools Install all necessary binaries for these scripts"
}
function print_checkpoint {
echo
bold_blue "================================== $@"
}
function green {
echo -e "\e[32m$1\e[0m"
}
function red {
echo -e "\e[31m$1\e[0m"
}
function bold_blue {
echo -e "\e[1;34m$1\e[0m"
}
function check_output {
eval "$@"
local status=$?
exit_on_failure $status
}
function exit_on_failure {
if [[ $1 -ne 0 ]]; then
red "SUITE FAILURE"
exit $1
fi
}
function run_cleaners {
print_checkpoint "Running Cleaners"
go get github.com/kisielk/gotool
if ! which goimports > /dev/null 2>&1; then
echo installing goimports
go get golang.org/x/tools/cmd/goimports
fi
if ! which misspell > /dev/null 2>&1; then
echo installing misspell
go get github.com/client9/misspell/cmd/misspell
fi
if ! which unconvert > /dev/null 2>&1; then
echo installing unconvert
go get github.com/mdempsky/unconvert
fi
local log_cache_pkg="code.cloudfoundry.org/log-cache"
local log_cache_dir="$(dirname $(dirname $0))/src/$log_cache_pkg"
echo running goimports
goimports -w "$log_cache_dir"
echo running gofmt
gofmt -s -w "$log_cache_dir"
echo running misspell
misspell -w "$log_cache_dir"
echo running unconvert
unconvert -v -apply "$log_cache_pkg/..."
echo running go vet
go vet "$log_cache_pkg/..."
return 0
}
function run_unit {
pushd $PROJECT_DIR/src > /dev/null
TEST_OPTIONS="-ginkgo.randomizeAllSpecs -ginkgo.slowSpecThreshold 20"
# The race detector dramatically increases the runtime of certain tests
# As a result, we first run the tests with race detection
# enabled, but only consider actual data races as failures. We then run the test suite
# without race detection and assert on test correctness.
print_checkpoint "Checking for Data Races"
race_log=$(mktemp log_cache_race_log.XXXXXXXX --tmpdir)
go test -mod=vendor -race ./... \
${TEST_OPTIONS} \
$@ 2>&1 > ${race_log} || true
if egrep 'DATA RACE|race detected' ${race_log}; then
cat ${race_log}
rm ${race_log}
return 1
fi
rm ${race_log}
echo "No races found"
print_checkpoint "Running Unit Tests"
go test ./... \
${TEST_OPTIONS} \
$@
exit_code=$?
popd > /dev/null
return $exit_code
}
function run_all {
check_output run_cleaners
check_output run_unit $@
}
function parse_argc {
command=run_all
if [[ $# -eq 0 ]]; then
return
fi
arg=$1
case "$arg" in
-h|-help|--help|help)
print_usage
exit 0
;;
all|unit|cleaners|linters|install_tools|ops_files)
command=run_$arg
;;
*)
echo "Invalid command: $arg\n"
print_usage
exit 1
;;
esac
}
function run_install_tools {
print_checkpoint "Installing Tools"
# testing
go get github.com/onsi/ginkgo/ginkgo
# cleaners
go get golang.org/x/tools/cmd/goimports
go get github.com/client9/misspell/cmd/misspell
go get github.com/mdempsky/unconvert
# linters
go get github.com/tsenart/deadcode
go get github.com/golang/lint/golint
go get github.com/opennota/check/cmd/aligncheck
go get github.com/opennota/check/cmd/structcheck
go get github.com/opennota/check/cmd/varcheck
go get github.com/kisielk/errcheck
go get github.com/gordonklaus/ineffassign
go get mvdan.cc/interfacer
go get honnef.co/go/tools/cmd/megacheck
}
function run_linters {
print_checkpoint "Running Linters"
local log_cache_pkg
if [ "$1" = "" ]; then
log_cache_pkg="code.cloudfoundry.org/log-cache"
else
log_cache_pkg="$1"
fi
local log_cache_dir="$(dirname $(dirname $0))/src/$log_cache_pkg"
echo running go vet
go vet "$log_cache_pkg/..."
echo running deadcode
deadcode $dir
echo running golint
golint "$log_cache_pkg/..."
echo running aligncheck
aligncheck "$log_cache_pkg/..."
echo running structcheck
structcheck "$log_cache_pkg/..."
echo running varcheck
varcheck "$log_cache_pkg/..."
echo running errcheck
errcheck -ignore '[cC]lose' "$log_cache_pkg/..."
echo running ineffassign
ineffassign "$log_cache_dir"
echo running interfacer
interfacer "$log_cache_pkg/..."
echo running megacheck
megacheck "$log_cache_pkg/..."
return 0
}
function run_ops_files {
print_checkpoint "Running Operation File Tests"
pushd $PROJECT_DIR > /dev/null
echo creating deploy-in-cf.yml ops file
tmp_dir=`mktemp -d`
cat <<EOF > $tmp_dir/consumes_cf.yml
- type: replace
path: /instance_groups/name=log-cache/jobs/name=log-cache/consumes/reverse_log_proxy/deployment?
value: cf
EOF
bosh int manifests/log-cache.yml --ops-file=$tmp_dir/consumes_cf.yml > $tmp_dir/adjusted.yml
ops_file_path=manifests/operations/deploy-in-cf.yml
instance_groups=`bosh int $tmp_dir/adjusted.yml --path /instance_groups | sed 's/^\-/ /' | sed 's/^/ /'`
variables=`bosh int $tmp_dir/adjusted.yml --path /variables | sed 's/^\-/ /' | sed 's/^/ /'`
cat <<EOF > manifests/operations/deploy-in-cf.yml
- type: replace
path: /releases/-
value:
name: log-cache
version: latest
- type: replace
path: /instance_groups/-
value:
$instance_groups
- type: replace
path: /variables/-
value:
$variables
EOF
popd > /dev/null
return 0
}
function setup_env {
export PATH="$PROJECT_DIR/bin:$PATH"
export GORACE="halt_on_error=1"
}
function main {
setup_env
parse_argc $1
shift
"$command" $@
result=$?
exit_on_failure $result
green "SWEET SUITE SUCCESS"
}
main $@