github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/common/step_create_floppy_test.go (about) 1 package common 2 3 import ( 4 "bytes" 5 "github.com/mitchellh/multistep" 6 "github.com/mitchellh/packer/packer" 7 "io/ioutil" 8 "os" 9 "path" 10 "strconv" 11 "testing" 12 ) 13 14 func TestStepCreateFloppy_Impl(t *testing.T) { 15 var raw interface{} 16 raw = new(StepCreateFloppy) 17 if _, ok := raw.(multistep.Step); !ok { 18 t.Fatalf("StepCreateFloppy should be a step") 19 } 20 } 21 22 func testStepCreateFloppyState(t *testing.T) multistep.StateBag { 23 state := new(multistep.BasicStateBag) 24 state.Put("ui", &packer.BasicUi{ 25 Reader: new(bytes.Buffer), 26 Writer: new(bytes.Buffer), 27 }) 28 return state 29 } 30 31 func TestStepCreateFloppy(t *testing.T) { 32 state := testStepCreateFloppyState(t) 33 step := new(StepCreateFloppy) 34 35 dir, err := ioutil.TempDir("", "packer") 36 if err != nil { 37 t.Fatalf("err: %s", err) 38 } 39 defer os.RemoveAll(dir) 40 41 count := 10 42 expected := count 43 files := make([]string, count) 44 45 prefix := "exists" 46 ext := ".tmp" 47 48 for i := 0; i < expected; i++ { 49 files[i] = path.Join(dir, prefix+strconv.Itoa(i)+ext) 50 51 _, err := os.Create(files[i]) 52 if err != nil { 53 t.Fatalf("err: %s", err) 54 } 55 } 56 57 lists := [][]string{ 58 files, 59 []string{dir + string(os.PathSeparator) + prefix + "*" + ext}, 60 []string{dir + string(os.PathSeparator) + prefix + "?" + ext}, 61 []string{dir + string(os.PathSeparator) + prefix + "[0123456789]" + ext}, 62 []string{dir + string(os.PathSeparator) + prefix + "[0-9]" + ext}, 63 []string{dir + string(os.PathSeparator)}, 64 []string{dir}, 65 } 66 67 for _, step.Files = range lists { 68 if action := step.Run(state); action != multistep.ActionContinue { 69 t.Fatalf("bad action: %#v for %v", action, step.Files) 70 } 71 72 if _, ok := state.GetOk("error"); ok { 73 t.Fatalf("state should be ok for %v", step.Files) 74 } 75 76 floppy_path := state.Get("floppy_path").(string) 77 78 if _, err := os.Stat(floppy_path); err != nil { 79 t.Fatal("file not found: %s for %v", floppy_path, step.Files) 80 } 81 82 if len(step.FilesAdded) != expected { 83 t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files) 84 } 85 86 step.Cleanup(state) 87 88 if _, err := os.Stat(floppy_path); err == nil { 89 t.Fatal("file found: %s for %v", floppy_path, step.Files) 90 } 91 } 92 } 93 94 func xxxTestStepCreateFloppy_missing(t *testing.T) { 95 state := testStepCreateFloppyState(t) 96 step := new(StepCreateFloppy) 97 98 dir, err := ioutil.TempDir("", "packer") 99 if err != nil { 100 t.Fatalf("err: %s", err) 101 } 102 defer os.RemoveAll(dir) 103 104 count := 2 105 expected := 0 106 files := make([]string, count) 107 108 prefix := "missing" 109 110 for i := 0; i < count; i++ { 111 files[i] = path.Join(dir, prefix+strconv.Itoa(i)) 112 } 113 114 lists := [][]string{ 115 files, 116 } 117 118 for _, step.Files = range lists { 119 if action := step.Run(state); action != multistep.ActionHalt { 120 t.Fatalf("bad action: %#v for %v", action, step.Files) 121 } 122 123 if _, ok := state.GetOk("error"); !ok { 124 t.Fatalf("state should not be ok for %v", step.Files) 125 } 126 127 floppy_path := state.Get("floppy_path") 128 129 if floppy_path != nil { 130 t.Fatalf("floppy_path is not nil for %v", step.Files) 131 } 132 133 if len(step.FilesAdded) != expected { 134 t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files) 135 } 136 } 137 } 138 139 func xxxTestStepCreateFloppy_notfound(t *testing.T) { 140 state := testStepCreateFloppyState(t) 141 step := new(StepCreateFloppy) 142 143 dir, err := ioutil.TempDir("", "packer") 144 if err != nil { 145 t.Fatalf("err: %s", err) 146 } 147 defer os.RemoveAll(dir) 148 149 count := 2 150 expected := 0 151 files := make([]string, count) 152 153 prefix := "notfound" 154 155 for i := 0; i < count; i++ { 156 files[i] = path.Join(dir, prefix+strconv.Itoa(i)) 157 } 158 159 lists := [][]string{ 160 []string{dir + string(os.PathSeparator) + prefix + "*"}, 161 []string{dir + string(os.PathSeparator) + prefix + "?"}, 162 []string{dir + string(os.PathSeparator) + prefix + "[0123456789]"}, 163 []string{dir + string(os.PathSeparator) + prefix + "[0-9]"}, 164 []string{dir + string(os.PathSeparator)}, 165 []string{dir}, 166 } 167 168 for _, step.Files = range lists { 169 if action := step.Run(state); action != multistep.ActionContinue { 170 t.Fatalf("bad action: %#v for %v", action, step.Files) 171 } 172 173 if _, ok := state.GetOk("error"); ok { 174 t.Fatalf("state should be ok for %v", step.Files) 175 } 176 177 floppy_path := state.Get("floppy_path").(string) 178 179 if _, err := os.Stat(floppy_path); err != nil { 180 t.Fatal("file not found: %s for %v", floppy_path, step.Files) 181 } 182 183 if len(step.FilesAdded) != expected { 184 t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files) 185 } 186 187 step.Cleanup(state) 188 189 if _, err := os.Stat(floppy_path); err == nil { 190 t.Fatal("file found: %s for %v", floppy_path, step.Files) 191 } 192 } 193 }