2014-10-28 18:09:22 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-10-28 22:29:51 -04:00
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2014-12-03 13:01:00 -05:00
|
|
|
"fmt"
|
2014-10-28 22:29:51 -04:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
2014-10-28 18:09:22 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPush_noArgs(t *testing.T) {
|
|
|
|
c := &PushCommand{Meta: testMeta(t)}
|
|
|
|
code := c.Run(nil)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %#v", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush_multiArgs(t *testing.T) {
|
|
|
|
c := &PushCommand{Meta: testMeta(t)}
|
|
|
|
code := c.Run([]string{"one", "two"})
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %#v", code)
|
|
|
|
}
|
|
|
|
}
|
2014-10-28 22:29:51 -04:00
|
|
|
|
|
|
|
func TestPush(t *testing.T) {
|
2014-10-28 22:34:19 -04:00
|
|
|
var actual []string
|
2014-10-28 22:29:51 -04:00
|
|
|
var actualOpts *uploadOpts
|
|
|
|
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
|
2014-10-28 22:34:19 -04:00
|
|
|
actual = testArchive(t, r)
|
2014-10-28 22:29:51 -04:00
|
|
|
actualOpts = opts
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
close(doneCh)
|
|
|
|
return doneCh, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
uploadFn: uploadFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{filepath.Join(testFixture("push"), "template.json")}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
archiveTemplateEntry,
|
|
|
|
"template.json",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
2014-10-28 22:43:41 -04:00
|
|
|
|
2014-12-03 13:01:00 -05:00
|
|
|
expectedBuilds := map[string]*uploadBuildInfo{
|
2016-11-01 17:08:04 -04:00
|
|
|
"dummy": {
|
2014-12-03 13:01:00 -05:00
|
|
|
Type: "dummy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actualOpts.Builds, expectedBuilds) {
|
|
|
|
t.Fatalf("bad: %#v", actualOpts.Builds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush_builds(t *testing.T) {
|
|
|
|
var actualOpts *uploadOpts
|
|
|
|
uploadFn := func(
|
|
|
|
r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
|
|
|
|
actualOpts = opts
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
close(doneCh)
|
|
|
|
return doneCh, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
uploadFn: uploadFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{filepath.Join(testFixture("push-builds"), "template.json")}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedBuilds := map[string]*uploadBuildInfo{
|
2016-11-01 17:08:04 -04:00
|
|
|
"dummy": {
|
2014-12-03 13:01:00 -05:00
|
|
|
Type: "dummy",
|
|
|
|
Artifact: true,
|
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
"foo": {
|
2014-12-03 13:01:00 -05:00
|
|
|
Type: "dummy",
|
|
|
|
},
|
|
|
|
}
|
2014-10-28 22:43:41 -04:00
|
|
|
if !reflect.DeepEqual(actualOpts.Builds, expectedBuilds) {
|
|
|
|
t.Fatalf("bad: %#v", actualOpts.Builds)
|
|
|
|
}
|
2014-10-28 22:29:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush_noName(t *testing.T) {
|
|
|
|
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
uploadFn: uploadFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{filepath.Join(testFixture("push-no-name"), "template.json")}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 15:53:57 -04:00
|
|
|
func TestPush_cliName(t *testing.T) {
|
|
|
|
var actual []string
|
|
|
|
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
|
|
|
|
actual = testArchive(t, r)
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
close(doneCh)
|
|
|
|
return doneCh, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
uploadFn: uploadFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-name=foo/bar",
|
|
|
|
filepath.Join(testFixture("push-no-name"), "template.json"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
archiveTemplateEntry,
|
|
|
|
"template.json",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 22:29:51 -04:00
|
|
|
func TestPush_uploadError(t *testing.T) {
|
|
|
|
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
|
|
|
|
return nil, nil, fmt.Errorf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
uploadFn: uploadFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{filepath.Join(testFixture("push"), "template.json")}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush_uploadErrorCh(t *testing.T) {
|
|
|
|
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
errCh <- fmt.Errorf("bad")
|
|
|
|
return nil, errCh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
uploadFn: uploadFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{filepath.Join(testFixture("push"), "template.json")}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:35:55 -04:00
|
|
|
func TestPush_vars(t *testing.T) {
|
|
|
|
var actualOpts *uploadOpts
|
|
|
|
uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) {
|
|
|
|
actualOpts = opts
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
close(doneCh)
|
|
|
|
return doneCh, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &PushCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
uploadFn: uploadFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var", "name=foo/bar",
|
2016-01-12 11:05:44 -05:00
|
|
|
"-var", "one=two",
|
2016-10-31 10:35:28 -04:00
|
|
|
"-var-file", filepath.Join(testFixture("push-vars"), "vars.json"),
|
2016-01-15 11:57:30 -05:00
|
|
|
"-var", "overridden=yes",
|
2015-05-29 18:35:55 -04:00
|
|
|
filepath.Join(testFixture("push-vars"), "template.json"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
2016-01-12 11:05:44 -05:00
|
|
|
if actualOpts.Slug != "foo/bar" {
|
2016-10-31 10:35:28 -04:00
|
|
|
t.Fatalf("bad slug: %s", actualOpts.Slug)
|
2015-05-29 18:35:55 -04:00
|
|
|
}
|
2016-01-12 11:05:44 -05:00
|
|
|
|
|
|
|
expected := map[string]string{
|
2016-01-15 11:57:30 -05:00
|
|
|
"bar": "baz",
|
|
|
|
"name": "foo/bar",
|
|
|
|
"null": "",
|
|
|
|
"one": "two",
|
|
|
|
"overridden": "yes",
|
2016-01-12 11:05:44 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actualOpts.Vars, expected) {
|
2016-10-31 10:35:28 -04:00
|
|
|
t.Fatalf("bad vars: got %#v\n expected %#v\n", actualOpts.Vars, expected)
|
2016-01-12 11:05:44 -05:00
|
|
|
}
|
2015-05-29 18:35:55 -04:00
|
|
|
}
|
|
|
|
|
2014-10-28 22:29:51 -04:00
|
|
|
func testArchive(t *testing.T, r io.Reader) []string {
|
|
|
|
// Finish the archiving process in-memory
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := io.Copy(&buf, r); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gzipR, err := gzip.NewReader(&buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tarR := tar.NewReader(gzipR)
|
|
|
|
|
|
|
|
// Read all the entries
|
|
|
|
result := make([]string, 0, 5)
|
|
|
|
for {
|
|
|
|
hdr, err := tarR.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, hdr.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(result)
|
|
|
|
return result
|
|
|
|
}
|