func realStart(cmd *cobra.Command, args []string) error { err := syscall.Setpgid(0, 0) if err != nil { glog.Errorln("Error setting process group ID") return err } socket := Viper.GetString("socket") os.Remove(socket) err = makeSocketDir(socket) if err != nil { glog.Errorln("Error creating directory for socket %q", socket) } l, err := server.NewUnixListener(Viper) defer l.Close() if err != nil { glog.Errorln("Error opening listening socket:", err) return err } server.Run(Viper, l) os.Remove(Viper.GetString("socket")) return nil }
// Integration testing client+server with an actual unix socket. func TestRunConnection(t *testing.T) { v := makeTestViper() l, err := server.NewUnixListener(v) if err != nil { t.Fatal(err) } defer l.Close() runFinished := make(chan struct{}) go func() { server.Run(v, l) close(runFinished) }() c, err := client.NewUnixConn(v) if err != nil { t.Fatal(err) } defer c.Close() req := &server.Request{ Type: server.REQUEST_GETPID, } err = client.SendRequest(c, req) if err != nil { t.Fatal(err) } resp, err := client.ReceiveResponse(c) if err != nil { t.Fatal("got error", err) } else if resp.Type == server.RESPONSE_ERR { t.Fatal("got error", resp.Message) } if resp.Getpid.Pid != os.Getpid() { t.Error("Pid didn't match") } req.Fds = []int{0, 1, 2} req.HasFds = true req.Type = server.REQUEST_RUN req.Run = &server.RequestRun{ Exe: "/bin/echo", Args: []string{"/bin/echo", "foo"}, Env: os.Environ(), } err = client.SendRequest(c, req) if err != nil { t.Fatal(err) } resp, err = client.ReceiveResponse(c) if err != nil { t.Fatal("got error", err) } else if resp.Type == server.RESPONSE_ERR { t.Fatal("got error", resp.Message) } sdr := &server.Request{ Type: server.REQUEST_SHUTDOWN, } err = client.SendRequest(c, sdr) if err != nil { t.Fatal(err) } resp, err = client.ReceiveResponse(c) if err != nil { t.Fatal("got error", err) } else if resp.Type == server.RESPONSE_ERR { t.Fatal("got error", resp.Message) } // This channel gets closed when Run() returns. <-runFinished }