harness-drone/metric/sink/datadog_test.go

96 lines
2.6 KiB
Go
Raw Normal View History

2020-02-07 16:45:48 +00:00
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2019-03-05 07:16:53 +00:00
package sink
import (
"context"
"testing"
2021-05-13 17:51:22 +00:00
"github.com/drone/drone/core"
2019-03-05 07:16:53 +00:00
"github.com/drone/drone/mock"
2019-03-16 19:56:52 +00:00
"github.com/drone/drone/version"
2019-03-05 07:16:53 +00:00
"github.com/golang/mock/gomock"
"github.com/h2non/gock"
)
var noContext = context.Background()
func TestDo(t *testing.T) {
controller := gomock.NewController(t)
gock.InterceptClient(httpClient)
defer func() {
gock.RestoreClient(httpClient)
gock.Off()
controller.Finish()
}()
users := mock.NewMockUserStore(controller)
users.EXPECT().Count(gomock.Any()).Return(int64(10), nil)
2021-05-13 17:51:22 +00:00
users.EXPECT().ListRange(gomock.Any(), gomock.Any()).Return([]*core.User{{Email: "jane@acme.com"}}, nil)
2019-03-05 07:16:53 +00:00
repos := mock.NewMockRepositoryStore(controller)
repos.EXPECT().Count(gomock.Any()).Return(int64(20), nil)
builds := mock.NewMockBuildStore(controller)
builds.EXPECT().Count(gomock.Any()).Return(int64(30), nil)
gock.New("https://api.datadoghq.com").
Post("/api/v1/series").
JSON(sample).
Reply(200)
d := new(Datadog)
d.users = users
d.repos = repos
d.builds = builds
d.system.Host = "test.example.com"
2019-03-13 22:16:42 +00:00
d.config.License = "trial"
d.config.EnableGithub = true
d.config.EnableAgents = true
2019-03-05 07:16:53 +00:00
d.config.Endpoint = "https://api.datadoghq.com/api/v1/series"
d.do(noContext, 915148800)
if gock.IsPending() {
t.Errorf("Unfinished requests")
}
}
var sample = `{
"series" : [
{
"metric": "drone.users",
"points": [[915148800, 10]],
"type": "gauge",
2019-03-13 22:16:42 +00:00
"host": "test.example.com",
2021-05-13 17:51:22 +00:00
"tags": ["version:` + version.Version.String() + `","remote:github:cloud","scheduler:internal:agents","license:trial","installer:jane@acme.com"]
2019-03-05 07:16:53 +00:00
},
{
"metric": "drone.repos",
"points": [[915148800, 20]],
"type": "gauge",
2019-03-13 22:16:42 +00:00
"host": "test.example.com",
2019-03-16 19:56:52 +00:00
"tags": ["version:` + version.Version.String() + `","remote:github:cloud","scheduler:internal:agents","license:trial"]
2019-03-05 07:16:53 +00:00
},
{
"metric": "drone.builds",
"points": [[915148800, 30]],
"type": "gauge",
2019-03-13 22:16:42 +00:00
"host": "test.example.com",
2019-03-16 19:56:52 +00:00
"tags": ["version:` + version.Version.String() + `","remote:github:cloud","scheduler:internal:agents","license:trial"]
2019-03-05 07:16:53 +00:00
}
]
}`