fix: server test

This commit is contained in:
bigeagle 2016-04-28 23:12:10 +08:00
parent 292a24ba20
commit ad2b65fcaa
No known key found for this signature in database
GPG Key ID: 9171A4571C27920A
2 changed files with 28 additions and 32 deletions

View File

@ -195,15 +195,7 @@ func (s *Manager) updateJobOfWorker(c *gin.Context) {
c.BindJSON(&status) c.BindJSON(&status)
mirrorName := status.Name mirrorName := status.Name
curStatus, err := s.adapter.GetMirrorStatus(workerID, mirrorName) curStatus, _ := s.adapter.GetMirrorStatus(workerID, mirrorName)
if err != nil {
err := fmt.Errorf("failed to get job %s of worker %s: %s",
mirrorName, workerID, err.Error(),
)
c.Error(err)
s.returnErrJSON(c, http.StatusInternalServerError, err)
return
}
// Only successful syncing needs last_update // Only successful syncing needs last_update
if status.Status == Success { if status.Status == Success {

View File

@ -82,13 +82,12 @@ func TestHTTPServer(t *testing.T) {
Convey("update mirror status of a existed worker", func(ctx C) { Convey("update mirror status of a existed worker", func(ctx C) {
status := MirrorStatus{ status := MirrorStatus{
Name: "arch-sync1", Name: "arch-sync1",
Worker: "test_worker1", Worker: "test_worker1",
IsMaster: true, IsMaster: true,
Status: Success, Status: Success,
LastUpdate: time.Now(), Upstream: "mirrors.tuna.tsinghua.edu.cn",
Upstream: "mirrors.tuna.tsinghua.edu.cn", Size: "3GB",
Size: "3GB",
} }
resp, err := PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s", baseURL, status.Worker, status.Name), status, nil) resp, err := PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s", baseURL, status.Worker, status.Name), status, nil)
defer resp.Body.Close() defer resp.Body.Close()
@ -96,31 +95,36 @@ func TestHTTPServer(t *testing.T) {
So(resp.StatusCode, ShouldEqual, http.StatusOK) So(resp.StatusCode, ShouldEqual, http.StatusOK)
Convey("list mirror status of an existed worker", func(ctx C) { Convey("list mirror status of an existed worker", func(ctx C) {
var ms []MirrorStatus
resp, err := GetJSON(baseURL+"/workers/test_worker1/jobs", &ms, nil)
expectedResponse, err := json.Marshal([]MirrorStatus{status})
So(err, ShouldBeNil)
resp, err := http.Get(baseURL + "/workers/test_worker1/jobs")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusOK) So(resp.StatusCode, ShouldEqual, http.StatusOK)
// err = json.NewDecoder(resp.Body).Decode(&mirrorStatusList) // err = json.NewDecoder(resp.Body).Decode(&mirrorStatusList)
body, err := ioutil.ReadAll(resp.Body) m := ms[0]
defer resp.Body.Close() So(m.Name, ShouldEqual, status.Name)
So(err, ShouldBeNil) So(m.Worker, ShouldEqual, status.Worker)
So(strings.TrimSpace(string(body)), ShouldEqual, string(expectedResponse)) So(m.Status, ShouldEqual, status.Status)
So(m.Upstream, ShouldEqual, status.Upstream)
So(m.Size, ShouldEqual, status.Size)
So(m.IsMaster, ShouldEqual, status.IsMaster)
So(time.Now().Sub(m.LastUpdate), ShouldBeLessThan, 1*time.Second)
}) })
Convey("list all job status of all workers", func(ctx C) { Convey("list all job status of all workers", func(ctx C) {
expectedResponse, err := json.Marshal( var ms []webMirrorStatus
[]webMirrorStatus{convertMirrorStatus(status)}, resp, err := GetJSON(baseURL+"/jobs", &ms, nil)
)
So(err, ShouldBeNil)
resp, err := http.Get(baseURL + "/jobs")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusOK) So(resp.StatusCode, ShouldEqual, http.StatusOK)
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close() m := ms[0]
So(err, ShouldBeNil) So(m.Name, ShouldEqual, status.Name)
So(strings.TrimSpace(string(body)), ShouldEqual, string(expectedResponse)) So(m.Status, ShouldEqual, status.Status)
So(m.Upstream, ShouldEqual, status.Upstream)
So(m.Size, ShouldEqual, status.Size)
So(m.IsMaster, ShouldEqual, status.IsMaster)
So(time.Now().Sub(m.LastUpdate.Time), ShouldBeLessThan, 1*time.Second)
}) })
}) })