Exemple #1
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"}}`)
}
Exemple #2
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
}
Exemple #3
0
func (a agent) handleGetTask(req boshmbus.Request) (resp boshmbus.Response) {
	taskId, err := parseTaskId(req.GetPayload())
	if err != nil {
		resp.Exception = fmt.Sprintf("Error finding task, %s", err.Error())
		return
	}

	task, found := a.taskService.FindTask(taskId)
	if !found {
		resp.Exception = fmt.Sprintf("Task with id %s could not be found", taskId)
		return
	}

	resp.AgentTaskId = task.Id
	resp.State = string(task.State)
	return
}
Exemple #4
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)
}