diff --git a/Rakefile b/Rakefile index aebe382..f9d76c5 100644 --- a/Rakefile +++ b/Rakefile @@ -47,8 +47,7 @@ end desc 'Run tests' task :test do - mock = ENV['FOG_MOCK'] || 'true' - sh("export FOG_MOCK=#{mock} && bundle exec shindont tests") + sh("bundle exec shindont tests") end desc 'Run mocked tests' @@ -61,6 +60,11 @@ task :live do sh("export FOG_MOCK=false && bundle exec shindont tests") end +desc 'Run live libvirt qemu:///session tests' +task :live_session do + sh("export FOG_LIBVIRT_URI=qemu:///session && bundle exec rake test minitest") +end + desc "Open an irb session preloaded with this library" task :console do sh "irb -rubygems -r ./lib/fog/libvirt.rb" diff --git a/minitests/test_helper.rb b/minitests/test_helper.rb index e317bf2..20a60ec 100644 --- a/minitests/test_helper.rb +++ b/minitests/test_helper.rb @@ -17,7 +17,11 @@ require 'fog/libvirt' -Fog.mock! -Fog.credentials = { - :libvirt_uri => 'test:///default', -}.merge(Fog.credentials) +Fog.credentials[:libvirt_uri] = ENV.fetch("FOG_LIBVIRT_URI", "test:///default") + +def real_libvirt? + Fog.credentials[:libvirt_uri].start_with?("qemu") +end + +enable_mocking = ENV.fetch("FOG_MOCK") { (!real_libvirt?).to_s } +Fog.mock! unless ["false", "no", "0"].include?(enable_mocking) diff --git a/tests/helper.rb b/tests/helper.rb index ad2fd77..fe9de75 100644 --- a/tests/helper.rb +++ b/tests/helper.rb @@ -21,3 +21,7 @@ else FOG_TESTING_TIMEOUT = Fog.timeout end + +Thread.current[:tags] ||= [] +Thread.current[:tags] << "-requires-mocks" unless Fog.mock? +Thread.current[:tags] << (real_libvirt? ? "-requires-test-driver" : "-requires-real-libvirt") diff --git a/tests/helpers/mock_helper.rb b/tests/helpers/mock_helper.rb index 5942e0e..ffa59fc 100644 --- a/tests/helpers/mock_helper.rb +++ b/tests/helpers/mock_helper.rb @@ -2,13 +2,11 @@ # # FOG_MOCK=true fog -if ENV["FOG_MOCK"] == "true" - Fog.mock! -end +Fog.credentials[:libvirt_uri] = ENV.fetch("FOG_LIBVIRT_URI", "test:///default") -# if in mocked mode, fill in some fake credentials for us -if Fog.mock? - Fog.credentials = { - :libvirt_uri => 'test:///default', - }.merge(Fog.credentials) +def real_libvirt? + Fog.credentials[:libvirt_uri].start_with?("qemu") end + +enable_mocking = ENV.fetch("FOG_MOCK") { (!real_libvirt?).to_s } +Fog.mock! unless ["false", "no", "0"].include?(enable_mocking) diff --git a/tests/libvirt/models/compute/network_tests.rb b/tests/libvirt/models/compute/network_tests.rb index a83886d..81b2358 100644 --- a/tests/libvirt/models/compute/network_tests.rb +++ b/tests/libvirt/models/compute/network_tests.rb @@ -1,14 +1,13 @@ Shindo.tests('Fog::Compute[:libvirt] | network model', ['libvirt']) do - networks = Fog::Compute[:libvirt].networks - network = networks.first + network = Fog::Compute[:libvirt].networks.new(:name => "default", :uuid => "dd8fe884-6c02-601e-7551-cca97df1c5df", :bridge_name => "virbr0") tests('The network model should') do tests('have the action') do test('reload') { network.respond_to? 'reload' } test('dhcp_leases') { network.respond_to? 'dhcp_leases' } end - tests('have a dhcp_leases action that') do + tests('have a dhcp_leases action that', 'requires-mocks') do test('returns an array') { network.dhcp_leases('aa:bb:cc:dd:ee:ff', 0).kind_of? Array } end tests('have attributes') do diff --git a/tests/libvirt/models/compute/networks_tests.rb b/tests/libvirt/models/compute/networks_tests.rb index e01226e..3bc180a 100644 --- a/tests/libvirt/models/compute/networks_tests.rb +++ b/tests/libvirt/models/compute/networks_tests.rb @@ -5,7 +5,7 @@ tests('The networks collection') do test('should be a kind of Fog::Libvirt::Compute::Networks') { networks.kind_of? Fog::Libvirt::Compute::Networks } tests('should be able to reload itself').succeeds { networks.reload } - tests('should be able to get a model') do + tests('should be able to get a model', 'requires-test-driver') do tests('by instance id').succeeds { networks.get networks.first.uuid } end end diff --git a/tests/libvirt/models/compute/nic_tests.rb b/tests/libvirt/models/compute/nic_tests.rb index f2d01e6..61882a3 100644 --- a/tests/libvirt/models/compute/nic_tests.rb +++ b/tests/libvirt/models/compute/nic_tests.rb @@ -1,6 +1,6 @@ Shindo.tests('Fog::Compute[:libvirt] | nic model', ['libvirt']) do - server = Fog::Compute[:libvirt].servers.create(:name => Fog::Mock.random_letters(8)) + server = Fog::Compute[:libvirt].servers.create(:name => "fog-nic-test-#{Fog::Mock.random_letters(8)}") nic = server.nics.first tests('The nic model should') do @@ -28,5 +28,6 @@ end test('be a kind of Fog::Libvirt::Compute::Nic') { nic.kind_of? Fog::Libvirt::Compute::Nic } end - +ensure + server&.destroy(:destroy_volumes => true) end diff --git a/tests/libvirt/models/compute/server_tests.rb b/tests/libvirt/models/compute/server_tests.rb index 10db92c..cae1cd8 100644 --- a/tests/libvirt/models/compute/server_tests.rb +++ b/tests/libvirt/models/compute/server_tests.rb @@ -2,8 +2,8 @@ servers = Fog::Compute[:libvirt].servers # Match the mac in dhcp_leases mock - nics = Fog.mock? ? [{ :type => 'network', :network => 'default', :mac => 'aa:bb:cc:dd:ee:ff' }] : nil - server = servers.create(:name => Fog::Mock.random_letters(8), :nics => nics) + nics = Fog.mock? ? [{ :type => 'network', :network => 'default', :mac => 'aa:bb:cc:dd:ee:ff' }] : [] + server = servers.create(:name => "fog-server-test-#{Fog::Mock.random_letters(8)}", :nics => nics) tests('The server model should') do tests('have the action') do @@ -24,7 +24,7 @@ } end end - tests('have an ip_address action that') do + tests('have an ip_address action that', 'requires-mocks') do test('returns the latest IP address lease') { server.public_ip_address() == '1.2.5.6' } end tests('have attributes') do @@ -66,7 +66,9 @@ end test('can destroy') do - servers.create(:name => Fog::Mock.random_letters(8)).destroy + server_name = "fog-test-destroy-#{Fog::Mock.random_letters(8)}" + servers.create(:name => server_name).destroy(:destroy_volumes => true) + servers.all.none? { |server| server.name == server_name } end test('be a kind of Fog::Libvirt::Compute::Server') { server.kind_of? Fog::Libvirt::Compute::Server } @@ -78,7 +80,7 @@ xml.match?(//) && xml.match?(%r{}) end test("with disk of type block") do - server = Fog::Libvirt::Compute::Server.new( + disk_server = Fog::Libvirt::Compute::Server.new( { :nics => [], :volumes => [ @@ -86,20 +88,20 @@ ] } ) - xml = server.to_xml + xml = disk_server.to_xml xml.match?(//) && xml.match?(%r{}) end - test("with q35 machine type on x86_64") { server.to_xml.match?(%r{hvm}) } + test("with q35 machine type on x86_64") { server.to_xml.include?('') } end test("with efi firmware") do - server = Fog::Libvirt::Compute::Server.new( + efi_server = Fog::Libvirt::Compute::Server.new( { :firmware => "efi", :nics => [], :volumes => [] } ) - xml = server.to_xml + xml = efi_server.to_xml os_firmware = xml.include?('') secure_boot = xml.include?('') @@ -108,7 +110,7 @@ os_firmware && secure_boot && loader_attributes end test("with secure boot enabled") do - server = Fog::Libvirt::Compute::Server.new( + secure_server = Fog::Libvirt::Compute::Server.new( { :firmware => "efi", :firmware_features => { @@ -120,7 +122,7 @@ :volumes => [] } ) - xml = server.to_xml + xml = secure_server.to_xml os_firmware = xml.include?('') secure_boot = xml.include?('') @@ -135,7 +137,7 @@ :persistent => true, :xml => <<~XML - lvm-pool + fog-test-lvm-pool vg_storage01 @@ -146,7 +148,7 @@ XML ) - server = Fog::Compute[:libvirt].servers.new( + volume_server = Fog::Compute[:libvirt].servers.new( :nics => [], :volumes => [ { @@ -155,13 +157,18 @@ } ] ) - server.volumes.each do |volume| + volume_server.volumes.each do |volume| volume.save # mock driver doesn't simulate the real thing # LVM doesn't have a volume type volume.format_type = nil end - !server.save.nil? + !volume_server.save.nil? + ensure + volume_server&.destroy(:destroy_volumes => true) + pool&.destroy end end +ensure + server&.destroy(:destroy_volumes => true) end diff --git a/tests/libvirt/models/compute/volume_tests.rb b/tests/libvirt/models/compute/volume_tests.rb index 4515941..99b5b61 100644 --- a/tests/libvirt/models/compute/volume_tests.rb +++ b/tests/libvirt/models/compute/volume_tests.rb @@ -1,6 +1,6 @@ Shindo.tests('Fog::Compute[:libvirt] | volume model', ['libvirt']) do - volume = Fog::Compute[:libvirt].volumes.create(:name => 'fog_test') + volume = Fog::Compute[:libvirt].volumes.create(:name => 'fog_test', :capacity => '1G') tests('The volume model should') do tests('have attributes') do @@ -29,10 +29,12 @@ tests('Cloning volumes should') do test('respond to clone_volume') { volume.respond_to? :clone_volume } - new_vol = volume.clone_volume('new_vol') + new_vol = volume.clone_volume('fog-test-new_vol') # We'd like to test that the :name attr has changed, but it seems that's # not possible, so we can at least check the new_vol xml exists properly test('succeed') { volume.xml == new_vol.xml } + ensure + new_vol&.destroy end test('to_xml') do @@ -42,7 +44,7 @@ fog_test 1 - 10 + 1 @@ -55,4 +57,6 @@ volume.to_xml == expected end end +ensure + volume&.destroy end diff --git a/tests/libvirt/models/compute/volumes_tests.rb b/tests/libvirt/models/compute/volumes_tests.rb index 5e644a5..2b8e0c3 100644 --- a/tests/libvirt/models/compute/volumes_tests.rb +++ b/tests/libvirt/models/compute/volumes_tests.rb @@ -2,7 +2,7 @@ volumes = Fog::Compute[:libvirt].volumes - volumes.create(:name => 'test') + created_volume = volumes.create(:name => 'fog-test-volume') tests('The volumes collection') do test('should not be empty') { not volumes.empty? } @@ -11,7 +11,8 @@ tests('should be able to get a model') do tests('by instance uuid').succeeds { volumes.get volumes.first.id } end - test('filtered should be empty') { volumes.all(:name => "does-not-exist").empty? } + test('filtered should be empty') { volumes.all(:name => "fog-test-volume-does-not-exist").empty? } end - +ensure + created_volume&.destroy end diff --git a/tests/libvirt/requests/compute/create_domain_tests.rb b/tests/libvirt/requests/compute/create_domain_tests.rb index 0f1cfe9..30261d8 100644 --- a/tests/libvirt/requests/compute/create_domain_tests.rb +++ b/tests/libvirt/requests/compute/create_domain_tests.rb @@ -1,7 +1,9 @@ Shindo.tests("Fog::Compute[:libvirt] | create_domain request", 'libvirt') do compute = Fog::Compute[:libvirt] - xml = compute.servers.new( :nics => [{:bridge => "br180"}]).to_xml + + server = compute.servers.new(:name => "fog-test-create-server", :nics => []) + xml = server.to_xml tests("Create Domain") do response = compute.create_domain(xml) @@ -17,5 +19,7 @@ test("error should be a kind of Libvirt::Error") { e.kind_of? Libvirt::Error} end end - +ensure + server_uuid = compute.servers.all(:name => server.name).first&.uuid + compute.servers.service.vm_action(server_uuid, :destroy) if server_uuid end diff --git a/tests/libvirt/requests/compute/define_domain_tests.rb b/tests/libvirt/requests/compute/define_domain_tests.rb index f581642..814acd5 100644 --- a/tests/libvirt/requests/compute/define_domain_tests.rb +++ b/tests/libvirt/requests/compute/define_domain_tests.rb @@ -1,11 +1,14 @@ Shindo.tests("Fog::Compute[:libvirt] | define_domain request", 'libvirt') do compute = Fog::Compute[:libvirt] - xml = compute.servers.new().to_xml + server = compute.servers.new(:name => "fog-test-define-server", :nics => []) + xml = server.to_xml tests("Define Domain") do response = compute.define_domain(xml) test("should be a kind of Libvirt::Domain") { response.kind_of? Libvirt::Domain} end - +ensure + server_uuid = compute.servers.all(:name => server.name).first&.uuid + compute.servers.service.vm_action(server_uuid, :undefine) if server_uuid end diff --git a/tests/libvirt/requests/compute/dhcp_leases_tests.rb b/tests/libvirt/requests/compute/dhcp_leases_tests.rb index 78f5f36..f143b7e 100644 --- a/tests/libvirt/requests/compute/dhcp_leases_tests.rb +++ b/tests/libvirt/requests/compute/dhcp_leases_tests.rb @@ -2,7 +2,7 @@ compute = Fog::Compute[:libvirt] - tests("DHCP leases response") do + tests("DHCP leases response", "requires-mocks") do response = compute.dhcp_leases("fbd4ac68-cbea-4f95-86ed-22953fd92384", "99:88:77:66:55:44", 0) test("should be an array") { response.kind_of? Array } test("should have one element") { response.length == 1 } diff --git a/tests/libvirt/requests/compute/list_pools_tests.rb b/tests/libvirt/requests/compute/list_pools_tests.rb index 2a52a2b..c5e97e2 100644 --- a/tests/libvirt/requests/compute/list_pools_tests.rb +++ b/tests/libvirt/requests/compute/list_pools_tests.rb @@ -41,7 +41,9 @@ def persistent? tests("Lists Pools") do response = compute.list_pools test("should be an array") { response.kind_of? Array } - test("should have two pools") { response.length == 1 } + tests("Test Driver", "requires-test-driver") do + test("should have a pool") { response.length == 1 } + end end tests("Handle Inactive Pools") do