From 9a65ddf036b93e8a39a2cc369338c8749cd0ce90 Mon Sep 17 00:00:00 2001 From: Andre Nathan Date: Tue, 14 Jan 2014 09:24:00 -0200 Subject: [PATCH] Use symbols for container states --- ext/lxc/lxc.c | 15 +++++++++------ test/test_lxc_running.rb | 16 ++++++++-------- test/test_lxc_undefined.rb | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ext/lxc/lxc.c b/ext/lxc/lxc.c index 5a6441c..1551dfa 100644 --- a/ext/lxc/lxc.c +++ b/ext/lxc/lxc.c @@ -369,18 +369,18 @@ container_running_p(VALUE self) * call-seq: * container.state * - * Returns the state of the container. + * Returns a symbol representing the state of the container. */ static VALUE container_state(VALUE self) { - const char *state; struct container_data *data; + VALUE rb_state; Data_Get_Struct(self, struct container_data, data); - state = data->container->state(data->container); + rb_state = rb_str_new2(data->container->state(data->container)); - return rb_str_new2(state); + return rb_str_intern(rb_funcall(rb_state, rb_intern("downcase"), 0)); } /* @@ -1614,11 +1614,14 @@ container_wait(int argc, VALUE *argv, VALUE self) int ret, timeout; char *state; struct container_data *data; - VALUE rb_state, rb_timeout; + VALUE rb_state_str, rb_state, rb_timeout; rb_scan_args(argc, argv, "11", &rb_state, &rb_timeout); - state = StringValuePtr(rb_state); + rb_state_str = rb_funcall(rb_state, rb_intern("to_s"), 0); + rb_state_str = rb_funcall(rb_state_str, rb_intern("upcase"), 0); + state = StringValuePtr(rb_state_str); + timeout = NIL_P(rb_timeout) ? -1 : NUM2INT(rb_timeout); Data_Get_Struct(self, struct container_data, data); diff --git a/test/test_lxc_running.rb b/test/test_lxc_running.rb index eb6a8c4..6831428 100644 --- a/test/test_lxc_running.rb +++ b/test/test_lxc_running.rb @@ -19,15 +19,15 @@ class TestLXCRunning < Test::Unit::TestCase @container.shutdown(3) if @container.running? @container.stop - @container.wait('STOPPED', 3) + @container.wait(:stopped, 3) end end def test_container_running - @container.wait('RUNNING', 3) + @container.wait(:running, 3) assert(@container.init_pid > 1) assert(@container.running?) - assert_equal('RUNNING', @container.state) + assert_equal(:running, @container.state) end def test_container_interfaces @@ -72,23 +72,23 @@ class TestLXCRunning < Test::Unit::TestCase def test_container_freeze @container.freeze - @container.wait('FROZEN', 3) + @container.wait(:frozen, 3) assert(@container.init_pid > 1) assert(@container.running?) - assert_equal('FROZEN', @container.state) + assert_equal(:frozen, @container.state) @container.unfreeze - @container.wait('RUNNING', 3) + @container.wait(:running, 3) assert(@container.init_pid > 1) assert(@container.running?) - assert_equal('RUNNING', @container.state) + assert_equal(:running, @container.state) end def test_container_clone teardown assert_nil(@container.init_pid) assert(!@container.running?) - assert_equal('STOPPED', @container.state) + assert_equal(:stopped, @container.state) assert_nothing_raised do begin diff --git a/test/test_lxc_undefined.rb b/test/test_lxc_undefined.rb index 9e22211..583931f 100644 --- a/test/test_lxc_undefined.rb +++ b/test/test_lxc_undefined.rb @@ -28,6 +28,6 @@ class TestLXCUndefined < Test::Unit::TestCase end def test_container_stopped - assert_equal('STOPPED', @container.state) + assert_equal(:stopped, @container.state) end end