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
use crate::{
account,
context::UserContext,
deploy,
dev_api_client::DevApiClient,
shared::{self, normalized_network_name, Home, Network, LATEST_USERNAME, TEST_USERNAME},
};
use anyhow::{anyhow, Result};
use core::convert::TryFrom;
use diem_crypto::{ed25519::Ed25519PrivateKey, PrivateKey};
use diem_sdk::{
client::AccountAddress, transaction_builder::TransactionFactory, types::LocalAccount,
};
use diem_types::{chain_id::ChainId, transaction::authenticator::AuthenticationKey};
use move_cli::package::cli::{self, UnitTestResult};
use move_package::BuildConfig;
use move_unit_test::UnitTestingConfig;
use std::{
path::{Path, PathBuf},
process::{Command, ExitStatus},
};
use structopt::StructOpt;
use tempfile::TempDir;
use url::Url;
pub async fn run_e2e_tests(
home: &Home,
project_path: &Path,
network: Network,
) -> Result<ExitStatus> {
let _config = shared::read_project_config(project_path)?;
println!("Connecting to {}...", network.get_json_rpc_url());
let client = DevApiClient::new(reqwest::Client::new(), network.get_dev_api_url())?;
let factory = TransactionFactory::new(ChainId::test());
let (private_key1, mut account1) =
create_account(home.get_root_key_path(), &client, &factory, &network).await?;
shared::codegen_typescript_libraries(project_path, &account1.address())?;
deploy::deploy(&client, &mut account1, project_path).await?;
let tmp_dir = TempDir::new()?;
let key1_path = tmp_dir.path().join("private1.key");
generate_key::save_key(private_key1, &key1_path);
let latest_user = UserContext::new(LATEST_USERNAME, account1.address(), &key1_path);
let (private_key2, account2) =
create_account(home.get_root_key_path(), &client, &factory, &network).await?;
let key2_path = tmp_dir.path().join("private2.key");
let test_user = UserContext::new(TEST_USERNAME, account2.address(), &key2_path);
generate_key::save_key(private_key2, &key2_path);
run_deno_test(home, project_path, &network, &[&latest_user, &test_user])
}
async fn create_account(
root_key_path: &Path,
client: &DevApiClient,
factory: &TransactionFactory,
network: &Network,
) -> Result<(Ed25519PrivateKey, LocalAccount)> {
let account_key = generate_key::generate_key();
let new_account = generate_new_account(&account_key, client).await?;
match network.get_faucet_url() {
Some(_) => account::create_account_via_faucet(network, &new_account).await?,
None => {
let mut treasury_account = account::get_treasury_account(client, root_key_path).await?;
account::create_account_via_dev_api(
&mut treasury_account,
&new_account,
factory,
client,
)
.await?;
}
};
Ok((account_key, new_account))
}
async fn generate_new_account(
account_key: &Ed25519PrivateKey,
client: &DevApiClient,
) -> Result<LocalAccount> {
let public_key = account_key.public_key();
let derived_address = AuthenticationKey::ed25519(&public_key).derived_address();
let seq_num = client
.get_account_sequence_number(derived_address)
.await
.unwrap_or(0);
let dupe_key = Ed25519PrivateKey::try_from(account_key.to_bytes().as_ref())?;
Ok(LocalAccount::new(derived_address, dupe_key, seq_num))
}
pub fn run_deno_test(
home: &Home,
project_path: &Path,
network: &Network,
users: &[&UserContext],
) -> Result<ExitStatus> {
let test_path = project_path.join("e2e");
run_deno_test_at_path(home, project_path, network, users, &test_path)
}
pub fn run_deno_test_at_path(
home: &Home,
project_path: &Path,
network: &Network,
users: &[&UserContext],
test_path: &Path,
) -> Result<ExitStatus> {
let filtered_envs = shared::get_filtered_envs_for_deno(home, project_path, network, users)?;
let env_names: String = filtered_envs
.keys()
.cloned()
.collect::<Vec<String>>()
.join(",");
let status = Command::new("deno")
.args([
"test",
"--unstable",
test_path.to_string_lossy().as_ref(),
format!("--allow-env={}", env_names).as_str(),
"--allow-read",
format!(
"--allow-net={},{}",
host_and_port(&network.get_dev_api_url())?,
host_and_port(&network.get_json_rpc_url())?,
)
.as_str(),
])
.envs(&filtered_envs)
.spawn()
.expect("deno failed to start, is it installed? brew install deno")
.wait()?;
Ok(status)
}
fn host_and_port(url: &Url) -> Result<String> {
Ok(format!(
"{}:{}",
url.host_str()
.ok_or_else(|| anyhow!("url should have domain host"))?,
url.port_or_known_default()
.ok_or_else(|| anyhow!("url should have port or default"))?,
))
}
pub fn run_move_unit_tests(project_path: &Path) -> Result<UnitTestResult> {
let unit_test_config = UnitTestingConfig {
report_storage_on_error: true,
..UnitTestingConfig::default_with_bound(None)
};
let publishing_address = AccountAddress::from_hex_literal(shared::PLACEHOLDER_ADDRESS)?;
cli::run_move_unit_tests(
&project_path.join(shared::MAIN_PKG_PATH),
generate_build_config_for_testing(
&project_path.join(shared::MAIN_PKG_PATH),
&publishing_address,
)?,
unit_test_config,
diem_vm::natives::diem_natives(),
false,
)
}
fn generate_build_config_for_testing(
pkg_path: &Path,
publishing_address: &AccountAddress,
) -> Result<BuildConfig> {
let additional_named_addresses =
shared::inject_publishing_address_into_manifest(pkg_path, publishing_address)?;
Ok(BuildConfig {
dev_mode: true,
test_mode: true,
generate_abis: true,
additional_named_addresses,
..Default::default()
})
}
#[derive(Debug, StructOpt)]
pub enum TestCommand {
#[structopt(about = "Runs end to end test in shuffle")]
E2e {
#[structopt(short, long)]
project_path: Option<PathBuf>,
#[structopt(short, long)]
network: Option<String>,
},
#[structopt(about = "Runs move move unit tests in project folder")]
Unit {
#[structopt(short, long)]
project_path: Option<PathBuf>,
},
#[structopt(
about = "Runs both end to end test in shuffle and move move unit tests in project folder"
)]
All {
#[structopt(short, long)]
project_path: Option<PathBuf>,
#[structopt(short, long)]
network: Option<String>,
},
}
pub async fn handle(home: &Home, cmd: TestCommand) -> Result<()> {
let exit_status = match cmd {
TestCommand::E2e {
project_path,
network,
} => {
run_e2e_tests(
home,
shared::normalized_project_path(project_path)?.as_path(),
home.get_network_struct_from_toml(
normalized_network_name(network.clone()).as_str(),
)?,
)
.await?
}
TestCommand::Unit { project_path } => ExitStatus::from(run_move_unit_tests(
shared::normalized_project_path(project_path)?.as_path(),
)?),
TestCommand::All {
project_path,
network,
} => {
let normalized_path = shared::normalized_project_path(project_path)?;
let normalized_network = home
.get_network_struct_from_toml(normalized_network_name(network.clone()).as_str())?;
let unit_status = ExitStatus::from(run_move_unit_tests(normalized_path.as_path())?);
let e2e_status =
run_e2e_tests(home, normalized_path.as_path(), normalized_network).await?;
if !unit_status.success() {
unit_status
} else {
e2e_status
}
}
};
std::process::exit(exit_status.code().unwrap_or(1));
}