github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/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": &uploadBuildInfo{
    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": &uploadBuildInfo{
    95  			Type:     "dummy",
    96  			Artifact: true,
    97  		},
    98  		"foo": &uploadBuildInfo{
    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  	var actualOpts *uploadOpts
   126  	uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
   127  		actual = testArchive(t, r)
   128  		actualOpts = opts
   129  
   130  		doneCh := make(chan struct{})
   131  		close(doneCh)
   132  		return doneCh, nil, nil
   133  	}
   134  
   135  	c := &PushCommand{
   136  		Meta:     testMeta(t),
   137  		uploadFn: uploadFn,
   138  	}
   139  
   140  	args := []string{
   141  		"-name=foo/bar",
   142  		filepath.Join(testFixture("push-no-name"), "template.json"),
   143  	}
   144  
   145  	if code := c.Run(args); code != 0 {
   146  		fatalCommand(t, c.Meta)
   147  	}
   148  
   149  	expected := []string{
   150  		archiveTemplateEntry,
   151  		"template.json",
   152  	}
   153  
   154  	if !reflect.DeepEqual(actual, expected) {
   155  		t.Fatalf("bad: %#v", actual)
   156  	}
   157  }
   158  
   159  func TestPush_uploadError(t *testing.T) {
   160  	uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
   161  		return nil, nil, fmt.Errorf("bad")
   162  	}
   163  
   164  	c := &PushCommand{
   165  		Meta:     testMeta(t),
   166  		uploadFn: uploadFn,
   167  	}
   168  
   169  	args := []string{filepath.Join(testFixture("push"), "template.json")}
   170  	if code := c.Run(args); code != 1 {
   171  		fatalCommand(t, c.Meta)
   172  	}
   173  }
   174  
   175  func TestPush_uploadErrorCh(t *testing.T) {
   176  	uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
   177  		errCh := make(chan error, 1)
   178  		errCh <- fmt.Errorf("bad")
   179  		return nil, errCh, nil
   180  	}
   181  
   182  	c := &PushCommand{
   183  		Meta:     testMeta(t),
   184  		uploadFn: uploadFn,
   185  	}
   186  
   187  	args := []string{filepath.Join(testFixture("push"), "template.json")}
   188  	if code := c.Run(args); code != 1 {
   189  		fatalCommand(t, c.Meta)
   190  	}
   191  }
   192  
   193  func TestPush_vars(t *testing.T) {
   194  	var actualOpts *uploadOpts
   195  	uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
   196  		actualOpts = opts
   197  
   198  		doneCh := make(chan struct{})
   199  		close(doneCh)
   200  		return doneCh, nil, nil
   201  	}
   202  
   203  	c := &PushCommand{
   204  		Meta:     testMeta(t),
   205  		uploadFn: uploadFn,
   206  	}
   207  
   208  	args := []string{
   209  		"-var", "name=foo/bar",
   210  		filepath.Join(testFixture("push-vars"), "template.json"),
   211  	}
   212  	if code := c.Run(args); code != 0 {
   213  		fatalCommand(t, c.Meta)
   214  	}
   215  
   216  	expected := "foo/bar"
   217  	if actualOpts.Slug != expected {
   218  		t.Fatalf("bad: %#v", actualOpts.Slug)
   219  	}
   220  }
   221  
   222  func testArchive(t *testing.T, r io.Reader) []string {
   223  	// Finish the archiving process in-memory
   224  	var buf bytes.Buffer
   225  	if _, err := io.Copy(&buf, r); err != nil {
   226  		t.Fatalf("err: %s", err)
   227  	}
   228  
   229  	gzipR, err := gzip.NewReader(&buf)
   230  	if err != nil {
   231  		t.Fatalf("err: %s", err)
   232  	}
   233  	tarR := tar.NewReader(gzipR)
   234  
   235  	// Read all the entries
   236  	result := make([]string, 0, 5)
   237  	for {
   238  		hdr, err := tarR.Next()
   239  		if err == io.EOF {
   240  			break
   241  		}
   242  		if err != nil {
   243  			t.Fatalf("err: %s", err)
   244  		}
   245  
   246  		result = append(result, hdr.Name)
   247  	}
   248  
   249  	sort.Strings(result)
   250  	return result
   251  }