From b24712944157caad1bad599ba9184492763bf6b9 Mon Sep 17 00:00:00 2001 From: Manisha Date: Mon, 23 Mar 2020 07:10:24 +0100 Subject: [PATCH] Proxmox extention for foreman_bootdisk --- .rubocop.yml | 3 ++ .../compute_resources/proxmox.rb | 19 +++++++---- test/factories/proxmox_factory.rb | 31 +++++++++++++++++ test/test_plugin_helper.rb | 3 ++ .../compute_resources/proxmox_test.rb | 33 +++++++++++++++++++ test/unit/concerns/host_test.rb | 2 +- .../concerns/orchestration/compute_test.rb | 15 +++++++++ 7 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 test/factories/proxmox_factory.rb create mode 100644 test/unit/concerns/compute_resources/proxmox_test.rb diff --git a/.rubocop.yml b/.rubocop.yml index 5f7fb49b..6b848112 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,6 +16,9 @@ Rails: Style: Enabled: false +Layout/LineLength: + Enabled: false + Metrics: Enabled: false diff --git a/app/models/concerns/foreman_bootdisk/compute_resources/proxmox.rb b/app/models/concerns/foreman_bootdisk/compute_resources/proxmox.rb index d28477a9..a02c6617 100644 --- a/app/models/concerns/foreman_bootdisk/compute_resources/proxmox.rb +++ b/app/models/concerns/foreman_bootdisk/compute_resources/proxmox.rb @@ -9,18 +9,23 @@ def capabilities def iso_upload(iso, vm_uuid) server = find_vm_by_uuid(vm_uuid) - server.ssh_options={password: fog_credentials[:pve_password]} - server.ssh_ip_address= bridges.first.address - server.scp_upload(iso,'/var/lib/vz/template/iso/') + config_attributes = { + 'bootdisk' => 'ide2', + 'boot' => 'dcn' + } + server.update(config_attributes) + server.ssh_options = { password: fog_credentials[:pve_password] } + server.ssh_ip_address = bridges.first.address + server.scp_upload(iso, '/var/lib/vz/template/iso/') server.reload - storage = storages(type='iso')[0] - storage.volumes.any? {|v| v.volid.include? File.basename(iso)} + storage = storages(type = 'iso')[0] + storage.volumes.any? { |v| v.volid.include? File.basename(iso) } end def iso_attach(iso, vm_uuid) server = find_vm_by_uuid(vm_uuid) - storage = storages(type='iso')[0] - volume = storage.volumes.detect {|v| v.volid.include? File.basename(iso)} + storage = storages(type = 'iso')[0] + volume = storage.volumes.detect { |v| v.volid.include? File.basename(iso) } config_hash = { ide2: "#{volume.volid},media=cdrom" } server.update(config_hash) end diff --git a/test/factories/proxmox_factory.rb b/test/factories/proxmox_factory.rb new file mode 100644 index 00000000..e762f600 --- /dev/null +++ b/test/factories/proxmox_factory.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'fog/compute/proxmox/models/node' + +FactoryBot.define do + factory :proxmox_resource, :class => ComputeResource do + sequence(:name) { |n| "compute_resource#{n}" } + organizations { [Organization.find_by(name: 'Organization 1')] } + locations { [Location.find_by(name: 'Location 1')] } + + trait :proxmox do + provider { 'Proxmox' } + user { 'root@pam' } + password { 'proxmox01' } + url { 'https://192.168.56.101:8006/api2/json' } + end + + after(:build) do + WebMock.stub_request(:post, "https://192.168.56.101:8006/api2/json/access/ticket") + .with( + body: "username=root%40pam&password=proxmox01", + headers: { + 'Accept' => 'application/json', + 'Host' => '192.168.56.101:8006', + 'User-Agent' => 'fog-core/2.1.0' + } + ).to_return(status: 200, body: "", headers: {}) + end + factory :proxmox_cr, :class => ForemanFogProxmox::Proxmox, :traits => [:proxmox] + end +end diff --git a/test/test_plugin_helper.rb b/test/test_plugin_helper.rb index f373e067..f0aec1b5 100644 --- a/test/test_plugin_helper.rb +++ b/test/test_plugin_helper.rb @@ -4,6 +4,9 @@ require 'webmock/minitest' require 'webmock' +FactoryBot.definition_file_paths << File.join(File.dirname(__FILE__), 'factories') +FactoryBot.reload + module ForemanBootdiskTestHelper def setup_bootdisk setup_routes diff --git a/test/unit/concerns/compute_resources/proxmox_test.rb b/test/unit/concerns/compute_resources/proxmox_test.rb new file mode 100644 index 00000000..4d2e98d2 --- /dev/null +++ b/test/unit/concerns/compute_resources/proxmox_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'test_plugin_helper' + +module ForemanBootdisk + class ProxmoxTest < ActiveSupport::TestCase + describe '#new_vm' do + setup do + @cr = FactoryBot.build(:proxmox_cr) + end + + test 'calls client with cdrom drive and correct boot order when bootdisk provisioning' do + attr = { 'boot' => 'dcn', 'bootdisk' => 'ide2' } + vm = mock('vm') + config = mock('config') + config.stubs(:inspect).returns('config') + vm.stubs(:config).returns(config) + @cr.stubs(:new_server_vm).with(attr).returns(vm) + assert_equal vm, @cr.new_vm(attr) + end + end + + describe '#capabilities' do + setup do + @cr = FactoryBot.build(:proxmox_cr) + end + + test 'should include bootdisk' do + assert_includes @cr.capabilities, :bootdisk + end + end + end +end diff --git a/test/unit/concerns/host_test.rb b/test/unit/concerns/host_test.rb index 19b53bef..5ab9ebe0 100644 --- a/test/unit/concerns/host_test.rb +++ b/test/unit/concerns/host_test.rb @@ -36,7 +36,7 @@ class HostTest < ActiveSupport::TestCase test 'host should not have bootdisk' do if unattended? h = FactoryBot.create(:host) - assert_equal false, h.bootdisk? + assert_not h.bootdisk? end end diff --git a/test/unit/concerns/orchestration/compute_test.rb b/test/unit/concerns/orchestration/compute_test.rb index e3b47f62..820c9610 100644 --- a/test/unit/concerns/orchestration/compute_test.rb +++ b/test/unit/concerns/orchestration/compute_test.rb @@ -10,6 +10,16 @@ class OrchestrationComputeTest < ActiveSupport::TestCase @host = FactoryBot.build(:host, :managed, compute_resource: @cr, provision_method: 'bootdisk') + + @proxmox_cr = FactoryBot.build(:proxmox_cr) + @proxmox_host = FactoryBot.build(:host, :managed, + compute_resource: @proxmox_cr, + provision_method: 'bootdisk') + end + + test 'provisioning a host with provision method bootdisk in proxmox should upload iso' do + @proxmox_cr.expects(:iso_upload) + @proxmox_host.send(:setIsoImage) end test 'provisioning a host with provision method bootdisk should upload iso' do @@ -17,6 +27,11 @@ class OrchestrationComputeTest < ActiveSupport::TestCase @host.send(:setIsoImage) end + test 'provisioning a host with provision method bootdisk in proxmox should attach iso' do + @proxmox_cr.expects(:iso_attach) + @proxmox_host.send(:setAttachIsoImage) + end + test 'provisioning a host with provision method bootdisk should attach iso' do @cr.expects(:iso_attach) @host.send(:setAttachIsoImage)