Beispiel #1
0
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)
}
Beispiel #2
0
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)
}
Beispiel #3
0
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)
}
Beispiel #4
0
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)
}
Beispiel #5
0
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)
}
Beispiel #6
0
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")
}
Beispiel #8
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)
}
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")
}
Beispiel #10
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)
}
Beispiel #11
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)
}
Beispiel #12
0
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")
}
Beispiel #13
0
func TestRunHandlesGetStateMessage(t *testing.T) {
	req := boshmbus.NewRequest("reply to me!", "get_state", []byte(`{}`))
	assertRequestIsProcessedSynchronously(t, req)
}
Beispiel #14
0
func TestRunHandlesGetTaskMessage(t *testing.T) {
	req := boshmbus.NewRequest("reply to me!", "get_task", []byte(`{"arguments":["57"]}`))
	assertRequestIsProcessedSynchronously(t, req)
}
Beispiel #15
0
func TestRunHandlesPingMessage(t *testing.T) {
	req := boshmbus.NewRequest("reply to me!", "ping", []byte("some payload"))
	assertRequestIsProcessedSynchronously(t, req)
}
Beispiel #16
0
func TestRunHandlesSshMessage(t *testing.T) {
	req := boshmbus.NewRequest("reply to me!", "ssh", []byte(`{"arguments":["setup",{"user":"******","password":"******"}]}`))
	assertRequestIsProcessedSynchronously(t, req)
}
Beispiel #17
0
func TestRunHandlesLogsMessage(t *testing.T) {
	req := boshmbus.NewRequest("reply to me!", "fetch_logs", []byte("some payload"))
	assertRequestIsProcessedAsynchronously(t, req)
}
Beispiel #18
0
func TestRunHandlesMigrateDiskMessage(t *testing.T) {
	req := boshmbus.NewRequest("reply to me!", "migrate_disk", []byte("some payload"))
	assertRequestIsProcessedAsynchronously(t, req)
}