Skip to content

Commit

Permalink
python: allow constructin RunningJail from name
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfreyer committed Jan 2, 2019
1 parent 197e2f0 commit bb00ef5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
33 changes: 27 additions & 6 deletions bindings/python/src/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,33 @@ impl DerefMut for RunningJail {
#[pymethods]
impl RunningJail {
#[new]
fn __new__(obj: &PyRawObject, jid: i32) -> PyResult<()> {
obj.init(|token| RunningJail {
inner: native::RunningJail::from_jid(jid),
dead: false,
token,
})
fn __new__(obj: &PyRawObject, identifier: PyObject) -> PyResult<()> {
// Attempt to parse the id as a jid
let py_num: Result<&PyInt, PyDowncastError> = identifier.as_ref(obj.py()).try_into();
if let Ok(jid) = py_num {
let jid: i32 = jid.extract()?;
return obj.init(|token| RunningJail {
inner: native::RunningJail::from_jid(jid),
dead: false,
token,
});
}

// Attempt to parse the id as a string and interpret it as a name
let py_string: Result<&PyString, PyDowncastError> = identifier.as_ref(obj.py()).try_into();
if let Ok(name) = py_string {
let name: String = name.extract()?;
let inner = native::RunningJail::from_name(&name)
.map_err(JailError::from)
.map_err::<PyErr, _>(|e| e.into())?;
return obj.init(|token| RunningJail {
inner: inner,
dead: false,
token,
});
}

Err(exceptions::ValueError::py_err("Invalid JID or name passed"))
}

/// Return a String representation of the Jail
Expand Down
17 changes: 17 additions & 0 deletions bindings/python/tests/test_jail.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,20 @@ def test_jls(benchmark):
r.stop();

assert started_jids <= jids


def get_jail_by_name(name):
r = RunningJail(name)
hostname = r.parameters['host.hostname']
del r
return hostname

def test_get_jail_by_name(benchmark):
s = StoppedJail('/rescue', name="foobar", parameters={"host.hostname": "foobar"})
r = s.start()

hostname = benchmark(get_jail_by_name, "foobar")

r.stop()

assert hostname == "foobar"

0 comments on commit bb00ef5

Please sign in to comment.