From fe0f240278ee1ed19dc00cb6bc44364df8eb1168 Mon Sep 17 00:00:00 2001 From: Amos Benari Date: Thu, 5 Apr 2012 17:02:45 +0300 Subject: [PATCH 1/2] added mock implementation --- lib/fog/core/errors.rb | 1 + lib/fog/libvirt/compute.rb | 20 +++++++++- lib/fog/libvirt/models/compute/server.rb | 1 + .../libvirt/requests/compute/create_domain.rb | 2 +- .../libvirt/requests/compute/define_domain.rb | 2 +- .../libvirt/requests/compute/list_domains.rb | 28 ++++++++++++- .../requests/compute/list_interfaces.rb | 12 +++++- .../libvirt/requests/compute/list_networks.rb | 12 +++++- .../libvirt/requests/compute/list_pools.rb | 16 ++++++++ .../libvirt/requests/compute/list_volumes.rb | 15 +++++++ .../requests/compute/mock_files/domain.xml | 40 +++++++++++++++++++ tests/helpers/mock_helper.rb | 1 + 12 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 lib/fog/libvirt/requests/compute/mock_files/domain.xml diff --git a/lib/fog/core/errors.rb b/lib/fog/core/errors.rb index 7b66c497d..5ea5376ec 100644 --- a/lib/fog/core/errors.rb +++ b/lib/fog/core/errors.rb @@ -57,6 +57,7 @@ An alternate file may be used by placing its path in the FOG_RC environment vari :ovirt_username: :ovirt_password: :ovirt_url: + :libvirt_uri: :rackspace_api_key: :rackspace_username: :rackspace_servicenet: diff --git a/lib/fog/libvirt/compute.rb b/lib/fog/libvirt/compute.rb index 747a12771..ee04722ec 100644 --- a/lib/fog/libvirt/compute.rb +++ b/lib/fog/libvirt/compute.rb @@ -45,11 +45,29 @@ module Fog request :destroy_interface request :get_node_info + module Shared + include Fog::Compute::LibvirtUtil + end + class Mock + include Shared + def initialize(options={}) + # libvirt is part of the gem => ruby-libvirt + require 'libvirt' + end + + private + attr_reader :client + + #read mocks xml + def read_xml(file_name) + file_path = File.join(File.dirname(__FILE__),"requests","compute","mock_files",file_name) + File.read(file_path) + end end class Real - include Fog::Compute::LibvirtUtil + include Shared attr_reader :client attr_reader :uri attr_reader :ip_command diff --git a/lib/fog/libvirt/models/compute/server.rb b/lib/fog/libvirt/models/compute/server.rb index b6cfb4890..6f93d0e12 100644 --- a/lib/fog/libvirt/models/compute/server.rb +++ b/lib/fog/libvirt/models/compute/server.rb @@ -90,6 +90,7 @@ module Fog poweroff unless stopped? connection.vm_action(uuid, :undefine) volumes.each { |vol| vol.destroy } if options[:destroy_volumes] + true end def reboot diff --git a/lib/fog/libvirt/requests/compute/create_domain.rb b/lib/fog/libvirt/requests/compute/create_domain.rb index 1bfc372ac..8bcb733e2 100644 --- a/lib/fog/libvirt/requests/compute/create_domain.rb +++ b/lib/fog/libvirt/requests/compute/create_domain.rb @@ -9,7 +9,7 @@ module Fog class Mock def create_domain(xml) - + ::Libvirt::Domain.new() end end end diff --git a/lib/fog/libvirt/requests/compute/define_domain.rb b/lib/fog/libvirt/requests/compute/define_domain.rb index 4a358b894..53b8fae03 100644 --- a/lib/fog/libvirt/requests/compute/define_domain.rb +++ b/lib/fog/libvirt/requests/compute/define_domain.rb @@ -9,7 +9,7 @@ module Fog class Mock def define_domain(xml) - + ::Libvirt::Domain.new() end end end diff --git a/lib/fog/libvirt/requests/compute/list_domains.rb b/lib/fog/libvirt/requests/compute/list_domains.rb index 2b7991207..46db88e63 100644 --- a/lib/fog/libvirt/requests/compute/list_domains.rb +++ b/lib/fog/libvirt/requests/compute/list_domains.rb @@ -15,7 +15,9 @@ module Fog end data.compact.map { |d| domain_to_attributes d } end + end + module Shared private def vnc_port xml @@ -70,8 +72,32 @@ module Fog end class Mock - def list_vms(filter = { }) + def list_domains(filter = { }) + dom1 = mock_domain 'fog-dom1' + dom2 = mock_domain 'fog-dom2' + dom3 = mock_domain 'a-fog-dom3' + [dom1, dom2, dom3] + end + def mock_domain name + xml = read_xml 'domain.xml' + { + :id => "dom.uuid", + :uuid => "dom.uuid", + :name => name, + :max_memory_size => 8, + :cputime => 7, + :memory_size => 6, + :vcpus => 5, + :autostart => false, + :os_type => "RHEL6", + :active => false, + :vnc_port => 5910, + :boot_order => boot_order(xml), + :nics => domain_interfaces(xml), + :volumes_path => domain_volumes(xml), + :state => 'shutoff' + } end end end diff --git a/lib/fog/libvirt/requests/compute/list_interfaces.rb b/lib/fog/libvirt/requests/compute/list_interfaces.rb index 30548169b..2b1d1b21a 100644 --- a/lib/fog/libvirt/requests/compute/list_interfaces.rb +++ b/lib/fog/libvirt/requests/compute/list_interfaces.rb @@ -39,7 +39,17 @@ module Fog class Mock def list_interfaces(filters={ }) - [] + if1 = mock_interface 'if1' + if2 = mock_interface 'if2' + [if1, if2] + end + + def mock_interface name + { + :mac => 'aa:bb:cc:dd:ee:ff', + :name => name, + :active => true + } end end end diff --git a/lib/fog/libvirt/requests/compute/list_networks.rb b/lib/fog/libvirt/requests/compute/list_networks.rb index 9eb89a2cf..508fd9937 100644 --- a/lib/fog/libvirt/requests/compute/list_networks.rb +++ b/lib/fog/libvirt/requests/compute/list_networks.rb @@ -40,7 +40,17 @@ module Fog class Mock def list_networks(filters={ }) - [] + net1 = mock_network 'net1' + net2 = mock_network 'net2' + [net1, net2] + end + + def mock_network name + { + :uuid => 'net.uuid', + :name => name, + :bridge_name => 'net.bridge_name' + } end end end diff --git a/lib/fog/libvirt/requests/compute/list_pools.rb b/lib/fog/libvirt/requests/compute/list_pools.rb index 3f0d0410e..feb635414 100644 --- a/lib/fog/libvirt/requests/compute/list_pools.rb +++ b/lib/fog/libvirt/requests/compute/list_pools.rb @@ -48,7 +48,23 @@ module Fog class Mock def list_pools(filter = { }) + pool1 = mock_pool 'pool1' + pool2 = mock_pool 'pool1' + [pool1, pool2] + end + def mock_pool name + { + :uuid => 'pool.uuid', + :persistent => true, + :autostart => true, + :active => true, + :name => name, + :allocation => 123456789, + :capacity => 123456789, + :num_of_volumes => 3, + :state => :running + } end end end diff --git a/lib/fog/libvirt/requests/compute/list_volumes.rb b/lib/fog/libvirt/requests/compute/list_volumes.rb index 01751f734..44a6c9205 100644 --- a/lib/fog/libvirt/requests/compute/list_volumes.rb +++ b/lib/fog/libvirt/requests/compute/list_volumes.rb @@ -70,7 +70,22 @@ module Fog class Mock def list_volumes(filters={ }) + vol1 = mock_volume 'vol1' + vol2 = mock_volume 'vol2' + [vol1, vol2] + end + def mock_volume name + { + :pool_name => 'vol.pool.name', + :key => 'vol.key', + :id => 'vol.key', + :path => 'vol.path', + :name => name, + :format_type => 'raw', + :allocation => 123, + :capacity => 123, + } end end end diff --git a/lib/fog/libvirt/requests/compute/mock_files/domain.xml b/lib/fog/libvirt/requests/compute/mock_files/domain.xml new file mode 100644 index 000000000..043e46ad5 --- /dev/null +++ b/lib/fog/libvirt/requests/compute/mock_files/domain.xml @@ -0,0 +1,40 @@ + + fog-449765558356062 + 262144 + 1 + + hvm + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/helpers/mock_helper.rb b/tests/helpers/mock_helper.rb index 499487560..9ac25b3bd 100644 --- a/tests/helpers/mock_helper.rb +++ b/tests/helpers/mock_helper.rb @@ -51,6 +51,7 @@ if Fog.mock? :ovirt_url => 'http://ovirt:8080/api', :ovirt_username => 'admin@internal', :ovirt_password => '123123', + :libvirt_uri => 'qemu://libvirt/system', :rackspace_api_key => 'rackspace_api_key', :rackspace_username => 'rackspace_username', :slicehost_password => 'slicehost_password', From 8b76b2ff0519372d7a2866b50ebfaf3c1f42be79 Mon Sep 17 00:00:00 2001 From: Amos Benari Date: Thu, 5 Apr 2012 17:12:34 +0300 Subject: [PATCH 2/2] removed unneeded dependency, --- lib/fog/libvirt/requests/compute/list_volumes.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/fog/libvirt/requests/compute/list_volumes.rb b/lib/fog/libvirt/requests/compute/list_volumes.rb index 44a6c9205..822029fd8 100644 --- a/lib/fog/libvirt/requests/compute/list_volumes.rb +++ b/lib/fog/libvirt/requests/compute/list_volumes.rb @@ -1,6 +1,3 @@ -require 'erb' -require "rexml/document" - module Fog module Compute class Libvirt @@ -23,8 +20,7 @@ module Fog def volume_to_attributes(vol) - xml = REXML::Document.new(vol.xml_desc) - format_type = xml.root.elements['/volume/target/format'].attributes['type'] + format_type = xml_element(vol.xml_desc, "/volume/target/format", "type") return nil if format_type == "dir" {