github.phpd.cn/hashicorp/packer@v1.3.2/builder/file/builder_test.go (about) 1 package file 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "testing" 7 8 builderT "github.com/hashicorp/packer/helper/builder/testing" 9 "github.com/hashicorp/packer/packer" 10 ) 11 12 func TestBuilder_implBuilder(t *testing.T) { 13 var _ packer.Builder = new(Builder) 14 } 15 16 func TestBuilderFileAcc_content(t *testing.T) { 17 builderT.Test(t, builderT.TestCase{ 18 Builder: &Builder{}, 19 Template: fileContentTest, 20 Check: checkContent, 21 }) 22 } 23 24 func TestBuilderFileAcc_copy(t *testing.T) { 25 builderT.Test(t, builderT.TestCase{ 26 Builder: &Builder{}, 27 Template: fileCopyTest, 28 Check: checkCopy, 29 }) 30 } 31 32 func checkContent(artifacts []packer.Artifact) error { 33 content, err := ioutil.ReadFile("contentTest.txt") 34 if err != nil { 35 return err 36 } 37 contentString := string(content) 38 if contentString != "hello world!" { 39 return fmt.Errorf("Unexpected file contents: %s", contentString) 40 } 41 return nil 42 } 43 44 func checkCopy(artifacts []packer.Artifact) error { 45 content, err := ioutil.ReadFile("copyTest.txt") 46 if err != nil { 47 return err 48 } 49 contentString := string(content) 50 if contentString != "Hello world.\n" { 51 return fmt.Errorf("Unexpected file contents: %s", contentString) 52 } 53 return nil 54 } 55 56 const fileContentTest = ` 57 { 58 "builders": [ 59 { 60 "type":"test", 61 "target":"contentTest.txt", 62 "content":"hello world!" 63 } 64 ] 65 } 66 ` 67 68 const fileCopyTest = ` 69 { 70 "builders": [ 71 { 72 "type":"test", 73 "target":"copyTest.txt", 74 "source":"test-fixtures/artifact.txt" 75 } 76 ] 77 } 78 `