From: Maciej Wereski Date: Tue, 10 Oct 2017 10:30:54 +0000 (+0200) Subject: HTTP API: Add Superviser API skeleton X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cab1fb00dcacdf5d58a6fc0cda9003939c289ce0;p=tools%2Fboruta.git HTTP API: Add Superviser API skeleton Currently only Requests and handlers of 2 Workers functions were present, although HTTP API should cover full Workers API. Tests will use mocked Workers and Superviser interfaces, so generation line was added to boruta.go. Change-Id: Id6d82a8c6e61121a2e75d5c90445edf35b2268a5 Signed-off-by: Maciej Wereski --- diff --git a/boruta.go b/boruta.go index 00f38b3..438aa36 100644 --- a/boruta.go +++ b/boruta.go @@ -22,7 +22,7 @@ package boruta //go:generate mockgen -destination=mocks/mock_requests.go -package=mocks git.tizen.org/tools/boruta Requests -//go:generate mockgen -destination=mocks/mock_workers.go -package=mocks git.tizen.org/tools/boruta Workers +//go:generate mockgen -destination=mocks/mock_workers.go -package=mocks git.tizen.org/tools/boruta Workers,Superviser import ( "crypto/rsa" diff --git a/mocks/mock_workers.go b/mocks/mock_workers.go index 590e252..d64b584 100644 --- a/mocks/mock_workers.go +++ b/mocks/mock_workers.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: git.tizen.org/tools/boruta (interfaces: Workers) +// Source: git.tizen.org/tools/boruta (interfaces: Workers,Superviser) // Package mocks is a generated GoMock package. package mocks @@ -94,3 +94,50 @@ func (m *MockWorkers) SetState(arg0 boruta.WorkerUUID, arg1 boruta.WorkerState) func (mr *MockWorkersMockRecorder) SetState(arg0, arg1 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetState", reflect.TypeOf((*MockWorkers)(nil).SetState), arg0, arg1) } + +// MockSuperviser is a mock of Superviser interface +type MockSuperviser struct { + ctrl *gomock.Controller + recorder *MockSuperviserMockRecorder +} + +// MockSuperviserMockRecorder is the mock recorder for MockSuperviser +type MockSuperviserMockRecorder struct { + mock *MockSuperviser +} + +// NewMockSuperviser creates a new mock instance +func NewMockSuperviser(ctrl *gomock.Controller) *MockSuperviser { + mock := &MockSuperviser{ctrl: ctrl} + mock.recorder = &MockSuperviserMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockSuperviser) EXPECT() *MockSuperviserMockRecorder { + return m.recorder +} + +// Register mocks base method +func (m *MockSuperviser) Register(arg0 boruta.Capabilities) error { + ret := m.ctrl.Call(m, "Register", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// Register indicates an expected call of Register +func (mr *MockSuperviserMockRecorder) Register(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Register", reflect.TypeOf((*MockSuperviser)(nil).Register), arg0) +} + +// SetFail mocks base method +func (m *MockSuperviser) SetFail(arg0 boruta.WorkerUUID, arg1 string) error { + ret := m.ctrl.Call(m, "SetFail", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetFail indicates an expected call of SetFail +func (mr *MockSuperviserMockRecorder) SetFail(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetFail", reflect.TypeOf((*MockSuperviser)(nil).SetFail), arg0, arg1) +} diff --git a/server/api/v1/api.go b/server/api/v1/api.go index 5cff344..ca6d6f5 100644 --- a/server/api/v1/api.go +++ b/server/api/v1/api.go @@ -57,8 +57,9 @@ type reqHandler func(*http.Request, map[string]string) responseData // API provides HTTP API handlers. type API struct { - r *httptreemux.TreeMux - reqs Requests + r *httptreemux.TreeMux + reqs Requests + workers Workers } // jsonMustMarshal tries to marshal responseData to JSON. Panics if error occurs. @@ -121,10 +122,12 @@ func routerSetHandler(grp *httptreemux.Group, path string, fn reqHandler, // NewAPI takes router and registers HTTP API in it. httptreemux.PanicHandler // function is set. Also other setting of the router may be modified. -func NewAPI(router *httptreemux.TreeMux, requestsAPI Requests) (api *API) { +func NewAPI(router *httptreemux.TreeMux, requestsAPI Requests, + workersAPI Workers) (api *API) { api = new(API) api.reqs = requestsAPI + api.workers = workersAPI api.r = router api.r.PanicHandler = panicHandler @@ -159,6 +162,14 @@ func NewAPI(router *httptreemux.TreeMux, requestsAPI Requests) (api *API) { routerSetHandler(workers, "/:id", api.getWorkerInfoHandler, http.StatusOK, http.MethodGet, http.MethodHead) + // Workers API - Admin part + routerSetHandler(workers, "/:id/setstate", api.setWorkerStateHandler, + http.StatusNoContent, http.MethodPost) + routerSetHandler(workers, "/:id/setgroups", api.setWorkerGroupsHandler, + http.StatusNoContent, http.MethodPost) + routerSetHandler(workers, "/:id/deregister", api.workerDeregister, + http.StatusNoContent, http.MethodPost) + return } diff --git a/server/api/v1/api_test.go b/server/api/v1/api_test.go index 854e10a..e624844 100644 --- a/server/api/v1/api_test.go +++ b/server/api/v1/api_test.go @@ -126,7 +126,7 @@ func initTest(t *testing.T) (*assert.Assertions, *allMocks, *API) { rq: mocks.NewMockRequests(ctrl), wm: mocks.NewMockWorkers(ctrl), } - return assert.New(t), m, NewAPI(httptreemux.New(), m.rq) + return assert.New(t), m, NewAPI(httptreemux.New(), m.rq, m.wm) } func (m *allMocks) finish() { diff --git a/server/api/v1/handlers.go b/server/api/v1/handlers.go index 54b6fbb..01a5be3 100644 --- a/server/api/v1/handlers.go +++ b/server/api/v1/handlers.go @@ -177,3 +177,21 @@ func (api *API) listWorkersHandler(r *http.Request, ps map[string]string) respon func (api *API) getWorkerInfoHandler(r *http.Request, ps map[string]string) responseData { return newServerError(ErrNotImplemented, "get worker info") } + +// setWorkerStateHandler parses HTTP workers for setting worker state and calls +// workers.SetState(). +func (api *API) setWorkerStateHandler(r *http.Request, ps map[string]string) responseData { + return newServerError(ErrNotImplemented, "set worker state") +} + +// setWorkerGroupsHandler parses HTTP workers for setting worker groups and calls +// workers.SetGroups(). +func (api *API) setWorkerGroupsHandler(r *http.Request, ps map[string]string) responseData { + return newServerError(ErrNotImplemented, "set worker groups") +} + +// workerDeregister parses HTTP workers for deregistering worker state and calls +// workers.Deregister(). +func (api *API) workerDeregister(r *http.Request, ps map[string]string) responseData { + return newServerError(ErrNotImplemented, "deregister worker") +} diff --git a/server/api/v1/handlers_test.go b/server/api/v1/handlers_test.go index 3730358..9ea727b 100644 --- a/server/api/v1/handlers_test.go +++ b/server/api/v1/handlers_test.go @@ -441,3 +441,57 @@ func TestGetWorkerInfoHandler(t *testing.T) { runTests(assert, api, tests) } + +func TestSetWorkerStateHandler(t *testing.T) { + assert, m, api := initTest(t) + defer m.finish() + + tests := []requestTest{ + { + name: "worker-set-state", + path: "/api/v1/workers/8/setstate", + methods: []string{http.MethodPost}, + json: ``, + contentType: contentTypeJSON, + status: http.StatusNotImplemented, + }, + } + + runTests(assert, api, tests) +} + +func TestSetWorkerGroupsHandler(t *testing.T) { + assert, m, api := initTest(t) + defer m.finish() + + tests := []requestTest{ + { + name: "worker-set-groups", + path: "/api/v1/workers/8/setgroups", + methods: []string{http.MethodPost}, + json: ``, + contentType: contentTypeJSON, + status: http.StatusNotImplemented, + }, + } + + runTests(assert, api, tests) +} + +func TestDeregisterWorkerHandler(t *testing.T) { + assert, m, api := initTest(t) + defer m.finish() + + tests := []requestTest{ + { + name: "worker-deregister", + path: "/api/v1/workers/8/deregister", + methods: []string{http.MethodPost}, + json: ``, + contentType: contentTypeJSON, + status: http.StatusNotImplemented, + }, + } + + runTests(assert, api, tests) +} diff --git a/server/api/v1/testdata/worker-deregister-POST.json b/server/api/v1/testdata/worker-deregister-POST.json new file mode 100644 index 0000000..daf5a87 --- /dev/null +++ b/server/api/v1/testdata/worker-deregister-POST.json @@ -0,0 +1 @@ +{"error":"not implemented yet: deregister worker"} \ No newline at end of file diff --git a/server/api/v1/testdata/worker-set-groups-POST.json b/server/api/v1/testdata/worker-set-groups-POST.json new file mode 100644 index 0000000..c4315bb --- /dev/null +++ b/server/api/v1/testdata/worker-set-groups-POST.json @@ -0,0 +1 @@ +{"error":"not implemented yet: set worker groups"} \ No newline at end of file diff --git a/server/api/v1/testdata/worker-set-state-POST.json b/server/api/v1/testdata/worker-set-state-POST.json new file mode 100644 index 0000000..8cede3a --- /dev/null +++ b/server/api/v1/testdata/worker-set-state-POST.json @@ -0,0 +1 @@ +{"error":"not implemented yet: set worker state"} \ No newline at end of file