This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
action_migrate_test.go
180 lines (147 loc) · 5.26 KB
/
action_migrate_test.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package holochain
import (
"fmt"
. "github.com/holochain/holochain-proto/hash"
peer "github.com/libp2p/go-libp2p-peer"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
// ActionMigrate
func TestMigrateName(t *testing.T) {
Convey("migrate action should have the right name", t, func() {
a := ActionMigrate{}
So(a.Name(), ShouldEqual, "migrate")
})
}
func TestMigrateEntry(t *testing.T) {
Convey("empty migrate action Entry() should be retreive a serialized JSON of an empty entry in a GobEntry", t, func() {
action := ActionMigrate{}
So(action.Entry(), ShouldResemble, &GobEntry{C: "{\"Type\":\"\",\"DNAHash\":\"\",\"Key\":\"\",\"Data\":\"\"}"})
})
Convey("entries with vals work with Entry()", t, func() {
dnaHash, err := genTestStringHash()
So(err, ShouldBeNil)
key, err := genTestStringHash()
So(err, ShouldBeNil)
entry := MigrateEntry{DNAHash: dnaHash, Key: key}
action := ActionMigrate{entry: entry}
So(action.Entry(), ShouldResemble, &GobEntry{C: "{\"Type\":\"\",\"DNAHash\":\"" + dnaHash.String() + "\",\"Key\":\"" + key.String() + "\",\"Data\":\"\"}"})
})
}
func TestMigrateEntryType(t *testing.T) {
action := ActionMigrate{}
Convey("migrate action EntryType() should return the correct type", t, func() {
So(action.EntryType(), ShouldEqual, MigrateEntryType)
})
}
func TestMigrateHeaderSetGet(t *testing.T) {
Convey("empty migrate action should have empty header", t, func() {
action := ActionMigrate{}
So(action.GetHeader(), ShouldEqual, nil)
})
Convey("migrate action should be able to set and get header", t, func() {
action := ActionMigrate{}
header, err := genTestHeader()
So(err, ShouldBeNil)
So(action.GetHeader(), ShouldEqual, nil)
action.SetHeader(header)
So(action.GetHeader(), ShouldEqual, header)
action.SetHeader(nil)
So(action.GetHeader(), ShouldEqual, nil)
})
}
func TestMigrateCallShare(t *testing.T) {
n := 3
mt := setupMultiNodeTesting(n)
ringConnect(t, mt.ctx, mt.nodes, n)
defer mt.cleanupMultiNodeTesting()
Convey("ActionMigrate should share as a PUT on the DHT and roundtrip as JSON", t, func() {
var err error
header, err := genTestHeader()
entry, err := genTestMigrateEntry()
So(err, ShouldBeNil)
action := ActionMigrate{header: header, entry: entry}
// Can share from some node
var dhtHash Hash
fn := &APIFnMigrate{action: action}
callResponse, err := fn.Call(mt.nodes[0])
dhtHash, ok := callResponse.(Hash)
So(ok, ShouldBeTrue)
So(err, ShouldBeNil)
// Can get the PUT MigrateEntry from any node
for i := 0; i < n; i++ {
fmt.Printf("\nTesting retrieval of MigrateEntry PUT from node %d\n", i)
request := GetReq{H: dhtHash, StatusMask: StatusLive, GetMask: GetMaskEntry}
response, err := callGet(mt.nodes[i], request, &GetOptions{GetMask: request.GetMask})
r, ok := response.(GetResp)
So(ok, ShouldBeTrue)
So(err, ShouldBeNil)
So(&r.Entry, ShouldResemble, action.Entry())
}
})
}
func TestMigrateActionSysValidation(t *testing.T) {
d, _, h := PrepareTestChain("test")
defer CleanupTestChain(h, d)
Convey("it should invalidate DNAEntryDef", t, func() {
action := ActionMigrate{}
err := action.SysValidation(h, DNAEntryDef, nil, []peer.ID{h.nodeID})
So(err, ShouldEqual, ErrEntryDefInvalid)
})
Convey("ActionMigrate SysValidation should return an ErrActionMissingHeader error if header is missing", t, func() {
action := ActionMigrate{}
err := action.SysValidation(h, action.entry.Def(), nil, []peer.ID{h.nodeID})
So(err, ShouldEqual, ErrActionMissingHeader)
})
Convey("ActionMigrate SysValidation should validate the entry", t, func() {
header, err := genTestHeader()
So(err, ShouldBeNil)
action := ActionMigrate{header: header}
err = action.SysValidation(h, action.entry.Def(), nil, []peer.ID{h.nodeID})
// the entry is empty so there should be validation complaints
So(err.Error(), ShouldEqual, "Validation Failed: Error (input isn't valid multihash) when decoding DNAHash value ''")
action.entry, err = genTestMigrateEntry()
So(err, ShouldBeNil)
err = action.SysValidation(h, action.entry.Def(), nil, []peer.ID{h.nodeID})
So(err, ShouldBeNil)
})
}
func TestMigrateCheckValidationRequest(t *testing.T) {
Convey("MigrateAction CheckValidationRequest should always pass", t, func() {
action := ActionMigrate{}
So(action.CheckValidationRequest(action.entry.Def()), ShouldBeNil)
})
}
func TestMigrateReceive(t *testing.T) {
mt := setupMultiNodeTesting(1)
defer mt.cleanupMultiNodeTesting()
h := mt.nodes[0]
Convey("MigrateAction Receive is always an error", t, func() {
action := ActionMigrate{}
msg := h.node.NewMessage(PUT_REQUEST, HoldReq{})
response, err := action.Receive(h.dht, msg)
So(err.Error(), ShouldEqual, "Action receive is invalid")
So(response, ShouldBeNil)
})
}
// APIFnMigrate
func TestAPIFnMigrateName(t *testing.T) {
Convey("migrate action function should have the right name", t, func() {
fn := &APIFnMigrate{}
So(fn.Name(), ShouldEqual, "migrate")
})
}
func TestAPIFnMigrateArgs(t *testing.T) {
Convey("APIFnMigrate should have the correct args", t, func() {
fn := &APIFnMigrate{}
expected := []Arg{{Name: "migrationType",
Type: StringArg},
{Name: "DNAHash",
Type: HashArg},
{Name: "Key",
Type: HashArg},
{Name: "data",
Type: StringArg}}
So(fn.Args(), ShouldResemble, expected)
})
}