Skip to content

Commit

Permalink
feat indexer gpu, health and ipv6 test
Browse files Browse the repository at this point in the history
Signed-off-by: nabil salah <nabil.salah203@gmail.com>
  • Loading branch information
Nabil-Salah committed Nov 27, 2024
1 parent ca52323 commit f4ea28e
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 4 deletions.
41 changes: 37 additions & 4 deletions grid-proxy/internal/indexer/gpu_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package indexer

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

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"
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
)


Expand All @@ -27,11 +26,45 @@ func TestNewGPUWork(t *testing.T){
}

func TestGPUGet(t *testing.T) {
dmi := NewGPUWork(2)
gpu := NewGPUWork(2)
ctrl := gomock.NewController(t)
ctx := context.Background()

t.Run("get gpu with valid twin id", func(t *testing.T) {
expected := zosGpu
twinID := uint32(1)
expected := []types.NodeGPU{
{
NodeTwinID: 1,
ID: "gpu-1",
Vendor: "NVIDIA",
Device: "RTX 3080",
Contract: 123,
UpdatedAt: 1234567890,
},
}
client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, gpuListCmd, nil, gomock.AssignableToTypeOf(&[]types.NodeGPU{})).DoAndReturn(
func(ctx context.Context, twin uint32, fn string, data, result interface{}) error {
*(result.(*[]types.NodeGPU)) = expected
return nil
},
)
got, err := gpu.Get(ctx, client, twinID)

assert.NoError(t, err)
assert.Equal(t, expected, got)
assert.Len(t, got, 1)
assert.Equal(t, "gpu-1", got[0].ID)
assert.Equal(t, "NVIDIA", got[0].Vendor)
})

t.Run("get gpu with invalid twin id", func(t *testing.T) {
twinID := uint32(2)
client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, gpuListCmd, nil, gomock.AssignableToTypeOf(&[]types.NodeGPU{})).Return(
assert.AnError,
)
_, err := gpu.Get(ctx, client, twinID)
assert.Error(t, err)
})
}
83 changes: 83 additions & 0 deletions grid-proxy/internal/indexer/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package indexer

import (
"context"
"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"
"github.com/threefoldtech/zos/pkg/diagnostics"
)

func TestNewHealthWork(t *testing.T) {
wanted := &HealthWork{
findersInterval: map[string]time.Duration{
"up": 2 * time.Minute,
"healthy": 2 * time.Minute,
},
}
health := NewHealthWork(2)
assert.Exactlyf(t, wanted, health, "got: %v , expected: %v", health, wanted)
}

func TestHealthGet(t *testing.T) {
health := NewHealthWork(2)
ctrl := gomock.NewController(t)
ctx := context.Background()

t.Run("get health with valid twin id", func(t *testing.T) {
twinID := uint32(1)
expected := []types.HealthReport{
{
NodeTwinId: 1,
Healthy: true,
UpdatedAt: time.Now().Unix(),
},
}
client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, healthCallCmd, nil, gomock.AssignableToTypeOf(&diagnostics.Diagnostics{})).DoAndReturn(
func(ctx context.Context, twin uint32, fn string, data, result interface{}) error {
diag := result.(*diagnostics.Diagnostics)
diag.Healthy = true
return nil
},
)
got, err := health.Get(ctx, client, twinID)

assert.NoError(t, err)
assert.Equal(t, expected[0].NodeTwinId, got[0].NodeTwinId)
assert.Equal(t, expected[0].Healthy, got[0].Healthy)
assert.Len(t, got, 1)
})

t.Run("get health with invalid twin id", func(t *testing.T) {
twinID := uint32(2)
client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, healthCallCmd, nil, gomock.AssignableToTypeOf(&diagnostics.Diagnostics{})).Return(
assert.AnError,
)
got, _ := health.Get(ctx, client, twinID)
// we expect an error here because the twin id is invalid but the implementation ignore errors
//assert.Error(t, err)
assert.Len(t, got, 1)
assert.False(t, got[0].Healthy)
})
}

func TestRemoveDuplicates(t *testing.T) {
reports := []types.HealthReport{
{NodeTwinId: 1, Healthy: true},
{NodeTwinId: 2, Healthy: false},
{NodeTwinId: 1, Healthy: true}, //Duplicate
{NodeTwinId: 3, Healthy: true},
}

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])
}
64 changes: 64 additions & 0 deletions grid-proxy/internal/indexer/ipv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package indexer

import (
"context"
"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 TestNewIpv6Work(t *testing.T) {
wanted := &Ipv6Work{
finders: map[string]time.Duration{
"up": 2 * time.Minute,
},
}
ipv6 := NewIpv6Work(2)
assert.Exactlyf(t, wanted, ipv6, "got: %v , expected: %v", ipv6, wanted)
}

func TestIpv6Get(t *testing.T) {
ipv6 := NewIpv6Work(2)
ctrl := gomock.NewController(t)
ctx := context.Background()

t.Run("get ipv6 with valid twin id", func(t *testing.T) {
twinID := uint32(1)
expected := []types.HasIpv6{
{
NodeTwinId: 1,
HasIpv6: true,
UpdatedAt: time.Now().Unix(),
},
}
client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, cmd, nil, gomock.Any()).DoAndReturn(
func(ctx context.Context, twin uint32, fn string, data, result interface{}) error {
*(result.(*bool)) = true
return nil
},
)
got, err := ipv6.Get(ctx, client, twinID)

assert.NoError(t, err)
assert.Equal(t, expected[0].NodeTwinId, got[0].NodeTwinId)
assert.Equal(t, expected[0].HasIpv6, got[0].HasIpv6)
assert.Len(t, got, 1)
})

t.Run("get ipv6 with invalid twin id", func(t *testing.T) {
twinID := uint32(2)
client := mock_rmb.NewMockClient(ctrl)
client.EXPECT().Call(gomock.Any(), twinID, cmd, nil, gomock.Any()).Return(
assert.AnError,
)
got, _ := ipv6.Get(ctx, client, twinID)
// we expect an error here because the twin id is invalid but the implementation ignore errors
//assert.NoError(t, err)
assert.Empty(t, got)
})
}

0 comments on commit f4ea28e

Please # to comment.