Skip to content

Commit ac7c37a

Browse files
Manciukicroypat
authored andcommitted
fix(tests): make integ tests pass clippy
Fix multiple clippy warnings on the rust integ tests so that we can run clippy without errors on them. cast_* and undocumented_unsafe_blocks are allowed in test and bench code for convenience. Signed-off-by: Riccardo Mancini <[email protected]>
1 parent abc6947 commit ac7c37a

File tree

10 files changed

+37
-14
lines changed

10 files changed

+37
-14
lines changed

src/clippy-tracing/tests/integration_tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// Allow test functions outside of test modules
5+
#![allow(clippy::tests_outside_test_module)]
6+
47
use std::fs::{OpenOptions, remove_file};
58
use std::io::{Read, Write};
69
use std::process::Command;
@@ -14,6 +17,7 @@ fn setup(text: &str) -> String {
1417
let path = format!("/tmp/{id}.rs");
1518
let mut file = OpenOptions::new()
1619
.create(true)
20+
.truncate(true)
1721
.read(false)
1822
.write(true)
1923
.open(&path)
@@ -203,6 +207,7 @@ fn exclude() {
203207

204208
let mut file_one = OpenOptions::new()
205209
.create(true)
210+
.truncate(true)
206211
.read(false)
207212
.write(true)
208213
.open(&file_path_one)
@@ -211,6 +216,7 @@ fn exclude() {
211216

212217
let mut file_two = OpenOptions::new()
213218
.create(true)
219+
.truncate(true)
214220
.read(false)
215221
.write(true)
216222
.open(&file_path_two)

src/firecracker/examples/seccomp/harmless.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
fn main() {
4+
// SAFETY: This is just an example to demonstrate syscall filtering.
5+
// The syscall is safe because we're only writing a static string to a file descriptor.
46
unsafe {
57
// Harmless print to standard output.
68
libc::syscall(libc::SYS_write, libc::STDOUT_FILENO, "Hello, world!\n", 14);

src/firecracker/examples/seccomp/malicious.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
fn main() {
4+
// SAFETY: This is just an example to demonstrate syscall filtering.
5+
// The syscall is safe because we're only writing a static string to a file descriptor.
46
unsafe {
57
// In this example, the malicious component is outputting to standard input.
68
libc::syscall(libc::SYS_write, libc::STDIN_FILENO, "Hello, world!\n", 14);

src/firecracker/examples/uffd/uffd_utils.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
// Not everything is used by both binaries
5-
#![allow(dead_code)]
4+
#![allow(
5+
clippy::cast_possible_truncation,
6+
clippy::cast_sign_loss,
7+
clippy::undocumented_unsafe_blocks,
8+
// Not everything is used by both binaries
9+
dead_code
10+
)]
611

712
use std::collections::{HashMap, HashSet};
813
use std::ffi::c_void;
@@ -290,12 +295,9 @@ impl Runtime {
290295
revents: 0,
291296
});
292297

293-
// We can skip polling on stream fd if
294-
// the connection is closed.
295-
let mut skip_stream: usize = 0;
296298
loop {
297-
let pollfd_ptr = pollfds[skip_stream..].as_mut_ptr();
298-
let pollfd_size = pollfds[skip_stream..].len() as u64;
299+
let pollfd_ptr = pollfds.as_mut_ptr();
300+
let pollfd_size = pollfds.len() as u64;
299301

300302
// # Safety:
301303
// Pollfds vector is valid
@@ -305,7 +307,7 @@ impl Runtime {
305307
panic!("Could not poll for events!")
306308
}
307309

308-
for i in skip_stream..pollfds.len() {
310+
for i in 0..pollfds.len() {
309311
if nready == 0 {
310312
break;
311313
}
@@ -324,17 +326,14 @@ impl Runtime {
324326
revents: 0,
325327
});
326328
self.uffds.insert(handler.uffd.as_raw_fd(), handler);
327-
328-
// If connection is closed, we can skip the socket from being polled.
329-
if pollfds[i].revents & (libc::POLLRDHUP | libc::POLLHUP) != 0 {
330-
skip_stream = 1;
331-
}
332329
} else {
333330
// Handle one of uffd page faults
334331
pf_event_dispatch(self.uffds.get_mut(&pollfds[i].fd).unwrap());
335332
}
336333
}
337334
}
335+
// If connection is closed, we can skip the socket from being polled.
336+
pollfds.retain(|pollfd| pollfd.revents & (libc::POLLRDHUP | libc::POLLHUP) == 0);
338337
}
339338
}
340339
}

src/firecracker/tests/verify_dependencies.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#![allow(clippy::tests_outside_test_module)]
5+
46
use std::collections::HashMap;
57
use std::fmt::Debug;
68
use std::path::Path;

src/vmm/benches/memory_access.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#![allow(clippy::undocumented_unsafe_blocks)]
5+
46
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
57
use vmm::resources::VmResources;
68
use vmm::vmm_config::machine_config::{HugePageConfig, MachineConfig};

src/vmm/benches/queue.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// * `Queue.add_used`
77
// * `DescriptorChain.next_descriptor`
88

9+
#![allow(clippy::cast_possible_truncation)]
10+
911
use std::num::Wrapping;
1012

1113
use criterion::{Criterion, criterion_group, criterion_main};

src/vmm/tests/devices.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![allow(clippy::undocumented_unsafe_blocks)]
4+
#![allow(
5+
clippy::cast_possible_truncation,
6+
clippy::tests_outside_test_module,
7+
clippy::undocumented_unsafe_blocks
8+
)]
59
use std::os::raw::{c_int, c_void};
610
use std::os::unix::io::{AsRawFd, RawFd};
711
use std::sync::{Arc, Mutex};

src/vmm/tests/integration_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#![allow(clippy::cast_possible_truncation, clippy::tests_outside_test_module)]
5+
46
use std::io::{Seek, SeekFrom};
57
use std::thread;
68
use std::time::Duration;

src/vmm/tests/io_uring.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#![allow(clippy::cast_possible_truncation, clippy::tests_outside_test_module)]
5+
46
use std::os::unix::fs::FileExt;
57
use std::os::unix::io::AsRawFd;
68
use std::thread;

0 commit comments

Comments
 (0)