Exemplo n.º 1
0
func (s *UnixListenerTestSuite) TestSendAndUnmarshalConnData() {
	if !s.NoError(s.Listener.Start(), "should start successfully") {
		return
	}

	in := map[string]string{
		"foo": "bar",
	}

	addr, _ := net.ResolveUnixAddr("unix", s.Listener.Addr())
	conn, err := net.DialUnix("unix", nil, addr)
	if !s.NoError(err, "failed to dial listener") {
		return
	}
	_ = acomm.SendConnData(conn, in)
	_ = conn.Close()

	lConn := s.Listener.NextConn()
	if !s.NotNil(lConn, "connection should not be nil") {
		return
	}

	out := map[string]string{}
	s.NoError(acomm.UnmarshalConnData(lConn, &out), "should succeed unmarshalling")

	s.Equal(in, out, "should have unmarshaled the correct data")

	s.Listener.DoneConn(lConn)
}
Exemplo n.º 2
0
func (s *ServerSuite) createResponseHandlers(result chan *params) (*httptest.Server, *acomm.UnixListener) {
	// HTTP response
	responseServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resp := &acomm.Response{}
		decoder := json.NewDecoder(r.Body)
		_ = decoder.Decode(resp)

		p := &params{}
		_ = resp.UnmarshalResult(p)
		result <- p
	}))

	// Unix response
	responseListener := acomm.NewUnixListener(filepath.Join(s.configData.SocketDir, "testResponse.sock"), 0)
	if !s.NoError(responseListener.Start(), "failed to start task listener") {
		return responseServer, nil
	}

	go func() {
		conn := responseListener.NextConn()
		if conn == nil {
			return
		}
		defer responseListener.DoneConn(conn)

		resp := &acomm.Response{}
		_ = acomm.UnmarshalConnData(conn, resp)
		p := &params{}
		_ = resp.UnmarshalResult(p)
		result <- p
	}()

	return responseServer, responseListener
}
Exemplo n.º 3
0
func (t *task) acceptRequest(conn net.Conn) {
	defer t.reqListener.DoneConn(conn)
	var respErr error

	req := &acomm.Request{}
	if err := acomm.UnmarshalConnData(conn, req); err != nil {
		respErr = err
	}

	if err := req.Validate(); err != nil {
		respErr = err
	}

	// Respond to the initial request
	resp, err := acomm.NewResponse(req, nil, nil, respErr)
	if err != nil {
		log.WithFields(log.Fields{
			"error":   err,
			"req":     req,
			"respErr": respErr,
		}).Error("failed to create initial response")
		return
	}

	if err := acomm.SendConnData(conn, resp); err != nil {
		return
	}

	if respErr != nil {
		return
	}
	// Actually perform the task
	t.waitgroup.Add(1)
	go t.handleRequest(req)
}
Exemplo n.º 4
0
func (s *Server) acceptInternalRequest(conn net.Conn) {
	defer s.internal.DoneConn(conn)
	var respErr error
	req := &acomm.Request{}
	defer func() {
		// Respond to the initial request
		resp, err := acomm.NewResponse(req, nil, nil, respErr)
		if err != nil {
			log.WithFields(log.Fields{
				"error":   err,
				"req":     req,
				"respErr": respErr,
			}).Error("failed to create initial response")
			return
		}

		if err := acomm.SendConnData(conn, resp); err != nil {
			log.WithFields(log.Fields{
				"error":   err,
				"req":     req,
				"respErr": respErr,
			}).Error("failed to create initial response")
			return
		}
	}()

	if err := acomm.UnmarshalConnData(conn, req); err != nil {
		respErr = err
		return
	}

	if err := req.Validate(); err != nil {
		respErr = err
		return
	}

	if err := s.handleRequest(req); err != nil {
		respErr = err
		return
	}

	return
}
Exemplo n.º 5
0
func (s *ServerSuite) createTaskListener(taskName string, result chan *params) *acomm.UnixListener {
	taskListener := acomm.NewUnixListener(filepath.Join(s.configData.SocketDir, taskName, "test.sock"), 0)
	if !s.NoError(taskListener.Start(), "failed to start task listener") {
		return nil
	}

	go func() {
		for {
			conn := taskListener.NextConn()
			if conn == nil {
				break
			}
			defer taskListener.DoneConn(conn)
			req := &acomm.Request{}
			if err := acomm.UnmarshalConnData(conn, req); err != nil {
				result <- nil
				continue
			}

			params := &params{}
			_ = req.UnmarshalArgs(params)

			// Respond to the initial request
			resp, _ := acomm.NewResponse(req, nil, nil, nil)
			if err := acomm.SendConnData(conn, resp); err != nil {
				result <- nil
				continue
			}

			// Response to hook
			resp, _ = acomm.NewResponse(req, req.Args, nil, nil)
			if err := req.Respond(resp); err != nil {
				result <- nil
				continue
			}
		}
	}()

	time.Sleep(time.Second)
	return taskListener
}
Exemplo n.º 6
0
func (s *ResponseTestSuite) TestSend() {
	// Mock HTTP response server
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resp := &acomm.Response{}
		body, err := ioutil.ReadAll(r.Body)
		s.NoError(err, "should not fail reading body")
		s.NoError(json.Unmarshal(body, resp), "should not fail unmarshalling response")
		ack, _ := json.Marshal(&acomm.Response{})
		_, _ = w.Write(ack)
		s.Responses <- resp
	}))
	defer ts.Close()

	// Mock Unix response listener
	f, err := ioutil.TempFile("", "acommTest-")
	if !s.NoError(err, "failed to create test unix socket") {
		return
	}
	_ = f.Close()
	_ = os.Remove(f.Name())
	socketPath := fmt.Sprintf("%s.sock", f.Name())
	listener, err := net.Listen("unix", socketPath)
	if !s.NoError(err, "failed to listen on unix socket") {
		return
	}
	defer func() { _ = listener.Close() }()
	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				return
			}
			resp := &acomm.Response{}
			s.NoError(acomm.UnmarshalConnData(conn, resp), "should not fail unmarshalling conn data")
			_ = acomm.SendConnData(conn, &acomm.Response{})
			s.Responses <- resp
			_ = conn.Close()
		}
	}()

	resultJ, _ := json.Marshal(map[string]string{"foo": "bar"})
	response := &acomm.Response{
		ID:     uuid.New(),
		Result: (*json.RawMessage)(&resultJ),
	}

	tests := []struct {
		responseHook string
		expectedErr  bool
	}{
		{ts.URL, false},
		{"http://badpath", true},
		{fmt.Sprintf("unix://%s", socketPath), false},
		{fmt.Sprintf("unix://%s", "badpath"), true},
		{"foobar://", true},
	}

	for _, test := range tests {
		msg := testMsgFunc(test.responseHook)
		u, _ := url.ParseRequestURI(test.responseHook)
		err := acomm.Send(u, response)
		resp := s.NextResp()
		if test.expectedErr {
			s.Error(err, msg("send should fail"))
			s.Nil(resp, msg("response hook should not receive a response"))
		} else {
			if !s.NoError(err, msg("send should not fail")) {
				continue
			}
			s.Equal(response.ID, resp.ID, msg("response should be what was sent"))
		}
	}
}