Skip to content

Commit

Permalink
add tests for speed, workload and finders indexers
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmaElsoly committed Nov 28, 2024
1 parent f4ea28e commit e6d912d
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 8 deletions.
125 changes: 125 additions & 0 deletions grid-proxy/internal/indexer/finders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package indexer

import (
"context"
"testing"
"time"

gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/internal/explorer/db"
mocks "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/mocks"
)

func TestNewNodeFinder(t *testing.T) {
ctrl := gomock.NewController(t)

mockDB := mocks.NewMockDatabase(ctrl)

idsChan := make(chan uint32, 10)

mockDB.EXPECT().GetLastNodeTwinID(gomock.Any()).Return(uint32(1), nil).Times(1)
mockDB.EXPECT().GetNodeTwinIDsAfter(gomock.Any(), gomock.Any()).Return([]uint32{2, 3}, nil).Times(1)

ctx, cancel := context.WithCancel(context.Background())

go newNodesFinder(ctx, time.Second, mockDB, idsChan)

time.Sleep(1 * time.Second)

cancel()

var ids []uint32
for {
select {
case id := <-idsChan:
ids = append(ids, id)
default:
goto done
}
}

done:

assert.Equal(t, []uint32{2, 3}, ids)

}

func TestHealthyNodesFinder(t *testing.T) {
ctrl := gomock.NewController(t)
mockDB := mocks.NewMockDatabase(ctrl)

idsChan := make(chan uint32, 10)

mockDB.EXPECT().GetHealthyNodeTwinIds(gomock.Any()).Return([]uint32{1, 2, 3}, nil).Times(1)

ctx, cancel := context.WithCancel(context.Background())

go healthyNodesFinder(ctx, time.Second, mockDB, idsChan)

time.Sleep(500 * time.Millisecond)

cancel()

var ids []uint32
for {
select {
case id := <-idsChan:
ids = append(ids, id)
default:
goto done
}
}

done:

assert.Equal(t, []uint32{1, 2, 3}, ids)
assert.Len(t, ids, 3)

}

func TestUpNodesFinder(t *testing.T) {
ctrl := gomock.NewController(t)
mockDB := mocks.NewMockDatabase(ctrl)

idsChan := make(chan uint32, 10)

mockDB.EXPECT().GetNodes(gomock.Any(), gomock.Any(), gomock.Any()).Return([]db.Node{
{
TwinID: 1,
NodeID: 1,
FarmID: 3,
FarmName: "farm",
},
{
TwinID: 2,
NodeID: 2,
FarmID: 3,
FarmName: "farm",
},
}, uint(2), nil)

ctx, cancel := context.WithCancel(context.Background())

go upNodesFinder(ctx, time.Second, mockDB, idsChan)

time.Sleep(500 * time.Millisecond)

cancel()

var ids []uint32
for {
select {
case id := <-idsChan:
ids = append(ids, id)
default:
goto done
}
}

done:

assert.Equal(t, []uint32{1, 2}, ids)
assert.Len(t, ids, 2)

}
7 changes: 2 additions & 5 deletions grid-proxy/internal/indexer/gpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import (
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
)




func TestNewGPUWork(t *testing.T){
func TestNewGPUWork(t *testing.T) {
wanted := &GPUWork{
findersInterval: map[string]time.Duration{
"up": 2 * time.Minute,
Expand Down Expand Up @@ -67,4 +64,4 @@ func TestGPUGet(t *testing.T) {
_, err := gpu.Get(ctx, client, twinID)
assert.Error(t, err)
})
}
}
6 changes: 3 additions & 3 deletions grid-proxy/internal/indexer/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestRemoveDuplicates(t *testing.T) {

result := removeDuplicates(reports)
assert.Len(t, result, 3)
assert.Contains(t,result, reports[0])
assert.Contains(t,result, reports[1])
assert.Contains(t,result, reports[3])
assert.Contains(t, result, reports[0])
assert.Contains(t, result, reports[1])
assert.Contains(t, result, reports[3])
}
70 changes: 70 additions & 0 deletions grid-proxy/internal/indexer/speed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package indexer

import (
"context"
"testing"
"time"

gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
mocks "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/mocks"
types "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
)

func TestNewSpeedWork(t *testing.T) {
wanted := &SpeedWork{
findersInterval: map[string]time.Duration{
"up": 2 * time.Minute,
},
}
speed := NewSpeedWork(2)
assert.Exactlyf(t, wanted, speed, "got: %v , expected: %v", speed, wanted)
}

func TestSpeedGet(t *testing.T) {
speed := NewSpeedWork(2)
ctrl := gomock.NewController(t)
t.Run("get speed with valid twin id", func(t *testing.T) {
twinID := uint32(1)
expected := TaskResult{
Name: "iperf",
Result: []IperfResult{{
UploadSpeed: float64(200),
DownloadSpeed: float64(100),
NodeID: uint32(1),
CpuReport: CPUUtilizationPercent{
HostTotal: float64(3),
HostUser: float64(1),
HostSystem: float64(2),
RemoteTotal: float64(3),
RemoteUser: float64(1),
RemoteSystem: float64(2),
},
},
},
}
client := mocks.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, perfTestCallCmd, gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, twin uint32, fn string, data, result interface{}) error {
*(result.(*TaskResult)) = expected
return nil
},
)
got, err := speed.Get(context.Background(), client, twinID)
assert.NoError(t, err)
assert.Len(t, got, 1)
assert.IsTypef(t, got, []types.Speed{}, "got: %v , expected: %v", got, []types.Speed{})
assert.Exactlyf(t, got[0].Upload, expected.Result.([]IperfResult)[0].UploadSpeed, "got: %v , expected: %v", got[0].Upload, expected.Result.([]IperfResult)[0].UploadSpeed)
assert.Exactlyf(t, got[0].Download, expected.Result.([]IperfResult)[0].DownloadSpeed, "got: %v , expected: %v", got[0].Download, expected.Result.([]IperfResult)[0].DownloadSpeed)
assert.Exactlyf(t, got[0].NodeTwinId, twinID, "got: %v , expected: %v", got[0].NodeTwinId, twinID)
})

t.Run("get speed with invalid twin id", func(t *testing.T) {
twinID := uint32(1)
client := mocks.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, perfTestCallCmd, gomock.Any(), gomock.Any()).Return(assert.AnError)
got, err := speed.Get(context.Background(), client, twinID)
assert.Error(t, err)
assert.Len(t, got, 0)
})
}
68 changes: 68 additions & 0 deletions grid-proxy/internal/indexer/workload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package indexer

import (
"context"
"errors"
"testing"
"time"

gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
mock_rmb "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/mocks"
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
)

func TextNewWorkloadWork(t *testing.T) {
wanted := &WorkloadWork{
findersInterval: map[string]time.Duration{
"up": time.Duration(2) * time.Minute,
},
}
workload := NewWorkloadWork(2)
assert.Exactly(t, wanted, workload)
}

func TestWorkloadGet(t *testing.T) {
workload := NewWorkloadWork(2)
ctrl := gomock.NewController(t)

t.Run("get workload with valid twin id", func(t *testing.T) {
twinID := uint32(1)
var response struct {
Users struct {
Workloads uint32 `json:"workloads"`
} `json:"users"`
}
ctx := context.Background()

client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, statsCall, nil, gomock.Any()).DoAndReturn(
func(ctx context.Context, twinId uint32, fn string, data, result interface{}) error {
response.Users.Workloads = 10
*result.(*struct {
Users struct {
Workloads uint32 `json:"workloads"`
} `json:"users"`
}) = response
return nil
},
)
workloads, err := workload.Get(ctx, client, twinID)
assert.NoError(t, err)
assert.Len(t, workloads, 1)
assert.Equal(t, response.Users.Workloads, workloads[0].WorkloadsNumber)
assert.Equal(t, twinID, workloads[0].NodeTwinId)
assert.IsType(t, workloads, []types.NodesWorkloads{})
})

t.Run("get workload with invalid twin id", func(t *testing.T) {
twinID := uint32(1)
ctx := context.Background()

client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, statsCall, nil, gomock.Any()).Return(errors.New("error"))
workloads, err := workload.Get(ctx, client, twinID)
assert.Error(t, err)
assert.Len(t, workloads, 0)
})
}

0 comments on commit e6d912d

Please sign in to comment.