Skip to content

Commit

Permalink
fixes metric tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed May 23, 2024
1 parent b33da21 commit ede05d9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 69 deletions.
1 change: 0 additions & 1 deletion backend/services/mgmt/v1alpha1/metrics-service/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ func getDailyUsageFromProm(ctx context.Context, api promv1.API, query string, st
errgrp, errctx := errgroup.WithContext(ctx)
errgrp.SetLimit(10)
mu := sync.Mutex{}

// Iterate through each day in the range
for d := start; !d.After(end); d = d.AddDate(0, 0, 1) {
d := d
Expand Down
139 changes: 71 additions & 68 deletions backend/services/mgmt/v1alpha1/metrics-service/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1alpha1_metricsservice

import (
"context"
"fmt"
"testing"
"time"

Expand All @@ -12,7 +13,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"

mockPromV1 "github.com/nucleuscloud/neosync/backend/internal/mocks/github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/nucleuscloud/neosync/backend/pkg/metrics"
Expand All @@ -29,26 +29,22 @@ const (
)

var (
startTime = timestamppb.New(time.Date(2024, 03, 10, 14, 14, 00, 00, time.Local))
endTime = timestamppb.New(time.Date(2024, 03, 11, 14, 14, 00, 00, time.Local))
startTime = time.Date(2024, 3, 10, 00, 00, 00, 00, time.UTC)
endTime = time.Date(2024, 3, 10, 23, 59, 59, 00, time.UTC)

startDate = mgmtv1alpha1.Date{Year: uint32(startTime.AsTime().Year()), Month: uint32(startTime.AsTime().Month()), Day: uint32(startTime.AsTime().Day())}
endDate = mgmtv1alpha1.Date{Year: uint32(endTime.AsTime().Year()), Month: uint32(endTime.AsTime().Month()), Day: uint32(endTime.AsTime().Day())}
startDate = mgmtv1alpha1.Date{Year: uint32(startTime.Year()), Month: uint32(startTime.Month()), Day: uint32(startTime.Day())}
endDate = mgmtv1alpha1.Date{Year: uint32(endTime.Year()), Month: uint32(endTime.Month()), Day: uint32(endTime.Day())}

testMatrix = model.Matrix{
testVector = model.Vector{
{
Metric: model.Metric{"foo": "bar"},
Values: []model.SamplePair{
{Timestamp: 0, Value: 1},
{Timestamp: 0, Value: 2},
},
Metric: model.Metric{"foo": "bar"},
Timestamp: 0,
Value: 2,
},
{
Metric: model.Metric{"foo": "bar2"},
Values: []model.SamplePair{
{Timestamp: 0, Value: 1},
{Timestamp: 0, Value: 2},
},
Metric: model.Metric{"foo": "bar2"},
Timestamp: 0,
Value: 2,
},
}
)
Expand All @@ -60,8 +56,8 @@ func Test_GetMetricCount_Empty_Matrix(t *testing.T) {

ctx := context.Background()

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(model.Matrix{}, promv1.Warnings{}, nil)
m.PromApiMock.On("Query", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(model.Vector{}, promv1.Warnings{}, nil)

resp, err := m.Service.GetMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetMetricCountRequest{
StartDay: &startDate,
Expand Down Expand Up @@ -99,8 +95,8 @@ func Test_GetMetricCount_AccountId(t *testing.T) {

ctx := context.Background()

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(testMatrix, promv1.Warnings{}, nil)
m.PromApiMock.On("Query", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(testVector, promv1.Warnings{}, nil)

resp, err := m.Service.GetMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetMetricCountRequest{
StartDay: &startDate,
Expand Down Expand Up @@ -128,8 +124,8 @@ func Test_GetMetricCount_JobId(t *testing.T) {
},
}), nil)

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(testMatrix, promv1.Warnings{}, nil)
m.PromApiMock.On("Query", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(testVector, promv1.Warnings{}, nil)

resp, err := m.Service.GetMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetMetricCountRequest{
StartDay: &startDate,
Expand Down Expand Up @@ -157,8 +153,8 @@ func Test_GetMetricCount_RunId(t *testing.T) {
},
}), nil)

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(testMatrix, promv1.Warnings{}, nil)
m.PromApiMock.On("Query", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(testVector, promv1.Warnings{}, nil)

resp, err := m.Service.GetMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetMetricCountRequest{
StartDay: &startDate,
Expand Down Expand Up @@ -208,9 +204,15 @@ func Test_GetMetricCount_Swapped_Times(t *testing.T) {

ctx := context.Background()

newStart := &mgmtv1alpha1.Date{
Month: endDate.Month,
Day: endDate.Day + 1,
Year: endDate.Year,
}

resp, err := m.Service.GetMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetMetricCountRequest{
StartDay: &endDate,
EndDay: &startDate,
StartDay: newStart,
EndDay: &endDate,
Metric: mgmtv1alpha1.RangedMetricName_RANGED_METRIC_NAME_INPUT_RECEIVED,
Identifier: &mgmtv1alpha1.GetMetricCountRequest_AccountId{
AccountId: mockAccountId,
Expand Down Expand Up @@ -238,7 +240,7 @@ func Test_GetMetricCount_Time_Limit(t *testing.T) {

ctx := context.Background()

newEndTime := startTime.AsTime().Add(timeLimit + 1)
newEndTime := startTime.Add(timeLimit + 1)
resp, err := m.Service.GetMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetMetricCountRequest{
StartDay: &startDate,
EndDay: &mgmtv1alpha1.Date{Year: uint32(newEndTime.Year()), Month: uint32(newEndTime.Month()), Day: uint32(newEndTime.Day())},
Expand Down Expand Up @@ -322,7 +324,7 @@ func Test_getPromQueryFromMetric(t *testing.T) {
assert.NotEmpty(t, output)
assert.Equal(
t,
`input_received_total{foo="bar",foo2="bar2"}`,
`sum(max_over_time(input_received_total{foo="bar",foo2="bar2"}[1d]))`,
output,
)
}
Expand All @@ -344,8 +346,10 @@ func Test_GetDailyMetricCount_Empty_Matrix(t *testing.T) {

ctx := context.Background()

m.PromApiMock.On("Query", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(model.Matrix{}, promv1.Warnings{}, nil)
m.PromApiMock.On("Query", mock.MatchedBy(func(ctx context.Context) bool {
return true
}), mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(model.Vector{}, promv1.Warnings{}, nil)

resp, err := m.Service.GetDailyMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetDailyMetricCountRequest{
Start: &startDate,
Expand Down Expand Up @@ -383,9 +387,11 @@ func Test_GetDailyMetricCount_AccountId(t *testing.T) {

ctx := context.Background()

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(testMatrix, promv1.Warnings{}, nil)

m.PromApiMock.On("Query", mock.MatchedBy(func(ctx context.Context) bool {
return true
}), mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(testVector, promv1.Warnings{}, nil)
fmt.Println(startTime, endTime)

Check failure on line 394 in backend/services/mgmt/v1alpha1/metrics-service/metrics_test.go

View workflow job for this annotation

GitHub Actions / golang-lint

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
resp, err := m.Service.GetDailyMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetDailyMetricCountRequest{
Start: &startDate,
End: &endDate,
Expand Down Expand Up @@ -414,8 +420,10 @@ func Test_GetDailyMetricCount_JobId(t *testing.T) {
},
}), nil)

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(testMatrix, promv1.Warnings{}, nil)
m.PromApiMock.On("Query", mock.MatchedBy(func(ctx context.Context) bool {
return true
}), mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(testVector, promv1.Warnings{}, nil)

resp, err := m.Service.GetDailyMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetDailyMetricCountRequest{
Start: &startDate,
Expand Down Expand Up @@ -445,8 +453,10 @@ func Test_GetDailyMetricCount_RunId(t *testing.T) {
},
}), nil)

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(testMatrix, promv1.Warnings{}, nil)
m.PromApiMock.On("Query", mock.MatchedBy(func(ctx context.Context) bool {
return true
}), mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(testVector, promv1.Warnings{}, nil)

resp, err := m.Service.GetDailyMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetDailyMetricCountRequest{
Start: &startDate,
Expand All @@ -470,21 +480,17 @@ func Test_GetDailyMetricCount_MultipleDays(t *testing.T) {

ctx := context.Background()

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(model.Matrix{
m.PromApiMock.On("Query", mock.MatchedBy(func(ctx context.Context) bool {
return true
}), mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(model.Vector{
{
Metric: model.Metric{"foo": "bar"},
Values: []model.SamplePair{
{Timestamp: model.Time(time.Date(2024, 10, 3, 0, 0, 0, 0, time.UTC).UnixMilli()), Value: 1},
{Timestamp: model.Time(time.Date(2024, 10, 3, 0, 1, 0, 0, time.UTC).UnixMilli()), Value: 2},
},
Value: 2,
},
{
Metric: model.Metric{"foo": "bar2"},
Values: []model.SamplePair{
{Timestamp: model.Time(time.Date(2024, 11, 3, 0, 0, 0, 0, time.UTC).UnixMilli()), Value: 1},
{Timestamp: model.Time(time.Date(2024, 11, 3, 0, 1, 0, 0, time.UTC).UnixMilli()), Value: 3},
},
Value: 3,
},
}, promv1.Warnings{}, nil)

Expand All @@ -501,9 +507,8 @@ func Test_GetDailyMetricCount_MultipleDays(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, resp)
results := resp.Msg.GetResults()
require.Len(t, results, 2)
assert.Equal(t, uint64(2), results[0].Count)
assert.Equal(t, uint64(3), results[1].Count)
require.Len(t, results, 1)
assert.Equal(t, uint64(5), results[0].Count)
}

func Test_GetDailyMetricCount_MultipleDays_Ordering(t *testing.T) {
Expand All @@ -513,21 +518,17 @@ func Test_GetDailyMetricCount_MultipleDays_Ordering(t *testing.T) {

ctx := context.Background()

m.PromApiMock.On("QueryRange", ctx, mock.AnythingOfType("string"), mock.AnythingOfType("v1.Range")).
Return(model.Matrix{
m.PromApiMock.On("Query", mock.MatchedBy(func(ctx context.Context) bool {
return true
}), mock.AnythingOfType("string"), mock.AnythingOfType("time.Time")).
Return(model.Vector{
{
Metric: model.Metric{"foo": "bar2"},
Values: []model.SamplePair{
{Timestamp: model.Time(time.Date(2024, 11, 3, 0, 0, 0, 0, time.UTC).UnixMilli()), Value: 1},
{Timestamp: model.Time(time.Date(2024, 11, 3, 0, 1, 0, 0, time.UTC).UnixMilli()), Value: 3},
},
Value: 3,
},
{
Metric: model.Metric{"foo": "bar"},
Values: []model.SamplePair{
{Timestamp: model.Time(time.Date(2024, 10, 3, 0, 0, 0, 0, time.UTC).UnixMilli()), Value: 1},
{Timestamp: model.Time(time.Date(2024, 10, 3, 0, 1, 0, 0, time.UTC).UnixMilli()), Value: 2},
},
Value: 2,
},
}, promv1.Warnings{}, nil)

Expand All @@ -544,11 +545,8 @@ func Test_GetDailyMetricCount_MultipleDays_Ordering(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, resp)
results := resp.Msg.GetResults()
require.Len(t, results, 2)
assert.Equal(t, uint64(2), results[0].Count)
assert.Equal(t, uint32(10), results[0].Date.Month, "the expected month should be 10")
assert.Equal(t, uint64(3), results[1].Count)
assert.Equal(t, uint32(11), results[1].Date.Month, "the expected month should be 11")
require.Len(t, results, 1)
assert.Equal(t, uint64(5), results[0].Count)
}

func Test_GetDailyMetricCount_Bad_Times(t *testing.T) {
Expand Down Expand Up @@ -586,9 +584,14 @@ func Test_GetDailyMetricCount_Swapped_Times(t *testing.T) {

ctx := context.Background()

newStart := &mgmtv1alpha1.Date{
Month: endDate.Month,
Day: endDate.Day + 1,
Year: endDate.Year,
}
resp, err := m.Service.GetDailyMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetDailyMetricCountRequest{
Start: &endDate,
End: &startDate,
Start: newStart,
End: &endDate,
Metric: mgmtv1alpha1.RangedMetricName_RANGED_METRIC_NAME_INPUT_RECEIVED,
Identifier: &mgmtv1alpha1.GetDailyMetricCountRequest_AccountId{
AccountId: mockAccountId,
Expand All @@ -604,7 +607,7 @@ func Test_GetDailyMetricCount_Time_Limit(t *testing.T) {

ctx := context.Background()

newEndTime := startTime.AsTime().Add(timeLimit + 1)
newEndTime := startTime.Add(timeLimit + 1)
resp, err := m.Service.GetDailyMetricCount(ctx, connect.NewRequest(&mgmtv1alpha1.GetDailyMetricCountRequest{
Start: &startDate,
End: &mgmtv1alpha1.Date{Year: uint32(newEndTime.Year()), Month: uint32(newEndTime.Month()), Day: uint32(newEndTime.Day())},
Expand Down

0 comments on commit ede05d9

Please sign in to comment.