From 988e45f7655963429c8788aa77af77fedcb5c9dc Mon Sep 17 00:00:00 2001 From: Guangwen Feng Date: Thu, 26 Mar 2020 14:36:29 +0800 Subject: [PATCH] test: add unit test case for func ReportMetrics Signed-off-by: Guangwen Feng --- dfget/core/api/supernode_api_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dfget/core/api/supernode_api_test.go b/dfget/core/api/supernode_api_test.go index 6bd661b19..1d4e5bf30 100644 --- a/dfget/core/api/supernode_api_test.go +++ b/dfget/core/api/supernode_api_test.go @@ -21,6 +21,7 @@ import ( "strings" "testing" + api_types "github.com/dragonflyoss/Dragonfly/apis/types" "github.com/dragonflyoss/Dragonfly/dfget/types" "github.com/dragonflyoss/Dragonfly/pkg/constants" "github.com/dragonflyoss/Dragonfly/pkg/httputils" @@ -123,6 +124,26 @@ func (s *SupernodeAPITestSuite) TestSupernodeAPI_ReportClientError(c *check.C) { c.Check(r.Code, check.Equals, 700) } +func (s *SupernodeAPITestSuite) TestSupernodeAPI_ReportMetrics(c *check.C) { + s.mock.PostJSONFunc = s.mock.CreatePostJSONFunc(0, nil, nil) + r, e := s.api.ReportMetrics(localhost, &api_types.TaskMetricsRequest{}) + c.Assert(r, check.IsNil) + c.Assert(e.Error(), check.Equals, "0:") + + s.mock.PostJSONFunc = s.mock.CreatePostJSONFunc(0, nil, + fmt.Errorf("test")) + r, e = s.api.ReportMetrics(localhost, &api_types.TaskMetricsRequest{}) + c.Assert(r, check.IsNil) + c.Assert(e.Error(), check.Equals, "test") + + res := types.RegisterResponse{BaseResponse: &types.BaseResponse{}} + s.mock.PostJSONFunc = s.mock.CreatePostJSONFunc(200, []byte(res.String()), nil) + r, e = s.api.ReportMetrics(localhost, &api_types.TaskMetricsRequest{}) + c.Assert(e, check.IsNil) + c.Assert(r, check.NotNil) + c.Assert(r.Code, check.Equals, 0) +} + func (s *SupernodeAPITestSuite) TestSupernodeAPI_get(c *check.C) { type testRes struct { A int