-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetcd_wbtest.mbt
More file actions
205 lines (193 loc) · 6.99 KB
/
Copy pathetcd_wbtest.mbt
File metadata and controls
205 lines (193 loc) · 6.99 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Reference vectors produced by CPython's `protobuf` (6.33) compiling the etcd
// `etcdserverpb` KV messages, so the self-built encoders must match the exact bytes a
// real etcd server produces and reads.
///|
fn etcd_nib(c : Char) -> Int {
if c >= '0' && c <= '9' {
c.to_int() - '0'.to_int()
} else if c >= 'a' && c <= 'f' {
c.to_int() - 'a'.to_int() + 10
} else {
c.to_int() - 'A'.to_int() + 10
}
}
///|
fn etcd_unhex(s : String) -> Bytes {
let buf = Buffer()
let chars = s.to_array()
let mut i = 0
while i + 1 < chars.length() {
buf.write_byte(
((etcd_nib(chars[i]) << 4) | etcd_nib(chars[i + 1])).to_byte(),
)
i += 2
}
buf.to_bytes()
}
///|
test "etcd RangeRequest encodes to the exact protobuf bytes protobuf produces" {
let req : EtcdRangeRequest = { key: b"foo", range_end: b"fop", limit: 100, }
// protobuf: RangeRequest(key="foo", range_end="fop", limit=100)
assert_eq(req.encode() == etcd_unhex("0a03666f6f1203666f701864"), true)
assert_eq(EtcdRangeRequest::decode(req.encode()) == req, true)
}
///|
test "etcd KeyValue encodes to the exact protobuf bytes and round-trips" {
let kv : EtcdKeyValue = {
key: b"k",
create_revision: 2,
mod_revision: 3,
version: 1,
value: b"v",
lease: 7,
}
// protobuf: KeyValue(key="k", create_revision=2, mod_revision=3, version=1,
// value="v", lease=7)
assert_eq(kv.encode() == etcd_unhex("0a016b1002180320012a01763007"), true)
assert_eq(EtcdKeyValue::decode(kv.encode()) == kv, true)
}
///|
test "etcd PutRequest encodes to the exact protobuf bytes and round-trips" {
let put : EtcdPutRequest = { key: b"k", value: b"v", lease: 7, }
// protobuf: PutRequest(key="k", value="v", lease=7)
assert_eq(put.encode() == etcd_unhex("0a016b1201761807"), true)
assert_eq(EtcdPutRequest::decode(put.encode()) == put, true)
}
///|
test "etcd RangeResponse carries nested KeyValues and a count through a round-trip" {
let resp : EtcdRangeResponse = {
kvs: [
{ ..EtcdKeyValue::empty(), key: b"a", value: b"1", mod_revision: 5, },
{ ..EtcdKeyValue::empty(), key: b"b", value: b"2", mod_revision: 6, },
],
count: 2,
}
let decoded = EtcdRangeResponse::decode(resp.encode())
assert_eq(decoded.count, 2)
assert_eq(decoded.kvs.length(), 2)
assert_eq(decoded.kvs[0].key == b"a", true)
assert_eq(decoded.kvs[1].mod_revision, 6)
assert_eq(decoded == resp, true)
}
///|
test "etcd default (empty/zero) fields are omitted, per proto3" {
// A RangeRequest for a single key with no range_end or limit encodes only field 1.
let req : EtcdRangeRequest = { key: b"x", range_end: b"", limit: 0, }
assert_eq(req.encode() == etcd_unhex("0a0178"), true)
}
///|
test "etcd Lease messages encode to the exact protobuf bytes and round-trip" {
let grant : EtcdLeaseGrantRequest = { ttl: 60, id: 1234, }
assert_eq(grant.encode() == etcd_unhex("083c10d209"), true)
assert_eq(EtcdLeaseGrantRequest::decode(grant.encode()) == grant, true)
let granted : EtcdLeaseGrantResponse = { id: 1234, ttl: 60, error: "", }
assert_eq(granted.encode() == etcd_unhex("10d209183c"), true)
assert_eq(EtcdLeaseGrantResponse::decode(granted.encode()) == granted, true)
let ka : EtcdLeaseKeepAliveRequest = { id: 1234, }
assert_eq(ka.encode() == etcd_unhex("08d209"), true)
assert_eq(EtcdLeaseKeepAliveRequest::decode(ka.encode()) == ka, true)
let kar : EtcdLeaseKeepAliveResponse = { id: 1234, ttl: 59, }
assert_eq(kar.encode() == etcd_unhex("10d209183b"), true)
assert_eq(EtcdLeaseKeepAliveResponse::decode(kar.encode()) == kar, true)
}
///|
test "etcd watch Event encodes to the exact protobuf bytes, PUT vs DELETE" {
// A PUT event carries the full key/value; type PUT (0) is the proto3 default so it
// is omitted from the wire.
let put : EtcdEvent = {
event_type: Put,
kv: {
..EtcdKeyValue::empty(),
key: b"svc/a",
value: b"1.2.3.4",
mod_revision: 9,
},
}
assert_eq(
put.encode() == etcd_unhex("12120a057376632f6118092a07312e322e332e34"),
true,
)
assert_eq(EtcdEvent::decode(put.encode()) == put, true)
// A DELETE event writes the type explicitly and just the key.
let del : EtcdEvent = {
event_type: Delete,
kv: { ..EtcdKeyValue::empty(), key: b"svc/a", },
}
assert_eq(del.encode() == etcd_unhex("080112070a057376632f61"), true)
let decoded = EtcdEvent::decode(del.encode())
assert_eq(decoded.event_type == Delete, true)
assert_eq(decoded.kv.key == b"svc/a", true)
}
///|
test "etcd Watch create/cancel requests encode to the exact protobuf bytes" {
let wc : EtcdWatchCreateRequest = {
key: b"svc/",
range_end: b"svc0",
start_revision: 5,
}
assert_eq(wc.encode() == etcd_unhex("0a047376632f1204737663301805"), true)
assert_eq(EtcdWatchCreateRequest::decode(wc.encode()) == wc, true)
// wrapped in the oneof WatchRequest as create_request (field 1)
let create_req : EtcdWatchRequest = Create(wc)
assert_eq(
create_req.encode() == etcd_unhex("0a0e0a047376632f1204737663301805"),
true,
)
assert_eq(EtcdWatchRequest::decode(create_req.encode()) == create_req, true)
// cancel_request (field 2)
let cancel_req : EtcdWatchRequest = Cancel({ watch_id: 42, })
assert_eq(cancel_req.encode() == etcd_unhex("1202082a"), true)
assert_eq(EtcdWatchRequest::decode(cancel_req.encode()) == cancel_req, true)
}
///|
test "etcd WatchResponse carries lifecycle flags and events through a round-trip" {
let resp : EtcdWatchResponse = {
watch_id: 7,
created: true,
canceled: false,
events: [
{
event_type: Put,
kv: { ..EtcdKeyValue::empty(), key: b"svc/a", value: b"h1", },
},
],
}
// protobuf: WatchResponse(watch_id=7, created=true, events=[PUT svc/a=h1])
assert_eq(
resp.encode() == etcd_unhex("100718015a0d120b0a057376632f612a026831"),
true,
)
let decoded = EtcdWatchResponse::decode(resp.encode())
assert_eq(decoded.watch_id, 7)
assert_eq(decoded.created, true)
assert_eq(decoded.events.length(), 1)
assert_eq(decoded.events[0].kv.key == b"svc/a", true)
assert_eq(decoded == resp, true)
}
///|
test "etcd DeleteRange and PutResponse encode to the exact protobuf bytes" {
let del : EtcdDeleteRangeRequest = {
key: b"svc/a",
range_end: b"",
prev_kv: true,
}
assert_eq(del.encode() == etcd_unhex("0a057376632f611801"), true)
assert_eq(EtcdDeleteRangeRequest::decode(del.encode()) == del, true)
let del_range : EtcdDeleteRangeRequest = {
key: b"svc/",
range_end: b"svc0",
prev_kv: false,
}
assert_eq(del_range.encode() == etcd_unhex("0a047376632f120473766330"), true)
let deleted : EtcdDeleteRangeResponse = { deleted: 1, prev_kvs: [], }
assert_eq(deleted.encode() == etcd_unhex("1001"), true)
assert_eq(EtcdDeleteRangeResponse::decode(deleted.encode()) == deleted, true)
let put_resp : EtcdPutResponse = {
prev_kv: { ..EtcdKeyValue::empty(), key: b"svc/a", value: b"old", },
}
assert_eq(
put_resp.encode() == etcd_unhex("120c0a057376632f612a036f6c64"),
true,
)
assert_eq(EtcdPutResponse::decode(put_resp.encode()) == put_resp, true)
}