-
Notifications
You must be signed in to change notification settings - Fork 296
/
tests.rs
491 lines (441 loc) · 18.2 KB
/
tests.rs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use super::*;
use ic_ic00_types::{
CanisterChange, CanisterChangeDetails, CanisterChangeOrigin, CanisterInstallMode,
LogVisibility, IC_00,
};
use ic_replicated_state::canister_state::system_state::CanisterHistory;
use ic_replicated_state::metadata_state::subnet_call_context_manager::InstallCodeCallId;
use ic_test_utilities::types::ids::user_test_id;
use ic_test_utilities::types::{
ids::canister_test_id,
messages::{IngressBuilder, RequestBuilder, ResponseBuilder},
};
use ic_test_utilities_logger::with_test_replica_logger;
use ic_test_utilities_time::mock_time;
use ic_test_utilities_tmpdir::tmpdir;
use ic_types::messages::{CanisterCall, CanisterMessage, CanisterMessageOrTask};
use itertools::Itertools;
use proptest::prelude::*;
use std::fs::File;
use std::sync::Arc;
fn default_canister_state_bits() -> CanisterStateBits {
CanisterStateBits {
controllers: BTreeSet::new(),
last_full_execution_round: ExecutionRound::from(0),
call_context_manager: None,
compute_allocation: ComputeAllocation::try_from(0).unwrap(),
accumulated_priority: AccumulatedPriority::default(),
execution_state_bits: None,
memory_allocation: MemoryAllocation::default(),
freeze_threshold: NumSeconds::from(0),
cycles_balance: Cycles::zero(),
cycles_debit: Cycles::zero(),
reserved_balance: Cycles::zero(),
reserved_balance_limit: None,
status: CanisterStatus::Stopped,
scheduled_as_first: 0,
skipped_round_due_to_no_messages: 0,
executed: 0,
interrupted_during_execution: 0,
certified_data: vec![],
consumed_cycles_since_replica_started: NominalCycles::from(0),
stable_memory_size: NumWasmPages::from(0),
heap_delta_debit: NumBytes::from(0),
install_code_debit: NumInstructions::from(0),
time_of_last_allocation_charge_nanos: mock_time().as_nanos_since_unix_epoch(),
task_queue: vec![],
global_timer_nanos: None,
canister_version: 0,
consumed_cycles_since_replica_started_by_use_cases: BTreeMap::new(),
canister_history: CanisterHistory::default(),
wasm_chunk_store_metadata: WasmChunkStoreMetadata::default(),
total_query_stats: TotalQueryStats::default(),
log_visibility: LogVisibility::default(),
}
}
#[test]
fn test_state_layout_diverged_state_paths() {
with_test_replica_logger(|log| {
let tempdir = tmpdir("state_layout");
let root_path = tempdir.path().to_path_buf();
let metrics_registry = ic_metrics::MetricsRegistry::new();
let state_layout = StateLayout::try_new(log, root_path.clone(), &metrics_registry).unwrap();
state_layout
.create_diverged_state_marker(Height::new(1))
.unwrap();
assert_eq!(
state_layout.diverged_state_heights().unwrap(),
vec![Height::new(1)],
);
assert!(state_layout
.diverged_state_marker_path(Height::new(1))
.starts_with(root_path.join("diverged_state_markers")));
state_layout
.remove_diverged_state_marker(Height::new(1))
.unwrap();
assert!(state_layout.diverged_state_heights().unwrap().is_empty());
});
}
#[test]
fn test_encode_decode_empty_controllers() {
// A canister state with empty controllers.
let canister_state_bits = default_canister_state_bits();
let pb_bits = pb_canister_state_bits::CanisterStateBits::from(canister_state_bits);
let canister_state_bits = CanisterStateBits::try_from(pb_bits).unwrap();
// Controllers are still empty, as expected.
assert_eq!(canister_state_bits.controllers, BTreeSet::new());
}
#[test]
fn test_encode_decode_non_empty_controllers() {
let mut controllers = BTreeSet::new();
controllers.insert(IC_00.into());
controllers.insert(canister_test_id(0).get());
// A canister state with non-empty controllers.
let canister_state_bits = CanisterStateBits {
controllers,
..default_canister_state_bits()
};
let pb_bits = pb_canister_state_bits::CanisterStateBits::from(canister_state_bits);
let canister_state_bits = CanisterStateBits::try_from(pb_bits).unwrap();
let mut expected_controllers = BTreeSet::new();
expected_controllers.insert(canister_test_id(0).get());
expected_controllers.insert(IC_00.into());
assert_eq!(canister_state_bits.controllers, expected_controllers);
}
#[test]
fn test_encode_decode_empty_history() {
let canister_history = CanisterHistory::default();
// A canister state with empty history.
let canister_state_bits = CanisterStateBits {
canister_history: canister_history.clone(),
..default_canister_state_bits()
};
let pb_bits = pb_canister_state_bits::CanisterStateBits::from(canister_state_bits);
let canister_state_bits = CanisterStateBits::try_from(pb_bits).unwrap();
assert_eq!(canister_state_bits.canister_history, canister_history);
}
#[test]
fn test_encode_decode_non_empty_history() {
let mut canister_history = CanisterHistory::default();
canister_history.add_canister_change(CanisterChange::new(
42,
0,
CanisterChangeOrigin::from_user(user_test_id(42).get()),
CanisterChangeDetails::canister_creation(vec![
canister_test_id(777).get(),
user_test_id(42).get(),
]),
));
canister_history.add_canister_change(CanisterChange::new(
123,
1,
CanisterChangeOrigin::from_canister(canister_test_id(123).get(), None),
CanisterChangeDetails::CanisterCodeUninstall,
));
canister_history.add_canister_change(CanisterChange::new(
222,
2,
CanisterChangeOrigin::from_canister(canister_test_id(123).get(), Some(777)),
CanisterChangeDetails::code_deployment(CanisterInstallMode::Install, [0; 32]),
));
canister_history.add_canister_change(CanisterChange::new(
222,
3,
CanisterChangeOrigin::from_canister(canister_test_id(123).get(), Some(888)),
CanisterChangeDetails::code_deployment(CanisterInstallMode::Upgrade, [1; 32]),
));
canister_history.add_canister_change(CanisterChange::new(
222,
4,
CanisterChangeOrigin::from_canister(canister_test_id(123).get(), Some(999)),
CanisterChangeDetails::code_deployment(CanisterInstallMode::Reinstall, [2; 32]),
));
canister_history.add_canister_change(CanisterChange::new(
333,
5,
CanisterChangeOrigin::from_canister(canister_test_id(123).get(), None),
CanisterChangeDetails::controllers_change(vec![
canister_test_id(123).into(),
user_test_id(666).get(),
]),
));
canister_history.add_canister_change(CanisterChange::new(
444,
6,
CanisterChangeOrigin::from_canister(canister_test_id(123).get(), None),
CanisterChangeDetails::controllers_change(vec![]),
));
// A canister state with non-empty history.
let canister_state_bits = CanisterStateBits {
canister_history: canister_history.clone(),
..default_canister_state_bits()
};
let pb_bits = pb_canister_state_bits::CanisterStateBits::from(canister_state_bits);
let canister_state_bits = CanisterStateBits::try_from(pb_bits).unwrap();
assert_eq!(canister_state_bits.canister_history, canister_history);
}
#[test]
fn test_canister_snapshots_decode() {
let canister_snapshot_bits = CanisterSnapshotBits {
snapshot_id: SnapshotId::new(5),
canister_id: canister_test_id(7),
taken_at_timestamp: mock_time(),
canister_version: 3,
binary_hash: Some(WasmHash::from(&CanisterModule::new(vec![2, 3, 4]))),
certified_data: vec![3, 4, 7],
wasm_chunk_store_metadata: WasmChunkStoreMetadata::default(),
};
let pb_bits = pb_canister_snapshot_bits::CanisterSnapshotBits::from(&canister_snapshot_bits);
let new_canister_snapshot_bits = CanisterSnapshotBits::try_from(pb_bits).unwrap();
assert_eq!(canister_snapshot_bits, new_canister_snapshot_bits);
}
#[test]
fn test_encode_decode_task_queue() {
let ingress = Arc::new(IngressBuilder::new().method_name("test_ingress").build());
let request = Arc::new(RequestBuilder::new().method_name("test_request").build());
let response = Arc::new(
ResponseBuilder::new()
.respondent(canister_test_id(42))
.build(),
);
let task_queue = vec![
ExecutionTask::AbortedInstallCode {
message: CanisterCall::Ingress(Arc::clone(&ingress)),
prepaid_execution_cycles: Cycles::new(1),
call_id: InstallCodeCallId::new(0),
},
ExecutionTask::AbortedExecution {
input: CanisterMessageOrTask::Message(CanisterMessage::Request(Arc::clone(&request))),
prepaid_execution_cycles: Cycles::new(2),
},
ExecutionTask::AbortedInstallCode {
message: CanisterCall::Request(Arc::clone(&request)),
prepaid_execution_cycles: Cycles::new(3),
call_id: InstallCodeCallId::new(3u64),
},
ExecutionTask::AbortedExecution {
input: CanisterMessageOrTask::Message(CanisterMessage::Response(Arc::clone(&response))),
prepaid_execution_cycles: Cycles::new(4),
},
ExecutionTask::AbortedExecution {
input: CanisterMessageOrTask::Message(CanisterMessage::Ingress(Arc::clone(&ingress))),
prepaid_execution_cycles: Cycles::new(5),
},
];
let canister_state_bits = CanisterStateBits {
task_queue: task_queue.clone(),
..default_canister_state_bits()
};
let pb_bits = pb_canister_state_bits::CanisterStateBits::from(canister_state_bits);
let canister_state_bits = CanisterStateBits::try_from(pb_bits).unwrap();
assert_eq!(canister_state_bits.task_queue, task_queue);
}
#[test]
fn test_removal_when_last_dropped() {
with_test_replica_logger(|log| {
let tempdir = tmpdir("state_layout");
let root_path = tempdir.path().to_path_buf();
let metrics_registry = ic_metrics::MetricsRegistry::new();
let state_layout = StateLayout::try_new(log, root_path, &metrics_registry).unwrap();
let scratchpad_dir = tmpdir("scratchpad");
let cp1 = state_layout
.scratchpad_to_checkpoint(
CheckpointLayout::<RwPolicy<()>>::new_untracked(
scratchpad_dir.path().to_path_buf().join("1"),
Height::new(1),
)
.unwrap(),
Height::new(1),
None,
)
.unwrap();
let cp2 = state_layout
.scratchpad_to_checkpoint(
CheckpointLayout::<RwPolicy<()>>::new_untracked(
scratchpad_dir.path().to_path_buf().join("2"),
Height::new(2),
)
.unwrap(),
Height::new(2),
None,
)
.unwrap();
// Add one checkpoint so that we never remove the last one and crash
let _cp3 = state_layout
.scratchpad_to_checkpoint(
CheckpointLayout::<RwPolicy<()>>::new_untracked(
scratchpad_dir.path().to_path_buf().join("3"),
Height::new(3),
)
.unwrap(),
Height::new(3),
None,
)
.unwrap();
assert_eq!(
vec![Height::new(1), Height::new(2), Height::new(3)],
state_layout.checkpoint_heights().unwrap(),
);
std::mem::drop(cp1);
state_layout.remove_checkpoint_when_unused(Height::new(1));
state_layout.remove_checkpoint_when_unused(Height::new(2));
assert_eq!(
vec![Height::new(2), Height::new(3)],
state_layout.checkpoint_heights().unwrap(),
);
std::mem::drop(cp2);
assert_eq!(
vec![Height::new(3)],
state_layout.checkpoint_heights().unwrap(),
);
});
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn test_last_removal_panics_in_debug() {
with_test_replica_logger(|log| {
let tempdir = tmpdir("state_layout");
let root_path = tempdir.path().to_path_buf();
let metrics_registry = ic_metrics::MetricsRegistry::new();
let state_layout = StateLayout::try_new(log, root_path, &metrics_registry).unwrap();
let scratchpad_dir = tmpdir("scratchpad");
let cp1 = state_layout
.scratchpad_to_checkpoint(
CheckpointLayout::<RwPolicy<()>>::new_untracked(
scratchpad_dir.path().to_path_buf().join("1"),
Height::new(1),
)
.unwrap(),
Height::new(1),
None,
)
.unwrap();
state_layout.remove_checkpoint_when_unused(Height::new(1));
std::mem::drop(cp1);
});
}
#[test]
fn test_canister_id_from_path() {
assert_eq!(
Some(CanisterId::from_u64(1)),
canister_id_from_path(Path::new(
"canister_states/00000000000000010101/canister.pbuf"
))
);
assert_eq!(
Some(CanisterId::from_u64(2)),
canister_id_from_path(Path::new(
"canister_states/00000000000000020101/queues.pbuf"
))
);
assert_eq!(
None,
canister_id_from_path(Path::new(
"foo/canister_states/00000000000000030101/queues.pbuf"
))
);
assert_eq!(None, canister_id_from_path(Path::new(SUBNET_QUEUES_FILE)));
assert_eq!(None, canister_id_from_path(Path::new("canister_states")));
assert_eq!(
None,
canister_id_from_path(Path::new("canister_states/not-a-canister-ID/queues.pbuf"))
);
}
// A strategy to create a randomly sampled and strictly monotonic sequence of `Height`.
fn random_sorted_unique_heights(max_length: usize) -> impl Strategy<Value = Vec<Height>> {
// Take a vector of length max_length, sort it and remove duplicate entries.
let unsorted = prop::collection::vec(0u64.., max_length);
unsorted.prop_map(|heights| {
let mut heights: Vec<Height> = heights.iter().map(|h| Height::new(*h)).collect();
heights.sort();
heights.iter().unique().cloned().collect()
})
}
proptest! {
#[test]
fn read_back_wasm_memory_overlay_file_names(heights in random_sorted_unique_heights(10)) {
let tmp = tmpdir("canister");
let canister_layout: CanisterLayout<WriteOnly> =
CanisterLayout::new(tmp.path().to_owned()).unwrap();
let overlay_names: Vec<PathBuf> = heights
.iter()
.map(|h| canister_layout.vmemory_0_overlay(*h))
.collect();
// Create the overlay files in the directory.
for overlay in &overlay_names {
File::create(overlay).unwrap();
}
// Create some other files that should be ignored.
File::create(canister_layout.raw_path().join("otherfile")).unwrap();
File::create(canister_layout.stable_memory_overlay(Height::new(42))).unwrap();
File::create(canister_layout.wasm_chunk_store_overlay(Height::new(42))).unwrap();
File::create(canister_layout.vmemory_0()).unwrap();
let existing_overlays = canister_layout.vmemory_0_overlays().unwrap();
// We expect the list of paths to be the same including ordering.
prop_assert_eq!(overlay_names, existing_overlays);
}
#[test]
fn read_back_stable_memory_overlay_file_names(heights in random_sorted_unique_heights(10)) {
let tmp = tmpdir("canister");
let canister_layout: CanisterLayout<WriteOnly> =
CanisterLayout::new(tmp.path().to_owned()).unwrap();
let overlay_names: Vec<PathBuf> = heights
.iter()
.map(|h| canister_layout.stable_memory_overlay(*h))
.collect();
// Create the overlay files in the directory.
for overlay in &overlay_names {
File::create(overlay).unwrap();
}
// Create some other files that should be ignored.
File::create(canister_layout.raw_path().join("otherfile")).unwrap();
File::create(canister_layout.vmemory_0_overlay(Height::new(42))).unwrap();
File::create(canister_layout.wasm_chunk_store_overlay(Height::new(42))).unwrap();
File::create(canister_layout.stable_memory_blob()).unwrap();
let existing_overlays = canister_layout.stable_memory_overlays().unwrap();
// We expect the list of paths to be the same including ordering.
prop_assert_eq!(overlay_names, existing_overlays);
}
#[test]
fn read_back_wasm_chunk_store_overlay_file_names(heights in random_sorted_unique_heights(10)) {
let tmp = tmpdir("canister");
let canister_layout: CanisterLayout<WriteOnly> =
CanisterLayout::new(tmp.path().to_owned()).unwrap();
let overlay_names: Vec<PathBuf> = heights
.iter()
.map(|h| canister_layout.wasm_chunk_store_overlay(*h))
.collect();
// Create the overlay files in the directory.
for overlay in &overlay_names {
File::create(overlay).unwrap();
}
// Create some other files that should be ignored.
File::create(canister_layout.raw_path().join("otherfile")).unwrap();
File::create(canister_layout.vmemory_0_overlay(Height::new(42))).unwrap();
File::create(canister_layout.stable_memory_overlay(Height::new(42))).unwrap();
File::create(canister_layout.wasm_chunk_store()).unwrap();
let existing_overlays = canister_layout.wasm_chunk_store_overlays().unwrap();
// We expect the list of paths to be the same including ordering.
prop_assert_eq!(overlay_names, existing_overlays);
}
#[test]
fn read_back_checkpoint_directory_names(heights in random_sorted_unique_heights(10)) {
with_test_replica_logger(|log| {
let tmp = tmpdir("state_layout");
let metrics_registry = ic_metrics::MetricsRegistry::new();
let state_layout = StateLayout::try_new(log, tmp.path().to_owned(), &metrics_registry).unwrap();
let checkpoint_names: Vec<PathBuf> = heights
.iter()
.map(|h| state_layout.checkpoints().join(StateLayout::checkpoint_name(*h)))
.collect();
// Create the (empty) checkpoint directories.
for checkpoint in &checkpoint_names {
std::fs::create_dir(checkpoint).unwrap();
}
let existing_heights = state_layout.checkpoint_heights().unwrap();
// We expect the list of heights to be the same including ordering.
assert_eq!(heights, existing_heights);
});
}
}