-
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.
add tests for speed, workload and finders indexers
- Loading branch information
1 parent
f4ea28e
commit e6d912d
Showing
5 changed files
with
268 additions
and
8 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
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) | ||
|
||
} |
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
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,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) | ||
}) | ||
} |
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,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) | ||
}) | ||
} |