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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use gumdrop::Options;
use httpmock::{Method::GET, Mock, MockServer};
use std::io::{Read, Write};
use std::net::TcpStream;
use std::{str, time};
use tokio_tungstenite::tungstenite::Message;
use goose::config::GooseConfiguration;
use goose::controller::{
GooseControllerCommand, GooseControllerWebSocketRequest, GooseControllerWebSocketResponse,
};
use goose::prelude::*;
mod common;
// Paths used in load tests performed during these tests.
const INDEX_PATH: &str = "/";
const ABOUT_PATH: &str = "/about.html";
// Indexes to the above paths.
const INDEX_KEY: usize = 0;
const ABOUT_KEY: usize = 1;
const USERS: usize = 5;
const HATCH_RATE: usize = 10;
const RUN_TIME: usize = 10;
// There are multiple test variations in this file.
#[derive(Clone)]
enum TestType {
// Enable --no-telnet.
WebSocket,
// Enable --no-websocket.
Telnet,
}
// State machine for tracking Controller state during tests.
struct TestState {
// A buffer for the telnet Controller.
buf: [u8; 2048],
// Track iterations through GooseControllerCommands.
position: usize,
// Track the steps within a given iteration.
step: usize,
// The Controller command currently being tested.
command: GooseControllerCommand,
// A TCP socket if testing the telnet Controller.
telnet_stream: Option<TcpStream>,
// A TCP socket if testing the WebSocket Controller.
#[cfg(not(feature = "rustls-tls"))]
websocket_stream: Option<tokio_tungstenite::tungstenite::WebSocket<std::net::TcpStream>>,
#[cfg(feature = "rustls-tls")]
websocket_stream: Option<
tokio_tungstenite::tungstenite::WebSocket<
tokio_tungstenite::tungstenite::stream::Stream<
std::net::TcpStream,
rustls::StreamOwned<rustls::ClientSession, TcpStream>,
>,
>,
>,
// A flag indicating whether or not to wait for a reply.
websocket_expect_reply: bool,
// A flag indicating whether or not the WebSocket controller is being tested.
websocket_controller: bool,
}
// Test task.
pub async fn get_index(user: &mut GooseUser) -> GooseTaskResult {
let _goose = user.get(INDEX_PATH).await?;
Ok(())
}
// Test task.
pub async fn get_about(user: &mut GooseUser) -> GooseTaskResult {
let _goose = user.get(ABOUT_PATH).await?;
Ok(())
}
// All tests in this file run against the following common endpoints.
fn setup_mock_server_endpoints(server: &MockServer) -> Vec<Mock> {
vec![
// First set up INDEX_PATH, store in vector at INDEX_KEY.
server.mock(|when, then| {
when.method(GET).path(INDEX_PATH);
then.status(200);
}),
// Next set up ABOUT_PATH, store in vector at ABOUT_KEY.
server.mock(|when, then| {
when.method(GET).path(ABOUT_PATH);
then.status(200);
}),
]
}
// Build appropriate configuration for these tests. Normally this also calls
// common::build_configuration() to get defaults most all tests needs, but
// for these tests we don't want a default configuration. We keep the signature
// the same to simplify reuse, accepting the MockServer but not using it.
fn common_build_configuration(_server: &MockServer, custom: &mut Vec<&str>) -> GooseConfiguration {
// Common elements in all our tests.
let mut configuration: Vec<&str> = vec!["--no-autostart", "--co-mitigation", "disabled"];
// Custom elements in some tests.
configuration.append(custom);
// Parse these options to generate a GooseConfiguration.
GooseConfiguration::parse_args_default(&configuration)
.expect("failed to parse options and generate a configuration")
}
// Helper to confirm all variations generate appropriate results.
fn validate_one_taskset(
goose_metrics: &GooseMetrics,
mock_endpoints: &[Mock],
configuration: &GooseConfiguration,
_test_type: TestType,
) {
//println!("goose_metrics: {:#?}", goose_metrics);
//println!("configuration: {:#?}", configuration);
// Confirm that we loaded the mock endpoints.
assert!(mock_endpoints[INDEX_KEY].hits() > 0);
assert!(mock_endpoints[ABOUT_KEY].hits() > 0);
// Get index and about out of goose metrics.
let index_metrics = goose_metrics
.requests
.get(&format!("GET {}", INDEX_PATH))
.unwrap();
let about_metrics = goose_metrics
.requests
.get(&format!("GET {}", ABOUT_PATH))
.unwrap();
// There should not have been any failures during this test.
assert!(index_metrics.fail_count == 0);
assert!(about_metrics.fail_count == 0);
// Users were correctly configured through the controller.
assert!(goose_metrics.users == USERS);
// Host was not configured at start time.
assert!(configuration.host.is_empty());
// The load test was manually shut down instead of running to completion.
assert!(goose_metrics.duration < RUN_TIME);
}
// Returns the appropriate taskset needed to build these tests.
fn get_tasks() -> GooseTaskSet {
taskset!("LoadTest")
.register_task(task!(get_index).set_weight(2).unwrap())
.register_task(task!(get_about).set_weight(1).unwrap())
}
// Helper to run all standalone tests.
async fn run_standalone_test(test_type: TestType) {
// Start the mock server.
let server = MockServer::start();
let server_url = server.base_url();
// Setup the endpoints needed for this test on the mock server.
let mock_endpoints = setup_mock_server_endpoints(&server);
let mut configuration_flags = match &test_type {
TestType::WebSocket => vec!["--no-telnet"],
TestType::Telnet => vec!["--no-websocket"],
};
// Keep a copy for validation.
let validate_test_type = test_type.clone();
// Build common configuration elements.
let configuration = common_build_configuration(&server, &mut configuration_flags);
// Create a new thread from which to test the Controller.
let _controller_handle = tokio::spawn(async move {
// Sleep a half a second allowing the GooseAttack to start.
tokio::time::sleep(time::Duration::from_millis(500)).await;
// Initiailize the state engine.
let mut test_state = update_state(None, &test_type);
loop {
// Process data received from the client in a loop.
let response;
let websocket_response: GooseControllerWebSocketResponse;
if let Some(stream) = test_state.telnet_stream.as_mut() {
let _ = match stream.read(&mut test_state.buf) {
Ok(data) => data,
Err(_) => {
panic!("ERROR: server disconnected!");
}
};
response = str::from_utf8(&test_state.buf).unwrap();
// Process data received from the client in a loop.
} else if let Some(stream) = test_state.websocket_stream.as_mut() {
if !test_state.websocket_expect_reply {
response = "";
test_state.websocket_expect_reply = true;
} else {
match stream.read_message() {
Ok(message) => {
if let Ok(r) = message.into_text() {
// Keep response around for the entire loop.
websocket_response = match serde_json::from_str(&r) {
Ok(c) => c,
Err(e) => panic!("invalid response from server: {}", e),
};
response = &websocket_response.response;
} else {
// @TODO: support non-text too
panic!("ERROR: invalid message type!");
}
}
Err(e) => {
panic!("error reading from server: {}", e);
}
}
}
} else {
unreachable!();
}
//println!("{:?}: {}", test_state.command, response);
match test_state.command {
GooseControllerCommand::Exit => {
match test_state.step {
// Exit the Controller.
0 => {
make_request(&mut test_state, "exit\r\n");
}
// Confirm that the Controller exited.
_ => {
assert!(response.starts_with("goodbye!"));
// Re-connect to the Controller.
test_state = update_state(None, &test_type);
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Help => {
match test_state.step {
0 => {
// Request the help text.
make_request(&mut test_state, "help\r\n");
}
1 => {
// Be sure we actually received the help text.
assert!(response.contains("controller commands:"));
// Request the help text, using the short form.
make_request(&mut test_state, "?\r\n");
}
_ => {
// Be sure we actually received the help text.
assert!(response.contains("controller commands:"));
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Host => {
match test_state.step {
// Set the host to be load tested.
0 => {
make_request(&mut test_state, &["host ", &server_url, "\r\n"].concat());
}
// Confirm the host was configured.
1 => {
assert!(response.starts_with("host configured"));
// Then try and set an invalid host.
make_request(&mut test_state, "host foobar\r\n");
}
// Confirm that we can't configure an invalid host that doesn't
// match the regex.
2 => {
assert!(response.starts_with("unrecognized command"));
// Try again to set an invalid host.
make_request(&mut test_state, "host http://$[foo\r\n");
}
// Confirm that we can't configure an invalid host that does
// match the regex.
_ => {
assert!(response.starts_with("unrecognized command"));
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Users => {
match test_state.step {
// Reconfigure the number of users simulated by the load test.
0 => {
make_request(
&mut test_state,
&["users ", &USERS.to_string(), "\r\n"].concat(),
);
}
// Confirm that the users are reconfigured.
1 => {
assert!(response.starts_with("users configured"));
// Attempt to reconfigure users with bad data.
make_request(&mut test_state, "users 1.1\r\n");
}
// Confirm we can't configure users with a float.
_ => {
// The number of users started is verified when the load test finishes,
// so no further validation required here.
assert!(response.starts_with("unrecognized command"));
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::HatchRate => {
match test_state.step {
// Configure a decimal hatch_rate.
0 => {
make_request(&mut test_state, "hatchrate .1\r\n");
}
// Confirm hatch_rate is configured.
1 => {
assert!(response.starts_with("hatch_rate configured"));
// Configure with leading and trailing zeros.
make_request(&mut test_state, "hatchrate 0.90\r\n");
}
// Confirm hatch_rate is configured.
2 => {
assert!(response.starts_with("hatch_rate configured"));
// Try to configure with an invalid decimal.
make_request(&mut test_state, "hatchrate 1.2.3\r\n");
}
// Confirm hatch_rate is not configured.
3 => {
assert!(response.starts_with("unrecognized command"));
// Configure hatch_rate with a single integer.
make_request(
&mut test_state,
&["hatchrate ", &HATCH_RATE.to_string(), "\r\n"].concat(),
);
}
// Confirm the final hatch_rate is configured.
_ => {
assert!(response.starts_with("hatch_rate configured"));
// The hatch_rate is verified when the load test finishes, so no
// further validation required here.
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::RunTime => {
match test_state.step {
// Configure run_time using h:m:s format.
0 => {
// Set run_time with hours and minutes and seconds.
make_request(&mut test_state, "runtime 1h2m3s\r\n");
}
// Confirm the run_time is configured.
1 => {
assert!(response.starts_with("run_time configured"));
// Set run_time with hours and seconds.
make_request(&mut test_state, "run_time 1h2s\r\n");
}
// Confirm the run_time is configured.
2 => {
assert!(response.starts_with("run_time configured"));
// Set run_time with hours alone.
make_request(&mut test_state, "run-time 1h\r\n");
}
// Confirm the run_time is configured.
3 => {
assert!(response.starts_with("run_time configured"));
// Set run_time with seconds alone.
make_request(&mut test_state, "runtime 10s\r\n");
}
// Confirm the run_time is configured.
4 => {
assert!(response.starts_with("run_time configured"));
// Try to set run_time with unsupported value.
make_request(&mut test_state, "runtime 10d\r\n");
}
// Confirm the run_time is not configured.
5 => {
assert!(response.starts_with("unrecognized command"));
// Set run_time with seconds alone, and no "s".
make_request(
&mut test_state,
&["runtime ", &RUN_TIME.to_string(), "\r\n"].concat(),
);
}
// Confirm the run_time is configured.
_ => {
assert!(response.starts_with("run_time configured"));
// The run_time is verified when the load test finishes, so no
// further validation required here. Unfortunately, if this fails
// the load test could run forever.
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Config => {
match test_state.step {
// Request the configuration.
0 => {
make_request(&mut test_state, "config\r\n");
}
_ => {
// Confirm the configuration is returned in jsonformat.
if test_state.websocket_controller {
assert!(response
.starts_with(r#"{"help":false,"version":false,"list":false,"#));
// Confirm the configuration object is returned.
} else {
assert!(response.starts_with(r"GooseConfiguration "));
}
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::ConfigJson => {
match test_state.step {
// Request the configuration in json format.
0 => {
make_request(&mut test_state, "config-json\r\n");
}
// Confirm the configuration is returned in jsonformat.
_ => {
assert!(response
.starts_with(r#"{"help":false,"version":false,"list":false,"#));
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Metrics => {
match test_state.step {
// Request the running metrics.
0 => {
make_request(&mut test_state, "metrics\r\n");
}
_ => {
// Confirm the metrics are returned in json format.
if test_state.websocket_controller {
assert!(response.starts_with(r#"{"hash":0,"#));
}
// Confirm the metrics are returned and pretty-printed.
else {
assert!(response.contains("=== PER TASK METRICS ==="));
}
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::MetricsJson => {
match test_state.step {
// Request the running metrics in json format.
0 => {
make_request(&mut test_state, "metrics-json\r\n");
}
// Confirm the metrics are returned in json format.
_ => {
assert!(response.starts_with(r#"{"hash":0,"#));
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Start => {
match test_state.step {
// Try to stop an idle load test.
0 => {
make_request(&mut test_state, "stop\r\n");
}
// Confirm an idle load test can not be stopped.
1 => {
assert!(response.starts_with("load test not running"));
// Send the start request.
make_request(&mut test_state, "start\r\n");
}
// Confirm an idle load test can be started.
2 => {
assert!(response.starts_with("load test started"));
// Send the start request again.
make_request(&mut test_state, "start\r\n");
}
// Confirm a running load test can not be started.
_ => {
assert!(response.starts_with("unable to start load test"));
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Stop => {
match test_state.step {
// Try to configure users on a running load test.
0 => {
make_request(&mut test_state, "users 1\r\n");
}
// Confirm users can not be configured on a running load test.
1 => {
assert!(response.starts_with("load test not idle"));
// Try to configure host on a running load test.
make_request(&mut test_state, "host http://localhost/\r\n");
}
// Confirm host can not be configured on a running load test.
2 => {
assert!(response.starts_with("failed to reconfigure host"));
// Try to stop a running load test.
make_request(&mut test_state, "stop\r\n");
}
// Confirm a running load test can be stopped.
_ => {
assert!(response.starts_with("load test stopped"));
// Give Goose a half second to stop before moving on.
tokio::time::sleep(time::Duration::from_millis(500)).await;
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
GooseControllerCommand::Shutdown => {
match test_state.step {
// Shut down the load test.
0 => {
make_request(&mut test_state, "shutdown\r\n");
}
// Confirm that the load test shut down.
_ => {
assert!(response.starts_with("load test shut down"));
// Move onto the next command.
test_state = update_state(Some(test_state), &test_type);
}
}
}
}
// Flush the buffer.
test_state.buf = [0; 2048];
// Give the parent process time to catch up.
tokio::time::sleep(time::Duration::from_millis(100)).await;
}
});
// Run the Goose Attack.
let goose_metrics = common::run_load_test(
common::build_load_test(configuration.clone(), &get_tasks(), None, None),
None,
)
.await;
// Confirm that the load test ran correctly.
validate_one_taskset(
&goose_metrics,
&mock_endpoints,
&configuration,
validate_test_type,
);
}
// Update (or create) the current testing state. A simple state maching for
// navigating through all supported Controller commands and test states.
fn update_state(test_state: Option<TestState>, test_type: &TestType) -> TestState {
// The commands being tested, and the order they are tested.
let commands_to_test = [
GooseControllerCommand::Exit,
GooseControllerCommand::Help,
GooseControllerCommand::Host,
GooseControllerCommand::Users,
GooseControllerCommand::HatchRate,
GooseControllerCommand::RunTime,
GooseControllerCommand::Start,
GooseControllerCommand::Config,
GooseControllerCommand::ConfigJson,
GooseControllerCommand::Metrics,
GooseControllerCommand::MetricsJson,
GooseControllerCommand::Stop,
GooseControllerCommand::Shutdown,
];
if let Some(mut state) = test_state {
state.position += 1;
state.step = 0;
if let Some(command) = commands_to_test.get(state.position) {
state.command = command.clone();
}
// Generate a new prompt.
if let Some(stream) = state.telnet_stream.as_mut() {
stream.write_all("\r\n".as_bytes()).unwrap();
} else {
state.websocket_expect_reply = false;
}
state
} else {
// Connect to telnet controller.
let telnet_stream = match test_type {
TestType::Telnet => Some(TcpStream::connect("127.0.0.1:5116").unwrap()),
_ => None,
};
// Connect to WebSocket controller.
let websocket_controller: bool;
let websocket_stream = match test_type {
TestType::WebSocket => {
let (mut stream, _) =
tokio_tungstenite::tungstenite::client::connect("ws://127.0.0.1:5117").unwrap();
// Send an empty message so the client performs a handshake.
stream.write_message(Message::Text("".into())).unwrap();
// Ignore the error that comes back.
let _ = stream.read_message().unwrap();
websocket_controller = true;
Some(stream)
}
TestType::Telnet => {
websocket_controller = false;
None
}
};
TestState {
buf: [0; 2048],
position: 0,
step: 0,
command: commands_to_test.first().unwrap().clone(),
telnet_stream,
websocket_stream,
websocket_expect_reply: false,
websocket_controller,
}
}
}
fn make_request(test_state: &mut TestState, command: &str) {
//println!("making request: {}", command);
if let Some(stream) = test_state.telnet_stream.as_mut() {
stream.write_all(command.as_bytes()).unwrap()
} else if let Some(stream) = test_state.websocket_stream.as_mut() {
stream
.write_message(Message::Text(
serde_json::to_string(&GooseControllerWebSocketRequest {
request: command.to_string(),
})
.unwrap(),
))
.unwrap()
}
test_state.step += 1;
}
// Test controlling a load test with Telnet.
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_telnet_controller() {
run_standalone_test(TestType::Telnet).await;
}
// Test controlling a load test with WebSocket controller.
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_websocket_controller() {
run_standalone_test(TestType::WebSocket).await;
}