"fmt"
"net"
"net/http"
+ "regexp"
"strconv"
. "git.tizen.org/tools/boruta"
workers Workers
}
+// uuidRE matches only valid UUID strings.
+var uuidRE = regexp.MustCompile("^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$")
+
// jsonMustMarshal tries to marshal responseData to JSON. Panics if error occurs.
// TODO(mwereski): check type of data.
func jsonMustMarshal(data responseData) []byte {
reqid, err := strconv.ParseUint(id, 10, 64)
return ReqID(reqid), err
}
+
+// isValidUUID checks if given string is properly formatted UUID.
+func isValidUUID(id string) bool {
+ return uuidRE.MatchString(id)
+}
dateLayout = "2006-01-02"
past = "1683-09-12"
future = "2222-12-31"
+ validUUID = "ec4898ac-0853-407c-8501-cbb24ef6bd77"
+ missingUUID = "8f8ade90-a319-4275-9407-977ca3e9607c"
validReqJSON = `{
"ID":1,
"State":"WAITING",
return
}
+func newWorker(uuid string, state WorkerState) WorkerInfo {
+ caps := make(Capabilities)
+ caps["UUID"] = uuid
+ return WorkerInfo{
+ WorkerUUID: WorkerUUID(uuid),
+ State: state,
+ Caps: caps,
+ }
+}
+
func runTests(assert *assert.Assertions, api *API, tests []requestTest) {
srv := httptest.NewServer(api.r)
defer srv.Close()
_, err = parseReqID(invalidID)
assert.NotNil(err)
}
+
+func TestIsValidUUID(t *testing.T) {
+ assert := assert.New(t)
+ assert.True(isValidUUID(validUUID))
+ assert.False(isValidUUID(invalidID))
+}
// ErrBadID is returned when User provided ID which can't be parsed into
// uint.
ErrBadID = errors.New("ID provided in URL isn't valid")
+ // ErrBadUUID is returned when User provided ID which isn't valid UUID.
+ ErrBadUUID = errors.New("ID provided in URL isn't valid UUID")
// ErrIDMismatch is returned when User provided different ID values in
// URL and JSON.
ErrIDMismatch = errors.New("request ID set in JSON doesn't match ID from URL")
// getWorkerInfoHandler parses HTTP request for obtaining worker information and
// calls GetWorkerInfo().
func (api *API) getWorkerInfoHandler(r *http.Request, ps map[string]string) responseData {
- return newServerError(ErrNotImplemented, "get worker info")
+ defer r.Body.Close()
+
+ if !isValidUUID(ps["id"]) {
+ return newServerError(ErrBadUUID)
+ }
+
+ workerinfo, err := api.workers.GetWorkerInfo(WorkerUUID(ps["id"]))
+ if err != nil {
+ return newServerError(err)
+ }
+
+ return workerinfo
}
// setWorkerStateHandler parses HTTP workers for setting worker state and calls
assert, m, api := initTest(t)
defer m.finish()
+ prefix := "worker-info-"
+ path := "/api/v1/workers/"
+ methods := []string{http.MethodGet, http.MethodHead}
+ notFoundTest := testFromTempl(notFoundTestTempl, prefix, path+missingUUID, methods...)
+
+ worker := newWorker(validUUID, IDLE)
+ missingErr := NotFoundError("Worker")
+ m.wm.EXPECT().GetWorkerInfo(WorkerUUID(validUUID)).Return(worker, nil).Times(2)
+ m.wm.EXPECT().GetWorkerInfo(WorkerUUID(missingUUID)).Return(WorkerInfo{}, missingErr).Times(2)
+
tests := []requestTest{
{
- name: "worker-info",
- path: "/api/v1/workers/8",
- methods: []string{http.MethodGet, http.MethodHead},
+ name: prefix + "valid",
+ path: path + validUUID,
+ methods: methods,
json: ``,
contentType: contentTypeJSON,
- status: http.StatusNotImplemented,
+ status: http.StatusOK,
},
+ {
+ name: prefix + "bad-uuid",
+ path: path + invalidID,
+ methods: methods,
+ json: ``,
+ contentType: contentTypeJSON,
+ status: http.StatusBadRequest,
+ },
+ notFoundTest,
}
runTests(assert, api, tests)
+++ /dev/null
-{"error":"not implemented yet: get worker info"}
\ No newline at end of file
--- /dev/null
+{"error":"invalid request: ID provided in URL isn't valid UUID"}
\ No newline at end of file
--- /dev/null
+{"error":"Worker not found"}
\ No newline at end of file
--- /dev/null
+{"WorkerUUID":"ec4898ac-0853-407c-8501-cbb24ef6bd77","State":"IDLE","Groups":null,"Caps":{"UUID":"ec4898ac-0853-407c-8501-cbb24ef6bd77"}}
\ No newline at end of file