forked from hyperledger-iroha/iroha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.rs
314 lines (291 loc) · 10.5 KB
/
fixtures.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
#![allow(clippy::needless_raw_string_hashes)] // triggered by `expect!` snapshots
use std::{
collections::{HashMap, HashSet},
fs,
path::{Path, PathBuf},
};
use assertables::{assert_contains, assert_contains_as_result};
use error_stack::ResultExt;
use expect_test::expect;
use iroha_config::parameters::{actual::Root as Config, user::Root as UserConfig};
use iroha_config_base::{env::MockEnv, read::ConfigReader};
use thiserror::Error;
fn fixtures_dir() -> PathBuf {
// CWD is the crate's root
PathBuf::from("tests/fixtures")
}
fn parse_env(raw: impl AsRef<str>) -> HashMap<String, String> {
raw.as_ref()
.lines()
.map(|line| {
let mut items = line.split('=');
let key = items
.next()
.expect("line should be in {key}={value} format");
let value = items
.next()
.expect("line should be in {key}={value} format");
(key.to_string(), value.to_string())
})
.collect()
}
fn test_env_from_file(p: impl AsRef<Path>) -> MockEnv {
let contents = fs::read_to_string(p).expect("the path should be valid");
let map = parse_env(contents);
MockEnv::with_map(map)
}
#[derive(Error, Debug)]
#[error("failed to load config from fixtures")]
struct FixtureConfigLoadError;
fn load_config_from_fixtures(
path: impl AsRef<Path>,
) -> error_stack::Result<Config, FixtureConfigLoadError> {
let config = ConfigReader::new()
.read_toml_with_extends(fixtures_dir().join(path))
.change_context(FixtureConfigLoadError)?
.read_and_complete::<UserConfig>()
.change_context(FixtureConfigLoadError)?
.parse()
.change_context(FixtureConfigLoadError)?;
Ok(config)
}
/// This test not only asserts that the minimal set of fields is enough;
/// it also gives an insight into every single default value
#[test]
#[allow(clippy::too_many_lines)]
fn minimal_config_snapshot() {
let config = load_config_from_fixtures("minimal_with_trusted_peers.toml")
.expect("config should be valid");
expect![[r#"
Root {
common: Common {
chain_id: ChainId(
"0",
),
key_pair: KeyPair {
public_key: PublicKey(
ed25519(
"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB",
),
),
private_key: "[REDACTED PrivateKey]",
},
peer_id: PeerId {
address: 127.0.0.1:1337,
public_key: PublicKey(
ed25519(
"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB",
),
),
},
},
network: Network {
address: WithOrigin {
value: 127.0.0.1:1337,
origin: File {
id: ParameterId(network.address),
path: "tests/fixtures/base.toml",
},
},
idle_timeout: 60s,
},
genesis: Genesis {
public_key: PublicKey(
ed25519(
"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB",
),
),
signed_file: None,
},
torii: Torii {
address: WithOrigin {
value: 127.0.0.1:8080,
origin: File {
id: ParameterId(torii.address),
path: "tests/fixtures/base.toml",
},
},
max_content_len_bytes: 16777216,
},
kura: Kura {
init_mode: Strict,
store_dir: WithOrigin {
value: "./storage",
origin: Default {
id: ParameterId(kura.store_dir),
},
},
debug_output_new_blocks: false,
},
sumeragi: Sumeragi {
trusted_peers: WithOrigin {
value: TrustedPeers {
myself: PeerId {
address: 127.0.0.1:1337,
public_key: PublicKey(
ed25519(
"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB",
),
),
},
others: UniqueVec(
[
PeerId {
address: 127.0.0.1:1338,
public_key: PublicKey(
ed25519(
"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB",
),
),
},
],
),
},
origin: File {
id: ParameterId(sumeragi.trusted_peers),
path: "tests/fixtures/base_trusted_peers.toml",
},
},
debug_force_soft_fork: false,
},
block_sync: BlockSync {
gossip_period: 10s,
gossip_max_size: 4,
},
transaction_gossiper: TransactionGossiper {
gossip_period: 1s,
gossip_max_size: 500,
},
live_query_store: LiveQueryStore {
idle_time: 30s,
},
logger: Logger {
level: INFO,
format: Full,
},
queue: Queue {
capacity: 65536,
capacity_per_user: 65536,
transaction_time_to_live: 86400s,
future_threshold: 1s,
},
snapshot: Snapshot {
mode: ReadWrite,
create_every: HumanDuration(
60s,
),
store_dir: WithOrigin {
value: "./storage/snapshot",
origin: Default {
id: ParameterId(snapshot.store_dir),
},
},
},
telemetry: None,
dev_telemetry: DevTelemetry {
out_file: None,
},
chain_wide: ChainWide {
max_transactions_in_block: 512,
block_time: 2s,
commit_time: 4s,
transaction_limits: TransactionLimits {
max_instruction_number: 4096,
max_wasm_size_bytes: 4194304,
},
domain_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
asset_definition_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
account_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
asset_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
trigger_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
ident_length_limits: LengthLimits {
min: 1,
max: 128,
},
executor_runtime: WasmRuntime {
fuel_limit: 55000000,
max_memory_bytes: 524288000,
},
wasm_runtime: WasmRuntime {
fuel_limit: 55000000,
max_memory_bytes: 524288000,
},
},
}"#]].assert_eq(&format!("{config:#?}"));
}
#[test]
fn config_with_genesis() {
let _config =
load_config_from_fixtures("minimal_alone_with_genesis.toml").expect("should be valid");
}
#[test]
fn self_is_presented_in_trusted_peers() {
let config =
load_config_from_fixtures("minimal_alone_with_genesis.toml").expect("valid config");
assert!(config
.sumeragi
.trusted_peers
.value()
.clone()
.into_non_empty_vec()
.contains(&config.common.peer_id()));
}
#[test]
fn missing_fields() {
let error = load_config_from_fixtures("bad.missing_fields.toml")
.expect_err("should fail without missing fields");
assert_contains!(format!("{error:?}"), "missing parameter: `chain_id`");
assert_contains!(format!("{error:?}"), "missing parameter: `public_key`");
assert_contains!(format!("{error:?}"), "missing parameter: `network.address`");
}
#[test]
fn extra_fields() {
let error = load_config_from_fixtures("bad.extra_fields.toml")
.expect_err("should fail with extra field");
assert_contains!(format!("{error:?}"), "Found unrecognised parameters");
assert_contains!(format!("{error:?}"), "unknown parameter: `bar`");
assert_contains!(format!("{error:?}"), "unknown parameter: `foo`");
}
/// Aims the purpose of checking that every single provided env variable is consumed and parsed
/// into a valid config.
#[test]
fn full_envs_set_is_consumed() {
let env = test_env_from_file(fixtures_dir().join("full.env"));
ConfigReader::new()
.with_env(env.clone())
.read_and_complete::<UserConfig>()
.expect("should be fine");
assert_eq!(env.unvisited(), HashSet::new());
assert_eq!(env.unknown(), HashSet::new());
}
#[test]
fn config_from_file_and_env() {
let env = test_env_from_file(fixtures_dir().join("minimal_file_and_env.env"));
ConfigReader::new()
.with_env(env)
.read_toml_with_extends(fixtures_dir().join("minimal_file_and_env.toml"))
.expect("files are fine")
.read_and_complete::<UserConfig>()
.expect("should be fine")
.parse()
.expect("should be fine, again");
}
#[test]
fn full_config_parses_fine() {
let _cfg = load_config_from_fixtures("full.toml").expect("should be fine");
}