1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

add suspend&resume support and change tests to fog way

This commit is contained in:
Cherdancev Evgeni 2015-11-19 14:25:32 +06:00
parent d57901fc2e
commit a4a70ca071
15 changed files with 291 additions and 133 deletions

View file

@ -1,8 +1,8 @@
require 'fog/opennebula/core'
module Fog
module Compute
class OpenNebula < Fog::Service
module Compute
class OpenNebula < Fog::Service
requires :opennebula_endpoint
recognizes :opennebula_username, :opennebula_password
@ -26,35 +26,106 @@ module Fog
request :vm_destroy
request :get_vnc_console
request :vm_resume
request :vm_suspend
request :vm_stop
request :template_pool
request :vm_disk_snapshot
request :vm_shutdown
request :image_pool
class Mock
include Collections
def initialize(options={})
require 'opennebula'
class Mock
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
"vms" => [
{
"onevm_object" => "",
"status" => "RUNNING",
"state" => "3",
"id" => 4,
"uuid" => "5",
"gid" => "5",
"name" => "MockVM",
"user" => "MockUser",
"group" => "MockGroup",
"cpu" => "2",
"memory" => "1024",
"mac" => "00:01:02:03:04:05",
"ip" => "1.1.1.1"
}
],
"image_pool" => [
{}
],
"template_pool" => [
{
"content" => %Q{
NAME = mock-vm
MEMORY = 512
VCPU = 1
CPU = 1
},
"id" => 1,
"name" => 'mock',
"cpu" => 1,
"vcpu" => 1,
"memory" => 512,
"sched_requirements" => 'CPUSPEED > 1000',
"sched_rank" => 'FREECPU',
"sched_ds_requirements" => "NAME=mock",
"sched_ds_rank" => "FREE_MB",
"disk" => {},
"nic" => {},
"os" => {
'ARCH' => 'x86_64'
},
"graphics" => {},
"raw" => %|["DATA"=>"<cpu match='exact'><model fallback='allow'>core2duo</model></cpu>", "TYPE"=>"kvm"]|,
"context" => {},
"user_variables" => {}
}
]
}
end
end
def self.reset
@data = nil
end
include Collections
def initialize(options={})
@opennebula_endpoint = options[:opennebula_endpoint]
@opennebula_username = options[:opennebula_username]
@opennebula_password = options[:opennebula_password]
require 'opennebula'
end
def client
return @client
end
def data
self.class.data[@opennebula_endpoint]
end
def reset_data
self.class.data.delete(@opennebula_endpoint)
end
end
def client
return @client
end
end
class Real
include Collections
class Real
include Collections
def client
return @client
end
def client
return @client
end
def initialize(options={})
require 'opennebula'
@client = ::OpenNebula::Client.new("#{options[:opennebula_username]}:#{options[:opennebula_password]}", options[:opennebula_endpoint])
def initialize(options={})
require 'opennebula'
@client = ::OpenNebula::Client.new("#{options[:opennebula_username]}:#{options[:opennebula_password]}", options[:opennebula_endpoint])
end
end
end
end
end
end

View file

@ -60,13 +60,21 @@ module Fog
service.vm_resume(id)
end
true
end
end
def stop
Fog::Logger.warning("stop VM: ID:#{id}")
service.vm_stop(id)
end
def suspend
service.vm_suspend(id)
end
def resume
service.vm_resume(id)
end
def destroy
service.vm_destroy(id)
end

View file

@ -18,10 +18,15 @@ module Fog
end
end
end
images.delete nil
images
end #def image_pool
end #class Real
class Mock
def image_pool(filter = {})
end
end
end #class OpenNebula
end #module Compute
end #module Fog

View file

@ -53,33 +53,15 @@ module Fog
class Mock
def list_vms(filter = {})
if filter[:id].nil?
vm1 = mock_vm 'fog-vm1'
vm2 = mock_vm 'fog-vm2'
vm3 = mock_vm 'fog-vm3'
return [vm1, vm2, vm3]
elsif filter[:mock_return]
mock_vm 'fog-vm1', filter[:id]
else
[]
vms = []
self.data['vms'].each do |vm|
if filter[:id].nil?
vms << vm
elsif filter[:id] == vm['id']
vms << vm
end
end
end
def mock_vm(name, id=4)
data = {}
data["onevm_object"] = ""
data["status"] = "Running"
data["state"] = "3"
data["id"] = id
data["uuid"] = "5"
data["gid"] = "5"
data["name"] = "MockVM-#{name}"
data["user"] = "MockUser"
data["group"] = "MockGroup"
data["cpu"] = "2"
data["memory"] = "1024"
data["mac"] = "00:01:02:03:04:05"
data["ip"] = "1.1.1.1"
data
vms
end
end
end

View file

@ -110,35 +110,11 @@ module Fog
nic1 = Mock_nic.new
nic1.vnet = networks.first
[
{
:content => %Q{
NAME = mock-vm
MEMORY = 512
VCPU = 1
CPU = 1
},
:id => 1,
:name => 'mock',
:cpu => 1,
:vcpu => 1,
:memory => 512,
:sched_requirements => 'CPUSPEED > 1000',
:sched_rank => 'FREECPU',
:sched_ds_requirements => "NAME=mock",
:sched_ds_rank => "FREE_MB",
:disk => {},
:nic => {},
:nic => [ nic1 ] ,
:os => {
'ARCH' => 'x86_64'
},
:graphics => {},
:raw => %|["DATA"=>"<cpu match='exact'><model fallback='allow'>core2duo</model></cpu>", "TYPE"=>"kvm"]|,
:context => {},
:user_variables => {}
}
]
self.data['template_pool']
self.data['template_pool'].each do |tmpl|
tmpl['nic'][0] = nic1
end
self.data['template_pool']
end
class Mock_nic
@ -150,6 +126,9 @@ module Fog
def name
"fogtest"
end
def model
"virtio-net"
end
end
end #class Mock
end #class OpenNebula

View file

@ -68,21 +68,30 @@ module Fog
class Mock
def vm_allocate(attr={ })
response = Excon::Response.new
response.status = 200
id = rand(1000)
ids = []
self.data['vms'].each do |vm|
ids << vm['id']
if vm['id'] == id
while ids.include?(id)
id = rand(1000)
end
break
end
end
data = {}
data["onevm_object"] = ""
data["status"] = "Running"
data["state"] = "3"
data["id"] = 4
data["uuid"] = "5"
data["gid"] = "5"
data["name"] = "MockVM"
data["user"] = "MockUser"
data["group"] = "MockGroup"
data["cpu"] = "2"
data["memory"] = "1024"
data["mac"] = "00:01:02:03:04:05"
data["ip"] = "1.1.1.1"
data
data['id'] = id
data['flavor'] = attr[:flavor]
data['name'] = attr[:name]
data['state'] = 'RUNNING'
data['status'] = 3
self.data['vms'] << data
data
end
end
end

View file

@ -4,16 +4,25 @@ module Fog
class Real
def vm_destroy(id)
vmpool = ::OpenNebula::VirtualMachinePool.new(client)
vmpool.info!(-2,id,id,-1)
vmpool.each do |vm|
vmpool.info!(-2,id,id,-1)
vmpool.each do |vm|
# true => delete and recreate vm
vm.delete(false)
vm.delete(false)
end
end
end
class Mock
def vm_destroy(id)
response = Excon::Response.new
response.status = 200
self.data['vms'].each do |vm|
if vm['id'] == id
self.data['vms'].delete(vm)
end
end
true
end
end

View file

@ -9,13 +9,19 @@ module Fog
rc = 0
vmpool.each do |vm|
rc = vm.disk_snapshot(disk_id, image_name)
rc = vm.disk_snapshot_create(disk_id, image_name)
if(rc.is_a? ::OpenNebula::Error)
raise(rc)
raise(rc)
end
end
rc
end #def vm_disk_snapshot
end
class Mock
def vm_disk_snapshot(id, disk_id, image_name)
end
end
end
end

View file

@ -2,34 +2,24 @@ module Fog
module Compute
class OpenNebula
class Real
def vm_resume(id)
vmpool = ::OpenNebula::VirtualMachinePool.new(client)
vmpool.info!(-2,id,id,-1)
puts "#{vmpool.entries.class} #{vmpool.entries.methods}"
puts "#{vmpool.entries.inspect} #{vmpool.entries.methods}"
vmpool.each do |vm|
vm.resume
vmpool.info!(-2,id,id,-1)
vmpool.each do |vm|
vm.resume
end
##if(attr[:id].nil?)
## raise(ArgumentError.new("Attribute :id is nil or empty! #{attr.inspect}"))
##end
#vmpool = ::OpenNebula::VirtualMachinePool.new(client)
#vmpool.info!
#vmpool.each do |vm|
# if vm.id == id then
# vm.resume
# return true
# end
#end
#false
end
end
class Mock
class Mock
def vm_resume(id)
self.data['vms'].each do |vm|
if id == vm['id']
vm['state'] = 'RUNNING'
vm['status'] = 3
end
end
end
end
end

View file

@ -8,7 +8,7 @@ module Fog
vmpool.info!(-2,id,id,-1)
vmpool.each do |vm|
vm.shutdown()
vm.shutdown
end
end #def vm_shutdown

View file

@ -2,19 +2,18 @@ module Fog
module Compute
class OpenNebula
class Real
def vm_stop(id)
vmpool = ::OpenNebula::VirtualMachinePool.new(client)
vmpool.info!(-2,id,id,-1)
puts "#{vmpool.entries.class} #{vmpool.entries.methods}"
puts "#{vmpool.entries.inspect} #{vmpool.entries.methods}"
vmpool.each do |vm|
vm.stop
vmpool.info!(-2,id,id,-1)
vmpool.each do |vm|
vm.stop
end
end #def vm_stop
end
class Mock
class Mock
def vm_stop
true
end
end
end

View file

@ -0,0 +1,31 @@
module Fog
module Compute
class OpenNebula
class Real
def vm_suspend(id)
vmpool = ::OpenNebula::VirtualMachinePool.new(client)
vmpool.info!(-2,id,id,-1)
vmpool.each do |vm|
vm.suspend
end
end
end
class Mock
def vm_suspend(id)
response = Excon::Response.new
response.status = 200
self.data['vms'].each do |vm|
if id == vm['id']
vm['state'] = 'LCM_INIT'
vm['status'] = 5
end
end
end
end
end
end
end

View file

@ -17,7 +17,7 @@ Shindo.tests('Fog::Compute[:opennebula] | flavor model', ['opennebula']) do
end
end
tests("The attributes hash should have key") do
[:name, :id, :content, :cpu, :vcpu, :memory, :os, :graphics, :raw, :context, :user_variables ].each do |attribute|
[:name, :id, :content, :cpu, :vcpu, :memory, :os, :graphics, :context, :user_variables ].each do |attribute|
test("#{attribute}") { model_attribute_hash.has_key? attribute }
end
end

View file

@ -1,4 +1,4 @@
Shindo.tests("Fog::Compute[:opennebula] | vm_create and destroy request", 'opennebula') do
Shindo.tests("Fog::Compute[:opennebula] | vm_create and vm_destroy request", 'opennebula') do
compute = Fog::Compute[:opennebula]
name_base = Time.now.to_i
@ -17,10 +17,27 @@ Shindo.tests("Fog::Compute[:opennebula] | vm_create and destroy request", 'openn
test("response should be a kind of Hash") { response.kind_of? Hash}
test("id should be a one-id (Fixnum)") { response['id'].is_a? Fixnum}
end
tests("Destroy VM") do
compute.vm_destroy(response['id'])
vms = compute.list_vms({:id => response['id'], :mock_return => false})
test("get vm should be empty") { compute.list_vms({:id => response['id']}).empty?}
test("vm should not be in array of vms") {
vm_not_exist = true
compute.list_vms.each do |vm|
if vm['id'] == response['id']
vm_not_exist = false
end
end
vm_not_exist
}
test("vm should not be in array of vms by filter") {
vm_not_exist = true
compute.list_vms({:id => response['id']}).each do |vm|
if vm['id'] == response['id']
vm_not_exist = false
end
end
vm_not_exist
}
end
#tests("Create VM from template (clone)") do

View file

@ -0,0 +1,52 @@
Shindo.tests("Fog::Compute[:opennebula] | vm_suspend and vm_resume request", 'opennebula') do
compute = Fog::Compute[:opennebula]
name_base = Time.now.to_i
f = compute.flavors.get_by_name("fogtest")
tests("Get 'fogtest' flavor/template") do
test("Got template with name 'fogtest'") {f.kind_of? Array}
raise ArgumentError, "Could not get a template with the name 'fogtest'! This is required for live tests!" unless f
end
f = f.first
newvm = compute.servers.new
newvm.flavor = f
newvm.name = 'fogtest-'+name_base.to_s
vm = newvm.save
vm.wait_for { (vm.state == 'RUNNING') }
tests("Suspend VM") do
compute.vm_suspend(vm.id)
vm.wait_for { (vm.state == 'LCM_INIT') }
test("response status should be LCM_INIT and 5") {
vm_state = false
compute.list_vms.each do |vm_|
if vm_['id'] == vm.id
if vm_['state'] == 'LCM_INIT' && vm_['status'] == 5
vm_state = true
end
end
end
vm_state
}
end
tests("Resume VM") do
compute.vm_resume(vm.id)
vm.wait_for { (vm.state == 'RUNNING') }
test("response status should be LCM_INIT and 5") {
vm_state = false
compute.list_vms.each do |vm_|
if vm_['id'] == vm.id
if vm_['state'] == 'RUNNING' && vm_['status'] == 3
vm_state = true
end
end
end
vm_state
}
end
vm.destroy
end