Skip to content

Commit

Permalink
Proxmox extention for foreman_bootdisk
Browse files Browse the repository at this point in the history
  • Loading branch information
Manisha15 committed Apr 6, 2020
1 parent 6353a53 commit c78d8a0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Rails:
Style:
Enabled: false

Layout/LineLength:
Enabled: false

Metrics:
Enabled: false

Expand Down
19 changes: 12 additions & 7 deletions app/models/concerns/foreman_bootdisk/compute_resources/proxmox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/factories/proxmox_factory.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/test_plugin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/unit/concerns/compute_resources/proxmox_test.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/unit/concerns/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions test/unit/concerns/orchestration/compute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@ 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
@cr.expects(:iso_upload)
@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)
Expand Down

0 comments on commit c78d8a0

Please sign in to comment.