func TestRunHandlesAPingMessage(t *testing.T) { taskService := &testtask.FakeService{} req := boshmbus.NewRequest("reply to me!", "ping", []byte("some payload")) expectedResp := boshmbus.Response{Value: "pong"} assertResponseForRequest(t, taskService, req, expectedResp) }
func TestRunHandlesGetTaskMessageWhenPayloadDoesNotHaveTaskId(t *testing.T) { taskService := &testtask.FakeService{ Tasks: map[string]boshtask.Task{}, } req := boshmbus.NewRequest("reply to me!", "get_task", []byte(`{"arguments":[]}`)) expectedResp := boshmbus.Response{Exception: "Error finding task, not enough arguments"} assertResponseForRequest(t, taskService, req, expectedResp) }
func TestRunHandlesGetTaskMessageWhenTaskIsNotFound(t *testing.T) { taskService := &testtask.FakeService{ Tasks: map[string]boshtask.Task{}, } req := boshmbus.NewRequest("reply to me!", "get_task", []byte(`{"arguments":["57"]}`)) expectedResp := boshmbus.Response{Exception: "Task with id 57 could not be found"} assertResponseForRequest(t, taskService, req, expectedResp) }
func TestRunHandlesGetTaskMessage(t *testing.T) { taskService := &testtask.FakeService{ Tasks: map[string]boshtask.Task{ "57": boshtask.Task{Id: "found-57-task-id", State: boshtask.TaskStateFailed}, }, } req := boshmbus.NewRequest("reply to me!", "get_task", []byte(`{"arguments":["57"]}`)) expectedResp := boshmbus.Response{State: boshtask.TaskStateFailed, AgentTaskId: "found-57-task-id"} assertResponseForRequest(t, taskService, req, expectedResp) }
func TestRunHandlesAnApplyMessage(t *testing.T) { taskService := &testtask.FakeService{ StartTaskStartedTask: boshtask.Task{ Id: "some-task-id", State: "running", }, } req := boshmbus.NewRequest("reply to me!", "apply", []byte("some payload")) expectedResp := boshmbus.Response{State: "running", AgentTaskId: "some-task-id"} assertResponseForRequestWithTask(t, taskService, req, expectedResp) }
func TestRunRespondsWithExceptionWhenTheMethodIsUnknown(t *testing.T) { req := boshmbus.NewRequest("reply to me", "gibberish", []byte{}) settings, handler, platform, taskService, actionFactory := getAgentDependencies() agent := New(settings, handler, platform, taskService, actionFactory) err := agent.Run() assert.NoError(t, err) assert.True(t, handler.ReceivedRun) resp := handler.Func(req) boshassert.MatchesJsonString(t, resp, `{"exception":{"message":"unknown message gibberish"}}`) }
func TestDispatchRespondsWithExceptionWhenTheMethodIsUnknown(t *testing.T) { logger, taskService, actionFactory, actionRunner := getActionDispatcherDependencies() req := boshmbus.NewRequest("reply to me", "gibberish", []byte{}) actionFactory.CreateErr = true dispatcher := NewActionDispatcher(logger, taskService, actionFactory, actionRunner) resp := dispatcher.Dispatch(req) boshassert.MatchesJsonString(t, resp, `{"exception":{"message":"unknown message gibberish"}}`) assert.Equal(t, actionFactory.CreateMethod, "gibberish") }
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) }
func TestDispatchHandlesSynchronousActionWhenErr(t *testing.T) { logger, taskService, actionFactory, actionRunner := getActionDispatcherDependencies() // when action returns an error actionFactory.CreateAction = &fakeaction.TestAction{} actionRunner.RunErr = errors.New("some error") dispatcher := NewActionDispatcher(logger, taskService, actionFactory, actionRunner) req := boshmbus.NewRequest("reply to me!", "some action", []byte("some payload")) resp := dispatcher.Dispatch(req) expectedJson := fmt.Sprintf("{\"exception\":{\"message\":\"Action Failed %s: some error\"}}", req.Method) boshassert.MatchesJsonString(t, resp, expectedJson) assert.Equal(t, actionFactory.CreateMethod, "some action") }
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) }
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) }
func TestDispatchHandlesAsynchronousAction(t *testing.T) { logger, taskService, actionFactory, actionRunner := getActionDispatcherDependencies() taskService.StartTaskStartedTask = boshtask.Task{Id: "found-57-id", State: boshtask.TaskStateDone} actionFactory.CreateAction = &fakeaction.TestAction{ Asynchronous: true, } actionRunner.RunValue = "some-task-result-value" dispatcher := NewActionDispatcher(logger, taskService, actionFactory, actionRunner) req := boshmbus.NewRequest("reply to me!", "some async action", []byte("some payload")) resp := dispatcher.Dispatch(req) boshassert.MatchesJsonString(t, resp, `{"value":{"agent_task_id":"found-57-id","state":"done"}}`) value, err := taskService.StartTaskFunc() assert.NoError(t, err) assert.Equal(t, "some-task-result-value", value) assert.Equal(t, req.Method, actionFactory.CreateMethod) assert.Equal(t, req.GetPayload(), actionRunner.RunPayload) assert.Equal(t, actionFactory.CreateMethod, "some async action") }
func TestRunHandlesGetStateMessage(t *testing.T) { req := boshmbus.NewRequest("reply to me!", "get_state", []byte(`{}`)) assertRequestIsProcessedSynchronously(t, req) }
func TestRunHandlesGetTaskMessage(t *testing.T) { req := boshmbus.NewRequest("reply to me!", "get_task", []byte(`{"arguments":["57"]}`)) assertRequestIsProcessedSynchronously(t, req) }
func TestRunHandlesPingMessage(t *testing.T) { req := boshmbus.NewRequest("reply to me!", "ping", []byte("some payload")) assertRequestIsProcessedSynchronously(t, req) }
func TestRunHandlesSshMessage(t *testing.T) { req := boshmbus.NewRequest("reply to me!", "ssh", []byte(`{"arguments":["setup",{"user":"******","password":"******"}]}`)) assertRequestIsProcessedSynchronously(t, req) }
func TestRunHandlesLogsMessage(t *testing.T) { req := boshmbus.NewRequest("reply to me!", "fetch_logs", []byte("some payload")) assertRequestIsProcessedAsynchronously(t, req) }
func TestRunHandlesMigrateDiskMessage(t *testing.T) { req := boshmbus.NewRequest("reply to me!", "migrate_disk", []byte("some payload")) assertRequestIsProcessedAsynchronously(t, req) }