Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Setup CI to run tests that require external resources #191

Merged
merged 15 commits into from
Aug 10, 2020
Merged
56 changes: 55 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2
version: 2.1
jobs:
build:
docker:
Expand Down Expand Up @@ -33,3 +33,57 @@ jobs:

- store_test_results:
path: /tmp/test-results

integration:

parameters:
target:
type: string
description: "The makefile target that will run the tests for the integration."

machine:
image: ubuntu-1604:202004-01

environment:
TEST_RESULTS: /tmp/test-results # path to where test results will be saved
INTEGRATION: << parameters.target >>

steps:
- checkout

- run: mkdir -p $TEST_RESULTS

- restore_cache: # restores saved cache if no changes are detected since last run
keys:
- go-pkg-mod-{{ checksum "go.sum" }}

- run:
name: "Integration test $INTEGRATION"
command: |
make $INTEGRATION
find . -name 'coverage.html' > "${TEST_RESULTS}/coverage.lst"
tar -n -cf - -T "${TEST_RESULTS}/coverage.lst" | tar -C "${TEST_RESULTS}" -xvf -

- save_cache:
key: go-pkg-mod-{{ checksum "go.sum" }}
paths:
- "/go/pkg/mod"

- store_artifacts:
path: /tmp/test-results
destination: opentelemetry-go-contrib-test-output

- store_test_results:
path: /tmp/test-results

workflows:
version: 2.1
build_and_test:
jobs:
- build
integration_test:
jobs:
- integration:
matrix:
parameters:
target: [test-gocql, test-mongo-driver]
42 changes: 42 additions & 0 deletions .circleci/wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

wait_for_cassandra () {
for i in 1 2 3 4 5; do
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
if docker exec $1 nodetool status | grep "^UN"; then
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
exit 0
fi
echo "Cassandra not yet available"
sleep 10
done
echo "Timeout waiting for cassandra to initialize"
exit 1
}

wait_for_mongo () {
for i in 1 2 3 4 5; do
if docker exec $1 mongo; then
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
exit 0
fi
echo "Mongo not yet available..."
sleep 10
done
echo "Timeout waiting for mongo to initialize"
exit 1
}

if [ -z "$CMD" ]; then
echo "CMD is undefined. exiting..."
exit 1
elif [ -z "$IMG_NAME" ]; then
echo "IMG_NAME is undefined. exiting..."
exit 1
fi

if [ "$CMD" == "cassandra" ]; then
wait_for_cassandra $IMG_NAME
elif [ "$CMD" == "mongo" ]; then
wait_for_mongo $IMG_NAME
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
else
echo "unknown CMD"
exit 1
fi
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ test-with-coverage:
.PHONY: ci
ci: precommit check-clean-work-tree test-with-coverage test-386

.PHONY: test-gocql
test-gocql:
set -e; \
docker run --name cass-integ --rm -p 9042:9042 -d cassandra:3; \
CMD=cassandra IMG_NAME=cass-integ ./.circleci/wait.sh
(cd instrumentation/github.com/gocql/gocql && \
$(GOTEST_WITH_COVERAGE) . && \
go tool cover -html=coverage.txt -o coverage.html); \
docker stop cass-integ; \

.PHONY: test-mongo-driver
test-mongo-driver:
set -e; \
docker run --name mongo-integ --rm -p 27017:27017 -d mongo; \
CMD=mongo IMG_NAME=mongo-integ ./.circleci/wait.sh
(cd instrumentation/go.mongodb.org/mongo-driver && \
$(GOTEST_WITH_COVERAGE) . && \
go tool cover -html=coverage.txt -o coverage.html); \
docker stop mongo-integ; \

.PHONY: check-clean-work-tree
check-clean-work-tree:
@if ! git diff --quiet; then \
Expand Down
7 changes: 3 additions & 4 deletions instrumentation/github.com/gocql/gocql/gocql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"github.com/stretchr/testify/assert"

mocktracer "go.opentelemetry.io/contrib/internal/trace"
"go.opentelemetry.io/contrib/internal/util"

"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/metric"
export "go.opentelemetry.io/otel/sdk/export/metric"
Expand Down Expand Up @@ -519,10 +521,7 @@ func afterEach() {
}

func TestMain(m *testing.M) {
if _, present := os.LookupEnv("INTEGRATION"); !present {
fmt.Println("--- SKIP: to enable integration test, set the INTEGRATION environment variable")
os.Exit(0)
}
util.IntegrationShouldRun("test-gocql")
beforeAll()
os.Exit(m.Run())
}
8 changes: 2 additions & 6 deletions instrumentation/go.mongodb.org/mongo-driver/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package mongo

import (
"context"
"fmt"
"os"
"testing"
"time"

mocktracer "go.opentelemetry.io/contrib/internal/trace"
"go.opentelemetry.io/contrib/internal/util"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -31,11 +31,7 @@ import (
)

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

Expand Down
31 changes: 31 additions & 0 deletions internal/util/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"fmt"
"os"
)

func IntegrationShouldRun(name string) {
if val, ok := os.LookupEnv("INTEGRATION"); !ok || val != name {
fmt.Println(
"--- SKIP: to enable integration test, set the INTEGRATION environment variable",
"to",
fmt.Sprintf("\"%s\"", name),
)
os.Exit(0)
}
}