-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·183 lines (148 loc) · 4.51 KB
/
test.sh
File metadata and controls
executable file
·183 lines (148 loc) · 4.51 KB
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
#!/usr/bin/env bash
set -ue
THIS_PATH="$(realpath "$(dirname "$0")")"
ROOT_PATH="$(realpath "${THIS_PATH}/../../../..")"
# paths relative to $ROOT_PATH
SCHEMA_DIR='schema'
TEST_RES_DIR='tools/src/test/resources'
REMOTE="https://github.com/${GITHUB_REPOSITORY:-CycloneDX/specification}.git"
BUF_IMAGE_VERSION='1.50.0'
BUF_IMAGE="bufbuild/buf:$BUF_IMAGE_VERSION"
## ----
function prepare () {
docker pull "$BUF_IMAGE"
}
function schema-lint () {
echo '> lint schema files' >&2
if [[ -n "${GITHUB_WORKFLOW:-}" ]]
then
LOG_FORMAT='github-actions'
else
LOG_FORMAT='text'
fi
docker run --rm \
--volume "${ROOT_PATH}/${SCHEMA_DIR}:/workspace/${SCHEMA_DIR}:ro" \
--volume "${THIS_PATH}/buf_lint.yaml:/workspace/buf.yaml:ro" \
--workdir '/workspace' \
"$BUF_IMAGE" \
lint --path "$SCHEMA_DIR" \
--error-format "$LOG_FORMAT"
echo '>> OK.' >&2
}
function schema-breaking-version () {
echo '> test schema for breaking changes against previous version' >&2
if [[ -n "${GITHUB_WORKFLOW:-}" ]]
then
LOG_FORMAT='github-actions'
else
LOG_FORMAT='text'
fi
function compare() {
NEW="bom-${1}.proto"
OLD="bom-${2}.proto"
NEW_NP="$(mktemp)"
OLD_NP="$(mktemp)"
# remove package identifier -> so that the comparisson works as expected
sed 's/^package .*//' "${ROOT_PATH}/${SCHEMA_DIR}/${NEW}" > "$NEW_NP"
sed 's/^package .*//' "${ROOT_PATH}/${SCHEMA_DIR}/${OLD}" > "$OLD_NP"
echo ">> compare new:${NEW} -VS- old:${OLD}" >&2
# stick with the original path and name of "$NEW", so the reporting makes sense...
docker run --rm \
--volume "${OLD_NP}:/workspaces/old/${SCHEMA_DIR}/${NEW}:ro" \
--volume "${NEW_NP}:/workspaces/new/${SCHEMA_DIR}/${NEW}:ro" \
--volume "${THIS_PATH}/buf_breaking-version.yaml:/workspaces/new/buf.yaml:ro" \
--workdir '/workspaces/new' \
"$BUF_IMAGE" \
breaking \
--against ../old \
--error-format "$LOG_FORMAT"
}
# compare '1.6' '1.5' # <-- possible breaks are acknowledged
# compare '1.5' '1.4' # <-- possible breaks are acknowledged
compare '1.4' '1.3'
echo '>> OK.' >&2
}
function schema-breaking-remote () {
echo '> test schema for breaking changes against remote' >&2
if [[ -n "${GITHUB_WORKFLOW:-}" ]]
then
LOG_FORMAT='github-actions'
else
LOG_FORMAT='text'
fi
docker run --rm \
--volume "${ROOT_PATH}/${SCHEMA_DIR}:/workspace/${SCHEMA_DIR}:ro" \
--volume "${THIS_PATH}/buf_breaking-remote.yaml:/workspace/buf.yaml:ro" \
--workdir '/workspace' \
"$BUF_IMAGE" \
breaking --path "$SCHEMA_DIR" \
--against "${REMOTE}" \
--error-format "$LOG_FORMAT"
echo '>> OK.' >&2
}
function schema-functional () {
echo '> test all examples against the respective schema' >&2
function validate() {
FILE="$1"
SCHEMA_VERS="$2"
SCHEMA_FILE="bom-${SCHEMA_VERS}.proto"
MESSAGE="cyclonedx.v${SCHEMA_VERS/./_}.Bom"
echo ">> validate $(realpath --relative-to="$PWD" "$FILE") as ${MESSAGE} of ${SCHEMA_FILE}" >&2
# this test method is a bare minimum, and it might not detect all kinds of malformed input.
# could be improved by utilizing protoc -- see https://github.com/CycloneDX/specification/pull/385/commits/8db0967c11cb913ac3c7a9a037159338df3f3bd9
docker run --rm \
--volume "${ROOT_PATH}/${SCHEMA_DIR}:/workspace/${SCHEMA_DIR}:ro" \
--volume "${FILE}:/workspace/test_res:ro" \
--workdir '/workspace' \
"$BUF_IMAGE" \
convert "${SCHEMA_DIR}/${SCHEMA_FILE}" \
--type "$MESSAGE" \
--from 'test_res#format=txtpb' \
--to /dev/null
}
shopt -s globstar
for test_res in "$ROOT_PATH"/"$TEST_RES_DIR"/*/valid-*.textproto
do
SCHEMA_VERS="$(basename "$(dirname "$test_res")")"
validate "$test_res" "$SCHEMA_VERS"
done
echo '>> OK.' >&2
}
## ----
case "${1:-all}" in
'schema-lint')
prepare
schema-lint
;;
'schema-breaking-version')
prepare
schema-breaking-version
;;
'schema-breaking-remote')
prepare
schema-breaking-remote
;;
'schema-breaking')
prepare
schema-breaking-version
schema-breaking-remote
;;
'schema-functional')
prepare
schema-functional
;;
'all')
# all the above
prepare
schema-lint
schema-breaking-version
schema-breaking-remote
schema-functional
;;
*)
echo 'unexpected argument. known arguments:' \
'schema-lint,schema-breaking-version,schema-breaking-remote,schema-breaking,schema-functional,all' \
>&2
exit 1
;;
esac