Skip to content

Commit

Permalink
test(bid): add tests for bid gspec match (#96)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Nov 20, 2023
1 parent cf0cd3a commit f788de5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
18 changes: 9 additions & 9 deletions go/node/deployment/v1beta3/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestGroupSpecValidation(t *testing.T) {
gspec: types.GroupSpec{
Name: "",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
},
expErr: types.ErrInvalidGroups,
},
Expand All @@ -117,7 +117,7 @@ func TestGroupSpecValidation(t *testing.T) {
gspec: types.GroupSpec{
Name: "hihi",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
},
expErr: nil,
},
Expand All @@ -137,7 +137,7 @@ func TestGroupPlacementRequirementsNoSigners(t *testing.T) {
group := types.GroupSpec{
Name: "spec",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
}

providerAttr := []atypes.Provider{
Expand All @@ -154,7 +154,7 @@ func TestGroupPlacementRequirementsSignerAllOf(t *testing.T) {
group := types.GroupSpec{
Name: "spec",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
}

group.Requirements.SignedBy.AllOf = append(group.Requirements.SignedBy.AllOf, "auditor1")
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestGroupPlacementRequirementsSignerAnyOf(t *testing.T) {
group := types.GroupSpec{
Name: "spec",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
}

group.Requirements.SignedBy.AnyOf = append(group.Requirements.SignedBy.AnyOf, "auditor1")
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestGroupPlacementRequirementsSignerAllOfAnyOf(t *testing.T) {
group := types.GroupSpec{
Name: "spec",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
}

group.Requirements.SignedBy.AllOf = append(group.Requirements.SignedBy.AllOf, "auditor1")
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestGroupSpec_MatchResourcesAttributes(t *testing.T) {
group := types.GroupSpec{
Name: "spec",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
}

group.Resources[0].Storage[0].Attributes = akashtypes.Attributes{
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestGroupSpec_MatchGPUAttributes(t *testing.T) {
group := types.GroupSpec{
Name: "spec",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
}

group.Resources[0].GPU.Attributes = akashtypes.Attributes{
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestGroupSpec_MatchGPUAttributesWildcard(t *testing.T) {
group := types.GroupSpec{
Name: "spec",
Requirements: testutil.PlacementRequirements(t),
Resources: testutil.ResourcesList(t),
Resources: testutil.ResourcesList(t, 1),
}

group.Resources[0].GPU.Attributes = akashtypes.Attributes{
Expand Down
46 changes: 46 additions & 0 deletions go/node/market/v1beta4/bid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v1beta4

import (
"testing"

"github.com/stretchr/testify/require"

testutil "github.com/akash-network/akash-api/go/testutil/v1beta3"
)

func TestBid_GSpecMatch_Valid(t *testing.T) {
gspec := testutil.GroupSpec(t)

rOffer := ResourceOfferFromRU(gspec.Resources)

require.True(t, rOffer.MatchGSpec(gspec))
}

func TestBid_GSpecMatch_Valid2(t *testing.T) {
gspec := testutil.GroupSpec(t)

if len(gspec.Resources) == 1 {
rl := testutil.ResourcesList(t, 2)
rl[0].Count = 4
gspec.Resources = append(gspec.Resources, rl...)
}

rOffer := ResourceOfferFromRU(gspec.Resources)

require.True(t, rOffer.MatchGSpec(gspec))
}

func TestBid_GSpecMatch_InvalidCount(t *testing.T) {
gspec := testutil.GroupSpec(t)

if len(gspec.Resources) == 1 {
rl := testutil.ResourcesList(t, 2)
gspec.Resources = append(gspec.Resources, rl...)
}

rOffer := ResourceOfferFromRU(gspec.Resources)

gspec.Resources[0].Count = 2

require.False(t, rOffer.MatchGSpec(gspec))
}
11 changes: 8 additions & 3 deletions go/testutil/v1beta3/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/rand"

dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
Expand Down Expand Up @@ -66,16 +67,17 @@ func RandStorageQuantity() uint64 {

// ResourcesList produces an attribute list for populating a Group's
// 'Resources' fields.
func ResourcesList(t testing.TB) dtypes.ResourceUnits {
t.Helper()
func ResourcesList(t testing.TB, startID uint32) dtypes.ResourceUnits {
require.GreaterOrEqual(t, uint32(1), startID)

count := uint32(rand.Intn(10)) + 1

vals := make(dtypes.ResourceUnits, 0, count)
for i := uint32(0); i < count; i++ {
coin := sdk.NewDecCoin(testutil.CoinDenom, sdk.NewInt(rand.Int63n(9999)+1))
res := dtypes.ResourceUnit{
Resources: types.Resources{
ID: i + 1,
ID: i + startID,
CPU: &types.CPU{
Units: types.NewResourceValue(uint64(dtypes.GetValidationConfig().Unit.Min.CPU)),
},
Expand All @@ -95,6 +97,9 @@ func ResourcesList(t testing.TB) dtypes.ResourceUnits {
Count: 1,
Price: coin,
}

startID++

vals = append(vals, res)
}
return vals
Expand Down
4 changes: 2 additions & 2 deletions go/testutil/v1beta3/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func DeploymentGroup(t testing.TB, did dtypes.DeploymentID, gseq uint32) dtypes.
GroupSpec: dtypes.GroupSpec{
Name: testutil.Name(t, "dgroup"),
Requirements: PlacementRequirements(t),
Resources: ResourcesList(t),
Resources: ResourcesList(t, 1),
},
}
}
Expand All @@ -46,7 +46,7 @@ func GroupSpec(t testing.TB) dtypes.GroupSpec {
return dtypes.GroupSpec{
Name: testutil.Name(t, "dgroup"),
Requirements: PlacementRequirements(t),
Resources: ResourcesList(t),
Resources: ResourcesList(t, 1),
}
}

Expand Down

0 comments on commit f788de5

Please sign in to comment.