github.com/jerryclinesmith/packer@v0.3.7/common/command/template_test.go (about) 1 package command 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "testing" 6 ) 7 8 func testTemplate() (*packer.Template, *packer.ComponentFinder) { 9 tplData := `{ 10 "builders": [ 11 { 12 "type": "foo" 13 }, 14 { 15 "type": "bar" 16 } 17 ] 18 }` 19 20 tpl, err := packer.ParseTemplate([]byte(tplData)) 21 if err != nil { 22 panic(err) 23 } 24 25 cf := &packer.ComponentFinder{ 26 Builder: func(string) (packer.Builder, error) { return new(packer.MockBuilder), nil }, 27 } 28 29 return tpl, cf 30 } 31 32 func TestBuildOptionsBuilds(t *testing.T) { 33 opts := new(BuildOptions) 34 bs, err := opts.Builds(testTemplate()) 35 if err != nil { 36 t.Fatalf("err: %s", err) 37 } 38 39 if len(bs) != 2 { 40 t.Fatalf("bad: %d", len(bs)) 41 } 42 } 43 44 func TestBuildOptionsBuilds_except(t *testing.T) { 45 opts := new(BuildOptions) 46 opts.Except = []string{"foo"} 47 48 bs, err := opts.Builds(testTemplate()) 49 if err != nil { 50 t.Fatalf("err: %s", err) 51 } 52 53 if len(bs) != 1 { 54 t.Fatalf("bad: %d", len(bs)) 55 } 56 57 if bs[0].Name() != "bar" { 58 t.Fatalf("bad: %s", bs[0].Name()) 59 } 60 } 61 62 func TestBuildOptionsBuilds_only(t *testing.T) { 63 opts := new(BuildOptions) 64 opts.Only = []string{"foo"} 65 66 bs, err := opts.Builds(testTemplate()) 67 if err != nil { 68 t.Fatalf("err: %s", err) 69 } 70 71 if len(bs) != 1 { 72 t.Fatalf("bad: %d", len(bs)) 73 } 74 75 if bs[0].Name() != "foo" { 76 t.Fatalf("bad: %s", bs[0].Name()) 77 } 78 } 79 80 func TestBuildOptionsBuilds_exceptNonExistent(t *testing.T) { 81 opts := new(BuildOptions) 82 opts.Except = []string{"i-dont-exist"} 83 84 _, err := opts.Builds(testTemplate()) 85 if err == nil { 86 t.Fatal("err should not be nil") 87 } 88 } 89 90 func TestBuildOptionsBuilds_onlyNonExistent(t *testing.T) { 91 opts := new(BuildOptions) 92 opts.Only = []string{"i-dont-exist"} 93 94 _, err := opts.Builds(testTemplate()) 95 if err == nil { 96 t.Fatal("err should not be nil") 97 } 98 } 99 100 func TestBuildOptionsValidate(t *testing.T) { 101 bf := new(BuildOptions) 102 103 err := bf.Validate() 104 if err != nil { 105 t.Fatalf("err: %s", err) 106 } 107 108 // Both set 109 bf.Except = make([]string, 1) 110 bf.Only = make([]string, 1) 111 err = bf.Validate() 112 if err == nil { 113 t.Fatal("should error") 114 } 115 116 // One set 117 bf.Except = make([]string, 1) 118 bf.Only = make([]string, 0) 119 err = bf.Validate() 120 if err != nil { 121 t.Fatalf("err: %s", err) 122 } 123 124 bf.Except = make([]string, 0) 125 bf.Only = make([]string, 1) 126 err = bf.Validate() 127 if err != nil { 128 t.Fatalf("err: %s", err) 129 } 130 } 131 132 func TestBuildOptionsValidate_userVarFiles(t *testing.T) { 133 bf := new(BuildOptions) 134 135 err := bf.Validate() 136 if err != nil { 137 t.Fatalf("err: %s", err) 138 } 139 140 // Non-existent file 141 bf.UserVarFiles = []string{"ireallyshouldntexistanywhere"} 142 err = bf.Validate() 143 if err == nil { 144 t.Fatal("should error") 145 } 146 }