-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
environment.go
255 lines (224 loc) 路 8.28 KB
/
environment.go
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2016-2022, Pulumi Corporation.
//
// 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 testing
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/pulumi/pulumi/sdk/v3/go/common/tools"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/fsutil"
"github.com/stretchr/testify/assert"
)
const (
//nolint:gosec
pulumiCredentialsPathEnvVar = "PULUMI_CREDENTIALS_PATH"
)
// Environment is an extension of the testing.T type that provides support for a test environment
// on the local disk. The Environment has a root directory (e.g. a newly created temp folder) and
// a current working directory (to virtually change directories).
type Environment struct {
*testing.T
// RootPath is a new temp directory where the environment starts.
RootPath string
// Current working directory.
CWD string
// Backend to use for commands
Backend string
// Environment variables to add to the environment for commands (`key=value`).
Env []string
// Passphrase for config secrets, if any
Passphrase string
// Set to true to turn off setting PULUMI_CONFIG_PASSPHRASE.
NoPassphrase bool
// Content to pass on stdin, if any
Stdin io.Reader
}
// WriteYarnRCForTest writes a .yarnrc file which sets global configuration for every yarn inovcation. We use this
// to work around some test issues we see in Travis.
func WriteYarnRCForTest(root string) error {
// Write a .yarnrc file to pass --mutex network to all yarn invocations, since tests
// may run concurrently and yarn may fail if invoked concurrently
// https://github.com/yarnpkg/yarn/issues/683
// Also add --network-concurrency 1 since we've been seeing
// https://github.com/yarnpkg/yarn/issues/4563 as well
return os.WriteFile(
filepath.Join(root, ".yarnrc"),
[]byte("--mutex network\n--network-concurrency 1\n"), 0o600)
}
// NewGoEnvironment returns a new Environment object, located in a GOPATH temp directory.
func NewGoEnvironment(t *testing.T) *Environment {
testRoot, err := tools.CreateTemporaryGoFolder("test-env")
if err != nil {
t.Errorf("error creating test directory %s", err)
}
t.Logf("Created new go test environment")
return &Environment{
T: t,
RootPath: testRoot,
CWD: testRoot,
}
}
// NewEnvironment returns a new Environment object, located in a temp directory.
func NewEnvironment(t *testing.T) *Environment {
root, err := os.MkdirTemp("", "test-env")
assert.NoError(t, err, "creating temp directory")
assert.NoError(t, WriteYarnRCForTest(root), "writing .yarnrc file")
t.Logf("Created new test environment: %v", root)
return &Environment{
T: t,
RootPath: root,
CWD: root,
}
}
// SetBackend sets the backend to use for commands in this environment.
func (e *Environment) SetBackend(backend string) {
e.Backend = backend
}
// SetEnvVars appends to the list of environment variables.
// According to https://pkg.go.dev/os/exec#Cmd.Env:
//
// If Env contains duplicate environment keys, only the last
// value in the slice for each duplicate key is used.
//
// So later values take precedence.
func (e *Environment) SetEnvVars(env ...string) {
e.Env = append(e.Env, env...)
}
// ImportDirectory copies a folder into the test environment.
func (e *Environment) ImportDirectory(path string) {
err := fsutil.CopyFile(e.RootPath, path, nil)
if err != nil {
e.T.Fatalf("error importing directory: %v", err)
}
}
// DeleteEnvironment deletes the environment's RootPath, and everything underneath it.
func (e *Environment) DeleteEnvironment() {
e.Helper()
err := os.RemoveAll(e.RootPath)
if err != nil {
// In CI, Windows sometimes lags behind in marking a resource
// as unused. This causes otherwise passing tests to fail.
// So ignore errors during cleanup.
e.Logf("error cleaning up test directory %q: %v", e.RootPath, err)
}
}
// DeleteEnvironment deletes the environment's RootPath, and everything
// underneath it. It tolerates failing to delete the environment.
func (e *Environment) DeleteEnvironmentFallible() error {
e.Helper()
return os.RemoveAll(e.RootPath)
}
// DeleteIfNotFailed deletes the environment's RootPath if the test hasn't failed. Otherwise
// keeps the files around for aiding debugging.
func (e *Environment) DeleteIfNotFailed() {
if !e.T.Failed() {
e.DeleteEnvironment()
}
}
// PathExists returns whether or not a file or directory exists relative to Environment's working directory.
func (e *Environment) PathExists(p string) bool {
fullPath := path.Join(e.CWD, p)
_, err := os.Stat(fullPath)
return err == nil
}
var YarnInstallMutex sync.Mutex
// RunCommand runs the command expecting a zero exit code, returning stdout and stderr.
func (e *Environment) RunCommand(cmd string, args ...string) (string, string) {
// We don't want to time out on yarn installs.
if cmd == "yarn" {
YarnInstallMutex.Lock()
defer YarnInstallMutex.Unlock()
}
e.Helper()
stdout, stderr, err := e.GetCommandResults(cmd, args...)
if err != nil {
e.Logf("Run Error: %v", err)
e.Logf("STDOUT: %v", stdout)
e.Logf("STDERR: %v", stderr)
e.Fatalf("Ran command %v args %v and expected success. Instead got failure.", cmd, args)
}
return stdout, stderr
}
// RunCommandExpectError runs the command expecting a non-zero exit code, returning stdout and stderr.
func (e *Environment) RunCommandExpectError(cmd string, args ...string) (string, string) {
e.Helper()
stdout, stderr, err := e.GetCommandResults(cmd, args...)
if err == nil {
e.Errorf("Ran command %v args %v and expected failure. Instead got success.", cmd, args)
e.Logf("STDOUT: %v", stdout)
e.Logf("STDERR: %v", stderr)
}
return stdout, stderr
}
// LocalURL returns a URL that uses the "fire and forget", storing its data inside the test folder (so multiple tests)
// may reuse stack names.
func (e *Environment) LocalURL() string {
return "file://" + filepath.ToSlash(e.RootPath)
}
// GetCommandResults runs the given command and args in the Environments CWD, returning
// STDOUT, STDERR, and the result of os/exec.Command{}.Run.
func (e *Environment) GetCommandResults(command string, args ...string) (string, string, error) {
e.T.Helper()
e.T.Logf("Running command %v %v", command, strings.Join(args, " "))
// Buffer STDOUT and STDERR so we can return them later.
var outBuffer bytes.Buffer
var errBuffer bytes.Buffer
passphrase := "correct horse battery staple"
if e.Passphrase != "" {
passphrase = e.Passphrase
}
//nolint:gas
cmd := exec.Command(command, args...)
cmd.Dir = e.CWD
if e.Stdin != nil {
cmd.Stdin = e.Stdin
}
cmd.Stdout = &outBuffer
cmd.Stderr = &errBuffer
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", pulumiCredentialsPathEnvVar, e.RootPath))
cmd.Env = append(cmd.Env, "PULUMI_DEBUG_COMMANDS=true")
if !e.NoPassphrase {
cmd.Env = append(cmd.Env, fmt.Sprintf("PULUMI_CONFIG_PASSPHRASE=%s", passphrase))
}
if e.Backend != "" {
cmd.Env = append(cmd.Env, fmt.Sprintf("PULUMI_BACKEND_URL=%s", e.Backend))
}
// According to https://pkg.go.dev/os/exec#Cmd.Env:
// If Env contains duplicate environment keys, only the last
// value in the slice for each duplicate key is used.
// By putting `append e.Env` last, we allow our users to override variables we include.
cmd.Env = append(cmd.Env, e.Env...)
runErr := cmd.Run()
return outBuffer.String(), errBuffer.String(), runErr
}
// WriteTestFile writes a new test file relative to the Environment's CWD with the given contents.
// Aborts the underlying test on any errors.
func (e *Environment) WriteTestFile(filename string, contents string) {
filename = filepath.Join(e.CWD, filename)
dir := filepath.Dir(filename)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
e.T.Fatalf("error making directories for test file (%v): %v", filename, err)
}
if err := os.WriteFile(filename, []byte(contents), os.ModePerm); err != nil {
e.T.Fatalf("writing test file (%v): %v", filename, err)
}
}