1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-23 22:50:09 +03:00

feature #595: added tests to OpenNebulaDriver

This commit is contained in:
Javi Fontan 2011-06-01 18:17:19 +02:00
parent a0853526a2
commit 8974940f1b
3 changed files with 211 additions and 0 deletions

View File

@ -0,0 +1,37 @@
class MonkeyPatcher
def self.patch(&block)
patcher=self.new
patcher.instance_eval &block
patcher.restore_all
end
def initialize
@objects=Hash.new
@klasses=Hash.new
end
def patch_class(klass, function, &block)
@klasses[klass]={} if !@klasses[klass]
@klasses[klass][function]=klass.instance_method(function)
klass.instance_eval do
define_method(function, block)
end
end
def restore_class(klass)
@klasses[klass].each do |function, method|
klass.instance_eval do
define_method(function, method)
end
end
end
def restore_all
@klasses.each do |klass, methods|
restore_class(klass)
end
end
end

View File

@ -0,0 +1,154 @@
require 'OpenNebulaDriver'
require 'test/MonkeyPatcher'
def create_driver(concurrecy, threaded, retries, directory, local_actions)
OpenNebulaDriver.new(directory,
:concurrency => concurrecy,
:threaded => threaded,
:retries => retries,
:local_actions => local_actions)
end
local_action_tests=[
['', {}],
['poll', {'POLL' => nil}],
['poll,migrate', {'POLL' => nil, 'MIGRATE' => nil}],
['deploy,shutdown,poll=poll_ganglia, cancel ',
{"POLL"=>"poll_ganglia", "DEPLOY"=>nil, "SHUTDOWN"=>nil,
"CANCEL"=>nil}]
]
# modify read configuration to get config file from fixtures dir
class OpenNebulaDriver
alias :read_configuration_old :read_configuration
def read_configuration
old_one_location=ENV['ONE_LOCATION']
ENV['ONE_LOCATION']=File.join(ENV['PWD'], 'test', 'fixtures')
result=read_configuration_old
ENV['ONE_LOCATION']=old_one_location
result
end
end
describe OpenNebulaDriver do
before(:all) do
@concurrecy=1
@threaded=false
@retries=0
@directory='vmm/dummy'
@local_actions={
'POLL' => 'local_poll',
'MIGRATE' => nil
}
@create_params=[@concurrecy, @threaded, @retries, @directory,
@local_actions]
end
before(:each) do
ENV['ONE_LOCATION']=nil
end
it 'should parse local actions' do
local_action_tests.each do |test|
OpenNebulaDriver.parse_actions_list(test[0]).should == test[1]
end
end
it 'should get correct paths' do
driver=create_driver(*@create_params)
driver.local_scripts_base_path.should == '/var/lib/one/remotes'
driver.local_scripts_path.should == '/var/lib/one/remotes/vmm/dummy'
driver.remote_scripts_base_path.should == '/var/tmp/one'
driver.remote_scripts_path.should == '/var/tmp/one/vmm/dummy'
ENV['ONE_LOCATION']='/one'
driver=create_driver(*@create_params)
driver.local_scripts_base_path.should == '/one/var/remotes'
driver.local_scripts_path.should == '/one/var/remotes/vmm/dummy'
driver.remote_scripts_base_path.should == '/var/tmp/one'
driver.remote_scripts_path.should == '/var/tmp/one/vmm/dummy'
end
it 'should distinguish local and remote actions' do
driver=create_driver(*@create_params)
driver.action_is_local?(:deploy).should == false
driver.action_is_local?('SHUTDOWN').should == false
driver.action_is_local?(:migrate).should == true
driver.action_is_local?('migrate').should == true
driver.action_is_local?(:MIGRATE).should == true
driver.action_is_local?('MIGRATE').should == true
end
it 'should call correct executable' do
driver=create_driver(*@create_params)
driver.action_command_line(:deploy, 'parameter').should ==
'/var/tmp/one/vmm/dummy/deploy parameter'
driver.action_command_line(:poll, 'parameter').should ==
'/var/lib/one/remotes/vmm/dummy/local_poll parameter'
driver.action_command_line(:migrate, 'parameter').should ==
'/var/lib/one/remotes/vmm/dummy/migrate parameter'
driver.action_command_line(:test, 'parameter', 'testscr').should ==
'/var/tmp/one/vmm/dummy/testscr parameter'
end
it 'should correctly send messages' do
result=""
driver=create_driver(*@create_params)
MonkeyPatcher.patch do
patch_class(IO, :puts) do |*args|
result=args
end
driver.send_message('action', 'SUCCESS', 15, 'some info')
end
result[0].should == "action SUCCESS 15 some info"
end
it 'should select remote or local execution correctly' do
local_action=[]
remotes_action=[]
driver=create_driver(*@create_params)
MonkeyPatcher.patch do
patch_class(OpenNebulaDriver, :local_action) do |*args|
local_action=args
end
patch_class(OpenNebulaDriver, :remotes_action) do |*args|
remotes_action=args
end
driver.do_action('some parameters', 15, 'localhost', :poll)
driver.do_action('some parameters', 15, 'localhost', :deploy,
:script_name => 'deploy_script')
end
local_action.should == [
"/var/lib/one/remotes/vmm/dummy/local_poll some parameters 15"<<
" localhost",
15,
:poll]
remotes_action.should == ["/var/tmp/one/vmm/dummy/deploy_script"<<
" some parameters 15 localhost",
15,
"localhost",
:deploy,
"/var/tmp/one/vmm/dummy",
nil]
end
end

20
src/mad/ruby/test/fixtures/var/config vendored Normal file
View File

@ -0,0 +1,20 @@
DB=BACKEND=sqlite
DEBUG_LEVEL=3
DEFAULT_DEVICE_PREFIX=hd
DEFAULT_IMAGE_TYPE=OS
HM_MAD=EXECUTABLE=one_hm
HOST_MONITORING_INTERVAL=600
IMAGE_MAD=ARGUMENTS=fs -t 15,EXECUTABLE=one_image
IMAGE_REPOSITORY_PATH=/Users/jfontan/tmp/borrar/git/one/install/var//images
IM_MAD=ARGUMENTS=-r 0 -t 15 kvm,EXECUTABLE=one_im_ssh,NAME=im_kvm
MAC_PREFIX=02:00
MANAGER_TIMER=15
NETWORK_SIZE=254
PORT=2633
SCRIPTS_REMOTE_DIR=/var/tmp/one
TM_MAD=ARGUMENTS=tm_nfs/tm_nfs.conf,EXECUTABLE=one_tm,NAME=tm_nfs
VM_DIR=/Users/jfontan/tmp/borrar/git/one/install/var/
VM_MAD=ARGUMENTS=-t 15 -r 0 kvm,DEFAULT=vmm_ssh/vmm_ssh_kvm.conf,EXECUTABLE=one_vmm_ssh,NAME=vmm_kvm,TYPE=kvm
VM_POLLING_INTERVAL=600
VNC_BASE_PORT=5900