github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/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 "bad-version": 54 fmt.Printf("%s1|tcp|:1234\n", APIVersion) 55 <-make(chan int) 56 case "builder": 57 server, err := Server() 58 if err != nil { 59 log.Printf("[ERR] %s", err) 60 os.Exit(1) 61 } 62 server.RegisterBuilder(new(packer.MockBuilder)) 63 server.Serve() 64 case "command": 65 server, err := Server() 66 if err != nil { 67 log.Printf("[ERR] %s", err) 68 os.Exit(1) 69 } 70 server.RegisterCommand(new(helperCommand)) 71 server.Serve() 72 case "hook": 73 server, err := Server() 74 if err != nil { 75 log.Printf("[ERR] %s", err) 76 os.Exit(1) 77 } 78 server.RegisterHook(new(packer.MockHook)) 79 server.Serve() 80 case "invalid-rpc-address": 81 fmt.Println("lolinvalid") 82 case "mock": 83 fmt.Printf("%s|tcp|:1234\n", APIVersion) 84 <-make(chan int) 85 case "post-processor": 86 server, err := Server() 87 if err != nil { 88 log.Printf("[ERR] %s", err) 89 os.Exit(1) 90 } 91 server.RegisterPostProcessor(new(helperPostProcessor)) 92 server.Serve() 93 case "provisioner": 94 server, err := Server() 95 if err != nil { 96 log.Printf("[ERR] %s", err) 97 os.Exit(1) 98 } 99 server.RegisterProvisioner(new(packer.MockProvisioner)) 100 server.Serve() 101 case "start-timeout": 102 time.Sleep(1 * time.Minute) 103 os.Exit(1) 104 case "stderr": 105 fmt.Printf("%s|tcp|:1234\n", APIVersion) 106 log.Println("HELLO") 107 log.Println("WORLD") 108 case "stdin": 109 fmt.Printf("%s|tcp|:1234\n", APIVersion) 110 data := make([]byte, 5) 111 if _, err := os.Stdin.Read(data); err != nil { 112 log.Printf("stdin read error: %s", err) 113 os.Exit(100) 114 } 115 116 if string(data) == "hello" { 117 os.Exit(0) 118 } 119 120 os.Exit(1) 121 default: 122 fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd) 123 os.Exit(2) 124 } 125 }