From e1dadfc57a8e326f54ed138bf9f5f1d5275096fc Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Thu, 26 Sep 2013 02:25:57 -0700 Subject: [PATCH] Unit tests. --- builder/amazon/chroot/copy_files.go | 2 +- builder/amazon/chroot/copy_files_test.go | 42 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 builder/amazon/chroot/copy_files_test.go diff --git a/builder/amazon/chroot/copy_files.go b/builder/amazon/chroot/copy_files.go index ef5cd70b5..81be8a9c8 100644 --- a/builder/amazon/chroot/copy_files.go +++ b/builder/amazon/chroot/copy_files.go @@ -6,7 +6,7 @@ import ( ) func copySingle(dest string, src string, copyCommand string) error { - cpCommand := fmt.Sprintf("sudo cp -fn %s %s", src, dest) + cpCommand := fmt.Sprintf("%s %s %s", copyCommand, src, dest) localCmd := exec.Command("/bin/sh", "-c", cpCommand) if err := localCmd.Run(); err != nil { return err diff --git a/builder/amazon/chroot/copy_files_test.go b/builder/amazon/chroot/copy_files_test.go new file mode 100644 index 000000000..9a0bb5a4a --- /dev/null +++ b/builder/amazon/chroot/copy_files_test.go @@ -0,0 +1,42 @@ +package chroot + +import ( + "io/ioutil" + "log" + "os" + "testing" +) + +func TestCopyFile(t *testing.T) { + 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.") + } + + if err := copySingle(newName, first.Name(), "cp"); err != nil { + 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.") + } +}