【UT】add unit test for collector system info (#18717)

add unit test for system collector test

Signed-off-by: lengrongfu <1275177125@qq.com>
This commit is contained in:
rongfu.leng 2023-06-08 10:06:09 +08:00 committed by GitHub
parent 2f51daf707
commit 8251fd2dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,23 +1,16 @@
package exporter
import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/suite"
)
type SysCollectorSuite struct {
suite.Suite
}
func (c *SysCollectorSuite) SetupTest() {
CacheInit(&Opt{
CacheDuration: 1,
})
}
func TestNewSystemInfoCollector(t *testing.T) {
type args struct {
hbrCli *HarborClient
@ -27,7 +20,15 @@ func TestNewSystemInfoCollector(t *testing.T) {
args args
want *SystemInfoCollector
}{
// TODO: Add test cases.
{
name: "test new system info collector",
args: args{
hbrCli: &HarborClient{},
},
want: &SystemInfoCollector{
HarborClient: &HarborClient{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -43,45 +44,79 @@ func TestSystemInfoCollector_Describe(t *testing.T) {
HarborClient *HarborClient
}
type args struct {
c chan<- *prometheus.Desc
c chan *prometheus.Desc
}
tests := []struct {
name string
fields fields
args args
want *prometheus.Desc
}{
// TODO: Add test cases.
{
name: "test describe",
fields: fields{
HarborClient: &HarborClient{},
},
args: args{
c: make(chan *prometheus.Desc),
},
want: harborSysInfo.Desc(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hc := &SystemInfoCollector{
HarborClient: tt.fields.HarborClient,
}
hc.Describe(tt.args.c)
go hc.Describe(tt.args.c)
desc := <-tt.args.c
if !reflect.DeepEqual(tt.want, desc) {
t.Errorf("SystemInfoCollector.Describe() = %v, want %v", desc, harborSysInfo.Desc())
}
})
}
}
func TestSystemInfoCollector_Collect(t *testing.T) {
CacheInit(&Opt{
CacheDuration: 60,
})
data := []prometheus.Metric{
prometheus.MustNewConstMetric(harborSysInfo.Desc(), prometheus.GaugeValue, 1, "ldap_auth", "v2.0.0", "true"),
}
CachePut(systemInfoCollectorName, data)
type fields struct {
HarborClient *HarborClient
}
type args struct {
c chan<- prometheus.Metric
c chan prometheus.Metric
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
{
name: "test collect",
fields: fields{
HarborClient: &HarborClient{},
},
args: args{
c: make(chan prometheus.Metric),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
InitHarborClient(tt.fields.HarborClient)
hc := &SystemInfoCollector{
HarborClient: tt.fields.HarborClient,
}
hc.Collect(tt.args.c)
go hc.Collect(tt.args.c)
metric := <-tt.args.c
if !reflect.DeepEqual(metric, data[0]) {
t.Errorf("SystemInfoCollector.Collect() = %v, want %v", metric, data[0])
}
})
}
}
@ -90,15 +125,43 @@ func TestSystemInfoCollector_getSysInfo(t *testing.T) {
type fields struct {
HarborClient *HarborClient
}
data := []prometheus.Metric{
prometheus.MustNewConstMetric(harborSysInfo.Desc(), prometheus.GaugeValue, 1, "ldap_auth", "v2.0.0", "true"),
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v2.0/systeminfo" {
w.Write([]byte(`{"auth_mode":"ldap_auth","harbor_version":"v2.0.0","self_registration":true}`))
w.WriteHeader(http.StatusOK)
} else {
http.NotFound(w, r)
}
}))
defer server.Close()
parse, err := url.Parse(server.URL)
if err != nil {
t.Fatal(err)
}
port, _ := strconv.Atoi(parse.Port())
tests := []struct {
name string
fields fields
want []prometheus.Metric
}{
// TODO: Add test cases.
{
name: "test get system info",
fields: fields{
HarborClient: &HarborClient{
HarborScheme: "http",
HarborHost: parse.Hostname(),
HarborPort: port,
},
},
want: data,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
InitHarborClient(tt.fields.HarborClient)
hc := &SystemInfoCollector{
HarborClient: tt.fields.HarborClient,
}