-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat indexer gpu, health and ipv6 test
Signed-off-by: nabil salah <nabil.salah203@gmail.com>
- Loading branch information
1 parent
ca52323
commit f4ea28e
Showing
3 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |