97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
// that can be found in the LICENSE file.
|
|
|
|
// +build !oss
|
|
|
|
package metric
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/drone/drone/core"
|
|
"github.com/drone/drone/mock"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
func TestStagePendingCount(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
|
|
// restore the default prometheus registerer
|
|
// when the unit test is complete.
|
|
snapshot := prometheus.DefaultRegisterer
|
|
defer func() {
|
|
prometheus.DefaultRegisterer = snapshot
|
|
controller.Finish()
|
|
}()
|
|
|
|
// creates a blank registry
|
|
registry := prometheus.NewRegistry()
|
|
prometheus.DefaultRegisterer = registry
|
|
|
|
// x5 stage count
|
|
data := []*core.Stage{{}, {}, {}, {}, {}}
|
|
|
|
stages := mock.NewMockStageStore(controller)
|
|
stages.EXPECT().ListState(gomock.Any(), core.StatusPending).Return(data, nil)
|
|
PendingJobCount(stages)
|
|
|
|
metrics, err := registry.Gather()
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if want, got := len(metrics), 1; want != got {
|
|
t.Errorf("Expect registered metric")
|
|
return
|
|
}
|
|
metric := metrics[0]
|
|
if want, got := metric.GetName(), "drone_pending_jobs"; want != got {
|
|
t.Errorf("Expect metric name %s, got %s", want, got)
|
|
}
|
|
if want, got := metric.Metric[0].Gauge.GetValue(), float64(len(data)); want != got {
|
|
t.Errorf("Expect metric value %f, got %f", want, got)
|
|
}
|
|
}
|
|
|
|
func TestStageRunningCount(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
|
|
// restore the default prometheus registerer
|
|
// when the unit test is complete.
|
|
snapshot := prometheus.DefaultRegisterer
|
|
defer func() {
|
|
prometheus.DefaultRegisterer = snapshot
|
|
controller.Finish()
|
|
}()
|
|
|
|
// creates a blank registry
|
|
registry := prometheus.NewRegistry()
|
|
prometheus.DefaultRegisterer = registry
|
|
|
|
// x5 stage count
|
|
data := []*core.Stage{{}, {}, {}, {}, {}}
|
|
|
|
stages := mock.NewMockStageStore(controller)
|
|
stages.EXPECT().ListState(gomock.Any(), core.StatusRunning).Return(data, nil)
|
|
RunningJobCount(stages)
|
|
|
|
metrics, err := registry.Gather()
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if want, got := len(metrics), 1; want != got {
|
|
t.Errorf("Expect registered metric")
|
|
return
|
|
}
|
|
metric := metrics[0]
|
|
if want, got := metric.GetName(), "drone_running_jobs"; want != got {
|
|
t.Errorf("Expect metric name %s, got %s", want, got)
|
|
}
|
|
if want, got := metric.Metric[0].Gauge.GetValue(), float64(len(data)); want != got {
|
|
t.Errorf("Expect metric value %f, got %f", want, got)
|
|
}
|
|
}
|