-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync_tests.rs
More file actions
233 lines (191 loc) · 6.03 KB
/
sync_tests.rs
File metadata and controls
233 lines (191 loc) · 6.03 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
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
use icp::{
fs::{create_dir_all, write_string},
prelude::*,
};
use indoc::formatdoc;
use predicates::{
prelude::PredicateBooleanExt,
str::{PredicateStrExt, contains},
};
use crate::common::{ENVIRONMENT_RANDOM_PORT, NETWORK_RANDOM_PORT, TestContext, clients};
mod common;
#[test]
fn sync_adapter_script_single() {
let ctx = TestContext::new();
// Setup project
let project_dir = ctx.create_project_dir("icp");
// Use vendored WASM
let wasm = ctx.make_asset("example_icp_mo.wasm");
// Project manifest
let pm = formatdoc! {r#"
canisters:
- name: my-canister
build:
steps:
- type: script
command: cp {wasm} "$ICP_WASM_OUTPUT_PATH"
sync:
steps:
- type: script
command: echo "syncing"
{NETWORK_RANDOM_PORT}
{ENVIRONMENT_RANDOM_PORT}
"#};
write_string(
&project_dir.join("icp.yaml"), // path
&pm, // contents
)
.expect("failed to write project manifest");
// Start network
let _g = ctx.start_network_in(&project_dir, "my-network");
// Wait for network
ctx.ping_until_healthy(&project_dir, "my-network");
// Deploy project (it should sync as well)
clients::icp(&ctx, &project_dir, Some("my-environment".to_string())).mint_cycles(10 * TRILLION);
ctx.icp()
.current_dir(&project_dir)
.args([
"--debug",
"deploy",
"--subnet",
common::SUBNET_ID,
"--environment",
"my-environment",
])
.assert()
.success()
.stdout(contains("syncing").trim());
// Invoke sync
ctx.icp()
.current_dir(project_dir)
.args(["--debug", "sync", "--environment", "my-environment"])
.assert()
.success()
.stdout(contains("syncing").trim());
}
#[test]
fn sync_adapter_script_multiple() {
let ctx = TestContext::new();
// Setup project
let project_dir = ctx.create_project_dir("icp");
// Use vendored WASM
let wasm = ctx.make_asset("example_icp_mo.wasm");
// Project manifest
let pm = formatdoc! {r#"
canisters:
- name: my-canister
build:
steps:
- type: script
command: cp {wasm} "$ICP_WASM_OUTPUT_PATH"
sync:
steps:
- type: script
command: echo "second"
- type: script
command: echo "first"
{NETWORK_RANDOM_PORT}
{ENVIRONMENT_RANDOM_PORT}
"#};
write_string(
&project_dir.join("icp.yaml"), // path
&pm, // contents
)
.expect("failed to write project manifest");
// Start network
let _g = ctx.start_network_in(&project_dir, "my-network");
// Wait for network
ctx.ping_until_healthy(&project_dir, "my-network");
// Deploy project (it should sync as well)
clients::icp(&ctx, &project_dir, Some("my-environment".to_string())).mint_cycles(10 * TRILLION);
ctx.icp()
.current_dir(&project_dir)
.args([
"--debug",
"deploy",
"--subnet",
common::SUBNET_ID,
"--environment",
"my-environment",
])
.assert()
.success()
.stdout(contains("first").and(contains("second")));
// Invoke sync
ctx.icp()
.current_dir(project_dir)
.args(["--debug", "sync", "--environment", "my-environment"])
.assert()
.success()
.stdout(contains("first").and(contains("second")));
}
#[tokio::test]
async fn sync_adapter_static_assets() {
let ctx = TestContext::new();
// Setup project
let project_dir = ctx.create_project_dir("icp");
let assets_dir = project_dir.join("www");
// Create assets directory
create_dir_all(&assets_dir).expect("failed to create assets directory");
// Create simple index page
write_string(&assets_dir.join("index.html"), "hello").expect("failed to create index page");
// Project manifest
let pm = formatdoc! {r#"
canisters:
- name: my-canister
build:
steps:
- type: pre-built
url: https://github.com/dfinity/sdk/raw/refs/tags/0.27.0/src/distributed/assetstorage.wasm.gz
sha256: 865eb25df5a6d857147e078bb33c727797957247f7af2635846d65c5397b36a6
sync:
steps:
- type: assets
dirs:
- {assets_dir}
{NETWORK_RANDOM_PORT}
{ENVIRONMENT_RANDOM_PORT}
"#};
write_string(
&project_dir.join("icp.yaml"), // path
&pm, // contents
)
.expect("failed to write project manifest");
// Start network
let _g = ctx.start_network_in(&project_dir, "my-network");
// Wait for network
ctx.ping_until_healthy(&project_dir, "my-network");
let network_port = ctx
.wait_for_network_descriptor(&project_dir, "my-network")
.gateway_port;
// Canister ID
let cid = "tqzl2-p7777-77776-aaaaa-cai";
// Deploy project
clients::icp(&ctx, &project_dir, Some("my-environment".to_string())).mint_cycles(10 * TRILLION);
ctx.icp()
.current_dir(&project_dir)
.args([
"deploy",
"--subnet",
common::SUBNET_ID,
"--environment",
"my-environment",
])
.assert()
.success();
// Invoke sync
ctx.icp()
.current_dir(project_dir)
.args(["sync", "--environment", "my-environment"])
.assert()
.success();
// Verify that assets canister was synced
let resp = reqwest::get(format!("http://localhost:{network_port}/?canisterId={cid}"))
.await
.expect("request failed");
let out = resp
.text()
.await
.expect("failed to read canister response body");
assert_eq!(out, "hello");
}