Skip to content

Commit

Permalink
Use test:///default in tests
Browse files Browse the repository at this point in the history
Rather than mocking, this uses the test connection to use real libvirt
objects. This brings compatibility with 0.8.3.

It only keeps mocking for dhcp_leases which isn't implemented in the
test connection. The UUIDs are modified to match.
  • Loading branch information
ekohl committed Jul 15, 2024
1 parent 0277e9b commit 801ce47
Show file tree
Hide file tree
Showing 33 changed files with 166 additions and 273 deletions.
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ group :development, :test do
gem "octokit", :require => false
end

# 0.8.3 breaks our tests
gem "ruby-libvirt", ">= 0.7.0", "< 0.8.3"

gemspec
32 changes: 9 additions & 23 deletions lib/fog/libvirt/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,12 @@ class Compute < Fog::Service

module Shared
include Fog::Libvirt::Util
end

class Mock
include Shared
def initialize(options={})
# libvirt is part of the gem => ruby-libvirt
require 'libvirt'
end

private

def client
return @client if defined?(@client)
end

#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 Shared
attr_reader :client
attr_reader :uri

def initialize(options={})
super()
@uri = ::Fog::Libvirt::Util::URI.new(enhance_uri(options[:libvirt_uri]))

# libvirt is part of the gem => ruby-libvirt
Expand Down Expand Up @@ -132,6 +110,14 @@ def enhance_uri(uri)
uri+append
end
end

class Real
include Shared
end

class Mock
include Shared
end
end
end
end
10 changes: 6 additions & 4 deletions lib/fog/libvirt/requests/compute/clone_volume.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def clone_volume(pool_name, xml, name)
vol = client.lookup_storage_pool_by_name(pool_name).lookup_volume_by_name(name)
client.lookup_storage_pool_by_name(pool_name).create_vol_xml_from(xml, vol)
end
end

class Real
include Shared
end

class Mock
def clone_volume(pool_name, xml, name)
Fog::Libvirt::Compute::Volume.new({:pool_name => pool_name, :xml => xml})
end
include Shared
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/fog/libvirt/requests/compute/create_domain.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def create_domain(xml)
client.create_domain_xml(xml)
end
end

class Real
include Shared
end

class Mock
def create_domain(xml)
::Libvirt::Domain.new()
end
include Shared
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/fog/libvirt/requests/compute/create_volume.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def create_volume(pool_name, xml)
client.lookup_storage_pool_by_name(pool_name).create_vol_xml(xml)
end
end

class Real
include Shared
end

class Mock
def create_volume(pool_name, xml)
end
include Shared
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/fog/libvirt/requests/compute/define_domain.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def define_domain(xml)
client.define_domain_xml(xml)
end
end

class Real
include Shared
end

class Mock
def define_domain(xml)
::Libvirt::Domain.new()
end
include Shared
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/fog/libvirt/requests/compute/define_pool.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def define_pool(xml)
client.define_storage_pool_xml(xml)
end
end

class Real
include Shared
end

class Mock
def define_pool(xml)
end
include Shared
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/fog/libvirt/requests/compute/destroy_interface.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
#shutdown the interface
def destroy_interface(uuid)
client.lookup_interface_by_uuid(uuid).destroy
end
end

class Real
include Shared
end

class Mock
def destroy_interface(uuid)
true
end
include Shared
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/fog/libvirt/requests/compute/destroy_network.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def destroy_network(uuid)
client.lookup_network_by_uuid(uuid).destroy
end
end

class Real
include Shared
end

class Mock
def destroy_network(uuid)
true
end
include Shared
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/libvirt/requests/compute/dhcp_leases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def dhcp_leases(uuid, mac, flags = 0)
end

class Mock
# Not implemented by the test driver
def dhcp_leases(uuid, mac, flags = 0)
leases1 = {
'aa:bb:cc:dd:ee:ff' => [
Expand All @@ -23,8 +24,8 @@ def dhcp_leases(uuid, mac, flags = 0)
]
}
networks = {
# should match mock net uuid from list_networks.rb
'a29146ea-39b2-412d-8f53-239eef117a32' => leases1,
# should match the default network from the test connection
'dd8fe884-6c02-601e-7551-cca97df1c5df' => leases1,
'fbd4ac68-cbea-4f95-86ed-22953fd92384' => leases2
}
if !networks[uuid].nil?
Expand Down
9 changes: 6 additions & 3 deletions lib/fog/libvirt/requests/compute/get_node_info.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def get_node_info
node_hash = Hash.new
node_info = client.node_get_info
Expand All @@ -28,9 +28,12 @@ def node_attr attr, xml
end
end

class Real
include Shared
end

class Mock
def get_node_info
end
include Shared
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/fog/libvirt/requests/compute/libversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def libversion()
client.libversion
end
end

class Real
include Shared
end

class Mock
def libversion()
return 1002009
end
include Shared
end
end
end
Expand Down
36 changes: 6 additions & 30 deletions lib/fog/libvirt/requests/compute/list_domains.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def list_domains(filter = { })
data=[]

Expand All @@ -25,9 +25,7 @@ def catchLibvirtExceptions
rescue ::Libvirt::RetrieveError, ::Libvirt::Error
nil
end
end

module Shared
private

def domain_display xml
Expand Down Expand Up @@ -90,34 +88,12 @@ def domain_to_attributes(dom)
end
end

class Mock
def list_domains(filter = { })
dom1 = mock_domain 'fog-dom1'
dom2 = mock_domain 'fog-dom2'
dom3 = mock_domain 'a-fog-dom3'
[dom1, dom2, dom3]
end
class Real
include Shared
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,
:cpus => 5,
:autostart => false,
:os_type => "hvm",
:active => false,
:vnc_port => 5910,
:boot_order => boot_order(xml),
:nics => domain_interfaces(xml),
:volumes_path => domain_volumes(xml),
:state => 'shutoff'
}
end
class Mock
include Shared
end
end
end
Expand Down
20 changes: 6 additions & 14 deletions lib/fog/libvirt/requests/compute/list_interfaces.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Fog
module Libvirt
class Compute
class Real
module Shared
def list_interfaces(filter = { })
data=[]
if filter.keys.empty?
Expand Down Expand Up @@ -37,20 +37,12 @@ def interface_to_attributes(net)
end
end

class Mock
def list_interfaces(filters={ })
if1 = mock_interface 'if1'
if2 = mock_interface 'if2'
[if1, if2]
end
class Real
include Shared
end

def mock_interface name
{
:mac => 'aa:bb:cc:dd:ee:ff',
:name => name,
:active => true
}
end
class Mock
include Shared
end
end
end
Expand Down
Loading

0 comments on commit 801ce47

Please sign in to comment.