foreman_fog_proxmox 0.8.0 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of foreman_fog_proxmox might be problematic. Click here for more details.

Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -1
  3. data/Rakefile +3 -1
  4. data/app/controllers/concerns/foreman_fog_proxmox/controller/parameters/compute_resource.rb +2 -2
  5. data/app/controllers/foreman_fog_proxmox/compute_resources_controller.rb +2 -2
  6. data/app/helpers/node_dashboard_helper.rb +13 -14
  7. data/app/helpers/proxmox_compute_selectors_helper.rb +49 -50
  8. data/app/helpers/proxmox_container_helper.rb +42 -36
  9. data/app/helpers/proxmox_form_helper.rb +38 -40
  10. data/app/helpers/proxmox_server_helper.rb +39 -41
  11. data/app/helpers/proxmox_vm_helper.rb +21 -25
  12. data/app/models/concerns/fog_extensions/proxmox/disk.rb +8 -8
  13. data/app/models/concerns/fog_extensions/proxmox/interface.rb +8 -8
  14. data/app/models/concerns/fog_extensions/proxmox/node.rb +24 -22
  15. data/app/models/concerns/fog_extensions/proxmox/server.rb +71 -59
  16. data/app/models/concerns/fog_extensions/proxmox/server_config.rb +47 -39
  17. data/app/models/concerns/fog_extensions/proxmox/volume.rb +8 -8
  18. data/app/models/concerns/host_ext/proxmox/interfaces.rb +21 -13
  19. data/app/models/concerns/orchestration/proxmox/compute.rb +14 -10
  20. data/app/models/foreman_fog_proxmox/options_select.rb +14 -14
  21. data/app/models/foreman_fog_proxmox/proxmox.rb +30 -466
  22. data/app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb +54 -0
  23. data/app/models/foreman_fog_proxmox/proxmox_connection.rb +67 -0
  24. data/app/models/foreman_fog_proxmox/proxmox_console.rb +41 -0
  25. data/app/models/foreman_fog_proxmox/proxmox_images.rb +53 -0
  26. data/app/models/foreman_fog_proxmox/proxmox_interfaces.rb +60 -0
  27. data/app/models/foreman_fog_proxmox/proxmox_operating_systems.rb +49 -0
  28. data/app/models/foreman_fog_proxmox/proxmox_token_expiration.rb +30 -0
  29. data/app/models/foreman_fog_proxmox/proxmox_version.rb +36 -0
  30. data/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb +108 -0
  31. data/app/models/foreman_fog_proxmox/proxmox_vm_new.rb +162 -0
  32. data/app/models/foreman_fog_proxmox/proxmox_vm_queries.rb +74 -0
  33. data/app/models/foreman_fog_proxmox/proxmox_volumes.rb +85 -0
  34. data/app/overrides/compute_resources_vms/form/add_clone_to_new_vm_compute_detail.rb +7 -6
  35. data/app/overrides/compute_resources_vms/form/add_from_profile_to_compute_attributes_form.rb +13 -11
  36. data/app/overrides/compute_resources_vms/form/add_vm_type_to_networks_form.rb +13 -11
  37. data/app/overrides/compute_resources_vms/form/add_vm_type_to_nic_provider_specific_form.rb +8 -7
  38. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_edit.rb +7 -6
  39. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_new_volume.rb +7 -6
  40. data/app/overrides/compute_resources_vms/form/remove_new_vm_from_removable_layout.rb +7 -6
  41. data/app/services/foreman_fog_proxmox/node_dashboard/data.rb +16 -16
  42. data/app/views/api/v2/compute_resources/proxmox.json.rabl +3 -1
  43. data/app/views/compute_resources/form/_proxmox.html.erb +3 -0
  44. data/app/views/compute_resources/show/_proxmox.html.erb +8 -0
  45. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_compute_attributes_form.html.erb +5 -0
  46. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_compute_form.html.erb +6 -1
  47. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_hosts_compute_detail_form.html.erb +6 -0
  48. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_form.html.erb +6 -2
  49. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_new_childs_form.html.erb +8 -2
  50. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_nic_provider_specific_form.html.erb +6 -0
  51. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_volumes_edit.html.erb +15 -1
  52. data/app/views/compute_resources_vms/form/proxmox/_base.html.erb +2 -2
  53. data/app/views/compute_resources_vms/form/proxmox/_general.html.erb +1 -1
  54. data/app/views/compute_resources_vms/form/proxmox/_removable_layout.html.erb +6 -1
  55. data/app/views/compute_resources_vms/form/proxmox/container/_extended.html.erb +1 -1
  56. data/app/views/compute_resources_vms/form/proxmox/container/_volume_mp.html.erb +1 -1
  57. data/app/views/compute_resources_vms/form/proxmox/server/_config.html.erb +1 -1
  58. data/app/views/compute_resources_vms/form/proxmox/server/_volume.html.erb +2 -2
  59. data/config/routes.rb +4 -4
  60. data/lib/foreman_fog_proxmox/engine.rb +14 -14
  61. data/lib/foreman_fog_proxmox/semver.rb +78 -78
  62. data/lib/foreman_fog_proxmox/value.rb +5 -3
  63. data/lib/foreman_fog_proxmox/version.rb +1 -1
  64. data/lib/tasks/foreman_fog_proxmox_tasks.rake +3 -13
  65. data/locale/en/foreman_fog_proxmox.edit.po +472 -0
  66. data/locale/en/foreman_fog_proxmox.po +48 -24
  67. data/locale/en/foreman_fog_proxmox.po.time_stamp +0 -0
  68. data/locale/foreman_fog_proxmox.pot +140 -102
  69. data/locale/fr/foreman_fog_proxmox.edit.po +472 -0
  70. data/locale/fr/foreman_fog_proxmox.po +52 -28
  71. data/locale/fr/foreman_fog_proxmox.po.time_stamp +0 -0
  72. data/locale/gemspec.rb +2 -0
  73. data/test/{unit/foreman_fog_proxmox/proxmox_test_helpers.rb → factories/foreman_fog_proxmox/proxmox_container_mock_factory.rb} +4 -140
  74. data/test/factories/foreman_fog_proxmox/proxmox_node_mock_factory.rb +61 -0
  75. data/test/factories/foreman_fog_proxmox/proxmox_server_mock_factory.rb +134 -0
  76. data/test/factories/proxmox_factory.rb +15 -15
  77. data/test/functional/compute_resources_controller_test.rb +12 -10
  78. data/test/test_plugin_helper.rb +2 -0
  79. data/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb +178 -182
  80. data/test/unit/foreman_fog_proxmox/helpers/proxmox_server_helper_test.rb +122 -126
  81. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_helper_test.rb +180 -187
  82. data/test/unit/foreman_fog_proxmox/proxmox_compute_attributes_test.rb +116 -0
  83. data/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb +71 -0
  84. data/test/unit/foreman_fog_proxmox/proxmox_test.rb +9 -479
  85. data/test/unit/foreman_fog_proxmox/proxmox_version_test.rb +82 -0
  86. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_container_test.rb +207 -0
  87. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_create_test.rb +92 -0
  88. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_test.rb +324 -0
  89. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_test.rb +43 -0
  90. data/test/unit/foreman_fog_proxmox/proxmox_vm_new_test.rb +71 -0
  91. data/test/unit/foreman_fog_proxmox/proxmox_vm_queries_test.rb +63 -0
  92. data/test/unit/foreman_fog_proxmox/semver_test.rb +67 -53
  93. metadata +52 -14
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'test_plugin_helper'
21
+ require 'models/compute_resources/compute_resource_test_helpers'
22
+ require 'factories/foreman_fog_proxmox/proxmox_node_mock_factory'
23
+ require 'factories/foreman_fog_proxmox/proxmox_server_mock_factory'
24
+ require 'factories/foreman_fog_proxmox/proxmox_container_mock_factory'
25
+ require 'active_support/core_ext/hash/indifferent_access'
26
+
27
+ module ForemanFogProxmox
28
+ class ProxmoxComputeAttributesTest < ActiveSupport::TestCase
29
+ include ComputeResourceTestHelpers
30
+ include ProxmoxNodeMockFactory
31
+ include ProxmoxServerMockFactory
32
+ include ProxmoxContainerMockFactory
33
+ include ProxmoxVmHelper
34
+
35
+ describe 'host_compute_attrs' do
36
+ before do
37
+ @cr = FactoryBot.build_stubbed(:proxmox_cr)
38
+ end
39
+
40
+ it 'raises Foreman::Exception when server ostype does not match os family' do
41
+ operatingsystem = FactoryBot.build(:solaris)
42
+ physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :primary => true)
43
+ host = FactoryBot.build(
44
+ :host_empty,
45
+ :interfaces => [physical_nic],
46
+ :operatingsystem => operatingsystem,
47
+ :compute_attributes => {
48
+ 'type' => 'qemu',
49
+ 'config_attributes' => {
50
+ 'ostype' => 'l26'
51
+ },
52
+ 'interfaces_attributes' => {
53
+ '0' => physical_nic
54
+ }
55
+ }
56
+ )
57
+ err = assert_raises Foreman::Exception do
58
+ @cr.host_compute_attrs(host)
59
+ end
60
+ assert err.message.end_with?('Operating system family Solaris is not consistent with l26')
61
+ end
62
+
63
+ it 'sets container hostname with host name' do
64
+ physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :primary => true)
65
+ host = FactoryBot.build(
66
+ :host_empty,
67
+ :interfaces => [physical_nic],
68
+ :compute_attributes => {
69
+ 'type' => 'lxc',
70
+ 'config_attributes' => {
71
+ 'hostname' => ''
72
+ },
73
+ 'interfaces_attributes' => {
74
+ '0' => {}
75
+ }
76
+ }
77
+ )
78
+ @cr.host_compute_attrs(host)
79
+ assert_equal host.name, host.compute_attributes['config_attributes']['hostname']
80
+ end
81
+ end
82
+
83
+ describe 'vm_compute_attributes' do
84
+ before do
85
+ @cr = FactoryBot.build_stubbed(:proxmox_cr)
86
+ end
87
+
88
+ it 'converts a server to hash' do
89
+ vm, config_attributes, volume_attributes, interface_attributes = mock_server_vm
90
+ vm_attrs = @cr.vm_compute_attributes(vm)
91
+ assert_not vm_attrs.key?(:config)
92
+ assert vm_attrs.key?(:config_attributes)
93
+ assert_equal config_attributes.reject { |key, value| [:vmid, :disks, :interfaces].include?(key) || value.to_s.empty? }, vm_attrs[:config_attributes]
94
+ assert_not vm_attrs[:config_attributes].key?(:disks)
95
+ assert vm_attrs.key?(:volumes_attributes)
96
+ assert_equal volume_attributes, vm_attrs[:volumes_attributes]['0']
97
+ assert_not vm_attrs[:config_attributes].key?(:interfaces)
98
+ assert vm_attrs.key?(:interfaces_attributes)
99
+ assert_equal interface_attributes, vm_attrs[:interfaces_attributes]['0']
100
+ end
101
+
102
+ it 'converts a container to hash' do
103
+ vm, config_attributes, volume_attributes, interface_attributes = mock_container_vm
104
+ vm_attrs = @cr.vm_compute_attributes(vm)
105
+ assert_not vm_attrs.key?(:config)
106
+ assert vm_attrs.key?(:config_attributes)
107
+ assert_equal config_attributes.reject { |key, value| [:vmid, :disks, :interfaces].include?(key) || value.to_s.empty? }, vm_attrs[:config_attributes]
108
+ assert_not vm_attrs[:config_attributes].key?(:disks)
109
+ assert vm_attrs.key?(:volumes_attributes)
110
+ assert_equal volume_attributes, vm_attrs[:volumes_attributes]['0']
111
+ assert vm_attrs.key?(:interfaces_attributes)
112
+ assert_equal interface_attributes, vm_attrs[:interfaces_attributes]['0']
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'test_plugin_helper'
21
+ require 'models/compute_resources/compute_resource_test_helpers'
22
+ require 'factories/foreman_fog_proxmox/proxmox_node_mock_factory'
23
+ require 'factories/foreman_fog_proxmox/proxmox_server_mock_factory'
24
+ require 'factories/foreman_fog_proxmox/proxmox_container_mock_factory'
25
+ require 'active_support/core_ext/hash/indifferent_access'
26
+
27
+ module ForemanFogProxmox
28
+ class ProxmoxInterfacesTest < ActiveSupport::TestCase
29
+ include ComputeResourceTestHelpers
30
+ include ProxmoxNodeMockFactory
31
+ include ProxmoxServerMockFactory
32
+ include ProxmoxContainerMockFactory
33
+ include ProxmoxVmHelper
34
+
35
+ describe 'host_interfaces_attrs' do
36
+ before do
37
+ @cr = FactoryBot.build_stubbed(:proxmox_cr)
38
+ end
39
+
40
+ it 'raises Foreman::Exception when physical identifier does not match net[k] with k integer' do
41
+ physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'eth0')
42
+ host = FactoryBot.build(:host_empty, :interfaces => [physical_nic])
43
+ err = assert_raises Foreman::Exception do
44
+ @cr.host_interfaces_attrs(host)
45
+ end
46
+ assert err.message.end_with?('Invalid identifier interface[0]. Must be net[n] with n integer >= 0')
47
+ end
48
+
49
+ it 'sets compute id with identifier, ip and ip6' do
50
+ ip = IPAddr.new(1, Socket::AF_INET).to_s
51
+ ip6 = Array.new(4) { format('%<x>s', x: rand(16**4)) }.join(':') + '::1'
52
+ physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :ip => ip, :ip6 => ip6)
53
+ host = FactoryBot.build(
54
+ :host_empty,
55
+ :interfaces => [physical_nic],
56
+ :compute_attributes => {
57
+ 'type' => 'qemu',
58
+ 'interfaces_attributes' => {
59
+ '0' => physical_nic
60
+ }
61
+ }
62
+ )
63
+ nic_attributes = @cr.host_interfaces_attrs(host).values.select(&:present?)
64
+ nic_attr = nic_attributes.first
65
+ assert_equal 'net0', nic_attr[:id]
66
+ assert_equal ip, nic_attr[:ip]
67
+ assert_equal ip6, nic_attr[:ip6]
68
+ end
69
+ end
70
+ end
71
+ end
@@ -19,13 +19,17 @@
19
19
 
20
20
  require 'test_plugin_helper'
21
21
  require 'models/compute_resources/compute_resource_test_helpers'
22
- require 'unit/foreman_fog_proxmox/proxmox_test_helpers'
22
+ require 'factories/foreman_fog_proxmox/proxmox_node_mock_factory'
23
+ require 'factories/foreman_fog_proxmox/proxmox_server_mock_factory'
24
+ require 'factories/foreman_fog_proxmox/proxmox_container_mock_factory'
25
+ require 'active_support/core_ext/hash/indifferent_access'
23
26
 
24
27
  module ForemanFogProxmox
25
-
26
28
  class ProxmoxTest < ActiveSupport::TestCase
27
29
  include ComputeResourceTestHelpers
28
- include ForemanFogProxmox::ProxmoxTestHelpers
30
+ include ProxmoxNodeMockFactory
31
+ include ProxmoxServerMockFactory
32
+ include ProxmoxContainerMockFactory
29
33
  include ProxmoxVmHelper
30
34
 
31
35
  should validate_presence_of(:url)
@@ -37,7 +41,7 @@ module ForemanFogProxmox
37
41
  should_not allow_value('a').for(:url)
38
42
  should allow_values('http://foo.com', 'http://bar.com/baz').for(:url)
39
43
 
40
- test "#associated_host matches any NIC" do
44
+ test '#associated_host matches any NIC' do
41
45
  mac = 'ca:d0:e6:32:16:97'
42
46
  host = FactoryBot.create(:host, :mac => mac)
43
47
  cr = FactoryBot.build_stubbed(:proxmox_cr)
@@ -45,485 +49,11 @@ module ForemanFogProxmox
45
49
  assert_equal host, (as_admin { cr.associated_host(vm) })
46
50
  end
47
51
 
48
- test "#node" do
52
+ test '#node' do
49
53
  node = mock('node')
50
54
  cr = FactoryBot.build_stubbed(:proxmox_cr)
51
55
  cr.stubs(:node).returns(node)
52
56
  assert_equal node, (as_admin { cr.node })
53
57
  end
54
-
55
- describe "destroy_vm" do
56
- it "handles situation when vm is not present" do
57
- cr = mock_cr_servers(ForemanFogProxmox::Proxmox.new, empty_servers)
58
- cr.expects(:find_vm_by_uuid).raises(ActiveRecord::RecordNotFound)
59
- assert cr.destroy_vm('abc')
60
- end
61
- end
62
-
63
- describe "find_vm_by_uuid" do
64
- it "returns nil when the uuid does not match" do
65
- cr = mock_node_servers_containers(ForemanFogProxmox::Proxmox.new, empty_servers, empty_servers)
66
- assert cr.find_vm_by_uuid('100').nil?
67
- end
68
-
69
- it "raises RecordNotFound when the compute raises error" do
70
- exception = Fog::Errors::Error.new
71
- cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers_raising_exception(exception))
72
- assert_raises ActiveRecord::RecordNotFound do
73
- cr.find_vm_by_uuid('100')
74
- end
75
- end
76
- end
77
-
78
- describe "host_interfaces_attrs" do
79
- before do
80
- @cr = FactoryBot.build_stubbed(:proxmox_cr)
81
- end
82
-
83
- it "raises Foreman::Exception when physical identifier does not match net[k] with k integer" do
84
- physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'eth0')
85
- host = FactoryBot.build(:host_empty, :interfaces => [physical_nic])
86
- err = assert_raises Foreman::Exception do
87
- @cr.host_interfaces_attrs(host)
88
- end
89
- assert err.message.end_with?('Invalid identifier interface[0]. Must be net[n] with n integer >= 0')
90
- end
91
-
92
- it "sets compute id with identifier, ip and ip6" do
93
- ip = IPAddr.new(1, Socket::AF_INET).to_s
94
- ip6 = Array.new(4) { '%x' % rand(16**4) }.join(':') + '::1'
95
- physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :ip => ip, :ip6 => ip6)
96
- host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :compute_attributes => {'type' => 'qemu', 'interfaces_attributes' => { '0' => physical_nic }})
97
- nic_attributes = @cr.host_interfaces_attrs(host).values.select(&:present?)
98
- nic_attr = nic_attributes.first
99
- assert_equal 'net0', nic_attr[:id]
100
- assert_equal ip, nic_attr[:ip]
101
- assert_equal ip6, nic_attr[:ip6]
102
- end
103
- end
104
-
105
- describe "host_compute_attrs" do
106
- before do
107
- @cr = FactoryBot.build_stubbed(:proxmox_cr)
108
- end
109
-
110
- it "raises Foreman::Exception when server ostype does not match os family" do
111
- operatingsystem = FactoryBot.build(:solaris)
112
- physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :primary => true)
113
- host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :operatingsystem => operatingsystem, :compute_attributes => { 'type' => 'qemu', 'config_attributes' => { 'ostype' => 'l26' }, 'interfaces_attributes' => { '0' => physical_nic } })
114
- err = assert_raises Foreman::Exception do
115
- @cr.host_compute_attrs(host)
116
- end
117
- assert err.message.end_with?('Operating system family Solaris is not consistent with l26')
118
- end
119
-
120
- it "sets container hostname with host name" do
121
- physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :primary => true)
122
- host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :compute_attributes => { 'type' => 'lxc', 'config_attributes' => { 'hostname' => '' }, 'interfaces_attributes' => { '0' => {} } })
123
- @cr.host_compute_attrs(host)
124
- assert_equal host.name, host.compute_attributes['config_attributes']['hostname']
125
- end
126
- end
127
-
128
-
129
- describe "vm_compute_attributes" do
130
- before do
131
- @cr = FactoryBot.build_stubbed(:proxmox_cr)
132
- end
133
-
134
- it "converts a server to hash" do
135
- vm, config_attributes, volume_attributes, interface_attributes = mock_server_vm
136
- vm_attrs = @cr.vm_compute_attributes(vm)
137
- assert !vm_attrs.has_key?(:config)
138
- assert vm_attrs.has_key?(:config_attributes)
139
- assert_equal config_attributes.reject { |key,value| [:vmid,:disks,:interfaces].include?(key) || value.to_s.empty?}, vm_attrs[:config_attributes]
140
- assert !vm_attrs[:config_attributes].has_key?(:disks)
141
- assert vm_attrs.has_key?(:volumes_attributes)
142
- assert_equal volume_attributes, vm_attrs[:volumes_attributes]['0']
143
- assert !vm_attrs[:config_attributes].has_key?(:interfaces)
144
- assert vm_attrs.has_key?(:interfaces_attributes)
145
- assert_equal interface_attributes, vm_attrs[:interfaces_attributes]['0']
146
- end
147
-
148
- it "converts a container to hash" do
149
- vm, config_attributes, volume_attributes, interface_attributes = mock_container_vm
150
- vm_attrs = @cr.vm_compute_attributes(vm)
151
- assert !vm_attrs.has_key?(:config)
152
- assert vm_attrs.has_key?(:config_attributes)
153
- assert_equal config_attributes.reject { |key,value| [:vmid,:disks,:interfaces].include?(key) || value.to_s.empty?}, vm_attrs[:config_attributes]
154
- assert !vm_attrs[:config_attributes].has_key?(:disks)
155
- assert vm_attrs.has_key?(:volumes_attributes)
156
- assert_equal volume_attributes, vm_attrs[:volumes_attributes]['0']
157
- assert vm_attrs.has_key?(:interfaces_attributes)
158
- assert_equal interface_attributes, vm_attrs[:interfaces_attributes]['0']
159
- end
160
-
161
- end
162
-
163
- describe 'save_vm' do
164
- before do
165
- @cr = FactoryBot.build_stubbed(:proxmox_cr)
166
- end
167
-
168
- it 'saves modified server config with same volumes' do
169
- uuid = '100'
170
- config = mock('config')
171
- config.stubs(:attributes).returns({ :cores => '' })
172
- vm = mock('vm')
173
- vm.stubs(:config).returns(config)
174
- vm.stubs(:container?).returns(false)
175
- vm.stubs(:type).returns('qemu')
176
- @cr.stubs(:find_vm_by_uuid).returns(vm)
177
- attr = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' } }
178
- @cr.stubs(:parse_server_vm).returns({ 'vmid' => '100', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1' })
179
- expected_attr = { :cores => '1', :cpulimit => '1' }
180
- vm.expects(:update, expected_attr)
181
- @cr.save_vm(uuid,attr)
182
- end
183
-
184
- it 'saves server as template' do
185
- uuid = '100'
186
- config = mock('config')
187
- config.stubs(:attributes).returns({ :cores => '' })
188
- vm = mock('vm')
189
- vm.stubs(:config).returns(config)
190
- vm.stubs(:container?).returns(false)
191
- vm.stubs(:templated?).returns(false)
192
- vm.stubs(:type).returns('qemu')
193
- @cr.stubs(:find_vm_by_uuid).returns(vm)
194
- attr = { 'templated' => '1' }
195
- vm.expects(:create_template)
196
- @cr.save_vm(uuid,attr)
197
- end
198
-
199
- it 'saves modified server config with added volumes' do
200
- uuid = '100'
201
- config = mock('config')
202
- disks = mock('disks')
203
- disk = mock('disk')
204
- disk.stubs(:size).returns(1073741824)
205
- disk.stubs(:storage).returns('local-lvm')
206
- disk.stubs(:id).returns('virtio0')
207
- disks.stubs(:get).returns()
208
- config.stubs(:disks).returns(disks)
209
- config.stubs(:attributes).returns({ :cores => '' })
210
- vm = mock('vm')
211
- vm.stubs(:config).returns(config)
212
- vm.stubs(:container?).returns(false)
213
- vm.stubs(:type).returns('qemu')
214
- @cr.stubs(:find_vm_by_uuid).returns(vm)
215
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'volumes_attributes' => { '0'=> { 'id' => 'scsi0', 'storage' => 'local-lvm', 'size' => '2147483648', 'cache' => 'none' } } }
216
- @cr.stubs(:parse_server_vm).returns({ 'vmid' => '100', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1' })
217
- expected_config_attr = { :cores => '1', :cpulimit => '1' }
218
- expected_volume_attr = { id: 'scsi0', storage: 'local:lvm', size: (2147483648 / GIGA).to_s }
219
- vm.expects(:attach, expected_volume_attr)
220
- vm.expects(:update, expected_config_attr)
221
- @cr.save_vm(uuid,new_attributes)
222
- end
223
-
224
- it 'saves modified server config with removed volumes' do
225
- uuid = '100'
226
- config = mock('config')
227
- disks = mock('disks')
228
- disk = mock('disk')
229
- disk.stubs(:size).returns(1073741824)
230
- disk.stubs(:storage).returns('local-lvm')
231
- disk.stubs(:id).returns('virtio0')
232
- disks.stubs(:get).returns(disk)
233
- config.stubs(:disks).returns(disks)
234
- config.stubs(:attributes).returns({ :cores => '' })
235
- vm = mock('vm')
236
- vm.stubs(:config).returns(config)
237
- vm.stubs(:container?).returns(false)
238
- vm.stubs(:type).returns('qemu')
239
- @cr.stubs(:find_vm_by_uuid).returns(vm)
240
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'volumes_attributes' => { '0'=> { '_delete' => '1', 'id' => 'scsi0', 'storage' => 'local-lvm', 'size' => '2147483648', 'cache' => 'none' } } }
241
- @cr.stubs(:parse_server_vm).returns({ 'vmid' => '100', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1' })
242
- expected_config_attr = { :cores => '1', :cpulimit => '1' }
243
- expected_volume_attr = 'scsi0'
244
- vm.expects(:detach, expected_volume_attr)
245
- vm.expects(:detach, 'unused0')
246
- vm.expects(:update, expected_config_attr)
247
- @cr.save_vm(uuid,new_attributes)
248
- end
249
-
250
- it 'saves modified server config with removed interfaces' do
251
- uuid = '100'
252
- config = mock('config')
253
- interfaces = mock('interfaces')
254
- interface = mock('interface')
255
- interface.stubs(:id).returns('net0')
256
- interfaces.stubs(:get).returns(interface)
257
- config.stubs(:interfaces).returns(interfaces)
258
- config.stubs(:attributes).returns({ :cores => '' })
259
- vm = mock('vm')
260
- vm.stubs(:config).returns(config)
261
- vm.stubs(:container?).returns(false)
262
- vm.stubs(:type).returns('qemu')
263
- @cr.stubs(:find_vm_by_uuid).returns(vm)
264
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'interfaces_attributes' => { '0'=> { '_delete' => '1', 'id' => 'net0' } } }
265
- @cr.stubs(:parse_server_vm).returns({ 'vmid' => '100', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'delete' => 'net0' })
266
- expected_config_attr = { :cores => '1', :cpulimit => '1', :delete => 'net0' }
267
- vm.expects(:update, expected_config_attr)
268
- @cr.save_vm(uuid,new_attributes)
269
- end
270
-
271
- it 'saves modified server config with resized volumes' do
272
- uuid = '100'
273
- config = mock('config')
274
- disks = mock('disks')
275
- disk = mock('disk')
276
- disk.stubs(:size).returns(1073741824)
277
- disk.stubs(:storage).returns('local-lvm')
278
- disks.stubs(:get).returns(disk)
279
- config.stubs(:disks).returns(disks)
280
- config.stubs(:attributes).returns({ :cores => '' })
281
- vm = mock('vm')
282
- vm.stubs(:config).returns(config)
283
- vm.stubs(:container?).returns(false)
284
- vm.stubs(:type).returns('qemu')
285
- @cr.stubs(:find_vm_by_uuid).returns(vm)
286
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'volumes_attributes' => { '0'=> { 'id' => 'scsi0', 'storage' => 'local-lvm', 'size' => '2147483648', 'cache' => 'none' } } }
287
- @cr.stubs(:parse_server_vm).returns({ 'vmid' => '100', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1' })
288
- expected_config_attr = { :cores => '1', :cpulimit => '1' }
289
- expected_volume_attr = ['scsi0', '+1G']
290
- vm.expects(:extend, expected_volume_attr)
291
- vm.expects(:update, expected_config_attr)
292
- @cr.save_vm(uuid,new_attributes)
293
- end
294
-
295
- it 'raises error unable to shrink volumes' do
296
- uuid = '100'
297
- config = mock('config')
298
- disks = mock('disks')
299
- disk = mock('disk')
300
- disk.stubs(:size).returns(1073741824)
301
- disk.stubs(:storage).returns('local-lvm')
302
- disks.stubs(:get).returns(disk)
303
- config.stubs(:disks).returns(disks)
304
- config.stubs(:attributes).returns({ :cores => '' })
305
- vm = mock('vm')
306
- vm.stubs(:config).returns(config)
307
- vm.stubs(:container?).returns(false)
308
- vm.stubs(:type).returns('qemu')
309
- @cr.stubs(:find_vm_by_uuid).returns(vm)
310
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'volumes_attributes' => { '0'=> { 'id' => 'scsi0', 'storage' => 'local-lvm', 'size' => '2', 'cache' => 'none' } } }
311
- @cr.stubs(:parse_server_vm).returns({ 'vmid' => '100', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1' })
312
- expected_config_attr = { :cores => '1', :cpulimit => '1' }
313
- err = assert_raises Foreman::Exception do
314
- @cr.save_vm(uuid,new_attributes)
315
- end
316
- assert err.message.end_with?('Unable to shrink scsi0 size. Proxmox allows only increasing size.')
317
- end
318
-
319
- it 'saves modified server config with moved volumes' do
320
- uuid = '100'
321
- config = mock('config')
322
- disks = mock('disks')
323
- disk = mock('disk')
324
- disk.stubs(:size).returns(1073741824)
325
- disk.stubs(:storage).returns('local-lvm')
326
- disks.stubs(:get).returns(disk)
327
- config.stubs(:disks).returns(disks)
328
- config.stubs(:attributes).returns({ :cores => '' })
329
- vm = mock('vm')
330
- vm.stubs(:config).returns(config)
331
- vm.stubs(:container?).returns(false)
332
- vm.stubs(:type).returns('qemu')
333
- @cr.stubs(:find_vm_by_uuid).returns(vm)
334
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'volumes_attributes' => { '0'=> { 'id' => 'scsi0', 'storage' => 'local-lvm2', 'size' => '1073741824', 'cache' => 'none' } } }
335
- @cr.stubs(:parse_server_vm).returns({ 'vmid' => '100', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1' })
336
- expected_config_attr = { :cores => '1', :cpulimit => '1' }
337
- expected_volume_attr = ['scsi0', 'local-lvm2']
338
- vm.expects(:move, expected_volume_attr)
339
- vm.expects(:update, expected_config_attr)
340
- @cr.save_vm(uuid,new_attributes)
341
- end
342
-
343
- it 'saves modified container config' do
344
- uuid = '100'
345
- config = mock('config')
346
- config.stubs(:attributes).returns({ :cores => '' })
347
- vm = mock('vm')
348
- vm.stubs(:config).returns(config)
349
- vm.stubs(:container?).returns(true)
350
- vm.stubs(:type).returns('lxc')
351
- @cr.stubs(:find_vm_by_uuid).returns(vm)
352
- attr = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' } }
353
- @cr.stubs(:parse_container_vm).returns({ 'vmid' => '100', 'type' => 'lxc', 'cores' => '1', 'cpulimit' => '1' })
354
- expected_attr = { :cores => '1', :cpulimit => '1' }
355
- vm.expects(:update, expected_attr)
356
- @cr.save_vm(uuid,attr)
357
- end
358
-
359
- it 'saves modified container config with added volumes' do
360
- uuid = '100'
361
- config = mock('config')
362
- disks = mock('disks')
363
- disk = mock('disk')
364
- disk.stubs(:size).returns(1073741824)
365
- disk.stubs(:storage).returns('local-lvm')
366
- disk.stubs(:id).returns('rootfs')
367
- disks.stubs(:get).returns()
368
- config.stubs(:disks).returns(disks)
369
- config.stubs(:attributes).returns({ :cores => '' })
370
- vm = mock('vm')
371
- vm.stubs(:config).returns(config)
372
- vm.stubs(:container?).returns(false)
373
- vm.stubs(:type).returns('lxc')
374
- @cr.stubs(:find_vm_by_uuid).returns(vm)
375
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'volumes_attributes' => { '0'=> { 'id' => 'mp0', 'storage' => 'local-lvm', 'size' => '2147483648', 'cache' => 'none', 'mp' => '/opt/path' } } }
376
- @cr.stubs(:parse_container_vm).returns({ 'vmid' => '100', 'type' => 'lxc', 'cores' => '1', 'cpulimit' => '1' })
377
- expected_config_attr = { :cores => '1', :cpulimit => '1' }
378
- expected_volume_attr = [{ id: 'mp0', storage: 'local:lvm', size: (2147483648 / GIGA).to_s }, { mp: '/opt/path' }]
379
- vm.expects(:attach, expected_volume_attr)
380
- vm.expects(:update, expected_config_attr)
381
- @cr.save_vm(uuid,new_attributes)
382
- end
383
-
384
- it 'saves modified container config with resized volumes' do
385
- uuid = '100'
386
- config = mock('config')
387
- disks = mock('disks')
388
- disk = mock('disk')
389
- disk.stubs(:size).returns(1073741824)
390
- disk.stubs(:storage).returns('local-lvm')
391
- disk.stubs(:id).returns('rootfs')
392
- disks.stubs(:get).returns(disk)
393
- config.stubs(:disks).returns(disks)
394
- config.stubs(:attributes).returns({ :cores => '' })
395
- vm = mock('vm')
396
- vm.stubs(:config).returns(config)
397
- vm.stubs(:container?).returns(false)
398
- vm.stubs(:type).returns('lxc')
399
- @cr.stubs(:find_vm_by_uuid).returns(vm)
400
- new_attributes = { 'templated' => '0', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1' }, 'volumes_attributes' => { '0'=> { 'id' => 'rootfs', 'storage' => 'local-lvm', 'size' => '2147483648', 'cache' => 'none' } } }
401
- @cr.stubs(:parse_container_vm).returns({ 'vmid' => '100', 'type' => 'lxc', 'cores' => '1', 'cpulimit' => '1' })
402
- expected_config_attr = { :cores => '1', :cpulimit => '1' }
403
- expected_volume_attr = ['rootfs', '+1G']
404
- vm.expects(:extend, expected_volume_attr)
405
- vm.expects(:update, expected_config_attr)
406
- @cr.save_vm(uuid,new_attributes)
407
- end
408
- end
409
-
410
- describe 'create_vm' do
411
-
412
- it 'raises Foreman::Exception when vmid is invalid' do
413
- args = { vmid: '100' }
414
- servers = mock('servers')
415
- servers.stubs(:id_valid?).returns(false)
416
- cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
417
- err = assert_raises Foreman::Exception do
418
- cr.create_vm(args)
419
- end
420
- assert err.message.end_with?('invalid vmid=100')
421
- end
422
-
423
- it 'creates server' do
424
- args = { vmid: '100', type: 'qemu' }
425
- servers = mock('servers')
426
- servers.stubs(:id_valid?).returns(true)
427
- cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
428
- cr.stubs(:convert_sizes).with(args)
429
- cr.stubs(:parse_server_vm).with(args).returns(args)
430
- servers.stubs(:create).with(args)
431
- vm = mock('vm')
432
- cr.stubs(:find_vm_by_uuid).with("#{args[:vmid]}").returns(vm)
433
- cr.create_vm(args)
434
- end
435
-
436
- it 'creates container' do
437
- args = { vmid: '100', type: 'lxc' }
438
- servers = mock('servers')
439
- servers.stubs(:id_valid?).returns(true)
440
- containers = mock('containers')
441
- containers.stubs(:create).with(vmid: 100, type: 'lxc')
442
- cr = mock_node_servers_containers(ForemanFogProxmox::Proxmox.new, servers, containers)
443
- cr.stubs(:convert_sizes).with(args)
444
- cr.stubs(:parse_container_vm).with(args).returns(args)
445
- vm = mock('vm')
446
- cr.stubs(:find_vm_by_uuid).with("#{args[:vmid]}").returns(vm)
447
- cr.create_vm(args)
448
- end
449
-
450
- it 'clones server' do
451
- args = { vmid: '100', type: 'qemu', image_id: '999', name: 'name' }
452
- servers = mock('servers')
453
- servers.stubs(:id_valid?).returns(true)
454
- cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
455
- cr.stubs(:convert_sizes).with(args)
456
- cr.stubs(:parse_server_vm).with(args).returns(args)
457
- servers.stubs(:create).with(args)
458
- image = mock('image')
459
- clone = mock('clone')
460
- image.stubs(:clone).with(100)
461
- servers.stubs(:get).with(100).returns(clone)
462
- servers.stubs(:get).with('999').returns(image)
463
- clone.stubs(:update).with(name: 'name')
464
- vm = mock('vm')
465
- cr.stubs(:find_vm_by_uuid).with("#{args[:vmid]}").returns(vm)
466
- cr.create_vm(args)
467
- end
468
-
469
- it 'clones container' do
470
- args = { vmid: '100', type: 'lxc', image_id: '999', name: 'name' }
471
- servers = mock('servers')
472
- servers.stubs(:id_valid?).returns(true)
473
- containers = mock('containers')
474
- containers.stubs(:create).with(vmid: 100, type: 'lxc')
475
- image = mock('image')
476
- clone = mock('clone')
477
- image.stubs(:clone).with(100)
478
- servers.stubs(:get).with(100).returns(clone)
479
- servers.stubs(:get).with('999').returns(image)
480
- clone.stubs(:update).with(name: 'name')
481
- cr = mock_node_servers_containers(ForemanFogProxmox::Proxmox.new, servers, containers)
482
- cr.stubs(:convert_sizes).with(args)
483
- cr.stubs(:parse_container_vm).with(args).returns(args)
484
- vm = mock('vm')
485
- cr.stubs(:find_vm_by_uuid).with("#{args[:vmid]}").returns(vm)
486
- cr.create_vm(args)
487
- end
488
- end
489
-
490
- describe 'new_vm' do
491
- before do
492
- @cr = FactoryBot.build_stubbed(:proxmox_cr)
493
- end
494
-
495
- it 'new server with attr empty' do
496
- attr = {}
497
- vm = mock('vm')
498
- config = mock('config')
499
- config.stubs(:inspect).returns('config')
500
- vm.stubs(:config).returns(config)
501
- @cr.stubs(:new_server_vm).with(attr).returns(vm)
502
- assert_equal vm, @cr.new_vm(attr)
503
- end
504
-
505
- it 'new server with attr not empty' do
506
- attr = {'type' => 'qemu'}
507
- vm = mock('vm')
508
- config = mock('config')
509
- config.stubs(:inspect).returns('config')
510
- vm.stubs(:config).returns(config)
511
- @cr.stubs(:new_server_vm).with(attr).returns(vm)
512
- assert_equal vm, @cr.new_vm(attr)
513
- end
514
-
515
- it 'new container with attr not empty' do
516
- attr = {'type' => 'lxc'}
517
- vm = mock('vm')
518
- config = mock('config')
519
- config.stubs(:inspect).returns('config')
520
- vm.stubs(:config).returns(config)
521
- @cr.stubs(:new_container_vm).with(attr).returns(vm)
522
- assert_equal vm, @cr.new_vm(attr)
523
- end
524
- end
525
-
526
-
527
-
528
58
  end
529
59
  end