ref: refs/heads/master
commit bbb180c3badc14ff8faae4b90d7377acd159adb3
Author: Ladislav Slezak
Date: Wed Aug 12 12:46:11 2009 +0200
system plugin - more tests, fixed functionality
---
plugins/system/README | 7 ++
.../system/app/controllers/system_controller.rb | 42 +++++++++----
plugins/system/app/models/system.rb | 12 +---
.../test/functional/system_controller_test.rb | 65 +++++++++++++++++++-
4 files changed, 103 insertions(+), 23 deletions(-)
diff --git a/plugins/system/README b/plugins/system/README
index 12f8a12..a97a94d 100644
--- a/plugins/system/README
+++ b/plugins/system/README
@@ -3,4 +3,11 @@ System
REST interface for accessing low-level basic system components
+
+It uses HAL for reboot/shutdown, the reasons are:
+
+- no need to create new public YaST functions
+- HAL access rights are used, no need to define/setup extra rights for YaST
+
+
Copyright (c) 2009 Novell, released under the MIT license
diff --git a/plugins/system/app/controllers/system_controller.rb b/plugins/system/app/controllers/system_controller.rb
index 8225706..f3dfbd3 100644
--- a/plugins/system/app/controllers/system_controller.rb
+++ b/plugins/system/app/controllers/system_controller.rb
@@ -14,28 +14,42 @@ class SystemController < ApplicationController
end
def update
- root = params[:time]
- if root == nil
- render ErrorResult.error(404, 2, "format or internal error") and return
+ root = params[:actions]
+ if root == nil || root == {}
+ render ErrorResult.error(404, 2, "format error - missing actions") and return
end
@system = System.instance
# do the action
- action = params.find { |k, v| v == true}
-
- if not action.blank?
- case action
- when :reboot
- @system.reboot
- when :shutdown
- @system.shutdown
- else
- render ErrorResult.error(404, 2, "format error") and return
+ root.each do |k, v|
+
+ if v.nil? or !v.has_key? 'active'
+ render ErrorResult.error(404, 2, "format error - missing requested status") and return
+ end
+
+ if v['active'] != true and v['active'] != false
+ render ErrorResult.error(404, 2, "format error - non-boolean active parameter") and return
+ end
+
+ # unknown action requested
+ if !@system.actions.has_key? k.to_sym
+ render ErrorResult.error(404, 2, "format error - unknown action requested") and return
+ end
+
+ if v['active'] == true and @system.actions[k.to_sym][:active] == false
+ case k
+ when 'reboot'
+ @system.reboot
+ when 'shutdown'
+ @system.shutdown
+ else
+ render ErrorResult.error(404, 2, "internal error - unknown action requested") and return
+ end
end
end
- render :show
+ show
end
# See update
diff --git a/plugins/system/app/models/system.rb b/plugins/system/app/models/system.rb
index 7635808..9739a6d 100644
--- a/plugins/system/app/models/system.rb
+++ b/plugins/system/app/models/system.rb
@@ -4,27 +4,23 @@ require 'singleton'
class System
+ attr_reader :actions
include Singleton
def initialize
- @reboot = false
- @shutdown = false
- end
-
- def actions
- return {:reboot => @reboot, :shutdown => @shutdown}
+ @actions = {:reboot => {:active => false}, :shutdown => {:active => false} }
end
def reboot
if hal_power_management(:reboot)
- @reboot = true
+ @actions[:reboot][:active] = true
end
end
def shutdown
if hal_power_management(:shutdown)
- @shutdown = true
+ @actions[:shutdown][:active] = true
end
end
diff --git a/plugins/system/test/functional/system_controller_test.rb b/plugins/system/test/functional/system_controller_test.rb
index c6530cb..52599b0 100644
--- a/plugins/system/test/functional/system_controller_test.rb
+++ b/plugins/system/test/functional/system_controller_test.rb
@@ -10,11 +10,74 @@ class SystemControllerTest < ActionController::TestCase
@request = ActionController::TestRequest.new
# http://railsforum.com/viewtopic.php?id=1719
@request.session[:account_id] = 1 # defined in fixtures
+
+ @model = System.instance
+ @model.stubs(:hal_power_management).with(:reboot).returns(true)
+ @model.stubs(:hal_power_management).with(:shutdown).returns(true)
end
test "check 'show' result" do
- get :show
+ ret = get :show
+ # success (200 OK)
+ assert_response :success
+
+ # is returned a valid XML?
+ ret_hash = Hash.from_xml(ret.body)
+ assert ret_hash
+ # actions active value must be a boolean
+ assert ret_hash['actions']['reboot'].has_key? 'active' and
+ (ret_hash['actions']['reboot']['active'] == true or ret_hash['actions']['reboot']['active'] == false)
+ assert ret_hash['actions']['shutdown'].has_key? 'active' and
+ (ret_hash['actions']['shutdown']['active'] == true or ret_hash['actions']['shutdown']['active'] == false)
+ end
+
+ test "request reboot" do
+ ret = put :update, :actions => {:reboot => {:active => true}}
+ assert_response :success
+
+ # :reboot action must be active
+ assert Hash.from_xml(ret.body)['actions']['reboot']['active']
+ end
+
+ test "request shutdown" do
+ ret = put :update, :actions => {:shutdown => {:active => true}}
assert_response :success
+
+ # :shutdown action must be active
+ assert Hash.from_xml(ret.body)['actions']['shutdown']['active']
+ end
+
+
+ # test invalid / malformed requests
+
+ test "invalid (empty) request" do
+ ret = put :update
+ assert_response :missing
+ end
+
+ test "invalid (malformed) request" do
+ ret = put :update, :zzzzzzz => :aaaaaaa
+ assert_response :missing
+ end
+
+ test "invalid (empty actions) request" do
+ ret = put :update, :actions => {}
+ assert_response :missing
+ end
+
+ test "invalid (unknown) request" do
+ ret = put :update, :actions => {:_invalid_action_ => {:active => true}}
+ assert_response :missing
+ end
+
+ test "invalid (missing active parameter) request" do
+ ret = put :update, :actions => {:reboot => {}}
+ assert_response :missing
+ end
+
+ test "invalid (non-boolean active parameter) request" do
+ ret = put :update, :actions => {:reboot => {:active => 'string'}}
+ assert_response :missing
end
end
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org