Skip to content

Commit

Permalink
Add tests (#5)
Browse files Browse the repository at this point in the history
* Add tests

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>

* Create logs dir

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling authored Apr 29, 2022
1 parent c0766b8 commit b0734e4
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
_build
test/logs
*.pid
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ OTELCOL_BUILDER ?= ${OTELCOL_BUILDER_DIR}/ocb
DISTRIBUTIONS ?= "sidecar,tracing"

ci: check build
check:
check: test

build: go ocb
@./scripts/build.sh -d "${DISTRIBUTIONS}" -b ${OTELCOL_BUILDER} -g ${GO}

test: build
@./test/test-all.sh -d "${DISTRIBUTIONS}"

generate: generate-sources

generate-sources: go ocb
Expand Down
2 changes: 2 additions & 0 deletions distributions/sidecar/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dist:
output_path: ./_build
otelcol_version: 0.46.0

extensions:
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/extension/healthcheckextension v0.46.0
receivers:
- import: go.opentelemetry.io/collector/receiver/otlpreceiver
gomod: go.opentelemetry.io/collector v0.46.0
Expand Down
2 changes: 2 additions & 0 deletions distributions/tracing/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dist:
output_path: ./_build
otelcol_version: 0.46.0

extensions:
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/extension/healthcheckextension v0.46.0
receivers:
- import: go.opentelemetry.io/collector/receiver/otlpreceiver
gomod: go.opentelemetry.io/collector v0.46.0
Expand Down
13 changes: 13 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tests

The tests are a bunch of shell scripts that, for each distribution:

- prepare the environment, such as installing tools like `tracegen`
- start the distribution
- create a data point (a trace, for now)
- check whether the data point was received
- stop the distribution

In the future, we might add more scenarios to the tests.

In the output, pay attention to the general outcome of each distribution's test. While you might see warnings or errors in the logs, they are not indicative of a failure. If you see a "PASS: sidecar", it means that the tests (eventually) passed. If you do NOT see this, check the logs for clues.
2 changes: 2 additions & 0 deletions test/check-trace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash

23 changes: 23 additions & 0 deletions test/config/sidecar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extensions:
health_check:

receivers:
otlp:
protocols:
grpc:

processors:

exporters:
otlp:
endpoint: example.com:4317 # this will fail, but the process shouldn't die because of it

service:
extensions: [health_check]
pipelines:
traces:
receivers:
- otlp
processors: []
exporters:
- otlp
32 changes: 32 additions & 0 deletions test/config/tracing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
extensions:
health_check:

receivers:
otlp:
protocols:
grpc:
jaeger:
protocols:
thrift_binary:
thrift_compact:
grpc:
zipkin:

processors:

exporters:
otlp:
endpoint: example:4317 # this will fail to connect, but it shouldn't break the process

service:
extensions: [health_check]
pipelines:
traces:
receivers:
- jaeger
- otlp
- zipkin
processors: []
exporters:
- otlp

21 changes: 21 additions & 0 deletions test/generate-trace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

while getopts d:s:b:g: flag
do
case "${flag}" in
d) distribution=${OPTARG};;
esac
done
if [[ -z $distribution ]]; then
echo "Distributioon to test not provided. Use '-d' to specify the names of the distribution to test. Ex.:"
echo "$0 -d tracing"
exit 1
fi

tracegen -otlp-endpoint localhost:4317 -otlp-insecure -service e2e-test &>> ./test/logs/tracegen-${distribution}.log
if [ $? != 0 ]; then
echo "Failed to generate a trace."
exit 1
fi

echo "✅ Traces generated."
3 changes: 3 additions & 0 deletions test/install-tracegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

go install "github.com/open-telemetry/opentelemetry-collector-contrib/tracegen@v0.46.0"
52 changes: 52 additions & 0 deletions test/start-otelcol.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

while getopts d:s:b:g: flag
do
case "${flag}" in
d) distribution=${OPTARG};;
esac
done
if [[ -z $distribution ]]; then
echo "Distributioon to test not provided. Use '-d' to specify the names of the distribution to test. Ex.:"
echo "$0 -d tracing"
exit 1
fi

max_retries=50

# start the distribution
./distributions/${distribution}/_build/otelcol --config test/config/${distribution}.yaml > ./test/logs/otelcol-${distribution}.log 2>&1 &
pid=$!

retries=0
while true
do
kill -0 "${pid}" >/dev/null 2>&1
if [ $? != 0 ]; then
echo "❌ FAIL. The Grafana Labs '${distribution}' distribution of the OpenTelemetry Collector isn't running. Startup log:"
failed=true
exit 1
fi

curl -s localhost:13133 | grep "Server available" > /dev/null
if [ $? == 0 ]; then
echo "✅ The Grafana Labs '${distribution}' distribution of the OpenTelemetry Collector started."
echo "${pid}" > "otelcol-${distribution}.pid"
break
fi

echo "Server still unavailable" >> ./test/logs/test-${distribution}.log

let "retries++"
if [ "$retries" -gt "$max_retries" ]; then
echo "❌ FAIL. Server wasn't up after about 5s."

kill "${pid}"
if [ $? != 0 ]; then
echo "Failed to stop the running instance. Return code: $? . Skipping tests."
exit 8
fi
exit 16
fi
sleep 0.1s
done
27 changes: 27 additions & 0 deletions test/stop-otelcol.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

while getopts d:s:b:g: flag
do
case "${flag}" in
d) distribution=${OPTARG};;
esac
done
if [[ -z $distribution ]]; then
echo "Distributioon to test not provided. Use '-d' to specify the names of the distribution to test. Ex.:"
echo "$0 -d tracing"
exit 1
fi

pid=$(cat otelcol-${distribution}.pid)
kill "${pid}"
if [ $? != 0 ]; then
echo "Failed to stop the running instance. Return code: $? . Skipping tests."
exit 2
fi

while kill -0 ${pid}
do
sleep 0.1s
done

echo "✅ Grafana Labs '${distribution}' distribution of the OpenTelemetry Collector stopped."
21 changes: 21 additions & 0 deletions test/test-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

while getopts d:s:b:g: flag
do
case "${flag}" in
d) distributions=${OPTARG};;
esac
done

if [[ -z $distributions ]]; then
echo "List of distributions to test not provided. Use '-d' to specify the names of the distributions to test. Ex.:"
echo "$0 -d sidecar,tracing"
exit 1
fi

echo "Distributions to test: $distributions";

for distribution in $(echo "$distributions" | tr "," "\n")
do
./test/test.sh -d "${distribution}"
done
69 changes: 69 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

while getopts d:s:b:g: flag
do
case "${flag}" in
d) distribution=${OPTARG};;
esac
done
if [[ -z $distribution ]]; then
echo "Distributioon to test not provided. Use '-d' to specify the names of the distribution to test. Ex.:"
echo "$0 -d tracing"
exit 1
fi

# register the teardown function before we can use it in the trap
function teardown {
## tear down
echo "🔧 Tearing down..."
./test/stop-otelcol.sh -d ${distribution}

mkdir -p ./test/logs

echo "🪵 Grafana Labs '${distribution}' distribution of the OpenTelemetry Collector logs"
cat ./test/logs/otelcol-${distribution}.log

echo "🪵 Test logs"
cat ./test/logs/test-${distribution}.log
}

## setup
echo "🔧 Setting up..."
for st in ./test/install-tracegen.sh
do
./${st}
rc=$?
if [ $rc != 0 ]; then
exit $rc
fi
done

# from this point and on, we run the teardown before we exit
trap teardown EXIT

## test
echo "🔧 Starting Grafana Labs '${distribution}' distribution of the OpenTelemetry Collector..."
./test/start-otelcol.sh -d ${distribution}
rc=$?
if [ $rc != 0 ]; then
exit $rc
fi

## generate a trace
echo "🔧 Generating trace..."
./test/generate-trace.sh -d ${distribution}
rc=$?
if [ $rc != 0 ]; then
exit $rc
fi

## check that a trace was received
echo "🔧 Checking for existence of a trace..."
./test/check-trace.sh -d ${distribution}
rc=$?
if [ $rc != 0 ]; then
exit $rc
fi

echo "✅ PASS: '${distribution}'"
exit 0

0 comments on commit b0734e4

Please sign in to comment.