2013-05-05 00:26:30 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2013-05-07 20:02:55 -04:00
|
|
|
"fmt"
|
2013-07-25 22:24:49 -04:00
|
|
|
"log"
|
2013-05-05 00:26:30 -04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
2013-05-07 20:02:55 -04:00
|
|
|
"time"
|
2017-08-07 13:20:01 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-20 13:21:29 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-05-05 00:26:30 -04:00
|
|
|
)
|
|
|
|
|
2013-05-10 20:01:24 -04:00
|
|
|
func helperProcess(s ...string) *exec.Cmd {
|
2013-05-05 00:26:30 -04:00
|
|
|
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:10:45 -04:00
|
|
|
defer os.Exit(0)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-08-07 13:20:01 -04:00
|
|
|
cmd, _ := args[0], args[1:]
|
2013-05-07 20:02:55 -04:00
|
|
|
switch cmd {
|
2013-09-22 12:51:14 -04:00
|
|
|
case "bad-version":
|
2013-12-11 15:24:45 -05:00
|
|
|
fmt.Printf("%s1|tcp|:1234\n", APIVersion)
|
2013-09-22 12:51:14 -04:00
|
|
|
<-make(chan int)
|
2013-05-09 01:25:47 -04:00
|
|
|
case "builder":
|
2013-12-10 16:47:18 -05:00
|
|
|
server, err := Server()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
server.RegisterBuilder(new(packer.MockBuilder))
|
|
|
|
server.Serve()
|
2013-05-11 13:46:17 -04:00
|
|
|
case "hook":
|
2013-12-10 16:47:18 -05:00
|
|
|
server, err := Server()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-11-20 13:21:29 -05:00
|
|
|
server.RegisterHook(new(packersdk.MockHook))
|
2013-12-10 16:47:18 -05:00
|
|
|
server.Serve()
|
2013-05-07 20:10:45 -04:00
|
|
|
case "invalid-rpc-address":
|
|
|
|
fmt.Println("lolinvalid")
|
2013-06-01 22:35:19 -04:00
|
|
|
case "mock":
|
2013-12-11 15:24:45 -05:00
|
|
|
fmt.Printf("%s|tcp|:1234\n", APIVersion)
|
2013-06-01 22:35:19 -04:00
|
|
|
<-make(chan int)
|
2013-06-18 16:49:07 -04:00
|
|
|
case "post-processor":
|
2013-12-10 16:47:18 -05:00
|
|
|
server, err := Server()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
server.RegisterPostProcessor(new(helperPostProcessor))
|
|
|
|
server.Serve()
|
2013-05-24 00:37:16 -04:00
|
|
|
case "provisioner":
|
2013-12-10 16:47:18 -05:00
|
|
|
server, err := Server()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
server.RegisterProvisioner(new(packer.MockProvisioner))
|
|
|
|
server.Serve()
|
2013-05-07 20:02:55 -04:00
|
|
|
case "start-timeout":
|
|
|
|
time.Sleep(1 * time.Minute)
|
2013-07-25 22:24:49 -04:00
|
|
|
os.Exit(1)
|
2013-07-30 13:38:48 -04:00
|
|
|
case "stderr":
|
2013-12-11 15:24:45 -05:00
|
|
|
fmt.Printf("%s|tcp|:1234\n", APIVersion)
|
2013-07-30 13:38:48 -04:00
|
|
|
log.Println("HELLO")
|
|
|
|
log.Println("WORLD")
|
2013-07-25 22:24:49 -04:00
|
|
|
case "stdin":
|
2013-12-11 15:24:45 -05:00
|
|
|
fmt.Printf("%s|tcp|:1234\n", APIVersion)
|
2013-07-25 22:24:49 -04:00
|
|
|
data := make([]byte, 5)
|
|
|
|
if _, err := os.Stdin.Read(data); err != nil {
|
|
|
|
log.Printf("stdin read error: %s", err)
|
|
|
|
os.Exit(100)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(data) == "hello" {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2013-05-07 20:02:55 -04:00
|
|
|
os.Exit(1)
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|