-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtests.rs
More file actions
475 lines (417 loc) · 15.9 KB
/
tests.rs
File metadata and controls
475 lines (417 loc) · 15.9 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
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
use super::ExecutorConfig;
use crate::executor::ExecutionContext;
use crate::executor::Executor;
use crate::system::SystemInfo;
use rstest_reuse::{self, *};
use shell_quote::{Bash, QuoteRefExt};
use tempfile::TempDir;
use tokio::sync::{OnceCell, Semaphore, SemaphorePermit};
const TESTS: [&str; 6] = [
// Simple echo command
"echo 'Hello, World!'",
// Multi-line commands without semicolons
"echo \"Working\"
echo \"with\"
echo \"multiple lines\"",
// Multi-line commands with semicolons
"echo \"Working\";
echo \"with\";
echo \"multiple lines\";",
// Directory change and validation
"cd /tmp
# Check that the directory is actually changed
if [ $(basename $(pwd)) != \"tmp\" ]; then
exit 1
fi",
// Quote escaping test
"#!/bin/bash
VALUE=\"He said \\\"Hello 'world'\\\" & echo \\$HOME\"
if [ \"$VALUE\" = \"He said \\\"Hello 'world'\\\" & echo \\$HOME\" ]; then
echo \"Quote test passed\"
else
echo \"ERROR: Quote handling failed\"
exit 1
fi",
// Command substitution test
"#!/bin/bash
RESULT=$(echo \"test 'nested' \\\"quotes\\\" here\")
COUNT=$(echo \"$RESULT\" | wc -w)
if [ \"$COUNT\" -eq \"4\" ]; then
echo \"Command substitution test passed\"
else
echo \"ERROR: Expected 4 words, got $COUNT\"
exit 1
fi",
];
fn env_var_validation_script(env: &str, expected: &str) -> String {
let expected: String = expected.quoted(Bash);
format!(
r#"
if [ "${env}" != {expected} ]; then
echo "FAIL: Environment variable not set correctly"
echo "Got: '${env}'"
exit 1
fi
"#
)
}
const ENV_TESTS: [(&str, &str); 8] = [
// Mixed quotes, backticks, and shell metacharacters
(
"quotes_and_escapes",
r#""'He said "Hello 'world' `date`" & echo "done" with \\n\\t\\"#,
),
// Multiline content with tabs and trailing whitespace
(
"multiline_and_whitespace",
"Line 1\nLine 2\tTabbed\n \t ",
),
// Shell metacharacters: pipes, redirects, operators
(
"shell_metacharacters",
r#"*.txt | grep "test" && echo "found" || echo "error" ; ls > /tmp/out"#,
),
// Variable expansion and command substitution
(
"variables_and_commands",
r#"$HOME ${PATH} $((1+1)) $(echo "embedded") VAR="value with spaces""#,
),
// Unicode characters and ANSI escape sequences
(
"unicode_and_special",
"🚀 café naïve\u{200b}hidden\x1b[31mRed\x1b[0m\x01\x02",
),
// Complex mix of quoting styles with shell operators
(
"complex_mixed",
r#"start'single'middle"double"end $VAR | cmd && echo "done" || fail"#,
),
// Empty string edge case
("empty", ""),
// Whitespace-only content
("space_only", " "),
];
#[template]
#[rstest::rstest]
#[case(TESTS[0])]
#[case(TESTS[1])]
#[case(TESTS[2])]
#[case(TESTS[3])]
#[case(TESTS[4])]
#[case(TESTS[5])]
fn test_cases(#[case] cmd: &str) {}
#[template]
#[rstest::rstest]
#[case(ENV_TESTS[0])]
#[case(ENV_TESTS[1])]
#[case(ENV_TESTS[2])]
#[case(ENV_TESTS[3])]
#[case(ENV_TESTS[4])]
#[case(ENV_TESTS[5])]
#[case(ENV_TESTS[6])]
#[case(ENV_TESTS[7])]
fn env_test_cases(#[case] env_case: (&str, &str)) {}
async fn create_test_setup(config: ExecutorConfig) -> (ExecutionContext, TempDir) {
let temp_dir = TempDir::new().unwrap();
let mut config = config;
// Provide a test token so authentication doesn't fail
if config.token.is_none() {
config.token = Some("test-token".to_string());
}
let profile_folder = temp_dir.path().to_path_buf();
let execution_context = ExecutionContext::new(config, profile_folder);
(execution_context, temp_dir)
}
// Uprobes set by memtrack, lead to crashes in valgrind because they work by setting breakpoints on the first
// instruction. Valgrind doesn't rethrow those breakpoint exceptions, which makes the test crash.
//
// Therefore, we can only execute either valgrind or memtrack at any time, and not both at the same time.
static BPF_INSTRUMENTATION_LOCK: OnceCell<Semaphore> = OnceCell::const_new();
async fn acquire_bpf_instrumentation_lock() -> SemaphorePermit<'static> {
let semaphore = BPF_INSTRUMENTATION_LOCK
.get_or_init(|| async { Semaphore::new(1) })
.await;
semaphore.acquire().await.unwrap()
}
mod valgrind {
use super::*;
use crate::executor::valgrind::executor::ValgrindExecutor;
async fn get_valgrind_executor() -> (SemaphorePermit<'static>, &'static ValgrindExecutor) {
static VALGRIND_EXECUTOR: OnceCell<ValgrindExecutor> = OnceCell::const_new();
let executor = VALGRIND_EXECUTOR
.get_or_init(|| async {
let executor = ValgrindExecutor;
let system_info = SystemInfo::new().unwrap();
executor.setup(&system_info, None).await.unwrap();
executor
})
.await;
let _lock = acquire_bpf_instrumentation_lock().await;
(_lock, executor)
}
fn valgrind_config(command: &str) -> ExecutorConfig {
ExecutorConfig {
command: command.to_string(),
..ExecutorConfig::test()
}
}
#[apply(test_cases)]
#[test_log::test(tokio::test)]
async fn test_valgrind_executor(#[case] cmd: &str) {
let (_lock, executor) = get_valgrind_executor().await;
let config = valgrind_config(cmd);
// Unset GITHUB_ACTIONS to force LocalProvider which supports repository_override
temp_env::async_with_vars(&[("GITHUB_ACTIONS", None::<&str>)], async {
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
})
.await;
}
#[apply(env_test_cases)]
#[test_log::test(tokio::test)]
async fn test_valgrind_executor_with_env(#[case] env_case: (&str, &str)) {
let (_lock, executor) = get_valgrind_executor().await;
let (env_var, env_value) = env_case;
temp_env::async_with_vars(
&[(env_var, Some(env_value)), ("GITHUB_ACTIONS", None)],
async {
let cmd = env_var_validation_script(env_var, env_value);
let config = valgrind_config(&cmd);
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
},
)
.await;
}
}
#[test_with::env(GITHUB_ACTIONS)]
mod walltime {
use super::*;
use crate::executor::wall_time::executor::WallTimeExecutor;
async fn get_walltime_executor() -> (SemaphorePermit<'static>, WallTimeExecutor) {
static WALLTIME_INIT: OnceCell<()> = OnceCell::const_new();
static WALLTIME_SEMAPHORE: OnceCell<Semaphore> = OnceCell::const_new();
WALLTIME_INIT
.get_or_init(|| async {
let executor = WallTimeExecutor::new();
let system_info = SystemInfo::new().unwrap();
executor.setup(&system_info, None).await.unwrap();
})
.await;
// We can't execute multiple walltime executors in parallel because perf isn't thread-safe (yet). We have to
// use a semaphore to limit concurrent access.
let semaphore = WALLTIME_SEMAPHORE
.get_or_init(|| async { Semaphore::new(1) })
.await;
let permit = semaphore.acquire().await.unwrap();
(permit, WallTimeExecutor::new())
}
fn walltime_config(command: &str, enable_perf: bool) -> ExecutorConfig {
ExecutorConfig {
command: command.to_string(),
enable_perf,
..ExecutorConfig::test()
}
}
#[apply(test_cases)]
#[rstest::rstest]
#[test_log::test(tokio::test)]
async fn test_walltime_executor(#[case] cmd: &str, #[values(false, true)] enable_perf: bool) {
let (_permit, executor) = get_walltime_executor().await;
let config = walltime_config(cmd, enable_perf);
// Unset GITHUB_ACTIONS to force LocalProvider which supports repository_override
temp_env::async_with_vars(&[("GITHUB_ACTIONS", None::<&str>)], async {
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
})
.await;
}
#[apply(env_test_cases)]
#[rstest::rstest]
#[test_log::test(tokio::test)]
async fn test_walltime_executor_with_env(
#[case] env_case: (&str, &str),
#[values(false, true)] enable_perf: bool,
) {
let (_permit, executor) = get_walltime_executor().await;
let (env_var, env_value) = env_case;
temp_env::async_with_vars(
&[(env_var, Some(env_value)), ("GITHUB_ACTIONS", None)],
async {
let cmd = env_var_validation_script(env_var, env_value);
let config = walltime_config(&cmd, enable_perf);
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
},
)
.await;
}
// Ensure that the working directory is used correctly
#[rstest::rstest]
#[test_log::test(tokio::test)]
async fn test_walltime_executor_in_working_dir(#[values(false, true)] enable_perf: bool) {
let (_permit, executor) = get_walltime_executor().await;
let cmd = r#"
if [ "$(basename "$(pwd)")" != "within_sub_directory" ]; then
echo "FAIL: Working directory is not 'within_sub_directory'"
exit 1
fi
"#;
let mut config = walltime_config(cmd, enable_perf);
let dir = TempDir::new().unwrap();
config.working_directory = Some(
dir.path()
.join("within_sub_directory")
.to_string_lossy()
.to_string(),
);
std::fs::create_dir_all(config.working_directory.as_ref().unwrap()).unwrap();
// Unset GITHUB_ACTIONS to force LocalProvider which supports repository_override
temp_env::async_with_vars(&[("GITHUB_ACTIONS", None::<&str>)], async {
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
})
.await;
}
// Ensure that commands that fail actually fail
#[rstest::rstest]
#[test_log::test(tokio::test)]
async fn test_walltime_executor_fails(#[values(false, true)] enable_perf: bool) {
let (_permit, executor) = get_walltime_executor().await;
let config = walltime_config("exit 1", enable_perf);
// Unset GITHUB_ACTIONS to force LocalProvider which supports repository_override
temp_env::async_with_vars(&[("GITHUB_ACTIONS", None::<&str>)], async {
let (execution_context, _temp_dir) = create_test_setup(config).await;
let result = executor.run(&execution_context, &None).await;
assert!(result.is_err(), "Command should fail");
})
.await;
}
//
// Exec-harness currently does not support the inline multi command scripts
#[template]
#[rstest::rstest]
#[case(TESTS[0])]
#[case(TESTS[1])]
#[case(TESTS[2])]
fn exec_harness_test_cases() -> Vec<&'static str> {
EXEC_HARNESS_COMMANDS.to_vec()
}
fn wrap_with_exec_harness(
walltime_args: &exec_harness::walltime::WalltimeExecutionArgs,
command: &[String],
) -> String {
shell_words::join(
std::iter::once(crate::executor::orchestrator::EXEC_HARNESS_COMMAND)
.chain(walltime_args.to_cli_args().iter().map(|s| s.as_str()))
.chain(command.iter().map(|s| s.as_str())),
)
}
// Ensure that the walltime executor works with the exec-harness
#[apply(exec_harness_test_cases)]
#[rstest::rstest]
#[test_log::test(tokio::test)]
async fn test_exec_harness(#[case] cmd: &str) {
use exec_harness::walltime::WalltimeExecutionArgs;
let (_permit, executor) = get_walltime_executor().await;
let walltime_args = WalltimeExecutionArgs {
warmup_time: Some("0s".to_string()),
max_time: None,
min_time: None,
max_rounds: Some(3),
min_rounds: None,
};
let cmd = cmd.split(" ").map(|s| s.to_owned()).collect::<Vec<_>>();
let wrapped_command = wrap_with_exec_harness(&walltime_args, &cmd);
// Unset GITHUB_ACTIONS to force LocalProvider which supports repository_override
temp_env::async_with_vars(&[("GITHUB_ACTIONS", None::<&str>)], async {
let config = walltime_config(&wrapped_command, true);
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
})
.await;
}
}
#[test_with::env(GITHUB_ACTIONS)]
mod memory {
use super::*;
use crate::executor::memory::executor::MemoryExecutor;
async fn get_memory_executor() -> (
SemaphorePermit<'static>,
SemaphorePermit<'static>,
MemoryExecutor,
) {
static MEMORY_INIT: OnceCell<()> = OnceCell::const_new();
static MEMORY_SEMAPHORE: OnceCell<Semaphore> = OnceCell::const_new();
MEMORY_INIT
.get_or_init(|| async {
let executor = MemoryExecutor;
let system_info = SystemInfo::new().unwrap();
executor.setup(&system_info, None).await.unwrap();
})
.await;
let semaphore = MEMORY_SEMAPHORE
.get_or_init(|| async { Semaphore::new(1) })
.await;
let permit = semaphore.acquire().await.unwrap();
// Memory executor uses heaptrack which uses BPF-based instrumentation, which conflicts with valgrind.
let _lock = acquire_bpf_instrumentation_lock().await;
(permit, _lock, MemoryExecutor)
}
fn memory_config(command: &str) -> ExecutorConfig {
ExecutorConfig {
command: command.to_string(),
..ExecutorConfig::test()
}
}
#[apply(test_cases)]
#[test_log::test(tokio::test)]
async fn test_memory_executor(#[case] cmd: &str) {
let (_permit, _lock, executor) = get_memory_executor().await;
// Unset GITHUB_ACTIONS to force LocalProvider which supports repository_override
temp_env::async_with_vars(&[("GITHUB_ACTIONS", None::<&str>)], async {
let config = memory_config(cmd);
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
})
.await;
}
#[apply(env_test_cases)]
#[test_log::test(tokio::test)]
async fn test_memory_executor_with_env(#[case] env_case: (&str, &str)) {
let (_permit, _lock, executor) = get_memory_executor().await;
let (env_var, env_value) = env_case;
temp_env::async_with_vars(
&[(env_var, Some(env_value)), ("GITHUB_ACTIONS", None)],
async {
let cmd = env_var_validation_script(env_var, env_value);
let config = memory_config(&cmd);
let (execution_context, _temp_dir) = create_test_setup(config).await;
executor.run(&execution_context, &None).await.unwrap();
},
)
.await;
}
#[test_log::test(tokio::test)]
async fn test_memory_executor_forwards_path() {
let custom_path = "/custom/test/path";
let current_path = std::env::var("PATH").unwrap();
let modified_path = format!("{custom_path}:{current_path}");
let cmd = format!(
r#"
if ! echo "$PATH" | grep -q "{custom_path}"; then
echo "FAIL: PATH does not contain custom path {custom_path}"
echo "Got PATH: $PATH"
exit 1
fi
"#
);
let config = memory_config(&cmd);
let (execution_context, _temp_dir) = create_test_setup(config).await;
let (_permit, _lock, executor) = get_memory_executor().await;
temp_env::async_with_vars(&[("PATH", Some(&modified_path))], async {
executor.run(&execution_context, &None).await.unwrap();
})
.await;
}
}