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