Esempio n. 1
0
		exe, err := exec.LookPath(args[0])
		if err != nil {
			panic(fmt.Errorf("Failed to find executable %v", args[0]))
		}
		req := &server.Request{
			Type:   server.REQUEST_RUN,
			HasFds: true,
			Fds:    fds,
			Run: &server.RequestRun{
				Exe:  exe,
				Args: args,
				Env:  os.Environ(),
				Cwd:  wd,
			},
		}
		err = client.SendRequest(c, req)
		if err != nil {
			panic(fmt.Errorf("Error sending request: %v", err))
		}
		resp, err := client.ReceiveResponse(c)
		if err != nil {
			panic(fmt.Errorf("Error receiving response: %v", err))
		}
		if resp.Type != server.RESPONSE_OK {
			panic(fmt.Errorf("Error in server response: %v", resp.Message))
		}
	},
}

func init() {
	RootCmd.AddCommand(runCmd)
Esempio n. 2
0
// 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
}