Skip to content

Commit ed87d23

Browse files
committed
Create terraform tests that install the collector directly
1 parent 0f11516 commit ed87d23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1132
-466
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
FROM golang:1.23 AS gobuild
1616
WORKDIR /src
17+
ARG BUILD_TAGS
1718

1819
# cache deps before copying source so that we don't re-download as much
1920
COPY go.mod go.sum ./
2021
RUN go mod download
2122

2223
COPY . .
23-
RUN CGO_ENABLED=0 go test -tags=e2e -c ./e2etestrunner -o opentelemetry-operations-e2e-testing.test
24+
RUN set -x; if [ "$BUILD_TAGS" = "e2ecollector" ]; then BUILD_DIRECTORY="e2etestrunner-collector"; else BUILD_TAGS="e2e"; BUILD_DIRECTORY="e2etestrunner"; fi; CGO_ENABLED=0 go test -timeout 3600s -tags=$BUILD_TAGS -c "./$BUILD_DIRECTORY" -o opentelemetry-operations-e2e-testing.test
2425

2526
FROM hashicorp/terraform:light as tfbuild
2627
COPY tf /src/tf

cloudbuild-collector.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2021 Google LLC
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+
# Builds a docker image, tagging it as _DOCKER_TAG (substitution must be
16+
# provided) and latest.
17+
18+
steps:
19+
- name: docker
20+
env:
21+
- DOCKER_BUILDKIT=1
22+
args:
23+
- build
24+
- --tag=${_TEST_RUNNER_IMAGE_NAME}:${_DOCKER_TAG}
25+
- --tag=${_TEST_RUNNER_IMAGE_NAME}:latest
26+
- --build-arg=BUILDKIT_INLINE_CACHE=1
27+
- --build-arg=BUILD_TAGS=e2ecollector
28+
- --cache-from=${_TEST_RUNNER_IMAGE_NAME}:latest
29+
- .
30+
31+
images: ["${_TEST_RUNNER_IMAGE_NAME}"]
32+
options:
33+
dynamic_substitutions: true
34+
substitutions:
35+
_TEST_RUNNER_IMAGE_NAME: us-central1-docker.pkg.dev/${PROJECT_ID}/e2e-testing/opentelemetry-operations-e2e-testing

cmd/testmatrix/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"bufio"
2626
"context"
2727
"fmt"
28+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util"
2829
"html/template"
2930
"log"
3031
"os"
@@ -33,7 +34,6 @@ import (
3334
"strings"
3435

3536
"cloud.google.com/go/storage"
36-
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etestrunner"
3737
"github.com/alexflint/go-arg"
3838
"golang.org/x/sync/errgroup"
3939
"google.golang.org/api/cloudbuild/v1"
@@ -88,7 +88,7 @@ This will fetch recent Cloud Build logs to automatically update the statuses in
8888
)
8989

9090
type Args struct {
91-
e2etestrunner.CmdWithProjectId
91+
util.CmdWithProjectId
9292
}
9393

9494
type status string
@@ -106,7 +106,7 @@ var (
106106
)
107107

108108
func main() {
109-
args := e2etestrunner.Args{}
109+
args := util.Args{}
110110
arg.MustParse(&args)
111111

112112
ctx := context.Background()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//go:build e2ecollector
2+
3+
package e2etestrunner_collector
4+
5+
import (
6+
"context"
7+
"flag"
8+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util"
9+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util/setuptf"
10+
"log"
11+
"math/rand"
12+
"os"
13+
"strings"
14+
"testing"
15+
"time"
16+
17+
"github.com/alexflint/go-arg"
18+
)
19+
20+
var (
21+
args util.Args
22+
resourceType string
23+
)
24+
25+
func TestMain(m *testing.M) {
26+
rand.New(rand.NewSource(time.Now().UnixNano()))
27+
p := arg.MustParse(&args)
28+
if p.Subcommand() == nil {
29+
p.Fail("missing command")
30+
}
31+
// Need a logger just for TestMain() before testing.T is available
32+
logger := log.New(os.Stdout, "TestMain: ", log.LstdFlags|log.Lshortfile)
33+
ctx := context.Background()
34+
35+
// Handle special case of just creating persistent resources
36+
if args.ApplyPersistent != nil {
37+
err := setuptf.ApplyPersistentCollector(ctx, args.ProjectID, args.ApplyPersistent.AutoApprove, logger)
38+
if err != nil {
39+
logger.Panic(err)
40+
}
41+
return
42+
}
43+
44+
// hacky but works
45+
os.Args = append([]string{os.Args[0]}, strings.Fields(args.GoTestFlags)...)
46+
flag.Parse()
47+
48+
// handle any complex defaults
49+
if args.TestRunID == "" {
50+
hex, err := util.RandomHex(6)
51+
if err != nil {
52+
logger.Fatalf("error generating random hex string: %v\n", err)
53+
}
54+
args.TestRunID = hex
55+
}
56+
57+
var setupFunc util.SetupCollectorFunc
58+
switch {
59+
case args.GceCollector != nil:
60+
setupFunc = SetupGceCollector
61+
resourceType = "gce_instance"
62+
case args.GkeCollector != nil:
63+
setupFunc = SetupGkeCollector
64+
resourceType = "k8s_container"
65+
case args.GkeOperatorCollector != nil:
66+
setupFunc = SetupGkeOperatorCollector
67+
resourceType = "k8s_container"
68+
case args.CloudRunCollector != nil:
69+
setupFunc = SetupCloudRunCollector
70+
resourceType = "generic_task"
71+
}
72+
cleanup, err := setupFunc(ctx, &args, logger)
73+
74+
defer cleanup()
75+
if err != nil {
76+
logger.Panic(err)
77+
}
78+
79+
// wait for instrumented test server to be healthy
80+
logger.Printf("Waiting for health check on (will timeout after %v)\n", args.HealthCheckTimeout)
81+
time.Sleep(args.HealthCheckTimeout)
82+
// Run tests
83+
logger.Print(util.BeginOutputArt)
84+
m.Run()
85+
logger.Print(util.EndOutputArt)
86+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 Google LLC
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+
// https://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 e2etestrunner_collector
16+
17+
import (
18+
"context"
19+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util"
20+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util/setuptf"
21+
"log"
22+
)
23+
24+
const gceCollectorTfDir string = "tf/gce-collector"
25+
26+
// SetupGceCollector Set up the collector to run in GCE container. Creates a new
27+
// GCE VM resources, and runs the specified container image. The
28+
// returned cleanup function tears down the VM.
29+
func SetupGceCollector(
30+
ctx context.Context,
31+
args *util.Args,
32+
logger *log.Logger,
33+
) (util.Cleanup, error) {
34+
_, cleanupTf, err := setuptf.SetupTf(
35+
ctx,
36+
args.ProjectID,
37+
args.TestRunID,
38+
gceCollectorTfDir,
39+
map[string]string{
40+
"image": args.GceCollector.Image,
41+
},
42+
logger,
43+
)
44+
if err != nil {
45+
return cleanupTf, err
46+
}
47+
48+
return cleanupTf, err
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 Google LLC
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+
// https://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 e2etestrunner_collector
16+
17+
import (
18+
"context"
19+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util"
20+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util/setuptf"
21+
"log"
22+
)
23+
24+
const cloudRunCollectorTfDir string = "tf/cloud-run-collector"
25+
26+
// SetupCloudRunCollector sets up the collector to run in Cloud Run.
27+
// Creates a new service and runs the specified container image as a revision.
28+
// The returned cleanup function tears down everything.
29+
func SetupCloudRunCollector(
30+
ctx context.Context,
31+
args *util.Args,
32+
logger *log.Logger,
33+
) (util.Cleanup, error) {
34+
_, cleanupTf, err := setuptf.SetupTf(
35+
ctx,
36+
args.ProjectID,
37+
args.TestRunID,
38+
cloudRunCollectorTfDir,
39+
map[string]string{
40+
"image": args.CloudRunCollector.Image,
41+
},
42+
logger,
43+
)
44+
if err != nil {
45+
return cleanupTf, err
46+
}
47+
48+
return cleanupTf, err
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package e2etestrunner_collector
2+
3+
import (
4+
"context"
5+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util"
6+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util/setuptf"
7+
"log"
8+
)
9+
10+
const gkeCollectorTfDir string = "tf/gke-collector"
11+
12+
// SetupGkeCollector Set up the collector to run in GKE.
13+
// Creates a new pod and runs the specified container image in a pod.
14+
// The returned cleanup function tears down the whole cluster.
15+
func SetupGkeCollector(
16+
ctx context.Context,
17+
args *util.Args,
18+
logger *log.Logger,
19+
) (util.Cleanup, error) {
20+
_, cleanupTf, err := setuptf.SetupTf(
21+
ctx,
22+
args.ProjectID,
23+
args.TestRunID,
24+
gkeCollectorTfDir,
25+
map[string]string{
26+
"image": args.GkeCollector.Image,
27+
},
28+
logger,
29+
)
30+
if err != nil {
31+
return cleanupTf, err
32+
}
33+
34+
return cleanupTf, err
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package e2etestrunner_collector
2+
3+
import (
4+
"context"
5+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util"
6+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/util/setuptf"
7+
"log"
8+
)
9+
10+
const gkeOperatorCollectorTfDir string = "tf/gke-operator-collector"
11+
12+
// SetupGkeOperatorCollector Set up the collector to run in GKE.
13+
// Creates a new pod and runs the specified container image in a pod.
14+
// The returned cleanup function tears down the whole cluster.
15+
func SetupGkeOperatorCollector(
16+
ctx context.Context,
17+
args *util.Args,
18+
logger *log.Logger,
19+
) (util.Cleanup, error) {
20+
_, cleanupTf, err := setuptf.SetupTf(
21+
ctx,
22+
args.ProjectID,
23+
args.TestRunID,
24+
gkeOperatorCollectorTfDir,
25+
map[string]string{
26+
"image": args.GkeOperatorCollector.Image,
27+
},
28+
logger,
29+
)
30+
if err != nil {
31+
return cleanupTf, err
32+
}
33+
34+
return cleanupTf, err
35+
}

0 commit comments

Comments
 (0)