Skip to content

Commit

Permalink
feat: Setup CI to run tests that require external resources (#191)
Browse files Browse the repository at this point in the history
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
reggiemcdonald and MrAlias authored Aug 10, 2020
1 parent 6585cf1 commit c318cd3
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 11 deletions.
47 changes: 46 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,48 @@ 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

- 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 -
- 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]
38 changes: 38 additions & 0 deletions .circleci/should_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# 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.

# Returns 0 (true) when the current diff contains files in the provided
# target directory. TARGET should be a unique package name in the directory
# structure. For example, for the gocql integration, set TARGET=gocql so that
# a diff in any of the files in the instrumentation/gocql/gocql directory
# will be picked up by the grep. Diffs are compared against the master branch.

TARGET=$1

if [ -z "$TARGET" ]; then
echo "TARGET is undefined"
exit 1
fi

if git diff --name-only origin/master HEAD | grep -q "$TARGET"; then
exit 0
else
echo "no changes found for $TARGET. skipping tests..."
exit 1
fi



56 changes: 56 additions & 0 deletions .circleci/wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

# 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.

wait_for_cassandra () {
for ((i = 0; i < 5; ++i)); do
if docker exec "$1" nodetool status | grep "^UN"; then
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 = 0; i < 5; ++i)); do
if docker exec "$1" mongo; then
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"
else
echo "unknown CMD"
exit 1
fi
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ test-with-coverage:
.PHONY: ci
ci: precommit check-clean-work-tree test-with-coverage test-386

.PHONY: test-gocql
test-gocql:
@if ./.circleci/should_build.sh gocql; then \
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; \
fi

.PHONY: test-mongo-driver
test-mongo-driver:
@if ./.circleci/should_build.sh mongo-driver; then \
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; \
fi

.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)
}
}

0 comments on commit c318cd3

Please sign in to comment.