Skip to content

Commit

Permalink
ensure that the code is well formatted using rustfmt.
Browse files Browse the repository at this point in the history
In this commit we add CI rule so travis would check whether the code
is well formatted using rustfmt.

Signed-off-by: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@canonical.com>
  • Loading branch information
sahid committed Jan 28, 2020
1 parent 83036b4 commit 8512967
Show file tree
Hide file tree
Showing 26 changed files with 1,717 additions and 1,377 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -39,7 +39,11 @@ install:
- sudo chmod a+rwx /var/run/libvirt/libvirt-sock*
- echo "pass" | sudo saslpasswd2 -p -a libvirt user

before_script:
- rustup component add rustfmt

script:
- cargo fmt -v -- --check;
- cargo test --verbose
# Due to an issue fixed in master we should not consider running
# integration tests in parallel.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -28,6 +28,12 @@ Rust from stable, beta to nightly.

### To execute locally tests and other excerices

`cargo fmt -v -- --check`

The code is formatted using `rustfmt`, you should ensure that the
check is passing before to submit your patch(es). It may be required
to execute `rustup component add rustfmt` in your environment.

`cargo test --verbose`

Integration tests use a real connection to libvirtd. For instance
Expand Down
19 changes: 12 additions & 7 deletions examples/auth.rs
Expand Up @@ -41,7 +41,7 @@ extern crate virt;

use std::{env, io};

use virt::connect::{Connect, ConnectCredential, ConnectAuth};
use virt::connect::{Connect, ConnectAuth, ConnectCredential};

fn main() {
let uri = match env::args().nth(1) {
Expand Down Expand Up @@ -69,9 +69,13 @@ fn main() {
}
}
};
let mut auth = ConnectAuth::new(vec![::virt::connect::VIR_CRED_AUTHNAME,
::virt::connect::VIR_CRED_PASSPHRASE],
callback);
let mut auth = ConnectAuth::new(
vec![
::virt::connect::VIR_CRED_AUTHNAME,
::virt::connect::VIR_CRED_PASSPHRASE,
],
callback,
);

println!("Attempting to connect to hypervisor: '{}'...", uri);
let mut conn = match Connect::open_auth(&uri, &mut auth, 0) {
Expand All @@ -82,8 +86,9 @@ fn main() {
Err(e) => panic!("Not connected, code: {}, message: {}", e.code, e.message),
};
if let Err(e) = conn.close() {
panic!("Failed to disconnect from hypervisor: code {}, message: {}",
e.code,
e.message);
panic!(
"Failed to disconnect from hypervisor: code {}, message: {}",
e.code, e.message
);
}
}
76 changes: 40 additions & 36 deletions examples/hello.rs
Expand Up @@ -29,34 +29,33 @@ use std::env;
use virt::connect::Connect;
use virt::error::Error;


fn show_hypervisor_info(conn: &Connect) -> Result<(), Error> {
if let Ok(hv_type) = conn.get_type() {
if let Ok(mut hv_ver) = conn.get_hyp_version() {
let major = hv_ver / 1000000;
hv_ver %= 1000000;
let minor = hv_ver / 1000;
let release = hv_ver % 1000;
println!("Hypervisor: '{}' version: {}.{}.{}",
hv_type,
major,
minor,
release);
println!(
"Hypervisor: '{}' version: {}.{}.{}",
hv_type, major, minor, release
);
return Ok(());
}
}
Err(Error::new())
}

fn show_domains(conn: &Connect) -> Result<(), Error> {
let flags = virt::connect::VIR_CONNECT_LIST_DOMAINS_ACTIVE |
virt::connect::VIR_CONNECT_LIST_DOMAINS_INACTIVE;
let flags = virt::connect::VIR_CONNECT_LIST_DOMAINS_ACTIVE
| virt::connect::VIR_CONNECT_LIST_DOMAINS_INACTIVE;

if let Ok(num_active_domains) = conn.num_of_domains() {
if let Ok(num_inactive_domains) = conn.num_of_defined_domains() {
println!("There are {} active and {} inactive domains",
num_active_domains,
num_inactive_domains);
println!(
"There are {} active and {} inactive domains",
num_active_domains, num_inactive_domains
);
/* Return a list of all active and inactive domains. Using this API
* instead of virConnectListDomains() and virConnectListDefinedDomains()
* is preferred since it "solves" an inherit race between separated API
Expand All @@ -80,25 +79,28 @@ fn show_domains(conn: &Connect) -> Result<(), Error> {
println!(" Hard Limit: {}", memtune.hard_limit.unwrap_or(0));
println!(" Soft Limit: {}", memtune.soft_limit.unwrap_or(0));
println!(" Min Guarantee: {}", memtune.min_guarantee.unwrap_or(0));
println!(" Swap Hard Limit: {}",
memtune.swap_hard_limit.unwrap_or(0));
println!(
" Swap Hard Limit: {}",
memtune.swap_hard_limit.unwrap_or(0)
);
}
if let Ok(numa) = dom.get_numa_parameters(0) {
println!("NUMA:");
println!(" Node Set: {}",
numa.node_set.unwrap_or(String::from("")));
println!(
" Node Set: {}",
numa.node_set.unwrap_or(String::from(""))
);
println!(" Mode: {}", numa.mode.unwrap_or(0));
}

if let Ok((sched_type, nparams)) = dom.get_scheduler_type() {
println!("SchedType: {}, nparams: {}",
sched_type, nparams);
println!("SchedType: {}, nparams: {}", sched_type, nparams);
}

if let Ok(sched_info) = dom.get_scheduler_parameters() {
println!("Schedule Information:");
println!("\tScheduler\t: {}", sched_info.scheduler_type);
if let Some(shares) = sched_info.cpu_shares {
if let Some(shares) = sched_info.cpu_shares {
println!("\tcpu_shares\t: {}", shares);
}
if let Some(period) = sched_info.vcpu_bw.period {
Expand Down Expand Up @@ -126,7 +128,6 @@ fn show_domains(conn: &Connect) -> Result<(), Error> {
println!("\tiothread_quota\t: {}", quota);
}
}

}
}
return Ok(());
Expand All @@ -144,42 +145,45 @@ fn main() {

let conn = match Connect::open(&uri) {
Ok(c) => c,
Err(e) => {
panic!("No connection to hypervisor: code {}, message: {}",
e.code,
e.message)
}
Err(e) => panic!(
"No connection to hypervisor: code {}, message: {}",
e.code, e.message
),
};

match conn.get_uri() {
Ok(u) => println!("Connected to hypervisor at '{}'", u),
Err(e) => {
disconnect(conn);
panic!("Failed to get URI for hypervisor connection: code {}, message: {}",
e.code,
e.message);
panic!(
"Failed to get URI for hypervisor connection: code {}, message: {}",
e.code, e.message
);
}
};

if let Err(e) = show_hypervisor_info(&conn) {
disconnect(conn);
panic!("Failed to show hypervisor info: code {}, message: {}",
e.code,
e.message);
panic!(
"Failed to show hypervisor info: code {}, message: {}",
e.code, e.message
);
}

if let Err(e) = show_domains(&conn) {
disconnect(conn);
panic!("Failed to show domains info: code {}, message: {}",
e.code,
e.message);
panic!(
"Failed to show domains info: code {}, message: {}",
e.code, e.message
);
}

fn disconnect(mut conn: Connect) {
if let Err(e) = conn.close() {
panic!("Failed to disconnect from hypervisor: code {}, message: {}",
e.code,
e.message);
panic!(
"Failed to disconnect from hypervisor: code {}, message: {}",
e.code, e.message
);
}
println!("Disconnected from hypervisor");
}
Expand Down
35 changes: 19 additions & 16 deletions examples/migrate.rs
Expand Up @@ -27,40 +27,43 @@ use virt::domain::Domain;

fn main() {
if env::args().len() < 4 {
panic!("Usage: {} <src uri> <dst uri> <domain name>",
env::args().nth(0).unwrap());
panic!(
"Usage: {} <src uri> <dst uri> <domain name>",
env::args().nth(0).unwrap()
);
}

let src_uri = env::args().nth(1).unwrap();
let dst_uri = env::args().nth(2).unwrap();
let dname = env::args().nth(3).unwrap();

println!("Attempting to migrate domain '{}' from '{}' to '{}'...",
dname,
src_uri,
dst_uri);
println!(
"Attempting to migrate domain '{}' from '{}' to '{}'...",
dname, src_uri, dst_uri
);

let mut conn = match Connect::open(&src_uri) {
Ok(c) => c,
Err(e) => {
panic!("No connection to source hypervisor: code {}, message: {}",
e.code,
e.message)
}
Err(e) => panic!(
"No connection to source hypervisor: code {}, message: {}",
e.code, e.message
),
};

if let Ok(dom) = Domain::lookup_by_name(&conn, &dname) {
let flags = ::virt::domain::VIR_MIGRATE_LIVE | ::virt::domain::VIR_MIGRATE_PEER2PEER |
::virt::domain::VIR_MIGRATE_TUNNELLED;
let flags = ::virt::domain::VIR_MIGRATE_LIVE
| ::virt::domain::VIR_MIGRATE_PEER2PEER
| ::virt::domain::VIR_MIGRATE_TUNNELLED;
if let Ok(_) = dom.migrate(&conn, flags, &dst_uri, 0) {
println!("Domain migrated");
}
}

if let Err(e) = conn.close() {
panic!("Failed to disconnect from hypervisor: code {}, message: {}",
e.code,
e.message);
panic!(
"Failed to disconnect from hypervisor: code {}, message: {}",
e.code, e.message
);
}
println!("Disconnected from source hypervisor");
}
31 changes: 16 additions & 15 deletions examples/suspend.rs
Expand Up @@ -28,7 +28,6 @@ use virt::connect::Connect;
use virt::domain::Domain;
use virt::error::Error;


fn suspend_and_resume(conn: &Connect, name: &str, sec: u64) -> Result<(), Error> {
if let Ok(dom) = Domain::lookup_by_name(conn, name) {
if dom.suspend().is_ok() {
Expand Down Expand Up @@ -70,30 +69,32 @@ fn main() {
println!("Attempting to connect to hypervisor: '{}'", uri);
let mut conn = match Connect::open(&uri) {
Ok(c) => c,
Err(e) => {
panic!("No connection to hypervisor: code {}, message: {}",
e.code,
e.message)
}
Err(e) => panic!(
"No connection to hypervisor: code {}, message: {}",
e.code, e.message
),
};

if name.len() == 0 {
if let Err(e) = fetch_domains(&conn) {
println!("Failed to fetch domains. code {}, message: {}",
e.code,
e.message);
println!(
"Failed to fetch domains. code {}, message: {}",
e.code, e.message
);
}
} else {
if let Err(e) = suspend_and_resume(&conn, &name, 1) {
println!("Failed to suspend/resume. code {}, message: {}",
e.code,
e.message);
println!(
"Failed to suspend/resume. code {}, message: {}",
e.code, e.message
);
}
}

if let Err(e) = conn.close() {
panic!("Failed to disconnect from hypervisor: code {}, message: {}",
e.code,
e.message);
panic!(
"Failed to disconnect from hypervisor: code {}, message: {}",
e.code, e.message
);
}
}

0 comments on commit 8512967

Please sign in to comment.