packer-cn/builder/amazon/chroot/copy_files_test.go

52 lines
1.1 KiB
Go
Raw Normal View History

2013-09-26 05:25:57 -04:00
package chroot
import (
2013-09-27 16:47:44 -04:00
"fmt"
2013-09-26 05:25:57 -04:00
"io/ioutil"
"os"
"runtime"
2013-09-26 05:25:57 -04:00
"testing"
"github.com/hashicorp/packer/packer-plugin-sdk/common"
2013-09-26 05:25:57 -04:00
)
func TestCopyFile(t *testing.T) {
if runtime.GOOS == "windows" {
return
}
2013-09-26 05:25:57 -04:00
first, err := ioutil.TempFile("", "copy_files_test")
if err != nil {
t.Fatalf("couldn't create temp file.")
}
defer os.Remove(first.Name())
newName := first.Name() + "-new"
payload := "copy_files_test.go payload"
if _, err = first.WriteString(payload); err != nil {
t.Fatalf("Couldn't write payload to first file.")
}
2013-09-27 07:55:19 -04:00
first.Sync()
2013-09-26 05:25:57 -04:00
cmd := common.ShellCommand(fmt.Sprintf("cp %s %s", first.Name(), newName))
2013-09-27 07:55:19 -04:00
if err := cmd.Run(); err != nil {
2013-09-26 05:25:57 -04:00
t.Fatalf("Couldn't copy file")
}
defer os.Remove(newName)
second, err := os.Open(newName)
if err != nil {
t.Fatalf("Couldn't open copied file.")
}
defer second.Close()
var copiedPayload = make([]byte, len(payload))
if _, err := second.Read(copiedPayload); err != nil {
t.Fatalf("Couldn't open copied file for reading.")
}
if string(copiedPayload) != payload {
t.Fatalf("payload not copied.")
}
}