mirror of https://github.com/hashicorp/packer
Merge pull request #432 from mwhooker/chroot_cmd
build/amazon/chroot: command_wrapper to support sudo-lesspull/919/head
commit
707fe57edd
@ -0,0 +1,19 @@
|
||||
package chroot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func ChrootCommand(chroot string, command string) *exec.Cmd {
|
||||
cmd := fmt.Sprintf("sudo chroot %s", chroot)
|
||||
return ShellCommand(cmd, command)
|
||||
}
|
||||
|
||||
func ShellCommand(commands ...string) *exec.Cmd {
|
||||
cmds := append([]string{"-c"}, commands...)
|
||||
cmd := exec.Command("/bin/sh", cmds...)
|
||||
log.Printf("ShellCommand: %s %v", cmd.Path, cmd.Args[1:])
|
||||
return cmd
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package chroot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"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.")
|
||||
}
|
||||
first.Sync()
|
||||
|
||||
cmd := ShellCommand(fmt.Sprintf("cp %s %s", first.Name(), newName))
|
||||
if err := cmd.Run(); 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.")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue