-
Notifications
You must be signed in to change notification settings - Fork 35
/
unit_tests.rs
496 lines (467 loc) · 16.9 KB
/
unit_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
492
493
494
495
496
use crate::config::dictionaries::DictionaryFormat;
use {
super::{FastlyConfig, LocalServerConfig, RawLocalServerConfig},
crate::config::DictionaryName,
std::{fs::File, io::Write},
tempfile::tempdir,
};
#[test]
fn fastly_toml_files_can_be_read() {
// Parse a valid `fastly.toml`, check that it succeeds.
let config = FastlyConfig::from_str(
r#"
name = "simple-toml-example"
description = "a simple toml example"
authors = ["Jill Bryson <jbryson@fastly.com>", "Rose McDowall <rmcdowall@fastly.com>"]
language = "rust"
"#,
)
.expect("can read toml data");
// Check that the name, description, authors, and language fields were parsed correctly.
assert_eq!(config.name(), "simple-toml-example");
assert_eq!(config.description(), "a simple toml example");
assert_eq!(
config.authors(),
[
"Jill Bryson <jbryson@fastly.com>",
"Rose McDowall <rmcdowall@fastly.com>"
]
);
assert_eq!(config.language(), "rust");
}
/// Show that we can successfully parse a `fastly.toml` with backend configurations.
///
/// This provides an example `fastly.toml` file including a `#[local_server.backends]` section. This
/// includes various backend definitions, that may or may not include an environment key.
#[test]
fn fastly_toml_files_with_simple_backend_configurations_can_be_read() {
let config = FastlyConfig::from_str(
r#"
manifest_version = "1.2.3"
name = "backend-config-example"
description = "a toml example with backend configuration"
authors = [
"Amelia Watson <awatson@fastly.com>",
"Inugami Korone <kinugami@fastly.com>",
]
language = "rust"
[local_server]
[local_server.backends]
[local_server.backends.dog]
url = "http://localhost:7878/dog-mocks"
[local_server.backends."shark.server"]
url = "http://localhost:7878/shark-mocks"
override_host = "somehost.com"
[local_server.backends.detective]
url = "http://www.elementary.org/"
"#,
)
.expect("can read toml data containing backend configurations");
let backend = config
.backends()
.get("dog")
.expect("backend configurations can be accessed");
assert_eq!(backend.uri, "http://localhost:7878/dog-mocks");
assert_eq!(backend.override_host, None);
let backend = config
.backends()
.get("shark.server")
.expect("backend configurations can be accessed");
assert_eq!(backend.uri, "http://localhost:7878/shark-mocks");
assert_eq!(
backend.override_host,
Some("somehost.com".parse().expect("can parse override_host"))
);
}
/// Show that we can successfully parse a `fastly.toml` with local_server.dictionaries configurations.
///
/// This provides an example `fastly.toml` file including a `#[local_server.dictionaries]` section.
#[test]
fn fastly_toml_files_with_simple_dictionary_configurations_can_be_read() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("a.json");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "{{}}").unwrap();
let config = FastlyConfig::from_str(format!(
r#"
manifest_version = "1.2.3"
name = "dictionary-config-example"
description = "a toml example with dictionary configuration"
authors = [
"Amelia Watson <awatson@fastly.com>",
"Inugami Korone <kinugami@fastly.com>",
]
language = "rust"
[local_server]
[local_server.dictionaries]
[local_server.dictionaries.a]
file="{}"
format = "json"
"#,
&file_path.to_str().unwrap()
))
.expect("can read toml data containing local dictionary configurations");
let dictionary = config
.dictionaries()
.get(&DictionaryName::new("a".to_string()))
.expect("dictionary configurations can be accessed");
assert_eq!(dictionary.file, file_path);
assert_eq!(dictionary.format, DictionaryFormat::Json);
}
/// Unit tests for the `local_server` section of a `fastly.toml` package manifest.
///
/// In particular, these tests check that we deserialize and validate the backend configurations
/// section of the TOML data properly. In the interest of brevity, this section works with TOML data
/// that would be placed beneath the `local_server` key, rather than an entire package manifest as in
/// the tests above.
mod local_server_config_tests {
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
use {
super::{LocalServerConfig, RawLocalServerConfig},
crate::error::{
BackendConfigError, DictionaryConfigError,
FastlyConfigError::{self, InvalidBackendDefinition, InvalidDictionaryDefinition},
},
std::convert::TryInto,
};
fn read_toml_config(toml: &str) -> Result<LocalServerConfig, FastlyConfigError> {
toml::from_str::<'_, RawLocalServerConfig>(toml)
.expect("valid toml data")
.try_into()
}
/// Check that the `local_server` section can be deserialized.
// This case is technically redundant, but it is nice to have a unit test that demonstrates the
// happy path for this group of unit tests.
#[test]
fn local_server_configs_can_be_deserialized() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("secrets.json");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "{{}}").unwrap();
let local_server = format!(
r#"
[backends]
[backends.dog]
url = "http://localhost:7878/dog-mocks"
[dicionaries]
[dicionaries.secrets]
file = "{}"
format = "json"
"#,
file_path.to_str().unwrap()
);
match read_toml_config(&local_server) {
Ok(_) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that backend definitions must be given as TOML tables.
#[test]
fn backend_configs_must_use_toml_tables() {
use BackendConfigError::InvalidEntryType;
static BAD_DEF: &str = r#"
[backends]
"shark" = "https://a.com"
"#;
match read_toml_config(BAD_DEF) {
Err(InvalidBackendDefinition {
err: InvalidEntryType,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that backend definitions cannot contain unrecognized keys.
#[test]
fn backend_configs_cannot_contain_unrecognized_keys() {
use BackendConfigError::UnrecognizedKey;
static BAD_DEFAULT: &str = r#"
[backends]
shark = { url = "https://a.com", shrimp = true }
"#;
match read_toml_config(BAD_DEFAULT) {
Err(InvalidBackendDefinition {
err: UnrecognizedKey(key),
..
}) if key == "shrimp" => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that backend definitions *must* include a `url` field.
#[test]
fn backend_configs_must_provide_a_url() {
use BackendConfigError::MissingUrl;
static NO_URL: &str = r#"
[backends]
"shark" = {}
"#;
match read_toml_config(NO_URL) {
Err(InvalidBackendDefinition {
err: MissingUrl, ..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that backend definitions *must* include a `url` field.
#[test]
fn backend_configs_must_provide_urls_as_a_string() {
use BackendConfigError::InvalidUrlEntry;
static BAD_URL_FIELD: &str = r#"
[backends]
"shark" = { url = 3 }
"#;
match read_toml_config(BAD_URL_FIELD) {
Err(InvalidBackendDefinition {
err: InvalidUrlEntry,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that backend definitions must include a *valid* `url` field.
#[test]
fn backend_configs_must_provide_a_valid_url() {
use BackendConfigError::InvalidUrl;
static BAD_URL_FIELD: &str = r#"
[backends]
"shark" = { url = "http:://[:::1]" }
"#;
match read_toml_config(BAD_URL_FIELD) {
Err(InvalidBackendDefinition {
err: InvalidUrl(_), ..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that override_host field is a string.
#[test]
fn backend_configs_must_provide_override_host_as_a_string() {
use BackendConfigError::InvalidOverrideHostEntry;
static BAD_OVERRIDE_HOST_FIELD: &str = r#"
[backends]
"shark" = { url = "http://a.com", override_host = 3 }
"#;
match read_toml_config(BAD_OVERRIDE_HOST_FIELD) {
Err(InvalidBackendDefinition {
err: InvalidOverrideHostEntry,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that override_host field is non empty.
#[test]
fn backend_configs_must_provide_a_non_empty_override_host() {
use BackendConfigError::EmptyOverrideHost;
static EMPTY_OVERRIDE_HOST_FIELD: &str = r#"
[backends]
"shark" = { url = "http://a.com", override_host = "" }
"#;
match read_toml_config(EMPTY_OVERRIDE_HOST_FIELD) {
Err(InvalidBackendDefinition {
err: EmptyOverrideHost,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that override_host field is valid.
#[test]
fn backend_configs_must_provide_a_valid_override_host() {
use BackendConfigError::InvalidOverrideHost;
static BAD_OVERRIDE_HOST_FIELD: &str = r#"
[backends]
"shark" = { url = "http://a.com", override_host = "somehost.com\n" }
"#;
match read_toml_config(BAD_OVERRIDE_HOST_FIELD) {
Err(InvalidBackendDefinition {
err: InvalidOverrideHost(_),
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that dictionary definitions must be given as TOML tables.
#[test]
fn dictionary_configs_must_use_toml_tables() {
use DictionaryConfigError::InvalidEntryType;
static BAD_DEF: &str = r#"
[dictionaries]
"thing" = "stuff"
"#;
match read_toml_config(BAD_DEF) {
Err(InvalidDictionaryDefinition {
err: InvalidEntryType,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that dictionary definitions cannot contain unrecognized keys.
#[test]
fn dictionary_configs_cannot_contain_unrecognized_keys() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("secrets.json");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "{{}}").unwrap();
use DictionaryConfigError::UnrecognizedKey;
let bad_default = format!(
r#"
[dictionaries]
thing = {{ file = "{}", format = "json", shrimp = true }}
"#,
file_path.to_str().unwrap()
);
match read_toml_config(&bad_default) {
Err(InvalidDictionaryDefinition {
err: UnrecognizedKey(key),
..
}) if key == "shrimp" => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that dictionary definitions *must* include a `file` field.
#[test]
fn dictionary_configs_must_provide_a_file() {
use DictionaryConfigError::MissingFile;
static NO_FILE: &str = r#"
[dictionaries]
thing = {format = "json"}
"#;
match read_toml_config(NO_FILE) {
Err(InvalidDictionaryDefinition {
err: MissingFile, ..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that dictionary definitions *must* include a `format` field.
#[test]
fn dictionary_configs_must_provide_a_format() {
use DictionaryConfigError::MissingFormat;
let dir = tempdir().unwrap();
let file_path = dir.path().join("secrets.json");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "{{}}").unwrap();
let no_format_field = format!(
r#"
[dictionaries]
"thing" = {{ file = "{}" }}
"#,
file_path.to_str().unwrap()
);
match read_toml_config(&no_format_field) {
Err(InvalidDictionaryDefinition {
err: MissingFormat, ..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that dictionary definitions must include a *valid* `name` field.
#[test]
fn dictionary_configs_must_provide_a_valid_name() {
use DictionaryConfigError::InvalidName;
let dir = tempdir().unwrap();
let file_path = dir.path().join("secrets.json");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "{{}}").unwrap();
let bad_name_field = format!(
r#"
[dictionaries]
"1" = {{ file = "{}", format = "json" }}
"#,
file_path.to_str().unwrap()
);
match read_toml_config(&bad_name_field) {
Err(InvalidDictionaryDefinition {
err: InvalidName(_),
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that file field is a string.
#[test]
fn dictionary_configs_must_provide_file_as_a_string() {
use DictionaryConfigError::InvalidFileEntry;
static BAD_FILE_FIELD: &str = r#"
[dictionaries]
"thing" = { file = 3, format = "json" }
"#;
match read_toml_config(BAD_FILE_FIELD) {
Err(InvalidDictionaryDefinition {
err: InvalidFileEntry,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that file field is non empty.
#[test]
fn dictionary_configs_must_provide_a_non_empty_file() {
use DictionaryConfigError::EmptyFileEntry;
static EMPTY_FILE_FIELD: &str = r#"
[dictionaries]
"thing" = { file = "", format = "json" }
"#;
match read_toml_config(EMPTY_FILE_FIELD) {
Err(InvalidDictionaryDefinition {
err: EmptyFileEntry,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that format field is a string.
#[test]
fn dictionary_configs_must_provide_format_as_a_string() {
use DictionaryConfigError::InvalidFormatEntry;
static BAD_FORMAT_FIELD: &str = r#"
[dictionaries]
"thing" = { format = 3}
"#;
match read_toml_config(BAD_FORMAT_FIELD) {
Err(InvalidDictionaryDefinition {
err: InvalidFormatEntry,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that format field is non empty.
#[test]
fn dictionary_configs_must_provide_a_non_empty_format() {
use DictionaryConfigError::EmptyFormatEntry;
static EMPTY_FORMAT_FIELD: &str = r#"
[dictionaries]
"thing" = { format = "" }
"#;
match read_toml_config(EMPTY_FORMAT_FIELD) {
Err(InvalidDictionaryDefinition {
err: EmptyFormatEntry,
..
}) => {}
res => panic!("unexpected result: {:?}", res),
}
}
/// Check that format field set to json is valid.
#[test]
fn valid_dictionary_config_with_format_set_to_json() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("secrets.json");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "{{}}").unwrap();
let dictionary = format!(
r#"
[dictionaries]
"thing" = {{ file = "{}", format = "json" }}
"#,
file_path.to_str().unwrap()
);
read_toml_config(&dictionary).expect(
"can read toml data containing local dictionary configurations using json format",
);
}
}