Rust bindings: Add Rust bindings

This patch includes Actions and their tests. Missing:

- Events
- Examples

Rust bindings: Add create / close functions

Rust bindings: Add 4 bindings tests

Rust bindings: Add generator of structs

Rust bindings: Add generator of structs for optional arguments

Rust bindings: Add generator of function signatures

Rust bindings: Complete actions

Rust bindings: Fix memory management

Rust bindings: Add bindtests

Rust bindings: Add additional 4 bindings tests

Rust bindings: Format test files

Rust bindings: Incorporate bindings to build system
This commit is contained in:
Hiroyuki_Katsura
2019-07-29 12:10:42 +09:00
committed by Richard W.M. Jones
parent 6d251e3828
commit 3bbd00c83e
31 changed files with 1562 additions and 3 deletions

3
rust/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

23
rust/Cargo.toml.in Normal file
View File

@@ -0,0 +1,23 @@
# libguestfs Rust tests
# Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
[package]
name = "guestfs"
version = "@VERSION@"
edition = "2018"
[dependencies]

45
rust/Makefile.am Normal file
View File

@@ -0,0 +1,45 @@
# libguestfs rust bindings
# Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
include $(top_srcdir)/subdir-rules.mk
generator_built = \
src/bin/bindtests.rs \
src/lib.rs
EXTRA_DIST = \
.gitignore \
$(generator_built) \
tests/*.rs \
src/*.rs \
Cargo.toml \
Cargo.lock \
run-bindtests \
run-tests
if HAVE_RUST
all: src/lib.rs
$(top_builddir)/run $(CARGO) build --release
TESTS = run-bindtests run-tests
CLEANFILES += target/*~
endif
TESTS_ENVIRONMENT = $(top_builddir)/run --test

23
rust/run-bindtests Executable file
View File

@@ -0,0 +1,23 @@
#!/bin/sh -
# libguestfs Rust bindings
# Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
set -e
$CARGO run --bin bindtests > bindtests.tmp
diff -u $srcdir/../bindtests bindtests.tmp
rm bindtests.tmp

21
rust/run-tests Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/sh -
# libguestfs Rust tests
# Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
set -e
$CARGO test

121
rust/src/base.rs Normal file
View File

@@ -0,0 +1,121 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
use crate::error;
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct guestfs_h {
_unused: [u32; 0],
}
#[link(name = "guestfs")]
extern "C" {
fn guestfs_create() -> *mut guestfs_h;
fn guestfs_create_flags(flags: i64) -> *mut guestfs_h;
fn guestfs_close(g: *mut guestfs_h);
}
const GUESTFS_CREATE_NO_ENVIRONMENT: i64 = 1;
const GUESTFS_CREATE_NO_CLOSE_ON_EXIT: i64 = 2;
pub struct Handle {
pub(crate) g: *mut guestfs_h,
}
impl Handle {
pub fn create() -> Result<Handle, error::Error> {
let g = unsafe { guestfs_create() };
if g.is_null() {
Err(error::Error::Create)
} else {
Ok(Handle { g })
}
}
pub fn create_flags(flags: CreateFlags) -> Result<Handle, error::Error> {
let g = unsafe { guestfs_create_flags(flags.to_libc_int()) };
if g.is_null() {
Err(error::Error::Create)
} else {
Ok(Handle { g })
}
}
}
impl Drop for Handle {
fn drop(&mut self) {
unsafe { guestfs_close(self.g) }
}
}
pub struct CreateFlags {
create_no_environment_flag: bool,
create_no_close_on_exit_flag: bool,
}
impl CreateFlags {
pub fn none() -> CreateFlags {
CreateFlags {
create_no_environment_flag: false,
create_no_close_on_exit_flag: false,
}
}
pub fn new() -> CreateFlags {
CreateFlags::none()
}
pub fn create_no_environment(mut self, flag: bool) -> CreateFlags {
self.create_no_environment_flag = flag;
self
}
pub fn create_no_close_on_exit_flag(mut self, flag: bool) -> CreateFlags {
self.create_no_close_on_exit_flag = flag;
self
}
unsafe fn to_libc_int(self) -> i64 {
let mut flag = 0;
flag |= if self.create_no_environment_flag {
GUESTFS_CREATE_NO_ENVIRONMENT
} else {
0
};
flag |= if self.create_no_close_on_exit_flag {
GUESTFS_CREATE_NO_CLOSE_ON_EXIT
} else {
0
};
flag
}
}
pub struct UUID {
uuid: [u8; 32],
}
impl UUID {
pub(crate) fn new(uuid: [u8; 32]) -> UUID {
UUID { uuid }
}
pub fn to_bytes(self) -> [u8; 32] {
self.uuid
}
}

0
rust/src/bin/.gitkeep Normal file
View File

70
rust/src/error.rs Normal file
View File

@@ -0,0 +1,70 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
use crate::base;
use crate::utils;
use std::convert;
use std::ffi;
use std::os::raw::{c_char, c_int};
use std::str;
#[link(name = "guestfs")]
extern "C" {
fn guestfs_last_error(g: *mut base::guestfs_h) -> *const c_char;
fn guestfs_last_errno(g: *mut base::guestfs_h) -> c_int;
}
#[derive(Debug)]
pub struct APIError {
operation: &'static str,
message: String,
errno: i32,
}
#[derive(Debug)]
pub enum Error {
API(APIError),
IllegalString(ffi::NulError),
Utf8Error(str::Utf8Error),
Create,
}
impl convert::From<ffi::NulError> for Error {
fn from(error: ffi::NulError) -> Self {
Error::IllegalString(error)
}
}
impl convert::From<str::Utf8Error> for Error {
fn from(error: str::Utf8Error) -> Self {
Error::Utf8Error(error)
}
}
impl base::Handle {
pub(crate) fn get_error_from_handle(&self, operation: &'static str) -> Error {
let c_msg = unsafe { guestfs_last_error(self.g) };
let message = unsafe { utils::char_ptr_to_string(c_msg).unwrap() };
let errno = unsafe { guestfs_last_errno(self.g) };
Error::API(APIError {
operation,
message,
errno,
})
}
}

26
rust/src/lib.rs Normal file
View File

@@ -0,0 +1,26 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
mod base;
mod error;
mod guestfs;
mod utils;
pub use crate::base::*;
pub use crate::error::*;
pub use crate::guestfs::*;

146
rust/src/utils.rs Normal file
View File

@@ -0,0 +1,146 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
use crate::error;
use std::collections;
use std::convert::TryFrom;
use std::ffi;
use std::os::raw::{c_char, c_void};
use std::str;
extern "C" {
fn free(buf: *const c_void);
}
pub(crate) struct NullTerminatedIter<T: Copy + Clone> {
p: *const *const T,
}
impl<T: Copy + Clone> NullTerminatedIter<T> {
pub(crate) fn new(p: *const *const T) -> NullTerminatedIter<T> {
NullTerminatedIter { p }
}
}
impl<T: Copy + Clone> Iterator for NullTerminatedIter<T> {
type Item = *const T;
fn next(&mut self) -> Option<*const T> {
let r = unsafe { *(self.p) };
if r.is_null() {
None
} else {
self.p = unsafe { self.p.offset(1) };
Some(r)
}
}
}
#[repr(C)]
pub(crate) struct RawList<T> {
size: u32,
ptr: *const T,
}
pub(crate) struct RawListIter<'a, T> {
current: u32,
list: &'a RawList<T>,
}
impl<T> RawList<T> {
fn iter<'a>(&'a self) -> RawListIter<'a, T> {
RawListIter {
current: 0,
list: self,
}
}
}
impl<'a, T> Iterator for RawListIter<'a, T> {
type Item = *const T;
fn next(&mut self) -> Option<*const T> {
if self.current >= self.list.size {
None
} else {
let elem = unsafe { self.list.ptr.offset(self.current as isize) };
self.current += 1;
Some(elem)
}
}
}
pub(crate) fn arg_string_list(v: &[&str]) -> Result<Vec<ffi::CString>, error::Error> {
let mut w = Vec::new();
for x in v.iter() {
let y: &str = x;
w.push(ffi::CString::new(y)?);
}
Ok(w)
}
pub(crate) fn free_string_list(l: *const *const c_char) {
for buf in NullTerminatedIter::new(l) {
unsafe { free(buf as *const c_void) };
}
unsafe { free(l as *const c_void) };
}
pub(crate) fn hashmap(
l: *const *const c_char,
) -> Result<collections::HashMap<String, String>, error::Error> {
let mut map = collections::HashMap::new();
let mut iter = NullTerminatedIter::new(l);
while let Some(key) = iter.next() {
if let Some(val) = iter.next() {
let key = unsafe { char_ptr_to_string(key) }?;
let val = unsafe { char_ptr_to_string(val) }?;
map.insert(key, val);
} else {
// Internal Error -> panic
panic!("odd number of items in hash table");
}
}
Ok(map)
}
pub(crate) fn struct_list<T, S: TryFrom<*const T, Error = error::Error>>(
l: *const RawList<T>,
) -> Result<Vec<S>, error::Error> {
let mut v = Vec::new();
for x in unsafe { &*l }.iter() {
v.push(S::try_from(x)?);
}
Ok(v)
}
pub(crate) fn string_list(l: *const *const c_char) -> Result<Vec<String>, error::Error> {
let mut v = Vec::new();
for x in NullTerminatedIter::new(l) {
let s = unsafe { char_ptr_to_string(x) }?;
v.push(s);
}
Ok(v)
}
pub(crate) unsafe fn char_ptr_to_string(ptr: *const c_char) -> Result<String, str::Utf8Error> {
fn char_ptr_to_string_inner(ptr: *const c_char) -> Result<String, str::Utf8Error> {
let s = unsafe { ffi::CStr::from_ptr(ptr) };
let s = s.to_str()?.to_string();
Ok(s)
}
char_ptr_to_string_inner(ptr)
}

24
rust/tests/010_load.rs Normal file
View File

@@ -0,0 +1,24 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
#[test]
fn load() {
// nop
}

24
rust/tests/020_create.rs Normal file
View File

@@ -0,0 +1,24 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
#[test]
fn create() {
guestfs::Handle::create().unwrap();
}

View File

@@ -0,0 +1,30 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
use guestfs::*;
#[test]
fn create_flags() {
let _h = Handle::create_flags(CreateFlags::none()).expect("create_flags fail");
// TODO: Add parse_environment to check the flag is created correctly
let flags = CreateFlags::new().create_no_environment(true);
let _h = Handle::create_flags(flags).expect("create_flags fail");
// TODO: Add parse_environment to check the flag is created correctly
}

View File

@@ -0,0 +1,38 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
fn create() -> guestfs::Handle {
match guestfs::Handle::create() {
Ok(g) => g,
Err(e) => panic!("fail: {:?}", e),
}
}
fn ignore(_x: guestfs::Handle, _y: guestfs::Handle, _z: guestfs::Handle) {
// drop
}
#[test]
fn create_multiple() {
let x = create();
let y = create();
let z = create();
ignore(x, y, z)
}

View File

@@ -0,0 +1,62 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
use std::default::Default;
#[test]
fn verbose() {
let g = guestfs::Handle::create().expect("create");
g.set_verbose(true).expect("set_verbose");
assert_eq!(g.get_verbose().expect("get_verbose"), true);
g.set_verbose(false).expect("set_verbose");
assert_eq!(g.get_verbose().expect("get_verbose"), false);
}
#[test]
fn trace() {
let g = guestfs::Handle::create().expect("create");
g.set_trace(true).expect("set_trace");
assert_eq!(g.get_trace().expect("get_trace"), true);
g.set_trace(false).expect("set_trace");
assert_eq!(g.get_trace().expect("get_trace"), false);
}
#[test]
fn autosync() {
let g = guestfs::Handle::create().expect("create");
g.set_autosync(true).expect("set_autosync");
assert_eq!(g.get_autosync().expect("get_autosync"), true);
g.set_autosync(false).expect("set_autosync");
assert_eq!(g.get_autosync().expect("get_autosync"), false);
}
#[test]
fn path() {
let g = guestfs::Handle::create().expect("create");
g.set_path(Some(".")).expect("set_path");
assert_eq!(g.get_path().expect("get_path"), ".");
}
#[test]
fn add_drive() {
let g = guestfs::Handle::create().expect("create");
g.add_drive("/dev/null", Default::default())
.expect("add_drive");
}

View File

@@ -0,0 +1,41 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
use std::default::Default;
#[test]
fn no_optargs() {
let g = guestfs::Handle::create().expect("create");
g.add_drive("/dev/null", Default::default())
.expect("add_drive");
}
#[test]
fn one_optarg() {
let g = guestfs::Handle::create().expect("create");
g.add_drive(
"/dev/null",
guestfs::AddDriveOptArgs {
readonly: Some(true),
..Default::default()
},
)
.expect("add_drive");
}

26
rust/tests/080_version.rs Normal file
View File

@@ -0,0 +1,26 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
#[test]
fn version() {
let g = guestfs::Handle::create().expect("create");
let v = g.version().expect("version");
assert_eq!(v.major, 1)
}

View File

@@ -0,0 +1,61 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
#[test]
fn rint() {
let g = guestfs::Handle::create().expect("create");
assert_eq!(g.internal_test_rint("10").unwrap(), 10);
assert!(g.internal_test_rinterr().is_err())
}
#[test]
fn rint64() {
let g = guestfs::Handle::create().expect("create");
assert_eq!(g.internal_test_rint64("10").unwrap(), 10);
assert!(g.internal_test_rint64err().is_err())
}
#[test]
fn rbool() {
let g = guestfs::Handle::create().expect("create");
assert!(g.internal_test_rbool("true").unwrap());
assert!(!g.internal_test_rbool("false").unwrap());
assert!(g.internal_test_rboolerr().is_err())
}
#[test]
fn rconststring() {
let g = guestfs::Handle::create().expect("create");
assert_eq!(
g.internal_test_rconststring("test").unwrap(),
"static string"
);
assert!(g.internal_test_rconststringerr().is_err())
}
#[test]
fn rconstoptstring() {
let g = guestfs::Handle::create().expect("create");
assert_eq!(
g.internal_test_rconstoptstring("test").unwrap(),
Some("static string")
);
assert_eq!(g.internal_test_rconstoptstringerr().unwrap(), None)
}

65
rust/tests/100_launch.rs Normal file
View File

@@ -0,0 +1,65 @@
/* libguestfs Rust bindings
* Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
extern crate guestfs;
use std::default::Default;
#[test]
fn launch() {
let g = guestfs::Handle::create().expect("create");
g.add_drive_scratch(500 * 1024 * 1024, Default::default())
.expect("add_drive_scratch");
g.launch().expect("launch");
g.pvcreate("/dev/sda").expect("pvcreate");
g.vgcreate("VG", &["/dev/sda"]).expect("vgcreate");
g.lvcreate("LV1", "VG", 200).expect("lvcreate");
g.lvcreate("LV2", "VG", 200).expect("lvcreate");
let lvs = g.lvs().expect("lvs");
assert_eq!(
lvs,
vec!["/dev/VG/LV1".to_string(), "/dev/VG/LV2".to_string()]
);
g.mkfs("ext2", "/dev/VG/LV1", Default::default())
.expect("mkfs");
g.mount("/dev/VG/LV1", "/").expect("mount");
g.mkdir("/p").expect("mkdir");
g.touch("/q").expect("touch");
let mut dirs = g.readdir("/").expect("readdir");
dirs.sort_by(|a, b| a.name.cmp(&b.name));
let mut v = Vec::new();
for x in &dirs {
v.push((x.name.as_str(), x.ftyp as u8));
}
assert_eq!(
v,
vec![
(".", b'd'),
("..", b'd'),
("lost+found", b'd'),
("p", b'd'),
("q", b'r')
]
);
g.shutdown().expect("shutdown");
}