github.com/xmidt-org/webpa-common@v1.11.9/device/drain/status_test.go (about) 1 package drain 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "strconv" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/xmidt-org/webpa-common/device/devicegate" 13 ) 14 15 func testStatus(t *testing.T, active bool, j Job, p Progress, expectedJSON string) { 16 var ( 17 assert = assert.New(t) 18 19 d = new(mockDrainer) 20 status = Status{d} 21 22 response = httptest.NewRecorder() 23 request = httptest.NewRequest("GET", "/", nil) 24 ) 25 26 d.On("Status").Return(active, j, p).Once() 27 status.ServeHTTP(response, request) 28 assert.Equal(http.StatusOK, response.Code) 29 assert.JSONEq(expectedJSON, response.Body.String()) 30 d.AssertExpectations(t) 31 } 32 33 func TestStatus(t *testing.T) { 34 var ( 35 zeroTime = time.Time{}.Format(time.RFC3339Nano) 36 now = time.Now() 37 38 df = &drainFilter{ 39 filter: &devicegate.FilterGate{ 40 FilterStore: devicegate.FilterStore(map[string]devicegate.Set{ 41 "test": &devicegate.FilterSet{Set: map[interface{}]bool{ 42 "test1": true, 43 "test2": true, 44 }}, 45 }), 46 }, 47 filterRequest: devicegate.FilterRequest{ 48 Key: "test", 49 Values: []interface{}{"test1", "test2"}, 50 }, 51 } 52 53 testData = []struct { 54 active bool 55 j Job 56 p Progress 57 expectedJSON string 58 }{ 59 { 60 // when no job has been run since the server started: 61 false, 62 Job{}, 63 Progress{}, 64 fmt.Sprintf(`{"active": false, "job": {"count": 0}, "progress": {"visited": 0, "drained": 0, "started": "%s"}}`, zeroTime), 65 }, 66 67 { 68 true, 69 Job{Count: 67283, Percent: 97, Rate: 127, Tick: 17 * time.Second}, 70 Progress{Visited: 12, Drained: 4, Started: now, Finished: &now}, 71 fmt.Sprintf(`{"active": true, "job": {"count": 67283, "percent": 97, "rate": 127, "tick": "17s"}, "progress": {"visited": 12, "drained": 4, "started": "%s", "finished": "%s"}}`, now.Format(time.RFC3339Nano), now.Format(time.RFC3339Nano)), 72 }, 73 { 74 true, 75 Job{Count: 67283, Percent: 97, Rate: 127, Tick: 17 * time.Second, DrainFilter: df}, 76 Progress{Visited: 12, Drained: 4, Started: now, Finished: &now}, 77 fmt.Sprintf(`{"active": true, "job": {"count": 67283, "percent": 97, "rate": 127, "tick": "17s", "filter": {"key":"test", "values":["test1", "test2"]}}, "progress": {"visited": 12, "drained": 4, "started": "%s", "finished": "%s"}}`, now.Format(time.RFC3339Nano), now.Format(time.RFC3339Nano)), 78 }, 79 } 80 ) 81 82 for i, record := range testData { 83 t.Run(strconv.Itoa(i), func(t *testing.T) { 84 testStatus(t, record.active, record.j, record.p, record.expectedJSON) 85 }) 86 } 87 }