Example #1
0
func (dispatcher concreteActionDispatcher) Dispatch(req boshmbus.Request) (resp boshmbus.Response) {
	action, err := dispatcher.actionFactory.Create(req.Method)

	switch {
	case err != nil:
		resp = boshmbus.NewExceptionResponse("unknown message %s", req.Method)
		dispatcher.logger.Error("Action Dispatcher", "Unknown action %s", req.Method)

	case action.IsAsynchronous():
		task := dispatcher.taskService.StartTask(func() (value interface{}, err error) {
			value, err = dispatcher.actionRunner.Run(action, req.GetPayload())
			return
		})

		resp = boshmbus.NewValueResponse(boshtask.TaskStateValue{
			AgentTaskId: task.Id,
			State:       task.State,
		})

	default:
		value, err := dispatcher.actionRunner.Run(action, req.GetPayload())

		if err != nil {
			err = bosherr.WrapError(err, "Action Failed %s", req.Method)
			resp = boshmbus.NewExceptionResponse(err.Error())
			dispatcher.logger.Error("Action Dispatcher", err.Error())
			return
		}
		resp = boshmbus.NewValueResponse(value)
	}
	return
}
Example #2
0
func (a agent) runMbusHandler(errChan chan error) {
	handlerFunc := func(req boshmbus.Request) (resp boshmbus.Response) {
		switch req.Method {
		case "apply":
			task := a.taskService.StartTask(func() (err error) {
				action := a.actionFactory.Create(req.Method)
				_, err = action.Run(req.GetPayload())
				return
			})

			resp = boshmbus.NewValueResponse(TaskValue{
				AgentTaskId: task.Id,
				State:       string(task.State),
			})
		case "get_task", "ping", "get_state":
			action := a.actionFactory.Create(req.Method)
			value, err := action.Run(req.GetPayload())
			if err != nil {
				resp = boshmbus.NewExceptionResponse(err.Error())
				return
			}
			resp = boshmbus.NewValueResponse(value)
		default:
			resp = boshmbus.NewExceptionResponse("unknown message %s", req.Method)
		}

		return
	}
	errChan <- a.mbusHandler.Run(handlerFunc)
}
Example #3
0
func (a agent) runMbusHandler(errChan chan error) {
	handlerFunc := func(req boshmbus.Request) (resp boshmbus.Response) {
		switch req.Method {
		case "get_task", "ping", "get_state", "ssh", "start":
			action := a.actionFactory.Create(req.Method)
			value, err := action.Run(req.GetPayload())

			if err != nil {
				err = bosherr.WrapError(err, "Action Failed %s", req.Method)
				resp = boshmbus.NewExceptionResponse(err.Error())
				boshlog.Error("Agent", err.Error())
				return
			}
			resp = boshmbus.NewValueResponse(value)
		case "apply", "fetch_logs", "stop", "drain":
			task := a.taskService.StartTask(func() (value interface{}, err error) {
				action := a.actionFactory.Create(req.Method)
				value, err = action.Run(req.GetPayload())
				return
			})

			resp = boshmbus.NewValueResponse(TaskValue{
				AgentTaskId: task.Id,
				State:       string(task.State),
			})
		default:
			resp = boshmbus.NewExceptionResponse("unknown message %s", req.Method)
			boshlog.Error("Agent", "Unknown action %s", req.Method)
		}

		return
	}
	errChan <- a.mbusHandler.Run(handlerFunc)
}
Example #4
0
func assertRequestIsProcessedSynchronously(t *testing.T, req boshmbus.Request) {
	settings, handler, platform, taskService, actionFactory := getAgentDependencies()

	// when action is successful
	actionFactory.CreateAction = &fakeaction.TestAction{
		RunValue: "some value",
	}

	agent := New(settings, handler, platform, taskService, actionFactory)

	err := agent.Run()
	assert.NoError(t, err)
	assert.True(t, handler.ReceivedRun)

	resp := handler.Func(req)
	assert.Equal(t, req.Method, actionFactory.CreateMethod)
	assert.Equal(t, req.GetPayload(), actionFactory.CreateAction.RunPayload)
	assert.Equal(t, boshmbus.NewValueResponse("some value"), resp)

	// when action returns an error
	actionFactory.CreateAction = &fakeaction.TestAction{
		RunErr: errors.New("some error"),
	}

	agent = New(settings, handler, platform, taskService, actionFactory)
	agent.Run()

	resp = handler.Func(req)
	boshassert.MatchesJsonString(t, resp, `{"exception":{"message":"some error"}}`)
}
Example #5
0
func TestRunSetsTheDispatcherAsMessageHandler(t *testing.T) {
	deps, agent := buildAgent()
	deps.actionDispatcher.DispatchResp = boshmbus.NewValueResponse("pong")

	err := agent.Run()

	assert.NoError(t, err)
	assert.True(t, deps.handler.ReceivedRun)

	req := boshmbus.NewRequest("reply to me!", "some action", []byte("some payload"))
	resp := deps.handler.Func(req)

	assert.Equal(t, deps.actionDispatcher.DispatchReq, req)
	assert.Equal(t, resp, deps.actionDispatcher.DispatchResp)
}
Example #6
0
func TestRunSetsTheDispatcherAsMessageHandler(t *testing.T) {
	settings, logger, handler, platform, actionDispatcher := getAgentDependencies()
	actionDispatcher.DispatchResp = boshmbus.NewValueResponse("pong")

	agent := New(settings, logger, handler, platform, actionDispatcher)
	err := agent.Run()

	assert.NoError(t, err)
	assert.True(t, handler.ReceivedRun)

	req := boshmbus.NewRequest("reply to me!", "some action", []byte("some payload"))
	resp := handler.Func(req)

	assert.Equal(t, actionDispatcher.DispatchReq, req)
	assert.Equal(t, resp, actionDispatcher.DispatchResp)
}
Example #7
0
func TestDispatchHandlesSynchronousAction(t *testing.T) {
	logger, taskService, actionFactory, actionRunner := getActionDispatcherDependencies()

	// when action is successful
	actionFactory.CreateAction = &fakeaction.TestAction{
		Asynchronous: false,
	}
	actionRunner.RunValue = "some value"

	dispatcher := NewActionDispatcher(logger, taskService, actionFactory, actionRunner)

	req := boshmbus.NewRequest("reply to me!", "some action", []byte("some payload"))
	resp := dispatcher.Dispatch(req)
	assert.Equal(t, req.Method, actionFactory.CreateMethod)
	assert.Equal(t, req.GetPayload(), actionRunner.RunPayload)
	assert.Equal(t, boshmbus.NewValueResponse("some value"), resp)
}
Example #8
0
func assertRequestIsProcessedAsynchronously(t *testing.T, req boshmbus.Request) {
	settings, handler, platform, taskService, actionFactory := getAgentDependencies()

	taskService.StartTaskStartedTask = boshtask.Task{Id: "found-57-id", State: boshtask.TaskStateDone}
	actionFactory.CreateAction = new(fakeaction.TestAction)

	agent := New(settings, handler, platform, taskService, actionFactory)

	err := agent.Run()
	assert.NoError(t, err)
	assert.True(t, handler.ReceivedRun)

	resp := handler.Func(req)
	assert.Equal(t, boshmbus.NewValueResponse(TaskValue{AgentTaskId: "found-57-id", State: boshtask.TaskStateDone}), resp)

	boshassert.MatchesJsonString(t, resp, `{"value":{"agent_task_id":"found-57-id","state":"done"}}`)

	taskService.StartTaskFunc()
	assert.Equal(t, req.Method, actionFactory.CreateMethod)
	assert.Equal(t, req.GetPayload(), actionFactory.CreateAction.RunPayload)
}