Skip to content

Commit

Permalink
metrics adapter tests (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
NajmudheenCT authored and leonwanghui committed Jul 9, 2019
1 parent 6239010 commit 759c355
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/controller/metrics/adapters/metrics_dispatcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 The OpenSDS Authors.
//
// 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.
package adapters

import (
"testing"

"github.com/opensds/opensds/pkg/model"
)

func TestSendMetricToRegisteredSenders(t *testing.T) {
type args struct {
metrics *model.MetricSpec
}
tests := []struct {
name string
args args
}{
{name: "test1", args: args{metrics: SamplemetricsSpec[0]}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SendMetricToRegisteredSenders(tt.args.metrics)
})
}
}

func TestStartDispatcher(t *testing.T) {
tests := []struct {
name string
}{
{name: "test1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
StartDispatcher()
})
}
}
101 changes: 101 additions & 0 deletions pkg/controller/metrics/adapters/metrics_sender_to_kafka_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2019 The OpenSDS Authors.
//
// 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.
package adapters

import (
"testing"

"github.com/opensds/opensds/pkg/model"
)

func TestKafkaMetricsSender_GetMetricsSender(t *testing.T) {
type fields struct {
Queue chan *model.MetricSpec
QuitChan chan bool
}
expectedMetricSender := &KafkaMetricsSender{
Queue: make(chan *model.MetricSpec),
QuitChan: make(chan bool),
}
tests := []struct {
name string
fields fields
want MetricsSenderIntf
}{
{name: "test1", fields: fields{Queue: make(chan *model.MetricSpec), QuitChan: make(chan bool)}, want: expectedMetricSender},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &KafkaMetricsSender{
Queue: tt.fields.Queue,
QuitChan: tt.fields.QuitChan,
}
got := p.GetMetricsSender()
if got == nil {
t.Errorf("unexpected response for GetMetricsSender() = %v, ", got)

}
})
}
}

func TestKafkaMetricsSender_Start(t *testing.T) {
type fields struct {
Queue chan *model.MetricSpec
QuitChan chan bool
}
tests := []struct {
name string
fields fields
}{
{name: "test1", fields: fields{
Queue: make(chan *model.MetricSpec),
QuitChan: make(chan bool)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &KafkaMetricsSender{
Queue: tt.fields.Queue,
QuitChan: tt.fields.QuitChan,
}
p.Start()
p.AssignMetricsToSend(SamplemetricsSpec[0])
p.Stop()

})
}
}

func TestKafkaMetricsSender_Stop(t *testing.T) {
type fields struct {
Queue chan *model.MetricSpec
QuitChan chan bool
}
tests := []struct {
name string
fields fields
}{
{name: "test1", fields: fields{Queue: make(chan *model.MetricSpec),
QuitChan: make(chan bool)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &KafkaMetricsSender{
Queue: tt.fields.Queue,
QuitChan: tt.fields.QuitChan,
}
p.Stop()
})
}
}
168 changes: 168 additions & 0 deletions pkg/controller/metrics/adapters/metrics_sender_to_prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2019 The OpenSDS Authors.
//
// 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.
package adapters

import (
"testing"

"github.com/opensds/opensds/pkg/model"
)

var SamplemetricsSpec = []*model.MetricSpec{
{InstanceID: "volume-38aa800e-dc4b-4e01-9a0f-926f58ee2f14",
InstanceName: "opensds--volumes--default-volume--38aa800e--dc4b--4e01--9a0f--926f58ee2f14",
Job: "lvm",
Labels: map[string]string{"device": "opensds--volumes--default-volume--38aa800e--dc4b--4e01--9a0f--926f58ee2f14"},
Component: "volume",
Name: "iops",
Unit: "tps",
AggrType: "",
MetricValues: []*model.Metric{
&model.Metric{
Timestamp: 1561465759,
Value: 32.14,
},
},
},
{InstanceID: "volume-38aa800e-dc4b-4e01-9a0f-926f58ee2f14",
InstanceName: "opensds--volumes--default-volume--38aa800e--dc4b--4e01--9a0f--926f58ee2f14",
Job: "lvm",
Labels: map[string]string{"device": "opensds--volumes--default-volume--38aa800e--dc4b--4e01--9a0f--926f58ee2f14"},
Component: "volume",
Name: "iops",
Unit: "tps",
AggrType: "total",
MetricValues: []*model.Metric{
&model.Metric{
Timestamp: 1561465759,
Value: 32.14,
},
},
},
}

func TestPrometheusMetricsSender_GetMetricsSender(t *testing.T) {
type fields struct {
Queue chan *model.MetricSpec
QuitChan chan bool
}
expectedMetricSender := &PrometheusMetricsSender{
Queue: make(chan *model.MetricSpec),
QuitChan: make(chan bool),
}
tests := []struct {
name string
fields fields
want MetricsSenderIntf
}{
{name: "test1", fields: fields{Queue: make(chan *model.MetricSpec), QuitChan: make(chan bool)}, want: expectedMetricSender},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &PrometheusMetricsSender{
Queue: tt.fields.Queue,
QuitChan: tt.fields.QuitChan,
}
got := p.GetMetricsSender()
if got == nil {
t.Errorf("unexpected response for GetMetricsSender() = %v, ", got)

}
})
}
}

func TestPrometheusMetricsSender_Start(t *testing.T) {
type fields struct {
Queue chan *model.MetricSpec
QuitChan chan bool
}
tests := []struct {
name string
fields fields
}{
{name: "test1", fields: fields{
Queue: make(chan *model.MetricSpec),
QuitChan: make(chan bool)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &PrometheusMetricsSender{
Queue: tt.fields.Queue,
QuitChan: tt.fields.QuitChan,
}
p.Start()
p.AssignMetricsToSend(SamplemetricsSpec[0])
p.Stop()

//t.SkipNow()
})
}
}

func Test_writeToFile(t *testing.T) {
type args struct {
metrics *model.MetricSpec
}
tests := []struct {
name string
args args
}{
{name: "test1", args: args{metrics: SamplemetricsSpec[0]}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
writeToFile(tt.args.metrics)
})
}
}

func Test_getMetricName(t *testing.T) {
type args struct {
metrics *model.MetricSpec
}
tests := []struct {
name string
args args
want string
}{
{name: "test1", args: args{metrics: SamplemetricsSpec[0]}, want: "lvm_volume_iops_tps"},
{name: "test2", args: args{metrics: SamplemetricsSpec[1]}, want: "lvm_volume_iops_tps_total"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getMetricName(tt.args.metrics); got != tt.want {
t.Errorf("getMetricName() = %v, want %v", got, tt.want)
}
})
}
}

func Test_sendToPushGateway(t *testing.T) {
type args struct {
metrics *model.MetricSpec
}
tests := []struct {
name string
args args
}{
{name: "test1", args: args{metrics: SamplemetricsSpec[0]}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sendToPushGateway(tt.args.metrics)
})
}
}

0 comments on commit 759c355

Please sign in to comment.