-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathe2e_testing.go
More file actions
190 lines (152 loc) · 7.26 KB
/
e2e_testing.go
File metadata and controls
190 lines (152 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2021 Google LLC
//
// 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
//
// https://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 e2etesting
import (
"context"
"flag"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etestrunner/testclient"
"github.com/alexflint/go-arg"
)
type ApplyPersistent struct {
AutoApprove bool `arg:"--auto-approve" default:"false" help:"Approve without prompting. Default is false."`
}
type CmdWithProjectId struct {
ProjectID string `arg:"required,--project-id,env:PROJECT_ID" help:"GCP project id/name"`
}
type CmdWithImage struct {
Image string `arg:"required" help:"docker container image to deploy and test"`
}
type LocalCmd struct {
CmdWithImage
Port string `default:"8000"`
// Needed when running without a metadata server for credentials
GoogleApplicationCredentials string `arg:"--google-application-credentials,env:GOOGLE_APPLICATION_CREDENTIALS" help:"Path to google credentials key file to mount into test server container"`
// May be needed when running this binary in a container
Network string `help:"Docker network to use when starting the container, optional"`
ContainerUser string `arg:"--container-user" help:"Optional user to use when running the container"`
}
type GceCmd struct {
CmdWithImage
}
type GceCollectorCmd struct {
CmdWithImage
}
type GceCollectorArmCmd struct {
CmdWithImage
}
type GkeCollectorCmd struct {
CmdWithImage
}
type GkeOperatorCollectorCmd struct {
CmdWithImage
}
type GkeCmd struct {
CmdWithImage
}
type GaeCmd struct {
CmdWithImage
Runtime string `arg:"required" help:"The language runtime for the instrumented test server, used in naming the service"`
}
type GaeStandardCmd struct {
Runtime string `arg:"required" help:"The language runtime for the instrumented test server, used in naming the service"`
AppSource string `arg:"required" help:"The full path of the zip file that contains the source code to run in GAE"`
Entrypoint string `help:"Optional entrypoint to control how GAE starts the application. See https://cloud.google.com/appengine/docs/standard/reference/app-yaml#entrypoint"`
}
type CloudRunCmd struct {
CmdWithImage
}
type CloudRunCollectorCmd struct {
CmdWithImage
}
type CloudFunctionsGen2Cmd struct {
// Needed to configure which language will the function instance support
Runtime string `arg:"required" help:"Configure the language runtime environment for CloudFunction"`
EntryPoint string `arg:"required" help:"The entry point for the code that will run in this CloudFunction"`
FunctionSource string `arg:"required" help:"The full path of the zip file that contains the code source that needs to run within the CloudFunction"`
}
type Args struct {
// This subcommand is a special case, it doesn't run any tests. It just
// applies the persistent resources which are used across tests. See
// tf/persistent/README.md for details on what is in there.
ApplyPersistent *ApplyPersistent `arg:"subcommand:apply-persistent" help:"Terraform apply the resources in tf/persistent and exit (does not run tests)."`
Local *LocalCmd `arg:"subcommand:local" help:"Deploy the test server locally with docker and execute tests"`
Gke *GkeCmd `arg:"subcommand:gke" help:"Deploy the test server on GKE and execute tests"`
Gce *GceCmd `arg:"subcommand:gce" help:"Deploy the test server on GCE and execute tests"`
GceCollector *GceCollectorCmd `arg:"subcommand:gce-collector" help:"Deploy the collector on GCE and execute tests"`
GceCollectorArm *GceCollectorArmCmd `arg:"subcommand:gce-collector-arm" help:"Deploy the collector on GCE and execute tests"`
GkeCollector *GkeCollectorCmd `arg:"subcommand:gke-collector" help:"Deploy the collector on GKE and execute tests"`
GkeOperatorCollector *GkeOperatorCollectorCmd `arg:"subcommand:gke-operator-collector" help:"Deploy the collector on GKE using the OpenTelemetry Operator and execute tests"`
Gae *GaeCmd `arg:"subcommand:gae" help:"Deploy the test server on GAE and execute tests"`
GaeStandard *GaeStandardCmd `arg:"subcommand:gae-standard" help:"Deploy the test server on GAE standard and execute tests"`
CloudRun *CloudRunCmd `arg:"subcommand:cloud-run" help:"Deploy the test server on Cloud Run and execute tests"`
CloudRunCollector *CloudRunCollectorCmd `arg:"subcommand:cloud-run-collector" help:"Deploy the collector on Cloud Run and execute tests"`
CloudFunctionsGen2 *CloudFunctionsGen2Cmd `arg:"subcommand:cloud-functions-gen2" help:"Deploy the test server on Cloud Function (2nd Gen) and execute tests"`
CmdWithProjectId
GoTestFlags string `help:"go test flags to pass through, e.g. --gotestflags='-test.v'"`
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"`
// This is used in a new terraform workspace's name and in the GCP resources
// we create. Pass the GCB build ID in CI to get the build id formatted into
// resources created for debugging. If not provided, we generate a hex
// string.
TestRunID string `arg:"--test-run-id,env:TEST_RUN_ID" help:"Optional test run id to use to partition terraform resources"`
CollectorSmokeTests []string `arg:"--collector-smoke-tests,separate" help:"Smoke tests to run (e.g. --collector-smoke-tests metrics --collector-smoke-tests logs)"`
}
type Cleanup func()
type SetupFunc func(
context.Context,
*Args,
*log.Logger,
) (*testclient.Client, Cleanup, error)
type SetupCollectorFunc func(
context.Context,
*Args,
*log.Logger,
) (Cleanup, error)
func NoopCleanup() {}
type ApplyPersistentFunc func(ctx context.Context, projectID string, autoApprove bool, logger *log.Logger) error
func InitTestMain(args *Args, applyPersistent ApplyPersistentFunc) (*log.Logger, context.Context) {
rand.New(rand.NewSource(time.Now().UnixNano()))
p := arg.MustParse(args)
if p.Subcommand() == nil {
p.Fail("missing command")
}
// Need a logger just for TestMain() before testing.T is available
logger := log.New(os.Stdout, "TestMain: ", log.LstdFlags|log.Lshortfile)
ctx := context.Background()
// Handle special case of just creating persistent resources
if args.ApplyPersistent != nil {
err := applyPersistent(ctx, args.ProjectID, args.ApplyPersistent.AutoApprove, logger)
if err != nil {
logger.Panic(err)
}
return nil, nil
}
// hacky but works
os.Args = append([]string{os.Args[0]}, strings.Fields(args.GoTestFlags)...)
flag.Parse()
// handle any complex defaults
if args.TestRunID == "" {
hex, err := RandomHex(6)
if err != nil {
logger.Fatalf("error generating random hex string: %v\n", err)
}
args.TestRunID = hex
}
return logger, ctx
}