github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/command/build_test.go (about) 1 package command 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/hashicorp/packer/builder/file" 10 "github.com/hashicorp/packer/packer" 11 ) 12 13 func TestBuildOnlyFileCommaFlags(t *testing.T) { 14 c := &BuildCommand{ 15 Meta: testMetaFile(t), 16 } 17 18 args := []string{ 19 "-only=chocolate,vanilla", 20 filepath.Join(testFixture("build-only"), "template.json"), 21 } 22 23 defer cleanup() 24 25 if code := c.Run(args); code != 0 { 26 fatalCommand(t, c.Meta) 27 } 28 29 if !fileExists("chocolate.txt") { 30 t.Error("Expected to find chocolate.txt") 31 } 32 if !fileExists("vanilla.txt") { 33 t.Error("Expected to find vanilla.txt") 34 } 35 if fileExists("cherry.txt") { 36 t.Error("Expected NOT to find cherry.txt") 37 } 38 } 39 40 func TestBuildStdin(t *testing.T) { 41 c := &BuildCommand{ 42 Meta: testMetaFile(t), 43 } 44 f, err := os.Open(filepath.Join(testFixture("build-only"), "template.json")) 45 if err != nil { 46 t.Fatal(err) 47 } 48 defer f.Close() 49 50 stdin := os.Stdin 51 os.Stdin = f 52 defer func() { os.Stdin = stdin }() 53 54 defer cleanup() 55 if code := c.Run([]string{"-"}); code != 0 { 56 fatalCommand(t, c.Meta) 57 } 58 59 if !fileExists("chocolate.txt") { 60 t.Error("Expected to find chocolate.txt") 61 } 62 if !fileExists("vanilla.txt") { 63 t.Error("Expected to find vanilla.txt") 64 } 65 if !fileExists("cherry.txt") { 66 t.Error("Expected to find cherry.txt") 67 } 68 } 69 70 func TestBuildOnlyFileMultipleFlags(t *testing.T) { 71 c := &BuildCommand{ 72 Meta: testMetaFile(t), 73 } 74 75 args := []string{ 76 "-only=chocolate", 77 "-only=cherry", 78 filepath.Join(testFixture("build-only"), "template.json"), 79 } 80 81 defer cleanup() 82 83 if code := c.Run(args); code != 0 { 84 fatalCommand(t, c.Meta) 85 } 86 87 if !fileExists("chocolate.txt") { 88 t.Error("Expected to find chocolate.txt") 89 } 90 if fileExists("vanilla.txt") { 91 t.Error("Expected NOT to find vanilla.txt") 92 } 93 if !fileExists("cherry.txt") { 94 t.Error("Expected to find cherry.txt") 95 } 96 } 97 98 func TestBuildExceptFileCommaFlags(t *testing.T) { 99 c := &BuildCommand{ 100 Meta: testMetaFile(t), 101 } 102 103 args := []string{ 104 "-except=chocolate", 105 filepath.Join(testFixture("build-only"), "template.json"), 106 } 107 108 defer cleanup() 109 110 if code := c.Run(args); code != 0 { 111 fatalCommand(t, c.Meta) 112 } 113 114 if fileExists("chocolate.txt") { 115 t.Error("Expected NOT to find chocolate.txt") 116 } 117 if !fileExists("vanilla.txt") { 118 t.Error("Expected to find vanilla.txt") 119 } 120 if !fileExists("cherry.txt") { 121 t.Error("Expected to find cherry.txt") 122 } 123 } 124 125 // fileExists returns true if the filename is found 126 func fileExists(filename string) bool { 127 if _, err := os.Stat(filename); err == nil { 128 return true 129 } 130 return false 131 } 132 133 // testCoreConfigBuilder creates a packer CoreConfig that has a file builder 134 // available. This allows us to test a builder that writes files to disk. 135 func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { 136 components := packer.ComponentFinder{ 137 Builder: func(n string) (packer.Builder, error) { 138 return &file.Builder{}, nil 139 }, 140 } 141 return &packer.CoreConfig{ 142 Components: components, 143 } 144 } 145 146 // testMetaFile creates a Meta object that includes a file builder 147 func testMetaFile(t *testing.T) Meta { 148 var out, err bytes.Buffer 149 return Meta{ 150 CoreConfig: testCoreConfigBuilder(t), 151 Ui: &packer.BasicUi{ 152 Writer: &out, 153 ErrorWriter: &err, 154 }, 155 } 156 } 157 158 func cleanup() { 159 os.RemoveAll("chocolate.txt") 160 os.RemoveAll("vanilla.txt") 161 os.RemoveAll("cherry.txt") 162 }