Skip to content

Commit

Permalink
Move connectbox client to external package.
Browse files Browse the repository at this point in the history
  • Loading branch information
tetafro committed Sep 22, 2023
1 parent aae1003 commit 22b3355
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 475 deletions.
16 changes: 8 additions & 8 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

// Collector collects metrics from a remote ConnectBox router.
type Collector struct {
targets map[string]MetricsClient
targets map[string]ConnectBox
}

// NewCollector creates new collector.
func NewCollector(targets map[string]MetricsClient) *Collector {
func NewCollector(targets map[string]ConnectBox) *Collector {
return &Collector{targets: targets}
}

Expand Down Expand Up @@ -59,7 +59,7 @@ func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (c *Collector) collectCMSSystemInfo(
ctx context.Context,
reg *prometheus.Registry,
client MetricsClient,
client ConnectBox,
) {
cmDocsisModeGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_docsis_mode",
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c *Collector) collectCMSSystemInfo(
reg.MustRegister(cmNetworkAccessGauge)

var data CMSystemInfo
err := client.GetMetrics(ctx, FnCMSystemInfo, &data)
err := client.Get(ctx, FnCMSystemInfo, &data)
if err != nil {
log.Printf("Failed to get CMSSystemInfo: %v", err)
return
Expand All @@ -115,7 +115,7 @@ func (c *Collector) collectCMSSystemInfo(
func (c *Collector) collectLANUserTable(
ctx context.Context,
reg *prometheus.Registry,
client MetricsClient,
client ConnectBox,
) {
clientGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_lan_client",
Expand All @@ -131,7 +131,7 @@ func (c *Collector) collectLANUserTable(
reg.MustRegister(clientGauge)

var data LANUserTable
err := client.GetMetrics(ctx, FnLANUserTable, &data)
err := client.Get(ctx, FnLANUserTable, &data)
if err != nil {
log.Printf("Failed to get LANUserTable: %v", err)
return
Expand Down Expand Up @@ -160,7 +160,7 @@ func (c *Collector) collectLANUserTable(
func (c *Collector) collectCMState(
ctx context.Context,
reg *prometheus.Registry,
client MetricsClient,
client ConnectBox,
) {
tunnerTemperatureGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_tunner_temperature",
Expand Down Expand Up @@ -190,7 +190,7 @@ func (c *Collector) collectCMState(
reg.MustRegister(wanIPv6AddrGauge)

var data CMState
err := client.GetMetrics(ctx, FnCMState, &data)
err := client.Get(ctx, FnCMState, &data)
if err != nil {
log.Printf("Failed to get CMState: %v", err)
return
Expand Down
39 changes: 20 additions & 19 deletions collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tetafro/connectbox"
"go.uber.org/mock/gomock"
)

func TestNewCollector(t *testing.T) {
c := NewCollector(map[string]MetricsClient{"test": &ConnectBox{}})
c := NewCollector(map[string]ConnectBox{"test": &connectbox.Client{}})
require.Len(t, c.targets, 1)
}

Expand All @@ -25,12 +26,12 @@ func TestCollector_ServeHTTP(t *testing.T) {
t.Run("success", func(t *testing.T) {
ctrl := gomock.NewController(t)

metrics := NewMockMetricsClient(ctrl)
metrics := NewMockConnectBox(ctrl)

metrics.EXPECT().Login(gomock.Any()).Return(nil)

var cmSystemInfoData CMSystemInfo
metrics.EXPECT().GetMetrics(
metrics.EXPECT().Get(
gomock.Any(), FnCMSystemInfo, &cmSystemInfoData,
).Do(func(ctx context.Context, fn string, out any) error {
data := out.(*CMSystemInfo)
Expand All @@ -44,7 +45,7 @@ func TestCollector_ServeHTTP(t *testing.T) {
})

var lanUserTableData LANUserTable
metrics.EXPECT().GetMetrics(
metrics.EXPECT().Get(
gomock.Any(), FnLANUserTable, &lanUserTableData,
).Do(func(ctx context.Context, fn string, out any) error {
data := out.(*LANUserTable)
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestCollector_ServeHTTP(t *testing.T) {
})

var cmStateData CMState
metrics.EXPECT().GetMetrics(
metrics.EXPECT().Get(
gomock.Any(), FnCMState, &cmStateData,
).Do(func(ctx context.Context, fn string, out any) error {
data := out.(*CMState)
Expand All @@ -89,7 +90,7 @@ func TestCollector_ServeHTTP(t *testing.T) {
metrics.EXPECT().Logout(gomock.Any()).Return(nil)

col := &Collector{
targets: map[string]MetricsClient{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
Expand Down Expand Up @@ -152,7 +153,7 @@ func TestCollector_ServeHTTP(t *testing.T) {

t.Run("no target", func(t *testing.T) {
col := &Collector{
targets: map[string]MetricsClient{},
targets: map[string]ConnectBox{},
}

req, err := http.NewRequest(http.MethodGet, "/probe?target=127.0.0.1", nil)
Expand All @@ -167,11 +168,11 @@ func TestCollector_ServeHTTP(t *testing.T) {
t.Run("failed to login", func(t *testing.T) {
ctrl := gomock.NewController(t)

metrics := NewMockMetricsClient(ctrl)
metrics := NewMockConnectBox(ctrl)
metrics.EXPECT().Login(gomock.Any()).Return(errors.New("fail"))

col := &Collector{
targets: map[string]MetricsClient{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
Expand All @@ -188,19 +189,19 @@ func TestCollector_ServeHTTP(t *testing.T) {
t.Run("failed to get metrics", func(t *testing.T) {
ctrl := gomock.NewController(t)

metrics := NewMockMetricsClient(ctrl)
metrics := NewMockConnectBox(ctrl)

metrics.EXPECT().Login(gomock.Any()).Return(nil)
metrics.EXPECT().GetMetrics(gomock.Any(), FnCMSystemInfo, gomock.Any()).
metrics.EXPECT().Get(gomock.Any(), FnCMSystemInfo, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().GetMetrics(gomock.Any(), FnLANUserTable, gomock.Any()).
metrics.EXPECT().Get(gomock.Any(), FnLANUserTable, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().GetMetrics(gomock.Any(), FnCMState, gomock.Any()).
metrics.EXPECT().Get(gomock.Any(), FnCMState, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Logout(gomock.Any()).Return(nil)

col := &Collector{
targets: map[string]MetricsClient{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
Expand All @@ -218,19 +219,19 @@ func TestCollector_ServeHTTP(t *testing.T) {
t.Run("failed to get metrics and to logout", func(t *testing.T) {
ctrl := gomock.NewController(t)

metrics := NewMockMetricsClient(ctrl)
metrics := NewMockConnectBox(ctrl)

metrics.EXPECT().Login(gomock.Any()).Return(nil)
metrics.EXPECT().GetMetrics(gomock.Any(), FnCMSystemInfo, gomock.Any()).
metrics.EXPECT().Get(gomock.Any(), FnCMSystemInfo, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().GetMetrics(gomock.Any(), FnLANUserTable, gomock.Any()).
metrics.EXPECT().Get(gomock.Any(), FnLANUserTable, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().GetMetrics(gomock.Any(), FnCMState, gomock.Any()).
metrics.EXPECT().Get(gomock.Any(), FnCMState, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Logout(gomock.Any()).Return(errors.New("fail"))

col := &Collector{
targets: map[string]MetricsClient{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
Expand Down
Loading

0 comments on commit 22b3355

Please sign in to comment.