2013-05-05 00:26:30 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2013-05-07 20:02:55 -04:00
|
|
|
"fmt"
|
2013-05-05 00:26:30 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
2013-05-07 20:02:55 -04:00
|
|
|
"time"
|
2013-05-05 00:26:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type helperCommand byte
|
|
|
|
|
|
|
|
func (helperCommand) Run(packer.Environment, []string) int {
|
|
|
|
return 42
|
|
|
|
}
|
|
|
|
|
|
|
|
func (helperCommand) Synopsis() string {
|
|
|
|
return "1"
|
|
|
|
}
|
|
|
|
|
|
|
|
func helperProcess(s... string) *exec.Cmd {
|
|
|
|
cs := []string{"-test.run=TestHelperProcess", "--"}
|
|
|
|
cs = append(cs, s...)
|
2013-05-06 18:27:44 -04:00
|
|
|
env := []string{
|
|
|
|
"GO_WANT_HELPER_PROCESS=1",
|
|
|
|
"PACKER_PLUGIN_MIN_PORT=10000",
|
|
|
|
"PACKER_PLUGIN_MAX_PORT=25000",
|
|
|
|
}
|
|
|
|
|
2013-05-05 00:26:30 -04:00
|
|
|
cmd := exec.Command(os.Args[0], cs...)
|
2013-05-06 18:27:44 -04:00
|
|
|
cmd.Env = append(env, os.Environ()...)
|
2013-05-05 00:26:30 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is not a real test. This is just a helper process kicked off by
|
|
|
|
// tests.
|
|
|
|
func TestHelperProcess(*testing.T) {
|
|
|
|
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-07 20:02:55 -04:00
|
|
|
args := os.Args
|
|
|
|
for len(args) > 0 {
|
|
|
|
if args[0] == "--" {
|
|
|
|
args = args[1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Fprintf(os.Stderr, "No command\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd, args := args[0], args[1:]
|
|
|
|
switch cmd {
|
|
|
|
case "command":
|
|
|
|
ServeCommand(new(helperCommand))
|
|
|
|
case "start-timeout":
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
os.Exit(1)
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|