From: Maciej Wereski Date: Thu, 21 Jun 2018 13:25:26 +0000 (+0200) Subject: HTTP API Client: Set worker groups X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=141b36bd0b5edace52e1fa39be4ff1ae2be9ac88;p=tools%2Fboruta.git HTTP API Client: Set worker groups Change-Id: I546f528cd57e88fc9aacaabde447313748dfbdfd Signed-off-by: Maciej Wereski --- diff --git a/http/client/client.go b/http/client/client.go index c3178ee..b6eee86 100644 --- a/http/client/client.go +++ b/http/client/client.go @@ -329,9 +329,17 @@ func (client *BorutaClient) SetState(uuid boruta.WorkerUUID, state boruta.Worker // SetGroups requests Boruta server to change groups of worker with provided // UUID. SetGroups is intended only for Boruta server administrators. -func (client *BorutaClient) SetGroups(uuid boruta.WorkerUUID, - groups boruta.Groups) error { - return util.ErrNotImplemented +func (client *BorutaClient) SetGroups(uuid boruta.WorkerUUID, groups boruta.Groups) error { + path := client.url + "workers/" + string(uuid) + "/setgroups" + req, err := json.Marshal(groups) + if err != nil { + return err + } + resp, err := http.Post(path, contentType, bytes.NewReader(req)) + if err != nil { + return err + } + return processResponse(resp, nil) } // Deregister requests Boruta server to deregister worker with provided UUID. diff --git a/http/client/client_test.go b/http/client/client_test.go index 6cbdef4..74359bd 100644 --- a/http/client/client_test.go +++ b/http/client/client_test.go @@ -915,11 +915,42 @@ func TestSetState(t *testing.T) { } func TestSetGroups(t *testing.T) { - assert, client := initTest(t, "") - assert.NotNil(client) + prefix := "worker-set-groups-" + path := "/api/v1/workers/" + groups := Groups{"foo", "bar"} - err := client.SetGroups(WorkerUUID(""), nil) - assert.Equal(util.ErrNotImplemented, err) + tests := []*testCase{ + &testCase{ + // valid + name: prefix + "valid", + path: path + validUUID + "/setgroups", + json: string(jsonMustMarshal(groups)), + contentType: contentJSON, + status: http.StatusNoContent, + }, + &testCase{ + // invalid UUID + name: prefix + "bad-uuid", + path: path + invalidID + "/setgroups", + json: string(jsonMustMarshal(groups)), + contentType: contentJSON, + status: http.StatusBadRequest, + }, + } + + srv := prepareServer(http.MethodPost, tests) + defer srv.Close() + assert, client := initTest(t, srv.URL) + + // valid + assert.Nil(client.SetGroups(validUUID, groups)) + + // invalid UUID + assert.Equal(util.NewServerError(util.ErrBadUUID), client.SetGroups(invalidID, groups)) + + // http.Post failure + client.url = "http://nosuchaddress.fail" + assert.NotNil(client.SetGroups(validUUID, groups)) } func TestDeregister(t *testing.T) {