From cb6c1cb799f07c9ddba0c7767951922b82eacf73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FEREZ=20Ga=C3=ABtan?= Date: Tue, 24 Dec 2024 11:11:34 +0100 Subject: [PATCH 1/3] Implement Ceph multiple pools and tests --- lib/fog/libvirt/models/compute/README.md | 2 +- lib/fog/libvirt/models/compute/server.rb | 23 +++++--- tests/libvirt/models/compute/server_tests.rb | 60 ++++++++++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/lib/fog/libvirt/models/compute/README.md b/lib/fog/libvirt/models/compute/README.md index d8e1eec..337d9fa 100644 --- a/lib/fog/libvirt/models/compute/README.md +++ b/lib/fog/libvirt/models/compute/README.md @@ -32,7 +32,7 @@ After adding the authentication key to a libvirt secret, it can be configured as ``` monitor=mon001.example.com,mon002.example.com,mon003.example.com port=6789 -libvirt_ceph_pool=rbd_pool_name +libvirt_ceph_pools=rbd_pool_name,second_rbd_pool_name auth_username=libvirt auth_uuid=uuid_of_libvirt_secret bus_type=virtio diff --git a/lib/fog/libvirt/models/compute/server.rb b/lib/fog/libvirt/models/compute/server.rb index 4745ea6..a94dc72 100644 --- a/lib/fog/libvirt/models/compute/server.rb +++ b/lib/fog/libvirt/models/compute/server.rb @@ -346,13 +346,13 @@ def to_xml volumes.each_with_index do |volume, index| target_device = "vd#{('a'..'z').to_a[index]}" - if ceph_args && volume.pool_name.include?(ceph_args["libvirt_ceph_pool"]) + if ceph_args && ceph_args["libvirt_ceph_pools"]&.include?(volume.pool_name) xml.disk(:type => "network", :device => "disk") do xml.driver(:name => "qemu", :type => volume.format_type, :cache => "writeback", :discard => "unmap") - xml.source(:protocol => "rbd", :name => volume.path) - - ceph_args["monitor"]&.split(",")&.each do |monitor| - xml.host(:name => monitor, :port => ceph_args["port"]) + xml.source(:protocol => "rbd", :name => volume.path) do + ceph_args["monitor"]&.each do |monitor| + xml.host(:name => monitor, :port => ceph_args["port"]) + end end xml.auth(:username => ceph_args["auth_username"]) do @@ -458,13 +458,20 @@ def read_ceph_args(path = "/etc/foreman/ceph.conf") args = {} + valid_keys = ["monitor", "port", "libvirt_ceph_pools", "auth_username", "auth_uuid", "bus_type"] + array_values = ["monitor", "libvirt_ceph_pools"] + File.readlines(path).each do |line| pair = line.strip.split("=") - key = pair[0] - value = pair[1] - args[key] = value + key = pair[0].strip + if valid_keys.include?(key) + value = array_values.include?(key) ? pair[1].split(',').map(&:strip) : pair[1].strip + args[key] = value + end end + puts args + args end diff --git a/tests/libvirt/models/compute/server_tests.rb b/tests/libvirt/models/compute/server_tests.rb index 448baef..1bb2d04 100644 --- a/tests/libvirt/models/compute/server_tests.rb +++ b/tests/libvirt/models/compute/server_tests.rb @@ -1,3 +1,29 @@ +RealFile = File +class FakeFile < RealFile + def self.file?(path) + if path == '/etc/foreman/ceph.conf' + return true + else + return RealFile.file(path) + end + end + + def self.readlines(path) + if path == '/etc/foreman/ceph.conf' + return [ + "monitor=mon001.example.com,mon002.example.com,mon003.example.com", + "port=6789", + "libvirt_ceph_pools=rbd_pool_name,second_rbd_pool_name", + "auth_username=libvirt", + "auth_uuid=uuid_of_libvirt_secret", + "bus_type=virtio" + ] + else + return RealFile.readlines(path) + end + end +end + Shindo.tests('Fog::Compute[:libvirt] | server model', ['libvirt']) do servers = Fog::Compute[:libvirt].servers @@ -5,6 +31,20 @@ 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) + before do + Object.class_eval do + remove_const(:File) + const_set(:File, FakeFile) + end + end + + after do + Object.class_eval do + remove_const(:File) + const_set(:File, RealFile) + end + end + tests('The server model should') do tests('have the action') do test('autostart') { server.respond_to? 'autostart' } @@ -89,6 +129,26 @@ xml = server.to_xml xml.match?(//) && xml.match?(%r{}) end + test("with disk of type ceph") do + server = Fog::Libvirt::Compute::Server.new( + { + :nics => [], + :volumes => [ + Fog::Libvirt::Compute::Volume.new({ :path => "rbd_pool_name/block-1", :pool_name => "rbd_pool_name" }) + ] + } + ) + + xml = server.to_xml + + network_disk = xml.match?(//) + mon_host = xml.match?(%r{}) + source_rbd = xml.match?(%r{}) + auth_username = xml.match?(//) + auth_secret = xml.match?(%r{}) + + network_disk && mon_host && source_rbd && auth_username && auth_secret + end test("with q35 machine type on x86_64") { server.to_xml.match?(%r{hvm}) } end test("with efi firmware") do From a2a8799514657b593bbf7a15950fb33e1e27de9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FEREZ=20Ga=C3=ABtan?= Date: Fri, 10 Jan 2025 10:38:15 +0100 Subject: [PATCH 2/3] Handle libvirt_ceph_pool as a complement of libvirt_ceph_pools --- lib/fog/libvirt/models/compute/server.rb | 7 +++++-- tests/libvirt/models/compute/server_tests.rb | 15 +++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/fog/libvirt/models/compute/server.rb b/lib/fog/libvirt/models/compute/server.rb index a94dc72..f35cf11 100644 --- a/lib/fog/libvirt/models/compute/server.rb +++ b/lib/fog/libvirt/models/compute/server.rb @@ -458,7 +458,7 @@ def read_ceph_args(path = "/etc/foreman/ceph.conf") args = {} - valid_keys = ["monitor", "port", "libvirt_ceph_pools", "auth_username", "auth_uuid", "bus_type"] + valid_keys = ["monitor", "port", "libvirt_ceph_pools", "libvirt_ceph_pool", "auth_username", "auth_uuid", "bus_type"] array_values = ["monitor", "libvirt_ceph_pools"] File.readlines(path).each do |line| @@ -470,7 +470,10 @@ def read_ceph_args(path = "/etc/foreman/ceph.conf") end end - puts args + if args.has_key?("libvirt_ceph_pool") + args.has_key?("libvirt_ceph_pools") ? args["libvirt_ceph_pools"] << args["libvirt_ceph_pool"] : args["libvirt_ceph_pools"] = [args["libvirt_ceph_pool"]] + args.delete("libvirt_ceph_pool") + end args end diff --git a/tests/libvirt/models/compute/server_tests.rb b/tests/libvirt/models/compute/server_tests.rb index 1bb2d04..526932c 100644 --- a/tests/libvirt/models/compute/server_tests.rb +++ b/tests/libvirt/models/compute/server_tests.rb @@ -1,11 +1,7 @@ RealFile = File class FakeFile < RealFile def self.file?(path) - if path == '/etc/foreman/ceph.conf' - return true - else - return RealFile.file(path) - end + path == '/etc/foreman/ceph.conf' || RealFile.file(path) end def self.readlines(path) @@ -14,6 +10,7 @@ def self.readlines(path) "monitor=mon001.example.com,mon002.example.com,mon003.example.com", "port=6789", "libvirt_ceph_pools=rbd_pool_name,second_rbd_pool_name", + "libvirt_ceph_pool=third_rbd_pool_name", "auth_username=libvirt", "auth_uuid=uuid_of_libvirt_secret", "bus_type=virtio" @@ -134,7 +131,8 @@ def self.readlines(path) { :nics => [], :volumes => [ - Fog::Libvirt::Compute::Volume.new({ :path => "rbd_pool_name/block-1", :pool_name => "rbd_pool_name" }) + Fog::Libvirt::Compute::Volume.new({ :path => "rbd_pool_name/block-1", :pool_name => "rbd_pool_name" }), + Fog::Libvirt::Compute::Volume.new({ :path => "third_rbd_pool_name/block-2", :pool_name => "third_rbd_pool_name" }) ] } ) @@ -143,11 +141,12 @@ def self.readlines(path) network_disk = xml.match?(//) mon_host = xml.match?(%r{}) - source_rbd = xml.match?(%r{}) + source_block1_rbd = xml.match?(%r{}) + source_block2_rbd = xml.match?(%r{}) auth_username = xml.match?(//) auth_secret = xml.match?(%r{}) - network_disk && mon_host && source_rbd && auth_username && auth_secret + network_disk && mon_host && source_block1_rbd && source_block2_rbd && auth_username && auth_secret end test("with q35 machine type on x86_64") { server.to_xml.match?(%r{hvm}) } end From 60db5066480e6da16b476fd200bf4234dc0b5977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FEREZ=20Ga=C3=ABtan?= Date: Fri, 10 Jan 2025 13:08:51 +0100 Subject: [PATCH 3/3] Handle libvirt_ceph_pool only if libvirt_ceph_pools not defined --- lib/fog/libvirt/models/compute/server.rb | 4 ++-- tests/libvirt/models/compute/server_tests.rb | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/fog/libvirt/models/compute/server.rb b/lib/fog/libvirt/models/compute/server.rb index f35cf11..e08b050 100644 --- a/lib/fog/libvirt/models/compute/server.rb +++ b/lib/fog/libvirt/models/compute/server.rb @@ -470,8 +470,8 @@ def read_ceph_args(path = "/etc/foreman/ceph.conf") end end - if args.has_key?("libvirt_ceph_pool") - args.has_key?("libvirt_ceph_pools") ? args["libvirt_ceph_pools"] << args["libvirt_ceph_pool"] : args["libvirt_ceph_pools"] = [args["libvirt_ceph_pool"]] + if args.has_key?("libvirt_ceph_pool") && !args.has_key?("libvirt_ceph_pools") + args["libvirt_ceph_pools"] = [args["libvirt_ceph_pool"]] args.delete("libvirt_ceph_pool") end diff --git a/tests/libvirt/models/compute/server_tests.rb b/tests/libvirt/models/compute/server_tests.rb index 526932c..dd82e95 100644 --- a/tests/libvirt/models/compute/server_tests.rb +++ b/tests/libvirt/models/compute/server_tests.rb @@ -10,7 +10,6 @@ def self.readlines(path) "monitor=mon001.example.com,mon002.example.com,mon003.example.com", "port=6789", "libvirt_ceph_pools=rbd_pool_name,second_rbd_pool_name", - "libvirt_ceph_pool=third_rbd_pool_name", "auth_username=libvirt", "auth_uuid=uuid_of_libvirt_secret", "bus_type=virtio" @@ -132,7 +131,7 @@ def self.readlines(path) :nics => [], :volumes => [ Fog::Libvirt::Compute::Volume.new({ :path => "rbd_pool_name/block-1", :pool_name => "rbd_pool_name" }), - Fog::Libvirt::Compute::Volume.new({ :path => "third_rbd_pool_name/block-2", :pool_name => "third_rbd_pool_name" }) + Fog::Libvirt::Compute::Volume.new({ :path => "rbd_pool_name/block-2", :pool_name => "rbd_pool_name" }) ] } ) @@ -142,7 +141,7 @@ def self.readlines(path) network_disk = xml.match?(//) mon_host = xml.match?(%r{}) source_block1_rbd = xml.match?(%r{}) - source_block2_rbd = xml.match?(%r{}) + source_block2_rbd = xml.match?(%r{}) auth_username = xml.match?(//) auth_secret = xml.match?(%r{})