forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_create_floppy_test.go
298 lines (241 loc) · 7.97 KB
/
step_create_floppy_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
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
289
290
291
292
293
294
295
296
297
298
package common
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
const TestFixtures = "test-fixtures"
// utility function for returning a directory structure as a list of strings
func getDirectory(path string) []string {
var result []string
walk := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && !strings.HasSuffix(path, "/") {
path = path + "/"
}
result = append(result, filepath.ToSlash(path))
return nil
}
filepath.Walk(path, walk)
return result
}
func TestStepCreateFloppy_Impl(t *testing.T) {
var raw interface{}
raw = new(StepCreateFloppy)
if _, ok := raw.(multistep.Step); !ok {
t.Fatalf("StepCreateFloppy should be a step")
}
}
func testStepCreateFloppyState(t *testing.T) multistep.StateBag {
state := new(multistep.BasicStateBag)
state.Put("ui", &packer.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
})
return state
}
func TestStepCreateFloppy(t *testing.T) {
state := testStepCreateFloppyState(t)
step := new(StepCreateFloppy)
dir, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(dir)
count := 10
expected := count
files := make([]string, count)
prefix := "exists"
ext := ".tmp"
for i := 0; i < expected; i++ {
files[i] = path.Join(dir, prefix+strconv.Itoa(i)+ext)
_, err := os.Create(files[i])
if err != nil {
t.Fatalf("err: %s", err)
}
}
lists := [][]string{
files,
{dir + string(os.PathSeparator) + prefix + "*" + ext},
{dir + string(os.PathSeparator) + prefix + "?" + ext},
{dir + string(os.PathSeparator) + prefix + "[0123456789]" + ext},
{dir + string(os.PathSeparator) + prefix + "[0-9]" + ext},
{dir + string(os.PathSeparator)},
{dir},
}
for _, step.Files = range lists {
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v for %v", action, step.Files)
}
if _, ok := state.GetOk("error"); ok {
t.Fatalf("state should be ok for %v", step.Files)
}
floppy_path := state.Get("floppy_path").(string)
if _, err := os.Stat(floppy_path); err != nil {
t.Fatalf("file not found: %s for %v", floppy_path, step.Files)
}
if len(step.FilesAdded) != expected {
t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
}
step.Cleanup(state)
if _, err := os.Stat(floppy_path); err == nil {
t.Fatalf("file found: %s for %v", floppy_path, step.Files)
}
}
}
func xxxTestStepCreateFloppy_missing(t *testing.T) {
state := testStepCreateFloppyState(t)
step := new(StepCreateFloppy)
dir, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(dir)
count := 2
expected := 0
files := make([]string, count)
prefix := "missing"
for i := 0; i < count; i++ {
files[i] = path.Join(dir, prefix+strconv.Itoa(i))
}
lists := [][]string{
files,
}
for _, step.Files = range lists {
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
t.Fatalf("bad action: %#v for %v", action, step.Files)
}
if _, ok := state.GetOk("error"); !ok {
t.Fatalf("state should not be ok for %v", step.Files)
}
floppy_path := state.Get("floppy_path")
if floppy_path != nil {
t.Fatalf("floppy_path is not nil for %v", step.Files)
}
if len(step.FilesAdded) != expected {
t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
}
}
}
func xxxTestStepCreateFloppy_notfound(t *testing.T) {
state := testStepCreateFloppyState(t)
step := new(StepCreateFloppy)
dir, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(dir)
count := 2
expected := 0
files := make([]string, count)
prefix := "notfound"
for i := 0; i < count; i++ {
files[i] = path.Join(dir, prefix+strconv.Itoa(i))
}
lists := [][]string{
{dir + string(os.PathSeparator) + prefix + "*"},
{dir + string(os.PathSeparator) + prefix + "?"},
{dir + string(os.PathSeparator) + prefix + "[0123456789]"},
{dir + string(os.PathSeparator) + prefix + "[0-9]"},
{dir + string(os.PathSeparator)},
{dir},
}
for _, step.Files = range lists {
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v for %v", action, step.Files)
}
if _, ok := state.GetOk("error"); ok {
t.Fatalf("state should be ok for %v", step.Files)
}
floppy_path := state.Get("floppy_path").(string)
if _, err := os.Stat(floppy_path); err != nil {
t.Fatalf("file not found: %s for %v", floppy_path, step.Files)
}
if len(step.FilesAdded) != expected {
t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
}
step.Cleanup(state)
if _, err := os.Stat(floppy_path); err == nil {
t.Fatalf("file found: %s for %v", floppy_path, step.Files)
}
}
}
func TestStepCreateFloppyDirectories(t *testing.T) {
const TestName = "floppy-hier"
// file-system hierarchies
var basePath = filepath.Join(".", TestFixtures, TestName)
type contentsTest struct {
dirs []string
result []string
}
// keep in mind that .FilesAdded doesn't keep track of the target filename or directories, but rather the source filename.
directories := [][]contentsTest{
{
{dirs: []string{"file1", "file2", "file3"}, result: []string{"file1", "file2", "file3"}},
{dirs: []string{"file?"}, result: []string{"file1", "file2", "file3"}},
{dirs: []string{"*"}, result: []string{"file1", "file2", "file3"}},
},
{
{dirs: []string{"dir1"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
{dirs: []string{"dir1/file1", "dir1/file2", "dir1/file3"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
{dirs: []string{"*"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
{dirs: []string{"*/*"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
},
{
{dirs: []string{"dir1"}, result: []string{"dir1/file1", "dir1/subdir1/file1", "dir1/subdir1/file2"}},
{dirs: []string{"dir2/*"}, result: []string{"dir2/subdir1/file1", "dir2/subdir1/file2"}},
{dirs: []string{"dir2/subdir1"}, result: []string{"dir2/subdir1/file1", "dir2/subdir1/file2"}},
{dirs: []string{"dir?"}, result: []string{"dir1/file1", "dir1/subdir1/file1", "dir1/subdir1/file2", "dir2/subdir1/file1", "dir2/subdir1/file2"}},
},
}
// create the hierarchy for each file
for i := 0; i < 2; i++ {
dir := filepath.Join(basePath, fmt.Sprintf("test-%d", i))
for _, test := range directories[i] {
// create a new state and step
state := testStepCreateFloppyState(t)
step := new(StepCreateFloppy)
// modify step.Directories with ones from testcase
step.Directories = []string{}
for _, c := range test.dirs {
step.Directories = append(step.Directories, filepath.Join(dir, filepath.FromSlash(c)))
}
log.Println(fmt.Sprintf("Trying against floppy_dirs : %v", step.Directories))
// run the step
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v for %v : %v", action, step.Directories, state.Get("error"))
}
if _, ok := state.GetOk("error"); ok {
t.Fatalf("state should be ok for %v : %v", step.Directories, state.Get("error"))
}
floppy_path := state.Get("floppy_path").(string)
if _, err := os.Stat(floppy_path); err != nil {
t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Directories, err)
}
// check the FilesAdded array to see if it matches
for _, rpath := range test.result {
fpath := filepath.Join(dir, filepath.FromSlash(rpath))
if !step.FilesAdded[fpath] {
t.Fatalf("unable to find file: %s for %v", fpath, step.Directories)
}
}
// cleanup the step
step.Cleanup(state)
if _, err := os.Stat(floppy_path); err == nil {
t.Fatalf("file found: %s for %v", floppy_path, step.Directories)
}
}
}
}