agent: fix clippy for rustc 1.5

Fixes: #1461

Signed-off-by: Tim Zhang <tim@hyper.sh>
Signed-off-by: Chelsea Mafrica <chelsea.e.mafrica@intel.com>
This commit is contained in:
Tim Zhang 2021-02-24 22:18:29 +08:00 committed by Chelsea Mafrica
parent 4f9b5fafcc
commit 2f67e831e3
11 changed files with 62 additions and 67 deletions

View File

@ -131,21 +131,21 @@ impl CgroupManager for Manager {
// set block_io resources
if let Some(blkio) = &r.block_io {
set_block_io_resources(&cg, blkio, res)?;
set_block_io_resources(&cg, blkio, res);
}
// set hugepages resources
if !r.hugepage_limits.is_empty() {
set_hugepages_resources(&cg, &r.hugepage_limits, res)?;
set_hugepages_resources(&cg, &r.hugepage_limits, res);
}
// set network resources
if let Some(network) = &r.network {
set_network_resources(&cg, network, res)?;
set_network_resources(&cg, network, res);
}
// set devices resources
set_devices_resources(&cg, &r.devices, res)?;
set_devices_resources(&cg, &r.devices, res);
info!(sl!(), "resources after processed {:?}", res);
// apply resources
@ -241,7 +241,7 @@ fn set_network_resources(
_cg: &cgroups::Cgroup,
network: &LinuxNetwork,
res: &mut cgroups::Resources,
) -> Result<()> {
) {
info!(sl!(), "cgroup manager set network");
// set classid
@ -263,14 +263,13 @@ fn set_network_resources(
res.network.update_values = true;
res.network.priorities = priorities;
Ok(())
}
fn set_devices_resources(
_cg: &cgroups::Cgroup,
device_resources: &[LinuxDeviceCgroup],
res: &mut cgroups::Resources,
) -> Result<()> {
) {
info!(sl!(), "cgroup manager set devices");
let mut devices = vec![];
@ -294,15 +293,13 @@ fn set_devices_resources(
res.devices.update_values = true;
res.devices.devices = devices;
Ok(())
}
fn set_hugepages_resources(
_cg: &cgroups::Cgroup,
hugepage_limits: &[LinuxHugepageLimit],
res: &mut cgroups::Resources,
) -> Result<()> {
) {
info!(sl!(), "cgroup manager set hugepage");
res.hugepages.update_values = true;
let mut limits = vec![];
@ -315,15 +312,13 @@ fn set_hugepages_resources(
limits.push(hr);
}
res.hugepages.limits = limits;
Ok(())
}
fn set_block_io_resources(
_cg: &cgroups::Cgroup,
blkio: &LinuxBlockIO,
res: &mut cgroups::Resources,
) -> Result<()> {
) {
info!(sl!(), "cgroup manager set block io");
res.blkio.update_values = true;
@ -350,8 +345,6 @@ fn set_block_io_resources(
build_blk_io_device_throttle_resource(&blkio.throttle_read_iops_device);
res.blkio.throttle_write_iops_device =
build_blk_io_device_throttle_resource(&blkio.throttle_write_iops_device);
Ok(())
}
fn set_cpu_resources(cg: &cgroups::Cgroup, cpu: &LinuxCPU) -> Result<()> {
@ -417,7 +410,7 @@ fn set_memory_resources(cg: &cgroups::Cgroup, memory: &LinuxMemory, update: bool
}
if let Some(swappiness) = memory.swappiness {
if swappiness >= 0 && swappiness <= 100 {
if (0..=100).contains(&swappiness) {
mem_controller.set_swappiness(swappiness as u64)?;
} else {
return Err(anyhow!(
@ -1008,7 +1001,7 @@ impl Manager {
}
pub fn update_cpuset_path(&self, guest_cpuset: &str, container_cpuset: &str) -> Result<()> {
if guest_cpuset == "" {
if guest_cpuset.is_empty() {
return Ok(());
}
info!(sl!(), "update_cpuset_path to: {}", guest_cpuset);

View File

@ -130,7 +130,7 @@ fn register_memory_event_v2(
// notify_on_oom returns channel on which you can expect event about OOM,
// if process died without OOM this channel will be closed.
fn notify_on_oom(cid: &str, dir: String) -> Result<Receiver<String>> {
if dir == "" {
if dir.is_empty() {
return Err(anyhow!("memory controller missing"));
}
@ -139,7 +139,7 @@ fn notify_on_oom(cid: &str, dir: String) -> Result<Receiver<String>> {
// level is one of "low", "medium", or "critical"
fn notify_memory_pressure(cid: &str, dir: String, level: &str) -> Result<Receiver<String>> {
if dir == "" {
if dir.is_empty() {
return Err(anyhow!("memory controller missing"));
}
@ -163,7 +163,7 @@ fn register_memory_event(
let event_control_path = Path::new(&cg_dir).join("cgroup.event_control");
let data;
if arg == "" {
if arg.is_empty() {
data = format!("{} {}", eventfd, event_file.as_raw_fd());
} else {
data = format!("{} {} {}", eventfd, event_file.as_raw_fd(), arg);

View File

@ -1036,7 +1036,7 @@ fn update_namespaces(logger: &Logger, spec: &mut Spec, init_pid: RawFd) -> Resul
TYPETONAME.get(namespace.r#type.as_str()).unwrap()
);
if namespace.path == "" {
if namespace.path.is_empty() {
namespace.path = ns_path;
}
}
@ -1048,7 +1048,7 @@ fn update_namespaces(logger: &Logger, spec: &mut Spec, init_pid: RawFd) -> Resul
fn get_pid_namespace(logger: &Logger, linux: &Linux) -> Result<Option<RawFd>> {
for ns in &linux.namespaces {
if ns.r#type == "pid" {
if ns.path == "" {
if ns.path.is_empty() {
return Ok(None);
}
@ -1076,7 +1076,7 @@ fn is_userns_enabled(linux: &Linux) -> bool {
linux
.namespaces
.iter()
.any(|ns| ns.r#type == "user" && ns.path == "")
.any(|ns| ns.r#type == "user" && ns.path.is_empty())
}
fn get_namespaces(linux: &Linux) -> Vec<LinuxNamespace> {

View File

@ -70,12 +70,8 @@ fn rootfs(root: &str) -> Result<()> {
Ok(())
}
fn network(_oci: &Spec) -> Result<()> {
Ok(())
}
fn hostname(oci: &Spec) -> Result<()> {
if oci.hostname.is_empty() || oci.hostname == "" {
if oci.hostname.is_empty() {
return Ok(());
}
@ -312,7 +308,6 @@ pub fn validate(conf: &Config) -> Result<()> {
};
rootfs(root).context("rootfs")?;
network(oci).context("network")?;
hostname(oci).context("hostname")?;
security(oci).context("security")?;
usernamespace(oci).context("usernamespace")?;

View File

@ -204,7 +204,7 @@ fn update_spec_device_list(device: &Device, spec: &mut Spec, devidx: &DevIndex)
// If no container_path is provided, we won't be able to match and
// update the device in the OCI spec device list. This is an error.
if device.container_path == "" {
if device.container_path.is_empty() {
return Err(anyhow!(
"container_path cannot empty for device {:?}",
device
@ -280,7 +280,7 @@ fn virtiommio_blk_device_handler(
_sandbox: &Arc<Mutex<Sandbox>>,
devidx: &DevIndex,
) -> Result<()> {
if device.vm_path == "" {
if device.vm_path.is_empty() {
return Err(anyhow!("Invalid path for virtio mmio blk device"));
}
@ -300,7 +300,7 @@ fn virtio_blk_device_handler(
// When "Id (PCIAddr)" is not set, we allow to use the predicted "VmPath" passed from kata-runtime
// Note this is a special code path for cloud-hypervisor when BDF information is not available
if device.id != "" {
if !device.id.is_empty() {
dev.vm_path = get_pci_device_name(sandbox, &device.id)?;
}
@ -325,7 +325,7 @@ fn virtio_nvdimm_device_handler(
_sandbox: &Arc<Mutex<Sandbox>>,
devidx: &DevIndex,
) -> Result<()> {
if device.vm_path == "" {
if device.vm_path.is_empty() {
return Err(anyhow!("Invalid path for nvdimm device"));
}
@ -381,15 +381,15 @@ fn add_device(
info!(sl!(), "device-id: {}, device-type: {}, device-vm-path: {}, device-container-path: {}, device-options: {:?}",
device.id, device.field_type, device.vm_path, device.container_path, device.options);
if device.field_type == "" {
if device.field_type.is_empty() {
return Err(anyhow!("invalid type for device {:?}", device));
}
if device.id == "" && device.vm_path == "" {
if device.id.is_empty() && device.vm_path.is_empty() {
return Err(anyhow!("invalid ID and VM path for device {:?}", device));
}
if device.container_path == "" {
if device.container_path.is_empty() {
return Err(anyhow!("invalid container path for device {:?}", device));
}
@ -436,9 +436,10 @@ mod tests {
#[test]
fn test_update_device_cgroup() {
let mut spec = Spec::default();
spec.linux = Some(Linux::default());
let mut spec = Spec {
linux: Some(Linux::default()),
..Default::default()
};
update_device_cgroup(&mut spec).unwrap();

View File

@ -7,7 +7,6 @@ use std::collections::HashMap;
use std::ffi::CString;
use std::fs;
use std::io;
use std::iter::FromIterator;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
@ -380,7 +379,7 @@ fn mount_storage(logger: &Logger, storage: &Storage) -> Result<()> {
}
let options_vec = storage.options.to_vec();
let options_vec = Vec::from_iter(options_vec.iter().map(String::as_str));
let options_vec = options_vec.iter().map(String::as_str).collect();
let (flags, options) = parse_mount_flags_and_options(options_vec);
info!(logger, "mounting storage";
@ -506,7 +505,7 @@ pub fn get_mount_fs_type(mount_point: &str) -> Result<String> {
// get_mount_fs_type_from_file returns the FS type corresponding to the passed mount point and
// any error ecountered.
pub fn get_mount_fs_type_from_file(mount_file: &str, mount_point: &str) -> Result<String> {
if mount_point == "" {
if mount_point.is_empty() {
return Err(anyhow!("Invalid mount point {}", mount_point));
}
@ -591,7 +590,7 @@ pub fn get_cgroup_mounts(
}
}
if fields[0] == "" {
if fields[0].is_empty() {
continue;
}
@ -798,7 +797,7 @@ mod tests {
let src_filename: String;
let dest_filename: String;
if d.src != "" {
if !d.src.is_empty() {
src = dir.path().join(d.src.to_string());
src_filename = src
.to_str()
@ -808,7 +807,7 @@ mod tests {
src_filename = "".to_owned();
}
if d.dest != "" {
if !d.dest.is_empty() {
dest = dir.path().join(d.dest.to_string());
dest_filename = dest
.to_str()
@ -820,7 +819,7 @@ mod tests {
// Create the mount directories
for d in [src_filename.clone(), dest_filename.clone()].iter() {
if d == "" {
if d.is_empty() {
continue;
}
@ -840,7 +839,7 @@ mod tests {
let msg = format!("{}: result: {:?}", msg, result);
if d.error_contains == "" {
if d.error_contains.is_empty() {
assert!(result.is_ok(), msg);
// Cleanup
@ -958,7 +957,7 @@ mod tests {
let msg = format!("{}: result: {:?}", msg, result);
if d.error_contains == "" {
if d.error_contains.is_empty() {
assert!(result.is_ok(), msg);
continue;
}
@ -1066,7 +1065,7 @@ mod tests {
// add more details if an assertion fails
let msg = format!("{}: result: {:?}", msg, result);
if d.error_contains == "" {
if d.error_contains.is_empty() {
let fs_type = result.unwrap();
assert!(d.fs_type == fs_type, msg);
@ -1223,7 +1222,7 @@ mod tests {
let result = get_cgroup_mounts(&logger, filename, false);
let msg = format!("{}: result: {:?}", msg, result);
if d.error_contains != "" {
if !d.error_contains.is_empty() {
assert!(result.is_err(), msg);
let error_msg = format!("{}", result.unwrap_err());

View File

@ -58,7 +58,7 @@ impl Namespace {
pub fn get_uts(mut self, hostname: &str) -> Self {
self.ns_type = NamespaceType::UTS;
if hostname != "" {
if !hostname.is_empty() {
self.hostname = Some(String::from(hostname));
}
self

View File

@ -513,7 +513,7 @@ impl Handle {
.as_ref()
.map(|to| to.address.as_str()) // Extract address field
.and_then(|addr| if addr.is_empty() { None } else { Some(addr) }) // Make sure it's not empty
.ok_or_else(|| nix::Error::Sys(nix::errno::Errno::EINVAL))?;
.ok_or(nix::Error::Sys(nix::errno::Errno::EINVAL))?;
let ip = IpAddr::from_str(&ip_address)
.map_err(|e| anyhow!("Failed to parse IP {}: {:?}", ip_address, e))?;
@ -611,7 +611,7 @@ fn parse_mac_address(addr: &str) -> Result<[u8; 6]> {
let v = u8::from_str_radix(
split
.next()
.ok_or_else(|| nix::Error::Sys(nix::errno::Errno::EINVAL))?,
.ok_or(nix::Error::Sys(nix::errno::Errno::EINVAL))?,
16,
)?;
Ok(v)

View File

@ -310,7 +310,7 @@ impl agentService {
"exec-id" => eid.clone(),
);
if eid == "" {
if eid.is_empty() {
init = true;
}
@ -1419,7 +1419,7 @@ fn find_process<'a>(
.get_container(cid)
.ok_or_else(|| anyhow!("Invalid container id"))?;
if init || eid == "" {
if init || eid.is_empty() {
return ctr
.processes
.get_mut(&ctr.init_process_pid)
@ -1487,8 +1487,10 @@ fn update_container_namespaces(
}
}
// update pid namespace
let mut pid_ns = LinuxNamespace::default();
pid_ns.r#type = NSTYPEPID.to_string();
let mut pid_ns = LinuxNamespace {
r#type: NSTYPEPID.to_string(),
..Default::default()
};
// Use shared pid ns if useSandboxPidns has been set in either
// the create_sandbox request or create_container request.
@ -1688,7 +1690,7 @@ fn setup_bundle(cid: &str, spec: &mut Spec) -> Result<PathBuf> {
}
fn load_kernel_module(module: &protocols::agent::KernelModule) -> Result<()> {
if module.name == "" {
if module.name.is_empty() {
return Err(anyhow!("Kernel module name is empty"));
}
@ -1753,10 +1755,12 @@ mod tests {
#[test]
fn test_load_kernel_module() {
let mut m = protocols::agent::KernelModule::default();
let mut m = protocols::agent::KernelModule {
name: "module_not_exists".to_string(),
..Default::default()
};
// case 1: module not exists
m.name = "module_not_exists".to_string();
let result = load_kernel_module(&m);
assert!(result.is_err(), "load module should failed");

View File

@ -624,13 +624,16 @@ mod tests {
}
fn create_dummy_opts() -> CreateOpts {
let mut root = Root::default();
root.path = String::from("/");
let root = Root {
path: String::from("/"),
..Default::default()
};
let linux = Linux::default();
let mut spec = Spec::default();
spec.root = Some(root);
spec.linux = Some(linux);
let spec = Spec {
linux: Some(Linux::default()),
root: Some(root),
..Default::default()
};
CreateOpts {
cgroup_name: "".to_string(),

View File

@ -51,7 +51,7 @@ impl Uevent {
self.action == U_EVENT_ACTION_ADD
&& self.subsystem == "block"
&& self.devpath.starts_with(PCI_ROOT_BUS_PATH)
&& self.devname != ""
&& !self.devname.is_empty()
}
fn handle_block_add_event(&self, sandbox: &Arc<Mutex<Sandbox>>) {