-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathin_test.go
233 lines (209 loc) · 7.39 KB
/
in_test.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
package concourse_tfe_resource
import (
"encoding/json"
"fmt"
"github.com/hashicorp/go-tfe"
"go.uber.org/mock/gomock"
"math"
"os"
"path"
"testing"
)
func inSetup() (inputJSON, tfe.VariableList, tfe.StateVersion, []*tfe.StateVersionOutput) {
input := inputJSON{
Source: sourceJSON{
Workspace: "foo",
},
Version: version{
Ref: "bar",
},
Params: paramsJSON{
Confirm: true,
},
}
vars := tfe.VariableList{Items: []*tfe.Variable{
{Key: "existing_var", ID: "var-123", Value: "something"},
{Key: "hcl_var", ID: "var-234", HCL: true, Value: "some hcl thing"},
{Key: "ENV_VAR", ID: "var-345", Category: tfe.CategoryEnv, Value: "KEY"},
}}
ov := []tfe.StateVersionOutput{
tfe.StateVersionOutput{
Name: "foo",
Value: "foo",
Sensitive: false,
},
tfe.StateVersionOutput{
Name: "bar",
Value: "secretbar",
Sensitive: true,
},
}
outputVars := []*tfe.StateVersionOutput{
&ov[0],
&ov[1],
}
sv := tfe.StateVersion{
ID: "stateversion",
Outputs: outputVars,
}
return input, vars, sv, outputVars
}
func TestIn(t *testing.T) {
input, vars, sv, _ := inSetup()
wd, _ := os.Getwd()
wd = path.Join(wd, "test_output")
t.Run("no params", func(t *testing.T) {
run := setup(t)
run.Actions.IsConfirmable = true
run.HasChanges = true
call := 0
statuses := []tfe.RunStatus{tfe.RunPending, tfe.RunPlanned, tfe.RunApplied}
runs.EXPECT().Read(gomock.Any(), gomock.Any()).Times(3).DoAndReturn(
func(_ interface{}, _ string) (*tfe.Run, error) {
run.Status = statuses[call]
call++
return &run, nil
})
runs.EXPECT().Apply(gomock.Any(), run.ID, gomock.Any()).Return(nil)
variables.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return(&vars, nil)
stateVersions.EXPECT().ReadCurrentWithOptions(gomock.Any(), "foo", gomock.Any()).Return(&sv, nil)
workingDirectory = path.Join(wd, "test_in_no_params")
os.MkdirAll(workingDirectory, os.FileMode(0755))
output, err := in(input)
if err != nil {
t.Error(err)
}
var result inOutputJSON
json.Unmarshal([]byte(output), &result)
for _, v := range vars.Items {
var fileName string
if v.Category == tfe.CategoryEnv {
fileName = path.Join(workingDirectory, "env_vars", v.Key)
} else if v.HCL {
fileName = path.Join(workingDirectory, "vars", "hcl", v.Key)
} else {
fileName = path.Join(workingDirectory, "vars", v.Key)
}
validateFileContents(t, fileName, v.Value)
}
// non-sensitive var should have its value
validateFileContents(t, path.Join(workingDirectory, "outputs", "foo"), "\"foo\"")
// sensitive var should be empty
validateFileContents(t, path.Join(workingDirectory, "outputs", "bar"), "")
if _, err := os.Stat(path.Join(workingDirectory, "outputs.json")); os.IsNotExist(err) {
t.Error("output json file doesn't exist/is in the wrong place")
}
for _, v := range result.Metadata {
if v.Name == "cost_delta" && v.Value != "+a billion dollars" {
t.Error("bad metadata value")
}
}
})
t.Run("sensitive values", func(t *testing.T) {
run := setup(t)
run.Status = tfe.RunPlannedAndFinished
runs.EXPECT().Read(gomock.Any(), gomock.Any()).Return(&run, nil)
variables.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return(&vars, nil)
stateVersions.EXPECT().ReadCurrentWithOptions(gomock.Any(), "foo", gomock.Any()).Return(&sv, nil)
workingDirectory = path.Join(wd, "test_in_sensitive")
os.MkdirAll(workingDirectory, os.FileMode(0755))
input.Params.Sensitive = true
_, err := in(input)
if err != nil {
t.Error(err)
}
validateFileContents(t, path.Join(workingDirectory, "outputs", "bar"), "\"secretbar\"")
})
t.Run("error retrieving run", func(t *testing.T) {
run := setup(t)
runs.EXPECT().Read(gomock.Any(), gomock.Any()).Return(&run, fmt.Errorf("foo"))
_, err := in(input)
if err.Error() != "error retrieving run: foo" {
t.Errorf("unexpected error: %s", err)
}
})
}
func TestWritingFunctionErrors(t *testing.T) {
run := setup(t)
input, vars, sv, _ := inSetup()
wd, _ := os.Getwd()
workingDirectory = path.Join(wd, "test_output", "test_unwriteable")
os.RemoveAll(workingDirectory)
os.MkdirAll(workingDirectory, os.FileMode(0444))
err := writeStateOutputs(true)
if didntErrorWithSubstr(err, "creating run output directory") {
t.Errorf("expected error creating directory, got %s", err)
}
_ = os.Chmod(workingDirectory, os.FileMode(0755))
_ = os.MkdirAll(path.Join(workingDirectory, "outputs"), os.FileMode(0555))
_ = os.Chmod(workingDirectory, os.FileMode(0555))
stateVersions.EXPECT().ReadCurrentWithOptions(gomock.Any(), "foo", gomock.Any()).Return(&sv, fmt.Errorf("NO"))
err = writeStateOutputs(true)
if didntErrorWithSubstr(err, "getting current workspace state") {
t.Errorf("expected error retrieving state, got %s", err)
}
stateVersions.EXPECT().ReadCurrentWithOptions(gomock.Any(), "foo", gomock.Any()).Return(&sv, nil)
err = writeStateOutputs(true)
if didntErrorWithSubstr(err, "creating ") {
t.Errorf("expected error creating output file, got %s", err)
}
variables.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return(&vars, nil)
err = writeWorkspaceVariables()
if didntErrorWithSubstr(err, "creating output directories") {
t.Errorf("expected error creating directory, got %s", err)
}
_ = os.Chmod(workingDirectory, os.FileMode(0755))
_ = os.MkdirAll(path.Join(workingDirectory, "vars", "hcl"), os.FileMode(0444))
_ = os.MkdirAll(path.Join(workingDirectory, "env_vars"), os.FileMode(0444))
_ = os.Chmod(workingDirectory, os.FileMode(0444))
variables.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return(&vars, fmt.Errorf("NO"))
err = writeWorkspaceVariables()
if didntErrorWithSubstr(err, "retrieving workspace variables") {
t.Errorf("expected error listing vars, got %s", err)
}
variables.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Return(&vars, nil)
err = writeWorkspaceVariables()
if didntErrorWithSubstr(err, "creating ") {
t.Errorf("expected error writing var file, got %s", err)
}
err = writeJSONFile(math.Inf(1), "infinite.json")
if didntErrorWithSubstr(err, "marshaling infinite.json") {
t.Errorf("expected marshalling error, got %s", err)
}
err = writeJSONFile(input, "infinite.json")
if didntErrorWithSubstr(err, "creating ") {
t.Errorf("expected marshalling error, got %s", err)
}
run.Status = tfe.RunPlannedAndFinished
runs.EXPECT().Read(gomock.Any(), gomock.Any()).Return(&run, nil)
if _, err = in(input); didntErrorWithSubstr(err, "creating ") {
t.Errorf("expected error writing file, got %s", err)
}
// don't leave files with messed up permissions
_ = os.Chmod(workingDirectory, os.FileMode(0755))
_ = os.Chmod(path.Join(workingDirectory, "vars"), os.FileMode(0755))
_ = os.Chmod(path.Join(workingDirectory, "env_vars"), os.FileMode(0755))
_ = os.Chmod(path.Join(workingDirectory, "vars", "hcl"), os.FileMode(0755))
_ = os.Chmod(path.Join(workingDirectory, "outputs"), os.FileMode(0755))
}
func validateFileContents(t *testing.T, fileName string, expectedValue string) {
f, err := os.OpenFile(fileName, os.O_RDONLY, 0700)
if err != nil {
t.Errorf("expected %s didn't exist", fileName)
return
}
s, err := f.Stat()
if err != nil {
t.Errorf("could not stat %s", fileName)
return
}
byteVal := make([]byte, s.Size())
_, err = f.Read(byteVal)
val := string(byteVal)
if err != nil {
t.Errorf("couldn't read %s: %s", fileName, err)
return
} else if val != expectedValue {
t.Errorf("wrong value for %s: %s", fileName, val)
}
}