Skip to content

Commit 526c8aa

Browse files
authored
Introduce shell linter (#360)
Add .editorconfig for `shfmt`. Update fmt task to execute `shfmt` for scripts folder. Add github action to validate syntaxis of bash scripts. Make sure there are 80 chars max per line in bash files.
1 parent 009a3d0 commit 526c8aa

File tree

5 files changed

+136
-46
lines changed

5 files changed

+136
-46
lines changed

.editorconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# Unix-style newlines with a newline ending every file
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
max_line_length = 100
10+
11+
[Makefile]
12+
indent_style = tab
13+
14+
[scripts/*, *.sh]
15+
max_line_length = 80
16+
indent_style = space
17+
indent_size = 2
18+
shell_variant = bash
19+
binary_next_line = true
20+
switch_case_indent = true
21+
space_redirects = true
22+
keep_padding = true

.github/workflows/analysis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ jobs:
2525
with:
2626
version: v1.42.1
2727

28+
-
29+
name: shellcheck
30+
uses: azohra/shell-linter@6bbeaa868df09c34ddc008e6030cfe89c03394a1
31+
with:
32+
path: scripts/*
33+
severity: error
34+
2835

2936
vulnerabilities:
3037
runs-on: ubuntu-latest

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ fmt:
3131
go fmt ./...
3232
goimports -w **/*.go
3333
golangci-lint run --fix
34+
shfmt -l -s -w -kp -i 2 scripts/* Dockerfile Makefile
3435

3536
.PHONY: lint
3637
lint:
3738
golangci-lint run
39+
shellcheck scripts/*
40+
shfmt -l -s -d -kp -i 2 scripts/*
3841

3942
.PHONY: build
4043
build: dist clean

scripts/test-e2e

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
set -ueo pipefail
44

55
wait_for_url() {
6-
echo "--- Waiting for HTTP connection available"
7-
timeout -s TERM 30s bash -c \
8-
'while [[ "$(curl -s -o /dev/null -L -w ''%{http_code}'' ${0})" != "200" ]]; do \
9-
echo "Waiting for ${0}" && sleep 2; \
10-
done' ${1}
11-
curl -s -I -X GET $1
6+
curl -s --retry-connrefused --retry 5 --retry-delay 2 --retry-max-time 30 \
7+
--max-time 1 -L -I -X GET "${1}"
128
}
139

14-
echo "== Setup"
15-
1610
# Stop all background jobs on exit
17-
trap 'pkill -15 -f "dist/toxiproxy-server$"; pkill -15 -f "exe/endpoint$"' EXIT SIGINT SIGTERM
11+
trap 'pkill -15 -f "dist/toxiproxy-server$"; pkill -15 -f "exe/endpoint$"' \
12+
EXIT SIGINT SIGTERM
1813

1914
echo "=== Starting Web service"
2015

@@ -31,7 +26,8 @@ wait_for_url http://localhost:8474/version
3126

3227
echo "=== Test client to manipulate proxy"
3328

34-
./dist/toxiproxy-cli -h http://localhost:8474 create -l localhost:20000 -u localhost:20002 shopify_http
29+
./dist/toxiproxy-cli -h http://localhost:8474 \
30+
create -l localhost:20000 -u localhost:20002 shopify_http
3531
./dist/toxiproxy-cli list
3632
./dist/toxiproxy-cli toggle shopify_http
3733
./dist/toxiproxy-cli inspect shopify_http
@@ -50,76 +46,103 @@ echo -e "-----------------\n"
5046

5147
echo "=== Latency toxic downstream"
5248

53-
./dist/toxiproxy-cli toxic add --downstream --type latency --toxicName "latency_downstream" \
54-
--attribute "latency=1000" --attribute="jitter=50" \
55-
--toxicity 0.99 shopify_http
49+
./dist/toxiproxy-cli toxic add --downstream \
50+
--type=latency \
51+
--toxicName="latency_downstream" \
52+
--attribute="latency=1000" \
53+
--attribute="jitter=50" \
54+
--toxicity=0.99 \
55+
shopify_http
5656
./dist/toxiproxy-cli inspect shopify_http
5757

5858
go test -bench=. ./test/e2e -v
5959

60-
./dist/toxiproxy-cli toxic update --toxicName "latency_downstream" --attribute="jitter=20" \
61-
--toxicity 0.7 shopify_http
60+
./dist/toxiproxy-cli toxic update --toxicName="latency_downstream" \
61+
--attribute="jitter=20" \
62+
--toxicity=0.7 \
63+
shopify_http
6264
./dist/toxiproxy-cli inspect shopify_http
6365

64-
./dist/toxiproxy-cli toxic delete --toxicName "latency_downstream" shopify_http
66+
./dist/toxiproxy-cli toxic delete --toxicName="latency_downstream" shopify_http
6567

6668
echo -e "-----------------\n"
6769

6870
echo "=== Latency toxic upstream"
6971

70-
./dist/toxiproxy-cli toxic add --upstream --type latency --toxicName "latency_upstream" \
71-
--attribute "latency=1000" --attribute "jitter=50" \
72-
--toxicity 1 shopify_http
72+
./dist/toxiproxy-cli toxic add --upstream \
73+
--type=latency \
74+
--toxicName="latency_upstream" \
75+
--attribute="latency=1000" \
76+
--attribute="jitter=50" \
77+
--toxicity=1 \
78+
shopify_http
7379
./dist/toxiproxy-cli inspect shopify_http
7480

7581
go test -bench=. ./test/e2e -v
7682

77-
./dist/toxiproxy-cli toxic update --toxicName "latency_upstream" --attribute="jitter=20" \
78-
--toxicity 0.3 shopify_http
83+
./dist/toxiproxy-cli toxic update --toxicName="latency_upstream" \
84+
--attribute="jitter=20" \
85+
--toxicity=0.3 \
86+
shopify_http
7987
./dist/toxiproxy-cli inspect shopify_http
8088

81-
./dist/toxiproxy-cli toxic delete --toxicName "latency_upstream" shopify_http
89+
./dist/toxiproxy-cli toxic delete --toxicName="latency_upstream" shopify_http
8290

8391
echo -e "-----------------\n"
8492

8593
echo "=== Bandwidth toxic"
8694

87-
./dist/toxiproxy-cli toxic add --type bandwidth --toxicName "bandwidth_kb_per_second" \
88-
--attribute "rate=1" --toxicity 0.5 shopify_http
89-
./dist/toxiproxy-cli toxic update --toxicName "bandwidth_kb_per_second" --attribute="rate=10" \
90-
--toxicity 1.0 shopify_http
95+
./dist/toxiproxy-cli toxic add --type=bandwidth \
96+
--toxicName="bandwidth_kb_per_second" \
97+
--attribute="rate=1" \
98+
--toxicity=0.5 \
99+
shopify_http
100+
./dist/toxiproxy-cli toxic update --toxicName="bandwidth_kb_per_second" \
101+
--attribute="rate=10" \
102+
--toxicity=1.0 \
103+
shopify_http
91104

92105
go test -bench=. ./test/e2e -v
93106

94-
./dist/toxiproxy-cli toxic delete --toxicName "bandwidth_kb_per_second" shopify_http
107+
./dist/toxiproxy-cli toxic delete --toxicName="bandwidth_kb_per_second" \
108+
shopify_http
95109

96110
echo -e "-----------------\n"
97111

98112
echo "=== Timeout toxic"
99113

100-
./dist/toxiproxy-cli toxic add --type timeout --toxicName "timeout_ms" \
101-
--attribute "timeout=10" --toxicity 0.1 shopify_http
102-
./dist/toxiproxy-cli toxic delete --toxicName "timeout_ms" shopify_http
114+
./dist/toxiproxy-cli toxic add --type=timeout \
115+
--toxicName="timeout_ms" \
116+
--attribute="timeout=10" \
117+
--toxicity=0.1 \
118+
shopify_http
119+
./dist/toxiproxy-cli toxic delete --toxicName="timeout_ms" shopify_http
103120

104121
echo -e "-----------------\n"
105122

106123
echo "=== Slicer toxic"
107124

108-
./dist/toxiproxy-cli toxic add --type slicer --toxicName "slicer_us" \
109-
--attribute "average_size=64" --attribute "size_variation=32" \
110-
--attribute="delay=10" --toxicity 1.0 shopify_http
125+
./dist/toxiproxy-cli toxic add --type=slicer \
126+
--toxicName="slicer_us" \
127+
--attribute="average_size=64" \
128+
--attribute="size_variation=32" \
129+
--attribute="delay=10" \
130+
--toxicity=1.0 \
131+
shopify_http
111132
go test -bench=. ./test/e2e -v
112-
./dist/toxiproxy-cli toxic delete --toxicName "slicer_us" shopify_http
133+
./dist/toxiproxy-cli toxic delete --toxicName="slicer_us" shopify_http
113134

114135
echo -e "-----------------\n"
115136

116137
echo "=== Reset peer toxic"
117138

118-
./dist/toxiproxy-cli toxic add --type reset_peer --toxicName "reset_peer" \
119-
--attribute "timeout=2000" \
120-
--toxicity 1.0 shopify_http
139+
./dist/toxiproxy-cli toxic add --type=reset_peer \
140+
--toxicName="reset_peer" \
141+
--attribute="timeout=2000" \
142+
--toxicity=1.0 \
143+
shopify_http
121144
./dist/toxiproxy-cli inspect shopify_http
122-
./dist/toxiproxy-cli toxic delete --toxicName "reset_peer" shopify_http
145+
./dist/toxiproxy-cli toxic delete --toxicName="reset_peer" shopify_http
123146

124147
echo -e "-----------------\n"
125148

scripts/test-release

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,57 @@ set -ueo pipefail
55
VERSION_FULL="$(git describe --abbrev=0 --tags)"
66
VERSION=${VERSION_FULL:1}
77
ARCH=$(uname -m)
8-
if [ $ARCH == "x86_64" ]; then
8+
if [ "${ARCH}" == "x86_64" ]; then
99
ARCH="amd64"
1010
fi
1111

1212
goreleaser release --rm-dist --skip-publish --skip-validate
13-
docker run -v $(PWD)/dist:/dist --pull always --rm -it ubuntu bash -c "set -xe; dpkg -i /dist/toxiproxy_*_linux_${ARCH}.deb; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-server --version ; /usr/bin/toxiproxy-cli --version ; /usr/bin/toxiproxy-cli --version | grep -o -e 'toxiproxy-cli version ${VERSION}'"
14-
docker run -v $(PWD)/dist:/dist --pull always --rm -it centos bash -c "set -xe; yum install -y /dist/toxiproxy_*_linux_${ARCH}.rpm; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-server --version ; /usr/bin/toxiproxy-cli --version | grep -o -e \"toxiproxy-cli version ${VERSION}\""
15-
docker run -v $(PWD)/dist:/dist --pull always --rm -it alpine sh -c "set -xe; apk add --allow-untrusted --no-cache /dist/toxiproxy_*_linux_${ARCH}.apk; ls -1 /usr/bin/toxiproxy-*; /usr/bin/toxiproxy-server --version ; /usr/bin/toxiproxy-cli --version | grep -o -e \"toxiproxy-cli version ${VERSION}\""
13+
14+
docker run -v "$(PWD)"/dist:/dist --pull always --rm -it ubuntu bash -c \
15+
"set -xe;
16+
dpkg -i /dist/toxiproxy_*_linux_${ARCH}.deb;
17+
ls -1 /usr/bin/toxiproxy-*;
18+
/usr/bin/toxiproxy-server --version;
19+
/usr/bin/toxiproxy-server --version \
20+
| grep -o -e 'toxiproxy-server version ${VERSION}';
21+
/usr/bin/toxiproxy-cli --version;
22+
/usr/bin/toxiproxy-cli --version \
23+
| grep -o -e 'toxiproxy-cli version ${VERSION}'"
24+
25+
docker run -v "$(PWD)"/dist:/dist --pull always --rm -it centos bash -c \
26+
"set -xe;
27+
yum install -y /dist/toxiproxy_*_linux_${ARCH}.rpm;
28+
ls -1 /usr/bin/toxiproxy-*;
29+
/usr/bin/toxiproxy-server --version;
30+
/usr/bin/toxiproxy-server --version \
31+
| grep -o -e 'toxiproxy-server version ${VERSION}';
32+
/usr/bin/toxiproxy-cli --version;
33+
/usr/bin/toxiproxy-cli --version \
34+
| grep -o -e 'toxiproxy-cli version ${VERSION}'"
35+
36+
docker run -v "$(PWD)"/dist:/dist --pull always --rm -it alpine sh -c \
37+
"set -xe;
38+
apk add --allow-untrusted --no-cache /dist/toxiproxy_*_linux_${ARCH}.apk;
39+
ls -1 /usr/bin/toxiproxy-*;
40+
/usr/bin/toxiproxy-server --version;
41+
/usr/bin/toxiproxy-server --version \
42+
| grep -o -e 'toxiproxy-server version ${VERSION}';
43+
/usr/bin/toxiproxy-cli --version;
44+
/usr/bin/toxiproxy-cli --version \
45+
| grep -o -e 'toxiproxy-cli version ${VERSION}'"
1646

1747
tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep -o -e toxiproxy-server
1848
tar -ztvf dist/toxiproxy_*_linux_amd64.tar.gz | grep -o -e toxiproxy-cli
1949

2050
goreleaser build --rm-dist --single-target --skip-validate --id server
21-
./dist/toxiproxy-server-* --help 2>&1 | grep -o -e "Usage of ./dist/toxiproxy-server"
22-
./dist/toxiproxy-server-* --version | grep -o -e "toxiproxy-server version ${VERSION}"
51+
bineries=(./dist/toxiproxy-server-*)
52+
server="${bineries[0]}"
53+
$server --help 2>&1 | grep -o -e "Usage of ./dist/toxiproxy-server"
54+
$server --version | grep -o -e "toxiproxy-server version ${VERSION}"
2355

2456
goreleaser build --rm-dist --single-target --skip-validate --id client
25-
./dist/toxiproxy-cli-* --help 2>&1 | grep -o -e "toxiproxy-cli - Simulate network and system conditions"
26-
./dist/toxiproxy-cli-* --version | grep -o -e "toxiproxy-cli version ${VERSION}"
57+
bineries=(./dist/toxiproxy-cli-*)
58+
cli="${bineries[0]}"
59+
$cli --help 2>&1 \
60+
| grep -o -e "toxiproxy-cli - Simulate network and system conditions"
61+
$cli --version | grep -o -e "toxiproxy-cli version ${VERSION}"

0 commit comments

Comments
 (0)