github.com/jerryclinesmith/packer@v0.3.7/packer/plugin/plugin_test.go (about) 1 package plugin 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/packer/packer" 6 "log" 7 "os" 8 "os/exec" 9 "testing" 10 "time" 11 ) 12 13 func helperProcess(s ...string) *exec.Cmd { 14 cs := []string{"-test.run=TestHelperProcess", "--"} 15 cs = append(cs, s...) 16 env := []string{ 17 "GO_WANT_HELPER_PROCESS=1", 18 "PACKER_PLUGIN_MIN_PORT=10000", 19 "PACKER_PLUGIN_MAX_PORT=25000", 20 } 21 22 cmd := exec.Command(os.Args[0], cs...) 23 cmd.Env = append(env, os.Environ()...) 24 return cmd 25 } 26 27 // This is not a real test. This is just a helper process kicked off by 28 // tests. 29 func TestHelperProcess(*testing.T) { 30 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { 31 return 32 } 33 34 defer os.Exit(0) 35 36 args := os.Args 37 for len(args) > 0 { 38 if args[0] == "--" { 39 args = args[1:] 40 break 41 } 42 43 args = args[1:] 44 } 45 46 if len(args) == 0 { 47 fmt.Fprintf(os.Stderr, "No command\n") 48 os.Exit(2) 49 } 50 51 cmd, args := args[0], args[1:] 52 switch cmd { 53 case "builder": 54 ServeBuilder(new(helperBuilder)) 55 case "command": 56 ServeCommand(new(helperCommand)) 57 case "hook": 58 ServeHook(new(packer.MockHook)) 59 case "invalid-rpc-address": 60 fmt.Println("lolinvalid") 61 case "mock": 62 fmt.Println(":1234") 63 <-make(chan int) 64 case "post-processor": 65 ServePostProcessor(new(helperPostProcessor)) 66 case "provisioner": 67 ServeProvisioner(new(packer.MockProvisioner)) 68 case "start-timeout": 69 time.Sleep(1 * time.Minute) 70 os.Exit(1) 71 case "stderr": 72 fmt.Println(":1234") 73 log.Println("HELLO") 74 log.Println("WORLD") 75 case "stdin": 76 fmt.Println(":1234") 77 data := make([]byte, 5) 78 if _, err := os.Stdin.Read(data); err != nil { 79 log.Printf("stdin read error: %s", err) 80 os.Exit(100) 81 } 82 83 if string(data) == "hello" { 84 os.Exit(0) 85 } 86 87 os.Exit(1) 88 default: 89 fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd) 90 os.Exit(2) 91 } 92 }