-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
e2e.go
288 lines (250 loc) · 7.76 KB
/
e2e.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package e2e
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/plans/planfile"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/statefile"
)
// Type binary represents the combination of a compiled binary
// and a temporary working directory to run it in.
type binary struct {
binPath string
workDir string
env []string
}
// NewBinary prepares a temporary directory containing the files from the
// given fixture and returns an instance of type binary that can run
// the generated binary in that directory.
//
// If the temporary directory cannot be created, a fixture of the given name
// cannot be found, or if an error occurs while _copying_ the fixture files,
// this function will panic. Tests should be written to assume that this
// function always succeeds.
func NewBinary(binaryPath, workingDir string) *binary {
tmpDir, err := ioutil.TempDir("", "binary-e2etest")
if err != nil {
panic(err)
}
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
panic(err)
}
// For our purposes here we do a very simplistic file copy that doesn't
// attempt to preserve file permissions, attributes, alternate data
// streams, etc. Since we only have to deal with our own fixtures in
// the testdata subdir, we know we don't need to deal with anything
// of this nature.
err = filepath.Walk(workingDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == workingDir {
// nothing to do at the root
return nil
}
if filepath.Base(path) == ".exists" {
// We use this file just to let git know the "empty" fixture
// exists. It is not used by any test.
return nil
}
srcFn := path
path, err = filepath.Rel(workingDir, path)
if err != nil {
return err
}
dstFn := filepath.Join(tmpDir, path)
if info.IsDir() {
return os.Mkdir(dstFn, os.ModePerm)
}
src, err := os.Open(srcFn)
if err != nil {
return err
}
dst, err := os.OpenFile(dstFn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
_, err = io.Copy(dst, src)
if err != nil {
return err
}
if err := src.Close(); err != nil {
return err
}
if err := dst.Close(); err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
return &binary{
binPath: binaryPath,
workDir: tmpDir,
}
}
// AddEnv appends an entry to the environment variable table passed to any
// commands subsequently run.
func (b *binary) AddEnv(entry string) {
b.env = append(b.env, entry)
}
// Cmd returns an exec.Cmd pre-configured to run the generated Terraform
// binary with the given arguments in the temporary working directory.
//
// The returned object can be mutated by the caller to customize how the
// process will be run, before calling Run.
func (b *binary) Cmd(args ...string) *exec.Cmd {
cmd := exec.Command(b.binPath, args...)
cmd.Dir = b.workDir
cmd.Env = os.Environ()
// Disable checkpoint since we don't want to harass that service when
// our tests run. (This does, of course, mean we can't actually do
// end-to-end testing of our Checkpoint interactions.)
cmd.Env = append(cmd.Env, "CHECKPOINT_DISABLE=1")
cmd.Env = append(cmd.Env, b.env...)
return cmd
}
// Run executes the generated Terraform binary with the given arguments
// and returns the bytes that it wrote to both stdout and stderr.
//
// This is a simple way to run Terraform for non-interactive commands
// that don't need any special environment variables. For more complex
// situations, use Cmd and customize the command before running it.
func (b *binary) Run(args ...string) (stdout, stderr string, err error) {
cmd := b.Cmd(args...)
cmd.Stdin = nil
cmd.Stdout = &bytes.Buffer{}
cmd.Stderr = &bytes.Buffer{}
err = cmd.Run()
stdout = cmd.Stdout.(*bytes.Buffer).String()
stderr = cmd.Stderr.(*bytes.Buffer).String()
return
}
// Path returns a file path within the temporary working directory by
// appending the given arguments as path segments.
func (b *binary) Path(parts ...string) string {
args := make([]string, len(parts)+1)
args[0] = b.workDir
args = append(args, parts...)
return filepath.Join(args...)
}
// OpenFile is a helper for easily opening a file from the working directory
// for reading.
func (b *binary) OpenFile(path ...string) (*os.File, error) {
flatPath := b.Path(path...)
return os.Open(flatPath)
}
// ReadFile is a helper for easily reading a whole file from the working
// directory.
func (b *binary) ReadFile(path ...string) ([]byte, error) {
flatPath := b.Path(path...)
return ioutil.ReadFile(flatPath)
}
// FileExists is a helper for easily testing whether a particular file
// exists in the working directory.
func (b *binary) FileExists(path ...string) bool {
flatPath := b.Path(path...)
_, err := os.Stat(flatPath)
return !os.IsNotExist(err)
}
// LocalState is a helper for easily reading the local backend's state file
// terraform.tfstate from the working directory.
func (b *binary) LocalState() (*states.State, error) {
return b.StateFromFile("terraform.tfstate")
}
// StateFromFile is a helper for easily reading a state snapshot from a file
// on disk relative to the working directory.
func (b *binary) StateFromFile(filename string) (*states.State, error) {
f, err := b.OpenFile(filename)
if err != nil {
return nil, err
}
defer f.Close()
stateFile, err := statefile.Read(f)
if err != nil {
return nil, fmt.Errorf("Error reading statefile: %s", err)
}
return stateFile.State, nil
}
// Plan is a helper for easily reading a plan file from the working directory.
func (b *binary) Plan(path string) (*plans.Plan, error) {
path = b.Path(path)
pr, err := planfile.Open(path)
if err != nil {
return nil, err
}
plan, err := pr.ReadPlan()
if err != nil {
return nil, err
}
return plan, nil
}
// SetLocalState is a helper for easily writing to the file the local backend
// uses for state in the working directory. This does not go through the
// actual local backend code, so processing such as management of serials
// does not apply and the given state will simply be written verbatim.
func (b *binary) SetLocalState(state *states.State) error {
path := b.Path("terraform.tfstate")
f, err := os.OpenFile(path, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create temporary state file %s: %s", path, err)
}
defer f.Close()
sf := &statefile.File{
Serial: 0,
Lineage: "fake-for-testing",
State: state,
}
return statefile.Write(sf, f)
}
// Close cleans up the temporary resources associated with the object,
// including its working directory. It is not valid to call Cmd or Run
// after Close returns.
//
// This method does _not_ stop any running child processes. It's the
// caller's responsibility to also terminate those _before_ closing the
// underlying binary object.
//
// This function is designed to run under "defer", so it doesn't actually
// do any error handling and will leave dangling temporary files on disk
// if any errors occur while cleaning up.
func (b *binary) Close() {
os.RemoveAll(b.workDir)
}
func GoBuild(pkgPath, tmpPrefix string) string {
dir, prefix := filepath.Split(tmpPrefix)
tmpFile, err := ioutil.TempFile(dir, prefix)
if err != nil {
panic(err)
}
tmpFilename := tmpFile.Name()
if err = tmpFile.Close(); err != nil {
panic(err)
}
cmd := exec.Command(
"go", "build",
"-o", tmpFilename,
pkgPath,
)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
// The go compiler will have already produced some error messages
// on stderr by the time we get here.
panic(fmt.Sprintf("failed to build executable: %s", err))
}
return tmpFilename
}
// WorkDir() returns the binary workdir
func (b *binary) WorkDir() string {
return b.workDir
}