Skip to content

Commit 82ad8f7

Browse files
authored
New collector based tests (#66)
Create terraform tests that install the collector directly
1 parent d58ab08 commit 82ad8f7

41 files changed

Lines changed: 1158 additions & 92 deletions

Some content is hidden

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

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
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

25-
FROM hashicorp/terraform:light as tfbuild
26+
FROM hashicorp/terraform:1.13 as tfbuild
2627
COPY tf /src/tf
2728
WORKDIR /src/tf
2829
ENV TF_PLUGIN_CACHE_DIR=/src/tf/terraform-plugin-cache

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/e2etesting"
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+
e2etesting.CmdWithProjectId
9292
}
9393

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

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

112112
ctx := context.Background()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package e2etestrunner
15+
package e2etesting
1616

1717
const BeginOutputArt = `
1818
Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package e2etestrunner
15+
package e2etesting
1616

1717
import (
1818
"context"
19+
"flag"
1920
"log"
21+
"math/rand"
22+
"os"
23+
"strings"
2024
"time"
2125

2226
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etestrunner/testclient"
27+
"github.com/alexflint/go-arg"
2328
)
2429

2530
type ApplyPersistent struct {
@@ -52,6 +57,22 @@ type GceCmd struct {
5257
CmdWithImage
5358
}
5459

60+
type GceCollectorCmd struct {
61+
CmdWithImage
62+
}
63+
64+
type GceCollectorArmCmd struct {
65+
CmdWithImage
66+
}
67+
68+
type GkeCollectorCmd struct {
69+
CmdWithImage
70+
}
71+
72+
type GkeOperatorCollectorCmd struct {
73+
CmdWithImage
74+
}
75+
5576
type GkeCmd struct {
5677
CmdWithImage
5778
}
@@ -72,6 +93,10 @@ type CloudRunCmd struct {
7293
CmdWithImage
7394
}
7495

96+
type CloudRunCollectorCmd struct {
97+
CmdWithImage
98+
}
99+
75100
type CloudFunctionsGen2Cmd struct {
76101
// Needed to configure which language will the function instance support
77102
Runtime string `arg:"required" help:"Configure the language runtime environment for CloudFunction"`
@@ -87,17 +112,22 @@ type Args struct {
87112
// tf/persistent/README.md for details on what is in there.
88113
ApplyPersistent *ApplyPersistent `arg:"subcommand:apply-persistent" help:"Terraform apply the resources in tf/persistent and exit (does not run tests)."`
89114

90-
Local *LocalCmd `arg:"subcommand:local" help:"Deploy the test server locally with docker and execute tests"`
91-
Gke *GkeCmd `arg:"subcommand:gke" help:"Deploy the test server on GKE and execute tests"`
92-
Gce *GceCmd `arg:"subcommand:gce" help:"Deploy the test server on GCE and execute tests"`
93-
Gae *GaeCmd `arg:"subcommand:gae" help:"Deploy the test server on GAE and execute tests"`
94-
GaeStandard *GaeStandardCmd `arg:"subcommand:gae-standard" help:"Deploy the test server on GAE standard and execute tests"`
95-
CloudRun *CloudRunCmd `arg:"subcommand:cloud-run" help:"Deploy the test server on Cloud Run and execute tests"`
96-
CloudFunctionsGen2 *CloudFunctionsGen2Cmd `arg:"subcommand:cloud-functions-gen2" help:"Deploy the test server on Cloud Function (2nd Gen) and execute tests"`
115+
Local *LocalCmd `arg:"subcommand:local" help:"Deploy the test server locally with docker and execute tests"`
116+
Gke *GkeCmd `arg:"subcommand:gke" help:"Deploy the test server on GKE and execute tests"`
117+
Gce *GceCmd `arg:"subcommand:gce" help:"Deploy the test server on GCE and execute tests"`
118+
GceCollector *GceCollectorCmd `arg:"subcommand:gce-collector" help:"Deploy the collector on GCE and execute tests"`
119+
GceCollectorArm *GceCollectorArmCmd `arg:"subcommand:gce-collector-arm" help:"Deploy the collector on GCE and execute tests"`
120+
GkeCollector *GkeCollectorCmd `arg:"subcommand:gke-collector" help:"Deploy the collector on GKE and execute tests"`
121+
GkeOperatorCollector *GkeOperatorCollectorCmd `arg:"subcommand:gke-operator-collector" help:"Deploy the collector on GKE using the OpenTelemetry Operator and execute tests"`
122+
Gae *GaeCmd `arg:"subcommand:gae" help:"Deploy the test server on GAE and execute tests"`
123+
GaeStandard *GaeStandardCmd `arg:"subcommand:gae-standard" help:"Deploy the test server on GAE standard and execute tests"`
124+
CloudRun *CloudRunCmd `arg:"subcommand:cloud-run" help:"Deploy the test server on Cloud Run and execute tests"`
125+
CloudRunCollector *CloudRunCollectorCmd `arg:"subcommand:cloud-run-collector" help:"Deploy the collector on Cloud Run and execute tests"`
126+
CloudFunctionsGen2 *CloudFunctionsGen2Cmd `arg:"subcommand:cloud-functions-gen2" help:"Deploy the test server on Cloud Function (2nd Gen) and execute tests"`
97127

98128
CmdWithProjectId
99129
GoTestFlags string `help:"go test flags to pass through, e.g. --gotestflags='-test.v'"`
100-
HealthCheckTimeout time.Duration `arg:"--health-check-timeout" help:"A duration (e.g. 5m) to wait for the test server health check. Default is 2m." default:"2m"`
130+
HealthCheckTimeout time.Duration `arg:"--health-check-timeout" help:"A duration (e.g. 5m) to wait for the test server health check. Default is 2m." default:"15m"`
101131

102132
// This is used in a new terraform workspace's name and in the GCP resources
103133
// we create. Pass the GCB build ID in CI to get the build id formatted into
@@ -113,4 +143,46 @@ type SetupFunc func(
113143
*log.Logger,
114144
) (*testclient.Client, Cleanup, error)
115145

146+
type SetupCollectorFunc func(
147+
context.Context,
148+
*Args,
149+
*log.Logger,
150+
) (Cleanup, error)
151+
116152
func NoopCleanup() {}
153+
154+
type ApplyPersistentFunc func(ctx context.Context, projectID string, autoApprove bool, logger *log.Logger) error
155+
156+
func InitTestMain(args *Args, applyPersistent ApplyPersistentFunc) (*log.Logger, context.Context) {
157+
rand.New(rand.NewSource(time.Now().UnixNano()))
158+
p := arg.MustParse(args)
159+
if p.Subcommand() == nil {
160+
p.Fail("missing command")
161+
}
162+
// Need a logger just for TestMain() before testing.T is available
163+
logger := log.New(os.Stdout, "TestMain: ", log.LstdFlags|log.Lshortfile)
164+
ctx := context.Background()
165+
166+
// Handle special case of just creating persistent resources
167+
if args.ApplyPersistent != nil {
168+
err := applyPersistent(ctx, args.ProjectID, args.ApplyPersistent.AutoApprove, logger)
169+
if err != nil {
170+
logger.Panic(err)
171+
}
172+
return nil, nil
173+
}
174+
175+
// hacky but works
176+
os.Args = append([]string{os.Args[0]}, strings.Fields(args.GoTestFlags)...)
177+
flag.Parse()
178+
179+
// handle any complex defaults
180+
if args.TestRunID == "" {
181+
hex, err := RandomHex(6)
182+
if err != nil {
183+
logger.Fatalf("error generating random hex string: %v\n", err)
184+
}
185+
args.TestRunID = hex
186+
}
187+
return logger, ctx
188+
}
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
)
2525

2626
const (
27-
tfPersistentDir = "tf/persistent"
28-
Push SubscriptionMode = "push"
29-
Pull SubscriptionMode = "pull"
27+
tfPersistentDir = "tf/persistent"
28+
tfPersistentCollectorDir = "tf/persistent-collector"
29+
Push SubscriptionMode = "push"
30+
Pull SubscriptionMode = "pull"
3031
)
3132

3233
type SubscriptionMode string
@@ -94,6 +95,8 @@ func SetupTf(
9495
return nil, func() {}, err
9596
}
9697

98+
logger.Printf("Running %s with image: %s\n", tfDir, tfVars["image"])
99+
97100
cleanup := func() {
98101
defer deleteWorkspace(ctx, testRunID, tfDir, logger)
99102

@@ -155,18 +158,37 @@ func SetupTf(
155158
return &tfOutput.PubsubInfoWrapper.Value, cleanup, nil
156159
}
157160

161+
func ApplyPersistent(
162+
ctx context.Context,
163+
projectID string,
164+
autoApprove bool,
165+
logger *log.Logger,
166+
) error {
167+
return applyPersistent(ctx, projectID, autoApprove, logger, tfPersistentDir)
168+
}
169+
170+
func ApplyPersistentCollector(
171+
ctx context.Context,
172+
projectID string,
173+
autoApprove bool,
174+
logger *log.Logger,
175+
) error {
176+
return applyPersistent(ctx, projectID, autoApprove, logger, tfPersistentCollectorDir)
177+
}
178+
158179
// Create persistent resources (in tf/persistent) that are used across tests. No
159180
// cleanup is required
160-
func ApplyPersistent(
181+
func applyPersistent(
161182
ctx context.Context,
162183
projectID string,
163184
autoApprove bool,
164185
logger *log.Logger,
186+
persistentDir string,
165187
) error {
166188
logger.Println("Applying any changes to persistent resources")
167189
// Run terraform init
168190
cmd := initCommand(ctx, projectID)
169-
cmd.Dir = tfPersistentDir
191+
cmd.Dir = persistentDir
170192
if err := runWithOutput(cmd, logger); err != nil {
171193
return err
172194
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package e2etestrunner
15+
package e2etesting
1616

1717
import (
18+
"crypto/rand"
1819
"encoding/hex"
19-
"math/rand"
2020
)
2121

2222
// Generates a random hex string of given length
23-
func randomHex(length uint8) (string, error) {
23+
func RandomHex(length uint8) (string, error) {
2424
bytes := make([]byte, length)
2525
if _, err := rand.Read(bytes); err != nil {
2626
return "", err

e2etestrunner/main_test.go

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,23 @@ package e2etestrunner
1919

2020
import (
2121
"context"
22-
"flag"
23-
"log"
24-
"math/rand"
25-
"os"
26-
"strings"
2722
"testing"
28-
"time"
2923

30-
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etestrunner/setuptf"
24+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etesting"
25+
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etesting/setuptf"
26+
3127
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etestrunner/testclient"
32-
"github.com/alexflint/go-arg"
3328
)
3429

3530
var (
36-
args Args
31+
args e2etesting.Args
3732
testServerClient *testclient.Client
3833
)
3934

4035
func TestMain(m *testing.M) {
41-
rand.Seed(time.Now().UnixNano())
42-
p := arg.MustParse(&args)
43-
if p.Subcommand() == nil {
44-
p.Fail("missing command")
45-
}
46-
// Need a logger just for TestMain() before testing.T is available
47-
logger := log.New(os.Stdout, "TestMain: ", log.LstdFlags|log.Lshortfile)
48-
ctx := context.Background()
49-
50-
// Handle special case of just creating persistent resources
51-
if args.ApplyPersistent != nil {
52-
err := setuptf.ApplyPersistent(ctx, args.ProjectID, args.ApplyPersistent.AutoApprove, logger)
53-
if err != nil {
54-
logger.Panic(err)
55-
}
56-
return
57-
}
58-
59-
// hacky but works
60-
os.Args = append([]string{os.Args[0]}, strings.Fields(args.GoTestFlags)...)
61-
flag.Parse()
62-
63-
// handle any complex defaults
64-
if args.TestRunID == "" {
65-
hex, err := randomHex(6)
66-
if err != nil {
67-
logger.Fatalf("error generating random hex string: %v\n", err)
68-
}
69-
args.TestRunID = hex
70-
}
36+
logger, ctx := e2etesting.InitTestMain(&args, setuptf.ApplyPersistent)
7137

72-
var setupFunc SetupFunc
38+
var setupFunc e2etesting.SetupFunc
7339
switch {
7440
case args.Local != nil:
7541
setupFunc = SetupLocal
@@ -106,7 +72,7 @@ func TestMain(m *testing.M) {
10672
}
10773

10874
// Run tests
109-
logger.Print(BeginOutputArt)
75+
logger.Print(e2etesting.BeginOutputArt)
11076
m.Run()
111-
logger.Print(EndOutputArt)
77+
logger.Print(e2etesting.EndOutputArt)
11278
}

0 commit comments

Comments
 (0)