Debian Codegraph @

cmd/builder/test/test.sh

raw
1 #!/bin/bash
2 #
3 # Copyright The OpenTelemetry Authors
4 # SPDX-License-Identifier: Apache-2.0
5
6 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
7 WORKSPACE_DIR=$( cd -- "$( dirname "$(dirname "$(dirname -- "${SCRIPT_DIR}")")" )" &> /dev/null && pwd )
8 export WORKSPACE_DIR
9
10 GOBIN=$(go env GOBIN)
11 if [[ "$GO" == "" ]]; then
12 GOBIN=$(which go)
13 fi
14 export GOBIN
15
16 if [[ "$GOBIN" == "" ]]; then
17 echo "Could not determine which Go binary to use."
18 exit 1
19 fi
20
21 echo "Using ${GOBIN} to compile the distributions."
22
23 test_build_config() {
24 local test="$1"
25 local build_config="$2"
26
27 out="${base}/${test}"
28 if ! mkdir -p "${out}"; then
29 echo "❌ FAIL ${test}. Failed to create test directory for the test. Aborting tests."
30 exit 2
31 fi
32
33 echo "Starting test '${test}' at $(date)" >> "${out}/test.log"
34
35 final_build_config=$(basename "${build_config}")
36 go tool -modfile "${WORKSPACE_DIR}/internal/tools/go.mod" envsubst \
37 -o "${out}/${final_build_config}" -i <(cat "$build_config" "$replaces")
38 if ! go run . --config "${out}/${final_build_config}" --output-path "${out}" > "${out}/builder.log" 2>&1; then
39 echo "❌ FAIL ${test}. Failed to compile the test ${test}. Build logs:"
40 cat "${out}/builder.log"
41 failed=true
42 return
43 fi
44
45 if [ ! -f "${out}/${test}" ]; then
46 echo "❌ FAIL ${test}. Binary not found for ${test} at '${out}/${test}'. Build logs:"
47 cat "${out}/builder.log"
48 failed=true
49 return
50 fi
51
52 # start the distribution
53 if [ ! -f "./test/${test}.otel.yaml" ]; then
54 echo "❌ FAIL ${test}. Config file for ${test} not found at './test/${test}.otel.yaml'"
55 failed=true
56 return
57 fi
58
59 "${out}/${test}" --config "./test/${test}.otel.yaml" > "${out}/otelcol.log" 2>&1 &
60 pid=$!
61
62 retries=0
63 while true
64 do
65 if ! kill -0 "${pid}" >/dev/null 2>&1; then
66 echo "❌ FAIL ${test}. The OpenTelemetry Collector isn't running. Startup log:"
67 cat "${out}/otelcol.log"
68 failed=true
69 break
70 fi
71
72 # Since the content of the servicez page depend on which extensions are
73 # built into the collector, we depend only on the zpages extension
74 # being present and serving something.
75 if curl --fail --silent --output /dev/null http://localhost:55679/debug/servicez; then
76 echo "✅ PASS ${test}"
77
78 kill "${pid}"
79 ret=$?
80 if [ $ret -ne 0 ]; then
81 echo "Failed to stop the running instance for test ${test}. Return code: ${ret} . Skipping tests."
82 exit 4
83 fi
84 break
85 fi
86
87 echo "Server still unavailable for test '${test}'" >> "${out}/test.log"
88
89 ((retries++))
90 if [ "$retries" -gt "$max_retries" ]; then
91 echo "❌ FAIL ${test}. Server wasn't up after about 5s."
92 failed=true
93
94 kill "${pid}"
95 ret=$?
96 if [ $ret -ne 0 ]; then
97 echo "Failed to stop the running instance for test ${test}. Return code: ${ret} . Skipping tests."
98 exit 8
99 fi
100 break
101 fi
102 sleep 0.1s
103 done
104
105 echo "Stopping server for '${test}' (pid: ${pid})" >> "${out}/test.log"
106
107 }
108
109 # each attempt pauses for 100ms before retrying
110 max_retries=50
111
112 tests="core"
113
114 base=$(mktemp -d)
115 echo "Running the tests in ${base}"
116
117 replaces="$base/replaces"
118 # Get path of all core modules, in sorted order
119 core_mods=$(cd ../.. && find . -type f -name "go.mod" -exec dirname {} \; | sort)
120 echo "replaces:" >> "$replaces"
121 for mod_path in $core_mods; do
122 mod=${mod_path#"."} # remove initial dot
123 echo " - go.opentelemetry.io/collector$mod => \${WORKSPACE_DIR}$mod" >> "$replaces"
124 done
125 echo "Wrote replace statements to $replaces"
126
127 failed=false
128
129 for test in $tests
130 do
131 test_build_config "$test" "./test/${test}.builder.yaml"
132 done
133
134 if [[ "$failed" == "true" ]]; then
135 exit 1
136 fi