-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_platform.go
189 lines (175 loc) · 6.32 KB
/
test_platform.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
// Package types contains the types that are used in the e2e tests
package types
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
teststructure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/require"
)
// TestPlatform is the test "state" that allows for helper functions such as deferring the teardown step.
type TestPlatform struct {
T *testing.T
TestFolder string
}
// NewTestPlatform generates the test "state" object that allows for helper functions such as deferring the teardown step.
func NewTestPlatform(t *testing.T) *TestPlatform {
t.Helper()
testPlatform := new(TestPlatform)
testPlatform.T = t
tempFolder := teststructure.CopyTerraformFolderToTemp(t, "..", "tf/public-ec2-instance")
testPlatform.TestFolder = tempFolder
// Since Terraform is going to be run with that temp folder as the CWD, we also need our .tool-versions file to be
// in that directory so that the right version of Terraform is being run there. I can neither confirm nor deny that
// this took me 2 days to figure out...
// Since we can't be sure what the working directory is, we are going to walk up one directory at a time until we
// find a .tool-versions file and then copy it into the temp folder
found := false
filePath := ".tool-versions"
for !found {
//nolint:gocritic
if _, err := os.Stat(filePath); err == nil {
// The file exists
found = true
} else if errors.Is(err, os.ErrNotExist) {
// The file does *not* exist. Add a "../" and try again
filePath = fmt.Sprintf("../%v", filePath)
} else {
// Schrodinger: file may or may not exist. See err for details.
// Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence
require.NoError(t, err)
}
}
err := copyFile(filePath, fmt.Sprintf("%v/.tool-versions", testPlatform.TestFolder))
require.NoError(t, err)
return testPlatform
}
// RunSSHCommand provides a simple way to run a shell command on the server that is created using Terraform.
func (platform *TestPlatform) RunSSHCommand(command string) (string, error) {
return platform.runSSHCommandWithOptionalSudo(command, false)
}
// RunSSHCommandAsSudo provides a simple way to run a shell command with sudo on the server that is created using Terraform.
func (platform *TestPlatform) RunSSHCommandAsSudo(command string) (string, error) {
return platform.runSSHCommandWithOptionalSudo(command, true)
}
func (platform *TestPlatform) runSSHCommandWithOptionalSudo(command string, asSudo bool) (string, error) {
precommand := "bash -c"
if asSudo {
precommand = fmt.Sprintf(`sudo %v`, precommand)
}
terraformOptions := teststructure.LoadTerraformOptions(platform.T, platform.TestFolder)
keyPair := teststructure.LoadEc2KeyPair(platform.T, platform.TestFolder)
host := ssh.Host{
Hostname: terraform.Output(platform.T, terraformOptions, "public_instance_ip"),
SshKeyPair: keyPair.KeyPair,
SshUserName: "ubuntu",
}
var output string
var err error
count := 0
done := false
// Try up to 3 times to do the command, to avoid "i/o timeout" errors which are transient
for !done && count < 3 {
count++
output, err = ssh.CheckSshCommandE(platform.T, host, fmt.Sprintf(`%v '%v'`, precommand, command))
if err != nil {
if strings.Contains(err.Error(), "i/o timeout") {
// There was an error, but it was an i/o timeout, so wait a few seconds and try again
logger.Default.Logf(platform.T, "i/o timeout error, trying again")
logger.Default.Logf(platform.T, output)
time.Sleep(3 * time.Second)
continue
} else {
logger.Default.Logf(platform.T, output)
return "nil", fmt.Errorf("ssh command failed: %w", err)
}
}
done = true
}
logger.Default.Logf(platform.T, output)
return output, nil
}
// Teardown brings down the Terraform infrastructure that was created.
func (platform *TestPlatform) Teardown() {
teststructure.RunTestStage(platform.T, "TEARDOWN", func() {
keyPair := teststructure.LoadEc2KeyPair(platform.T, platform.TestFolder)
terraformOptions := teststructure.LoadTerraformOptions(platform.T, platform.TestFolder)
terraform.Destroy(platform.T, terraformOptions)
aws.DeleteEC2KeyPair(platform.T, keyPair)
})
}
// copyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherwise, attempt to create a hard link
// between the two files. If that fails, copy the file contents from src to dst.
func copyFile(src string, dest string) error {
sourceFileInfo, err := os.Stat(src)
if err != nil {
return fmt.Errorf("failed to stat file: %w", err)
}
if !sourceFileInfo.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("non-regular source file %s (%q)", sourceFileInfo.Name(), sourceFileInfo.Mode().String())
}
destFileInfo, err := os.Stat(dest)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("unknown error: %w", err)
}
} else {
if !(destFileInfo.Mode().IsRegular()) {
return fmt.Errorf("non-regular destination file %s (%q)", destFileInfo.Name(), destFileInfo.Mode().String())
}
if os.SameFile(sourceFileInfo, destFileInfo) {
return nil
}
}
err = os.Link(src, dest)
if err == nil {
return nil
}
err = copyFileContents(src, dest)
if err != nil {
return err
}
return nil
}
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src string, dest string) error {
cleanSrc := filepath.Clean(src)
cleanDst := filepath.Clean(dest)
srcFile, err := os.Open(cleanSrc)
if err != nil {
return fmt.Errorf("unable to open src file: %w", err)
}
defer func(in *os.File) {
_ = in.Close()
}(srcFile)
dstFile, err := os.Create(cleanDst)
if err != nil {
return fmt.Errorf("unable to create dest file: %w", err)
}
defer func() {
cerr := dstFile.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(dstFile, srcFile); err != nil {
return fmt.Errorf("unable to copy file: %w", err)
}
err = dstFile.Sync()
return nil
}