HTTP API Client: List and filter workers 37/182237/7
authorMaciej Wereski <m.wereski@partner.samsung.com>
Thu, 21 Jun 2018 08:26:09 +0000 (10:26 +0200)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 10 Jul 2018 08:01:25 +0000 (10:01 +0200)
Change-Id: I3e3c59c4c7f5a22e97de867daede6587f4673cfc
Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com>
http/client/client.go
http/client/client_test.go

index 9419c83..cbc901e 100644 (file)
@@ -282,7 +282,21 @@ func (client *BorutaClient) ProlongAccess(reqID boruta.ReqID) error {
 // lists all workers.
 func (client *BorutaClient) ListWorkers(groups boruta.Groups,
        caps boruta.Capabilities) ([]boruta.WorkerInfo, error) {
-       return nil, util.ErrNotImplemented
+       req, err := json.Marshal(&util.WorkersFilter{
+               Groups:       groups,
+               Capabilities: caps,
+       })
+       if err != nil {
+               return nil, err
+       }
+       resp, err := http.Post(client.url+"workers/list", contentType,
+               bytes.NewReader(req))
+       if err != nil {
+               return nil, err
+       }
+       list := new([]boruta.WorkerInfo)
+       err = processResponse(resp, list)
+       return *list, err
 }
 
 // GetWorkerInfo queries Boruta server for information about worker with given
index 51d98a0..39dd807 100644 (file)
@@ -204,6 +204,23 @@ func prepareServer(method string, tests []*testCase) *httptest.Server {
        return httptest.NewServer(mux)
 }
 
+// from http/server/api/v1/api_test.go
+func newWorker(uuid string, state WorkerState, groups Groups, caps Capabilities) (w WorkerInfo) {
+       if caps == nil {
+               caps = make(Capabilities)
+       }
+       caps["UUID"] = uuid
+       w = WorkerInfo{
+               WorkerUUID: WorkerUUID(uuid),
+               State:      state,
+               Caps:       caps,
+       }
+       if len(groups) != 0 {
+               w.Groups = groups
+       }
+       return
+}
+
 func TestNewBorutaClient(t *testing.T) {
        assert, client := initTest(t, "")
 
@@ -727,12 +744,88 @@ func TestProlongAccess(t *testing.T) {
 }
 
 func TestListWorkers(t *testing.T) {
-       assert, client := initTest(t, "")
-       assert.NotNil(client)
+       prefix := "filter-workers-"
+       path := "/api/v1/workers/list"
+
+       // based on http/server/api/v1/handlers_test.go
+       armCaps := make(Capabilities)
+       armCaps["architecture"] = "AArch64"
+       riscvCaps := make(Capabilities)
+       riscvCaps["architecture"] = "RISC-V"
+       workers := []WorkerInfo{
+               newWorker("0", IDLE, Groups{"Lędzianie"}, armCaps),
+               newWorker("1", FAIL, Groups{"Malinowy Chruśniak"}, armCaps),
+               newWorker("2", IDLE, Groups{"Malinowy Chruśniak", "Lędzianie"}, riscvCaps),
+               newWorker("3", FAIL, Groups{"Malinowy Chruśniak"}, riscvCaps),
+       }
+       validFilter := util.WorkersFilter{
+               Groups:       Groups{"Lędzianie"},
+               Capabilities: map[string]string{"architecture": "AArch64"},
+       }
+       validHeader := make(http.Header)
+       validHeader.Set("Boruta-Worker-Count", "2")
+       allHeader := make(http.Header)
+       allHeader.Set("Boruta-Worker-Count", "4")
+       missingFilter := util.WorkersFilter{
+               Groups: Groups{"Fern Flower"},
+       }
+       missingHeader := make(http.Header)
+       missingHeader.Set("Boruta-Worker-Count", "0")
 
-       list, err := client.ListWorkers(nil, nil)
-       assert.Nil(list)
-       assert.Equal(util.ErrNotImplemented, err)
+       tests := []*testCase{
+               &testCase{
+                       // valid request
+                       name:        prefix + "valid-filter",
+                       path:        path,
+                       json:        string(jsonMustMarshal(validFilter)),
+                       contentType: contentJSON,
+                       status:      http.StatusOK,
+                       header:      validHeader,
+               },
+               &testCase{
+                       // list all
+                       name:        prefix + "empty-filter",
+                       path:        path,
+                       json:        string(jsonMustMarshal(util.WorkersFilter{nil, nil})),
+                       contentType: contentJSON,
+                       status:      http.StatusOK,
+                       header:      allHeader,
+               },
+               &testCase{
+                       // no matches
+                       name:        prefix + "nomatch",
+                       path:        path,
+                       json:        string(jsonMustMarshal(missingFilter)),
+                       contentType: contentJSON,
+                       status:      http.StatusOK,
+                       header:      missingHeader,
+               },
+       }
+
+       srv := prepareServer(http.MethodPost, tests)
+       defer srv.Close()
+       assert, client := initTest(t, srv.URL)
+
+       // list some
+       list, err := client.ListWorkers(validFilter.Groups, validFilter.Capabilities)
+       assert.Nil(err)
+       assert.Equal(workers[:2], list)
+
+       // list all
+       list, err = client.ListWorkers(nil, nil)
+       assert.Nil(err)
+       assert.Equal(workers, list)
+
+       // no matches
+       list, err = client.ListWorkers(missingFilter.Groups, missingFilter.Capabilities)
+       assert.Nil(err)
+       assert.Empty(list)
+
+       // http.Post failure
+       client.url = "http://nosuchaddress.fail"
+       list, err = client.ListWorkers(nil, nil)
+       assert.Zero(list)
+       assert.NotNil(err)
 }
 
 func TestGetWorkerInfo(t *testing.T) {