summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaciej Wereski <m.wereski@partner.samsung.com>2018-06-21 15:13:45 +0200
committerMaciej Wereski <m.wereski@partner.samsung.com>2018-07-10 10:01:25 +0200
commitd932bc55fb656413dbfc53a62f1290dd2f7a9406 (patch)
tree9ca01dda33fa0140c76caa647df0f058ff0d4ec7
parentc1cf72cddc5a22a222cb44330ade3c83713f7c20 (diff)
downloadboruta-d932bc55fb656413dbfc53a62f1290dd2f7a9406.tar.gz
boruta-d932bc55fb656413dbfc53a62f1290dd2f7a9406.tar.bz2
boruta-d932bc55fb656413dbfc53a62f1290dd2f7a9406.zip
HTTP API Client: Set worker state
Change-Id: I96b3e4cab8827dd00fa02f5c4ced3e8b8bc58bff Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com>
-rw-r--r--http/client/client.go14
-rw-r--r--http/client/client_test.go38
2 files changed, 45 insertions, 7 deletions
diff --git a/http/client/client.go b/http/client/client.go
index e2c887f..c3178ee 100644
--- a/http/client/client.go
+++ b/http/client/client.go
@@ -314,9 +314,17 @@ func (client *BorutaClient) GetWorkerInfo(uuid boruta.WorkerUUID) (boruta.Worker
// SetState requests Boruta server to change state of worker with provided UUID.
// SetState is intended only for Boruta server administrators.
-func (client *BorutaClient) SetState(uuid boruta.WorkerUUID,
- state boruta.WorkerState) error {
- return util.ErrNotImplemented
+func (client *BorutaClient) SetState(uuid boruta.WorkerUUID, state boruta.WorkerState) error {
+ path := client.url + "workers/" + string(uuid) + "/setstate"
+ req, err := json.Marshal(&util.WorkerStatePack{state})
+ if err != nil {
+ return err
+ }
+ resp, err := http.Post(path, contentType, bytes.NewReader(req))
+ if err != nil {
+ return err
+ }
+ return processResponse(resp, nil)
}
// SetGroups requests Boruta server to change groups of worker with provided
diff --git a/http/client/client_test.go b/http/client/client_test.go
index 9cf9520..6cbdef4 100644
--- a/http/client/client_test.go
+++ b/http/client/client_test.go
@@ -877,11 +877,41 @@ func TestGetWorkerInfo(t *testing.T) {
}
func TestSetState(t *testing.T) {
- assert, client := initTest(t, "")
- assert.NotNil(client)
+ prefix := "worker-set-state-"
+ path := "/api/v1/workers/"
- err := client.SetState(WorkerUUID(""), FAIL)
- assert.Equal(util.ErrNotImplemented, err)
+ tests := []*testCase{
+ &testCase{
+ // valid
+ name: prefix + "valid",
+ path: path + validUUID + "/setstate",
+ json: string(jsonMustMarshal(&util.WorkerStatePack{IDLE})),
+ contentType: contentJSON,
+ status: http.StatusNoContent,
+ },
+ &testCase{
+ // invalid UUID
+ name: prefix + "bad-uuid",
+ path: path + invalidID + "/setstate",
+ json: string(jsonMustMarshal(&util.WorkerStatePack{FAIL})),
+ contentType: contentJSON,
+ status: http.StatusBadRequest,
+ },
+ }
+
+ srv := prepareServer(http.MethodPost, tests)
+ defer srv.Close()
+ assert, client := initTest(t, srv.URL)
+
+ // valid
+ assert.Nil(client.SetState(validUUID, IDLE))
+
+ // invalid UUID
+ assert.Equal(util.NewServerError(util.ErrBadUUID), client.SetState(invalidID, FAIL))
+
+ // http.Post failure
+ client.url = "http://nosuchaddress.fail"
+ assert.NotNil(client.SetState(validUUID, FAIL))
}
func TestSetGroups(t *testing.T) {