github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/command/push_test.go (about) 1 package command 2 3 import ( 4 "archive/tar" 5 "bytes" 6 "compress/gzip" 7 "fmt" 8 "io" 9 "path/filepath" 10 "reflect" 11 "sort" 12 "testing" 13 ) 14 15 func TestPush_noArgs(t *testing.T) { 16 c := &PushCommand{Meta: testMeta(t)} 17 code := c.Run(nil) 18 if code != 1 { 19 t.Fatalf("bad: %#v", code) 20 } 21 } 22 23 func TestPush_multiArgs(t *testing.T) { 24 c := &PushCommand{Meta: testMeta(t)} 25 code := c.Run([]string{"one", "two"}) 26 if code != 1 { 27 t.Fatalf("bad: %#v", code) 28 } 29 } 30 31 func TestPush(t *testing.T) { 32 var actual []string 33 var actualOpts *uploadOpts 34 uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { 35 actual = testArchive(t, r) 36 actualOpts = opts 37 38 doneCh := make(chan struct{}) 39 close(doneCh) 40 return doneCh, nil, nil 41 } 42 43 c := &PushCommand{ 44 Meta: testMeta(t), 45 uploadFn: uploadFn, 46 } 47 48 args := []string{filepath.Join(testFixture("push"), "template.json")} 49 if code := c.Run(args); code != 0 { 50 fatalCommand(t, c.Meta) 51 } 52 53 expected := []string{ 54 archiveTemplateEntry, 55 "template.json", 56 } 57 58 if !reflect.DeepEqual(actual, expected) { 59 t.Fatalf("bad: %#v", actual) 60 } 61 62 expectedBuilds := map[string]*uploadBuildInfo{ 63 "dummy": { 64 Type: "dummy", 65 }, 66 } 67 if !reflect.DeepEqual(actualOpts.Builds, expectedBuilds) { 68 t.Fatalf("bad: %#v", actualOpts.Builds) 69 } 70 } 71 72 func TestPush_builds(t *testing.T) { 73 var actualOpts *uploadOpts 74 uploadFn := func( 75 r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { 76 actualOpts = opts 77 78 doneCh := make(chan struct{}) 79 close(doneCh) 80 return doneCh, nil, nil 81 } 82 83 c := &PushCommand{ 84 Meta: testMeta(t), 85 uploadFn: uploadFn, 86 } 87 88 args := []string{filepath.Join(testFixture("push-builds"), "template.json")} 89 if code := c.Run(args); code != 0 { 90 fatalCommand(t, c.Meta) 91 } 92 93 expectedBuilds := map[string]*uploadBuildInfo{ 94 "dummy": { 95 Type: "dummy", 96 Artifact: true, 97 }, 98 "foo": { 99 Type: "dummy", 100 }, 101 } 102 if !reflect.DeepEqual(actualOpts.Builds, expectedBuilds) { 103 t.Fatalf("bad: %#v", actualOpts.Builds) 104 } 105 } 106 107 func TestPush_noName(t *testing.T) { 108 uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { 109 return nil, nil, nil 110 } 111 112 c := &PushCommand{ 113 Meta: testMeta(t), 114 uploadFn: uploadFn, 115 } 116 117 args := []string{filepath.Join(testFixture("push-no-name"), "template.json")} 118 if code := c.Run(args); code != 1 { 119 fatalCommand(t, c.Meta) 120 } 121 } 122 123 func TestPush_cliName(t *testing.T) { 124 var actual []string 125 uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { 126 actual = testArchive(t, r) 127 128 doneCh := make(chan struct{}) 129 close(doneCh) 130 return doneCh, nil, nil 131 } 132 133 c := &PushCommand{ 134 Meta: testMeta(t), 135 uploadFn: uploadFn, 136 } 137 138 args := []string{ 139 "-name=foo/bar", 140 filepath.Join(testFixture("push-no-name"), "template.json"), 141 } 142 143 if code := c.Run(args); code != 0 { 144 fatalCommand(t, c.Meta) 145 } 146 147 expected := []string{ 148 archiveTemplateEntry, 149 "template.json", 150 } 151 152 if !reflect.DeepEqual(actual, expected) { 153 t.Fatalf("bad: %#v", actual) 154 } 155 } 156 157 func TestPush_uploadError(t *testing.T) { 158 uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { 159 return nil, nil, fmt.Errorf("bad") 160 } 161 162 c := &PushCommand{ 163 Meta: testMeta(t), 164 uploadFn: uploadFn, 165 } 166 167 args := []string{filepath.Join(testFixture("push"), "template.json")} 168 if code := c.Run(args); code != 1 { 169 fatalCommand(t, c.Meta) 170 } 171 } 172 173 func TestPush_uploadErrorCh(t *testing.T) { 174 uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { 175 errCh := make(chan error, 1) 176 errCh <- fmt.Errorf("bad") 177 return nil, errCh, nil 178 } 179 180 c := &PushCommand{ 181 Meta: testMeta(t), 182 uploadFn: uploadFn, 183 } 184 185 args := []string{filepath.Join(testFixture("push"), "template.json")} 186 if code := c.Run(args); code != 1 { 187 fatalCommand(t, c.Meta) 188 } 189 } 190 191 func TestPush_vars(t *testing.T) { 192 var actualOpts *uploadOpts 193 uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { 194 actualOpts = opts 195 196 doneCh := make(chan struct{}) 197 close(doneCh) 198 return doneCh, nil, nil 199 } 200 201 c := &PushCommand{ 202 Meta: testMeta(t), 203 uploadFn: uploadFn, 204 } 205 206 args := []string{ 207 "-var", "name=foo/bar", 208 "-var", "one=two", 209 "-var-file", filepath.Join(testFixture("push-vars"), "vars.json"), 210 "-var", "overridden=yes", 211 "-sensitive", "super,secret", 212 filepath.Join(testFixture("push-vars"), "template.json"), 213 } 214 if code := c.Run(args); code != 0 { 215 fatalCommand(t, c.Meta) 216 } 217 218 if actualOpts.Slug != "foo/bar" { 219 t.Fatalf("bad slug: %s", actualOpts.Slug) 220 } 221 222 expected := map[string]string{ 223 "bar": "baz", 224 "name": "foo/bar", 225 "null": "", 226 "one": "two", 227 "overridden": "yes", 228 "super": "this should be secret", 229 "secret": "this one too", 230 } 231 if !reflect.DeepEqual(actualOpts.Vars, expected) { 232 t.Fatalf("bad vars: got %#v\n expected %#v\n", actualOpts.Vars, expected) 233 } 234 235 expected_sensitive := []string{"super", "secret"} 236 if !reflect.DeepEqual(actualOpts.SensitiveVars, expected_sensitive) { 237 t.Fatalf("bad vars: got %#v\n expected %#v\n", actualOpts.SensitiveVars, expected_sensitive) 238 } 239 } 240 241 func testArchive(t *testing.T, r io.Reader) []string { 242 // Finish the archiving process in-memory 243 var buf bytes.Buffer 244 if _, err := io.Copy(&buf, r); err != nil { 245 t.Fatalf("err: %s", err) 246 } 247 248 gzipR, err := gzip.NewReader(&buf) 249 if err != nil { 250 t.Fatalf("err: %s", err) 251 } 252 tarR := tar.NewReader(gzipR) 253 254 // Read all the entries 255 result := make([]string, 0, 5) 256 for { 257 hdr, err := tarR.Next() 258 if err == io.EOF { 259 break 260 } 261 if err != nil { 262 t.Fatalf("err: %s", err) 263 } 264 265 result = append(result, hdr.Name) 266 } 267 268 sort.Strings(result) 269 return result 270 }