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

* Added missing attributes to Server model

* Added create_server_raw request and tests
* Added VIF.save action
* more tests
This commit is contained in:
Sergio Rubio 2012-04-13 12:44:49 +02:00
parent fb54e330b2
commit d7edfcd443
8 changed files with 213 additions and 15 deletions

View file

@ -12,7 +12,7 @@ module Fog
attribute :uuid
attribute :name, :aliases => :name_label
attribute :affinity
attribute :__affinity, :aliases => :affinity
attribute :allowed_operations
attribute :consoles
attribute :domarch
@ -32,15 +32,23 @@ module Fog
attribute :power_state
attribute :pv_args, :aliases => :PV_args
attribute :pv_bootloader, :aliases => :PV_bootloader
attribute :pv_bootloader_args, :aliases => :PV_bootloader_args
attribute :pv_kernel, :aliases => :PV_kernel
attribute :pv_ramdisk, :aliases => :PV_ramdisk
attribute :pv_legacy_args, :aliases => :PV_legacy_args
attribute :__resident_on, :aliases => :resident_on
# Virtual Block Devices
attribute :__vbds, :aliases => :VBDs
# Virtual CPUs
attribute :vcpus_at_startup, :aliases => :VCPUs_at_startup
attribute :vcpus_max, :aliases => :VCPUs_max
attribute :vcpus_params, :aliases => :VCPUs_params
# Virtual Interfaces (NIC)
attribute :__vifs, :aliases => :VIFs
attribute :template_name
attribute :hvm_boot_policy, :aliases => :HVM_boot_policy
attribute :hvm_boot_params, :aliases => :HVM_boot_params
attribute :pci_bus, :aliases => :PCI_bus
def initialize(attributes={})
super
@ -50,6 +58,10 @@ module Fog
__vbds.collect {|vbd| connection.vbds.get vbd }
end
def affinity
connection.hosts.get __affinity
end
def destroy
# Make sure it's halted
stop('hard')
@ -141,11 +153,18 @@ module Fog
else
auto_start = params[:auto_start]
end
attributes = connection.get_record(
connection.create_server( name, template_name, nets, :auto_start => auto_start),
'VM'
)
merge_attributes attributes
if template_name
attr = connection.get_record(
connection.create_server( name, template_name, nets, :auto_start => auto_start),
'VM'
)
else
attr = connection.get_record(
connection.create_server_raw(attributes),
'VM'
)
end
merge_attributes attr
true
end

View file

@ -36,6 +36,14 @@ module Fog
connection.servers.get __vm
end
def save
requires :server
raise ArgumentError.new('network is required for this operation') \
unless attributes[:__network]
ref = connection.create_vif attributes[:server], attributes[:__network]
merge_attributes connection.vifs.get(ref).attributes
end
end
end

View file

@ -7,6 +7,69 @@ module Fog
@connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.get_by_name_label' }, label)
end
def create_server_raw(config = {})
config[:name_label] = config[:name] if config[:name]
config.delete :name
config[:affinity] = config[:__affinity] if config[:__affinity]
config.delete :__affinity
raise ArgumentError.new("Invalid :name_label attribute") \
if !config[:name_label]
raise ArgumentError.new("Invalid :affinity attribute") \
if not config[:affinity]
config[:affinity] = config[:affinity].reference \
if config[:affinity].kind_of? Fog::Compute::XenServer::Host
config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
%w{ VCPUs_at_startup
VCPUs_max
VCPUs_params
PV_bootloader_args
PV_bootloader
PV_kernel
PV_ramdisk
PV_legacy_args
HVM_boot_params
HVM_boot_params
}.each do |k|
if config[k.to_sym.downcase]
config[k.to_sym] = config[k.to_sym.downcase]
config.delete k.to_sym.downcase
end
end
vm_record = {
:name_label => '',
:name_description => 'description',
:user_version => '0',
:affinity => '',
:is_a_template => true,
:auto_power_on => false,
:memory_static_max => '512',
:memory_static_min => '512',
:memory_dynamic_max => '512',
:memory_dynamic_min => '512',
:VCPUs_params => {},
:VCPUs_max => '1',
:VCPUs_at_startup => '1',
:actions_after_shutdown => 'Destroy',
:actions_after_reboot => 'Restart',
:actions_after_crash => 'Restart',
:platform => { 'nx' => false, 'acpi' => true, 'apic' => 'true', 'pae' => true, 'viridian' => true},
:platform => {},
:other_config => {},
:pool_name => '',
:PV_bootloader => 'pygrub', #pvgrub, eliloader
:PV_kernel => '',
:PV_ramdisk => '',
:PV_args => '',
:PV_bootloader_args => '',
:PV_legacy_args => '',
:HVM_boot_policy => '',
:HVM_boot_params => {},
:PCI_bus => '',
:recommendations => '',
}.merge config
@connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.create' }, vm_record)
end
def create_server( name_label, template = nil, networks = [], extra_args = {})
if !networks.kind_of? Array
raise "Invalid networks argument"
@ -51,6 +114,10 @@ module Fog
Fog::Mock.not_implemented
end
def create_server_raw(config = {})
Fog::Mock.not_implemented
end
end
end

View file

@ -7,6 +7,8 @@ module Fog
def create_vif( vm_ref, network_ref, device = -1)
raise ArgumentError.new('Invalid vm_ref') if vm_ref.nil?
raise ArgumentError.new('Invalid network_ref') if network_ref.nil?
vm_ref = vm_ref.reference if vm_ref.kind_of? Fog::Model
network_ref = network_ref.reference if network_ref.kind_of? Fog::Model
vif_config = default_vif_config(vm_ref, network_ref, device.to_s)
@connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VIF.create'}, vif_config )
end

View file

@ -7,6 +7,9 @@ Shindo.tests('Fog::Compute[:xenserver] | server model', ['xenserver']) do
(servers.all :name_matches => test_ephemeral_vm_name).each do |s|
s.destroy
end
(servers.templates.find_all { |t| t.name == test_ephemeral_vm_name}).each do |s|
s.destroy
end
server = Fog::Compute[:xenserver].servers.create(:name => test_ephemeral_vm_name,
:template_name => test_template_name)
@ -15,7 +18,7 @@ Shindo.tests('Fog::Compute[:xenserver] | server model', ['xenserver']) do
tests('The server model should') do
tests('have the action') do
test('reload') { server.respond_to? 'reload' }
%w{ set_attribute refresh stop clean_shutdown hard_shutdown start destroy reboot hard_reboot clean_reboot }.each do |action|
%w{ affinity set_attribute refresh stop clean_shutdown hard_shutdown start destroy reboot hard_reboot clean_reboot }.each do |action|
test(action) { server.respond_to? action }
#test("#{action} returns successfully") { server.send(action.to_sym) ? true : false }
end
@ -26,7 +29,7 @@ Shindo.tests('Fog::Compute[:xenserver] | server model', ['xenserver']) do
:reference,
:uuid,
:is_a_template,
:affinity,
:__affinity,
:allowed_operations,
:consoles,
:domarch,
@ -47,9 +50,17 @@ Shindo.tests('Fog::Compute[:xenserver] | server model', ['xenserver']) do
:pv_args,
:__resident_on,
:__vbds,
:vcpus_at_startup,
:vcpus_max,
:__vifs
:__vifs,
:vcpus_params,
:vcpus_at_startup,
:vcpus_max,
:hvm_boot_policy,
:hvm_boot_params,
:pci_bus,
:pv_kernel,
:pv_ramdisk,
:pv_legacy_args,
:pv_bootloader_args
]
tests("The server model should respond to") do
attributes.each do |attribute|
@ -64,6 +75,7 @@ Shindo.tests('Fog::Compute[:xenserver] | server model', ['xenserver']) do
end
test('be a kind of Fog::Compute::XenServer::Server') { server.kind_of? Fog::Compute::XenServer::Server }
#test('return a Fog::Compute::XenServer::Host affinity') { server.affinity.kind_of? Fog::Compute::XenServer::Host }
end

View file

@ -5,7 +5,7 @@ Shindo.tests('Fog::Compute[:xenserver] | VIF model', ['VIF']) do
tests('The VIF model should') do
tests('have the action') do
test('reload') { vif.respond_to? 'reload' }
test('reload save destroy network server') { vif.respond_to? 'reload' }
end
tests('have attributes') do
model_attribute_hash = vif.attributes

View file

@ -6,6 +6,9 @@ Shindo.tests('Fog::Compute[:xenserver] | create_server request', ['xenserver'])
(servers.all :name_matches => test_ephemeral_vm_name).each do |s|
s.destroy
end
(servers.templates.find_all { |t| t.name == test_ephemeral_vm_name}).each do |s|
s.destroy
end
tests('create_server should') do
raises(StandardError, 'raise exception when template nil') do
@ -30,6 +33,96 @@ Shindo.tests('Fog::Compute[:xenserver] | create_server request', ['xenserver'])
returns(nil, 'return nil if VM does not exist') { compute.get_vm_by_name('sdfsdf') }
end
tests('create_server_raw should') do
raises(ArgumentError, 'raise exception when name_label nil') do
compute.create_server_raw
end
test('create a server') do
ref = compute.create_server_raw(
{
:name_label => test_ephemeral_vm_name,
:affinity => compute.hosts.first
}
)
valid_ref? ref
end
test('create a server with name foobar') do
ref = compute.create_server_raw(
{
:name_label => test_ephemeral_vm_name,
:affinity => compute.hosts.first
}
)
(compute.servers.get ref).name == test_ephemeral_vm_name
end
test('set the PV_bootloader attribute to eliloader') do
ref = compute.create_server_raw(
{
:name_label => test_ephemeral_vm_name,
:affinity => compute.hosts.first,
:PV_bootloader => 'eliloader',
}
)
(compute.servers.get ref).pv_bootloader == 'eliloader'
end
test('set the :pv_bootloader attribute to eliloader') do
ref = compute.create_server_raw(
{
:name_label => test_ephemeral_vm_name,
:affinity => compute.hosts.first,
:pv_bootloader => 'eliloader',
}
)
(compute.servers.get ref).pv_bootloader == 'eliloader'
end
test('set the "vcpus_attribute" to 1') do
ref = compute.create_server_raw(
{
:name_label => test_ephemeral_vm_name,
:affinity => compute.hosts.first,
'vcpus_max' => '1',
}
)
(compute.servers.get ref).vcpus_max == '1'
end
tests('set lowercase hash attributes') do
%w{
VCPUs_params
HVM_boot_params
}.each do |a|
test("set the :#{a} to { :foo => 'bar' }") do
ref = compute.create_server_raw(
{
:name_label => test_ephemeral_vm_name,
:affinity => compute.hosts.first,
a.to_sym.downcase => {:foo => :bar},
}
)
eval "(compute.servers.get ref).#{a.to_s.downcase}['foo'] == 'bar'"
end
end
%w{ VCPUs_at_startup
VCPUs_max
PV_bootloader_args
PV_bootloader
PV_kernel
PV_ramdisk
PV_legacy_args
}.each do |a|
test("set the :#{a} to 1") do
ref = compute.create_server_raw(
{
:name_label => test_ephemeral_vm_name,
:affinity => compute.hosts.first,
a.to_sym.downcase => '1',
}
)
eval "(compute.servers.get ref).#{a.to_s.downcase} == '1'"
end
end
end
end
tests('The expected options') do
raises(ArgumentError, 'raises ArgumentError when ref,class missing') { compute.create_server }
end

View file

@ -1,9 +1,6 @@
Shindo.tests('Fog::Compute[:xenserver] | create_vdi request', ['xenserver']) do
compute = Fog::Compute[:xenserver]
#(servers.templates.find_all { |t| t.name == test_ephemeral_vm_name}).each do |s|
# s.destroy
#end
sr = compute.storage_repositories.find { |sr| sr.name == 'Local storage' }
tests('create_vdi should') do