-
Notifications
You must be signed in to change notification settings - Fork 207
/
deployment.go
128 lines (107 loc) · 3.07 KB
/
deployment.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
120
121
122
123
124
125
126
127
128
package testutil
import (
"fmt"
"math/rand"
"testing"
apptypes "github.com/ovrclk/akash/app/types"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
"github.com/stretchr/testify/assert"
crypto "github.com/tendermint/go-crypto"
)
func RandUint32() uint32 {
return uint32(rand.Int31n(100))
}
func RandUint64() uint64 {
return uint64(rand.Int63n(100))
}
func CreateDeployment(t *testing.T, app apptypes.Application, account *types.Account, key crypto.PrivKey, nonce uint64) (*types.Deployment, *types.DeploymentGroups) {
deployment := Deployment(account.Address, nonce)
groups := DeploymentGroups(deployment.Address, nonce)
ttl := int64(5)
specs := []*types.GroupSpec{}
for _, group := range groups.GetItems() {
s := &types.GroupSpec{
Resources: group.Resources,
Requirements: group.Requirements,
}
specs = append(specs, s)
}
deploymenttx := &types.TxPayload_TxCreateDeployment{
TxCreateDeployment: &types.TxCreateDeployment{
Tenant: account.Address,
Nonce: nonce,
OrderTTL: ttl,
Groups: specs,
},
}
ctx := apptypes.NewContext(&types.Tx{
Key: key.PubKey().Bytes(),
Payload: types.TxPayload{
Payload: deploymenttx,
},
})
assert.True(t, app.AcceptTx(ctx, deploymenttx))
cresp := app.CheckTx(ctx, deploymenttx)
assert.True(t, cresp.IsOK())
dresp := app.DeliverTx(ctx, deploymenttx)
assert.Len(t, dresp.Log, 0, fmt.Sprint("Log should be empty but is: ", dresp.Log))
assert.True(t, dresp.IsOK())
return deployment, groups
}
func CloseDeployment(t *testing.T, app apptypes.Application, deployment *base.Bytes, key crypto.PrivKey) {
tx := &types.TxPayload_TxCloseDeployment{
TxCloseDeployment: &types.TxCloseDeployment{
Deployment: *deployment,
Reason: types.TxCloseDeployment_TENANT_CLOSE,
},
}
ctx := apptypes.NewContext(&types.Tx{
Key: key.PubKey().Bytes(),
Payload: types.TxPayload{
Payload: tx,
},
})
assert.True(t, app.AcceptTx(ctx, tx))
cresp := app.CheckTx(ctx, tx)
assert.True(t, cresp.IsOK())
dresp := app.DeliverTx(ctx, tx)
assert.Len(t, dresp.Log, 0, fmt.Sprint("Log should be empty but is: ", dresp.Log))
assert.True(t, dresp.IsOK())
}
func Deployment(tenant base.Bytes, nonce uint64) *types.Deployment {
return &types.Deployment{
Tenant: tenant,
Address: state.DeploymentAddress(tenant, nonce),
}
}
func DeploymentGroups(deployment base.Bytes, nonce uint64) *types.DeploymentGroups {
orderTTL := int64(5)
// nonce++
runit := types.ResourceUnit{
Cpu: RandUint32(),
Memory: RandUint32(),
Disk: RandUint64(),
}
rgroup := types.ResourceGroup{
Unit: runit,
Count: RandUint32(),
Price: RandUint32(),
}
pattr := types.ProviderAttribute{
Name: "region",
Value: "us-west",
}
group := &types.DeploymentGroup{
DeploymentGroupID: types.DeploymentGroupID{
Deployment: deployment,
Seq: nonce,
},
Resources: []types.ResourceGroup{rgroup},
Requirements: []types.ProviderAttribute{pattr},
OrderTTL: orderTTL,
}
groups := []*types.DeploymentGroup{group}
return &types.DeploymentGroups{Items: groups}
}