-
Notifications
You must be signed in to change notification settings - Fork 210
/
base.go
119 lines (101 loc) · 3.09 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package testutil
import (
"fmt"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/libs/rand"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
// ensure sdkutil.init() to seal SDK config for the tests
_ "github.com/akash-network/akash-api/go/sdkutil"
)
// CoinDenom provides ability to create coins in test functions and
// pass them into testutil functionality.
const (
CoinDenom = "uakt"
)
// Name generates a random name with the given prefix
func Name(_ testing.TB, prefix string) string {
return fmt.Sprintf("%s-%v", prefix, rand.Uint64())
}
// Hostname generates a random hostname with a "test.com" domain
func Hostname(t testing.TB) string {
return Name(t, "hostname") + ".test.com"
}
func ProviderHostname(t testing.TB) string {
return "https://" + Hostname(t)
}
// Attribute generates a random sdk.Attribute
func Attribute(t testing.TB) types.Attribute {
t.Helper()
return types.NewStringAttribute(Name(t, "attr-key"), Name(t, "attr-value"))
}
// Attributes generates a set of sdk.Attribute
func Attributes(t testing.TB) []types.Attribute {
t.Helper()
count := rand.Intn(10) + 1
vals := make([]types.Attribute, 0, count)
for i := 0; i < count; i++ {
vals = append(vals, Attribute(t))
}
return vals
}
// PlacementRequirements generates placement requirements
func PlacementRequirements(t testing.TB) types.PlacementRequirements {
return types.PlacementRequirements{
Attributes: Attributes(t),
}
}
func RandCPUUnits() uint {
return RandRangeUint(
dtypes.GetValidationConfig().Unit.Min.CPU,
dtypes.GetValidationConfig().Unit.Max.CPU)
}
func RandGPUUnits() uint {
return RandRangeUint(
dtypes.GetValidationConfig().Unit.Min.GPU,
dtypes.GetValidationConfig().Unit.Max.GPU)
}
func RandMemoryQuantity() uint64 {
return RandRangeUint64(
dtypes.GetValidationConfig().Unit.Min.Memory,
dtypes.GetValidationConfig().Unit.Max.Memory)
}
func RandStorageQuantity() uint64 {
return RandRangeUint64(
dtypes.GetValidationConfig().Unit.Min.Storage,
dtypes.GetValidationConfig().Unit.Max.Storage)
}
// Resources produces an attribute list for populating a Group's
// 'Resources' fields.
func Resources(t testing.TB) []dtypes.ResourceUnit {
t.Helper()
count := rand.Intn(10) + 1
vals := make(dtypes.ResourceUnits, 0, count)
for i := 0; i < count; i++ {
coin := sdk.NewDecCoin(CoinDenom, sdk.NewInt(rand.Int63n(9999)+1))
res := dtypes.ResourceUnit{
Resources: types.Resources{
ID: uint32(i) + 1,
CPU: &types.CPU{
Units: types.NewResourceValue(uint64(dtypes.GetValidationConfig().Unit.Min.CPU)),
},
GPU: &types.GPU{
Units: types.NewResourceValue(uint64(dtypes.GetValidationConfig().Unit.Min.GPU)),
},
Memory: &types.Memory{
Quantity: types.NewResourceValue(dtypes.GetValidationConfig().Unit.Min.Memory),
},
Storage: types.Volumes{
types.Storage{
Quantity: types.NewResourceValue(dtypes.GetValidationConfig().Unit.Min.Storage),
},
},
},
Count: 1,
Price: coin,
}
vals = append(vals, res)
}
return vals
}