Skip to content

Commit c318cd3

Browse files
feat: Setup CI to run tests that require external resources (#191)
Co-authored-by: Tyler Yahn <[email protected]>
1 parent 6585cf1 commit c318cd3

File tree

7 files changed

+200
-11
lines changed

7 files changed

+200
-11
lines changed

.circleci/config.yml

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 2
1+
version: 2.1
22
jobs:
33
build:
44
docker:
@@ -33,3 +33,48 @@ jobs:
3333

3434
- store_test_results:
3535
path: /tmp/test-results
36+
37+
integration:
38+
39+
parameters:
40+
target:
41+
type: string
42+
description: "The makefile target that will run the tests for the integration."
43+
44+
machine:
45+
image: ubuntu-1604:202004-01
46+
47+
environment:
48+
TEST_RESULTS: /tmp/test-results # path to where test results will be saved
49+
INTEGRATION: << parameters.target >>
50+
51+
steps:
52+
- checkout
53+
54+
- run: mkdir -p $TEST_RESULTS
55+
56+
- run:
57+
name: "Integration test $INTEGRATION"
58+
command: |
59+
make $INTEGRATION
60+
find . -name 'coverage.html' > "${TEST_RESULTS}/coverage.lst"
61+
tar -n -cf - -T "${TEST_RESULTS}/coverage.lst" | tar -C "${TEST_RESULTS}" -xvf -
62+
63+
- store_artifacts:
64+
path: /tmp/test-results
65+
destination: opentelemetry-go-contrib-test-output
66+
67+
- store_test_results:
68+
path: /tmp/test-results
69+
70+
workflows:
71+
version: 2.1
72+
build_and_test:
73+
jobs:
74+
- build
75+
integration_test:
76+
jobs:
77+
- integration:
78+
matrix:
79+
parameters:
80+
target: [test-gocql, test-mongo-driver]

.circleci/should_build.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Returns 0 (true) when the current diff contains files in the provided
18+
# target directory. TARGET should be a unique package name in the directory
19+
# structure. For example, for the gocql integration, set TARGET=gocql so that
20+
# a diff in any of the files in the instrumentation/gocql/gocql directory
21+
# will be picked up by the grep. Diffs are compared against the master branch.
22+
23+
TARGET=$1
24+
25+
if [ -z "$TARGET" ]; then
26+
echo "TARGET is undefined"
27+
exit 1
28+
fi
29+
30+
if git diff --name-only origin/master HEAD | grep -q "$TARGET"; then
31+
exit 0
32+
else
33+
echo "no changes found for $TARGET. skipping tests..."
34+
exit 1
35+
fi
36+
37+
38+

.circleci/wait.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
wait_for_cassandra () {
18+
for ((i = 0; i < 5; ++i)); do
19+
if docker exec "$1" nodetool status | grep "^UN"; then
20+
exit 0
21+
fi
22+
echo "Cassandra not yet available"
23+
sleep 10
24+
done
25+
echo "Timeout waiting for cassandra to initialize"
26+
exit 1
27+
}
28+
29+
wait_for_mongo () {
30+
for ((i = 0; i < 5; ++i)); do
31+
if docker exec "$1" mongo; then
32+
exit 0
33+
fi
34+
echo "Mongo not yet available..."
35+
sleep 10
36+
done
37+
echo "Timeout waiting for mongo to initialize"
38+
exit 1
39+
}
40+
41+
if [ -z "$CMD" ]; then
42+
echo "CMD is undefined. exiting..."
43+
exit 1
44+
elif [ -z "$IMG_NAME" ]; then
45+
echo "IMG_NAME is undefined. exiting..."
46+
exit 1
47+
fi
48+
49+
if [ "$CMD" == "cassandra" ]; then
50+
wait_for_cassandra "$IMG_NAME"
51+
elif [ "$CMD" == "mongo" ]; then
52+
wait_for_mongo "$IMG_NAME"
53+
else
54+
echo "unknown CMD"
55+
exit 1
56+
fi

Makefile

+24
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ test-with-coverage:
5454
.PHONY: ci
5555
ci: precommit check-clean-work-tree test-with-coverage test-386
5656

57+
.PHONY: test-gocql
58+
test-gocql:
59+
@if ./.circleci/should_build.sh gocql; then \
60+
set -e; \
61+
docker run --name cass-integ --rm -p 9042:9042 -d cassandra:3; \
62+
CMD=cassandra IMG_NAME=cass-integ ./.circleci/wait.sh; \
63+
(cd instrumentation/github.com/gocql/gocql && \
64+
$(GOTEST_WITH_COVERAGE) . && \
65+
go tool cover -html=coverage.txt -o coverage.html); \
66+
docker stop cass-integ; \
67+
fi
68+
69+
.PHONY: test-mongo-driver
70+
test-mongo-driver:
71+
@if ./.circleci/should_build.sh mongo-driver; then \
72+
set -e; \
73+
docker run --name mongo-integ --rm -p 27017:27017 -d mongo; \
74+
CMD=mongo IMG_NAME=mongo-integ ./.circleci/wait.sh; \
75+
(cd instrumentation/go.mongodb.org/mongo-driver && \
76+
$(GOTEST_WITH_COVERAGE) . && \
77+
go tool cover -html=coverage.txt -o coverage.html); \
78+
docker stop mongo-integ; \
79+
fi
80+
5781
.PHONY: check-clean-work-tree
5882
check-clean-work-tree:
5983
@if ! git diff --quiet; then \

instrumentation/github.com/gocql/gocql/gocql_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
"github.com/stretchr/testify/assert"
3636

3737
mocktracer "go.opentelemetry.io/contrib/internal/trace"
38+
"go.opentelemetry.io/contrib/internal/util"
39+
3840
"go.opentelemetry.io/otel/api/kv"
3941
"go.opentelemetry.io/otel/api/metric"
4042
export "go.opentelemetry.io/otel/sdk/export/metric"
@@ -519,10 +521,7 @@ func afterEach() {
519521
}
520522

521523
func TestMain(m *testing.M) {
522-
if _, present := os.LookupEnv("INTEGRATION"); !present {
523-
fmt.Println("--- SKIP: to enable integration test, set the INTEGRATION environment variable")
524-
os.Exit(0)
525-
}
524+
util.IntegrationShouldRun("test-gocql")
526525
beforeAll()
527526
os.Exit(m.Run())
528527
}

instrumentation/go.mongodb.org/mongo-driver/mongo_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ package mongo
1616

1717
import (
1818
"context"
19-
"fmt"
2019
"os"
2120
"testing"
2221
"time"
2322

2423
mocktracer "go.opentelemetry.io/contrib/internal/trace"
24+
"go.opentelemetry.io/contrib/internal/util"
2525

2626
"go.mongodb.org/mongo-driver/bson"
2727
"go.mongodb.org/mongo-driver/mongo"
@@ -31,11 +31,7 @@ import (
3131
)
3232

3333
func TestMain(m *testing.M) {
34-
_, ok := os.LookupEnv("INTEGRATION")
35-
if !ok {
36-
fmt.Println("--- SKIP: to enable integration test, set the INTEGRATION environment variable")
37-
os.Exit(0)
38-
}
34+
util.IntegrationShouldRun("test-mongo-driver")
3935
os.Exit(m.Run())
4036
}
4137

internal/util/testutil.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import (
18+
"fmt"
19+
"os"
20+
)
21+
22+
func IntegrationShouldRun(name string) {
23+
if val, ok := os.LookupEnv("INTEGRATION"); !ok || val != name {
24+
fmt.Println(
25+
"--- SKIP: to enable integration test, set the INTEGRATION environment variable",
26+
"to",
27+
fmt.Sprintf("\"%s\"", name),
28+
)
29+
os.Exit(0)
30+
}
31+
}

0 commit comments

Comments
 (0)