foreman_fog_proxmox 0.12.0

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 (113) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +619 -0
  3. data/README.md +290 -0
  4. data/Rakefile +49 -0
  5. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_compute_resource.js +33 -0
  6. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm.js +187 -0
  7. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm_container.js +24 -0
  8. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm_server.js +125 -0
  9. data/app/controllers/concerns/foreman_fog_proxmox/controller/parameters/compute_resource.rb +41 -0
  10. data/app/controllers/foreman_fog_proxmox/compute_resources_controller.rb +86 -0
  11. data/app/helpers/node_dashboard_helper.rb +35 -0
  12. data/app/helpers/proxmox_compute_selectors_helper.rb +184 -0
  13. data/app/helpers/proxmox_container_helper.rb +163 -0
  14. data/app/helpers/proxmox_form_helper.rb +70 -0
  15. data/app/helpers/proxmox_server_helper.rb +155 -0
  16. data/app/helpers/proxmox_vm_helper.rb +88 -0
  17. data/app/models/concerns/fog_extensions/proxmox/disk.rb +29 -0
  18. data/app/models/concerns/fog_extensions/proxmox/interface.rb +40 -0
  19. data/app/models/concerns/fog_extensions/proxmox/node.rb +41 -0
  20. data/app/models/concerns/fog_extensions/proxmox/server.rb +93 -0
  21. data/app/models/concerns/fog_extensions/proxmox/server_config.rb +71 -0
  22. data/app/models/concerns/fog_extensions/proxmox/volume.rb +29 -0
  23. data/app/models/concerns/host_ext/proxmox/interfaces.rb +52 -0
  24. data/app/models/concerns/orchestration/proxmox/compute.rb +42 -0
  25. data/app/models/foreman_fog_proxmox/options_select.rb +36 -0
  26. data/app/models/foreman_fog_proxmox/proxmox.rb +133 -0
  27. data/app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb +67 -0
  28. data/app/models/foreman_fog_proxmox/proxmox_connection.rb +67 -0
  29. data/app/models/foreman_fog_proxmox/proxmox_console.rb +41 -0
  30. data/app/models/foreman_fog_proxmox/proxmox_images.rb +61 -0
  31. data/app/models/foreman_fog_proxmox/proxmox_interfaces.rb +103 -0
  32. data/app/models/foreman_fog_proxmox/proxmox_operating_systems.rb +49 -0
  33. data/app/models/foreman_fog_proxmox/proxmox_pools.rb +56 -0
  34. data/app/models/foreman_fog_proxmox/proxmox_token_expiration.rb +30 -0
  35. data/app/models/foreman_fog_proxmox/proxmox_version.rb +36 -0
  36. data/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb +116 -0
  37. data/app/models/foreman_fog_proxmox/proxmox_vm_new.rb +178 -0
  38. data/app/models/foreman_fog_proxmox/proxmox_vm_queries.rb +75 -0
  39. data/app/models/foreman_fog_proxmox/proxmox_volumes.rb +94 -0
  40. data/app/models/foreman_fog_proxmox/vms.rb +37 -0
  41. data/app/overrides/compute_resources_vms/form/add_clone_to_new_vm_compute_detail.rb +25 -0
  42. data/app/overrides/compute_resources_vms/form/add_from_profile_to_compute_attributes_form.rb +34 -0
  43. data/app/overrides/compute_resources_vms/form/add_vm_type_node_to_new_form.rb +25 -0
  44. data/app/overrides/compute_resources_vms/form/add_vm_type_to_networks_form.rb +34 -0
  45. data/app/overrides/compute_resources_vms/form/add_vm_type_to_nic_provider_specific_form.rb +26 -0
  46. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_edit.rb +25 -0
  47. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_new_volume.rb +25 -0
  48. data/app/overrides/compute_resources_vms/form/remove_new_vm_from_removable_layout.rb +25 -0
  49. data/app/services/foreman_fog_proxmox/node_dashboard/data.rb +37 -0
  50. data/app/views/api/v2/compute_resources/proxmox.json.rabl +3 -0
  51. data/app/views/compute_resources/form/_proxmox.html.erb +29 -0
  52. data/app/views/compute_resources/show/_proxmox.html.erb +37 -0
  53. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_compute_attributes_form.html.erb +23 -0
  54. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_compute_form.html.erb +23 -0
  55. data/app/views/compute_resources_vms/form/proxmox/_add_from_profile_to_hosts_compute_detail_form.html.erb +24 -0
  56. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_node_to_new_form.html.erb +24 -0
  57. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_form.html.erb +28 -0
  58. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_new_childs_form.html.erb +32 -0
  59. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_nic_provider_specific_form.html.erb +30 -0
  60. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_volumes_edit.html.erb +71 -0
  61. data/app/views/compute_resources_vms/form/proxmox/_base.html.erb +39 -0
  62. data/app/views/compute_resources_vms/form/proxmox/_general.html.erb +39 -0
  63. data/app/views/compute_resources_vms/form/proxmox/_network.html.erb +19 -0
  64. data/app/views/compute_resources_vms/form/proxmox/_removable_layout.html.erb +27 -0
  65. data/app/views/compute_resources_vms/form/proxmox/container/_advanced.html.erb +31 -0
  66. data/app/views/compute_resources_vms/form/proxmox/container/_config.html.erb +41 -0
  67. data/app/views/compute_resources_vms/form/proxmox/container/_extended.html.erb +27 -0
  68. data/app/views/compute_resources_vms/form/proxmox/container/_network.html.erb +33 -0
  69. data/app/views/compute_resources_vms/form/proxmox/container/_volume_mp.html.erb +27 -0
  70. data/app/views/compute_resources_vms/form/proxmox/container/_volume_rootfs.html.erb +24 -0
  71. data/app/views/compute_resources_vms/form/proxmox/server/_advanced.html.erb +31 -0
  72. data/app/views/compute_resources_vms/form/proxmox/server/_config.html.erb +60 -0
  73. data/app/views/compute_resources_vms/form/proxmox/server/_network.html.erb +30 -0
  74. data/app/views/compute_resources_vms/form/proxmox/server/_volume.html.erb +28 -0
  75. data/app/views/compute_resources_vms/index/_proxmox.html.erb +49 -0
  76. data/app/views/compute_resources_vms/show/_proxmox.html.erb +42 -0
  77. data/app/views/dashboard/_foreman_fog_proxmox_widget.erb +21 -0
  78. data/app/views/images/form/_proxmox.html.erb +21 -0
  79. data/config/routes.rb +30 -0
  80. data/lib/foreman_fog_proxmox.rb +23 -0
  81. data/lib/foreman_fog_proxmox/engine.rb +105 -0
  82. data/lib/foreman_fog_proxmox/semver.rb +102 -0
  83. data/lib/foreman_fog_proxmox/value.rb +26 -0
  84. data/lib/foreman_fog_proxmox/version.rb +22 -0
  85. data/lib/tasks/foreman_fog_proxmox_tasks.rake +37 -0
  86. data/locale/Makefile +60 -0
  87. data/locale/en/foreman_fog_proxmox.po +401 -0
  88. data/locale/foreman_fog_proxmox.pot +627 -0
  89. data/locale/fr/foreman_fog_proxmox.po +401 -0
  90. data/locale/gemspec.rb +4 -0
  91. data/test/factories/foreman_fog_proxmox/proxmox_container_mock_factory.rb +137 -0
  92. data/test/factories/foreman_fog_proxmox/proxmox_node_mock_factory.rb +87 -0
  93. data/test/factories/foreman_fog_proxmox/proxmox_server_mock_factory.rb +134 -0
  94. data/test/factories/proxmox_factory.rb +79 -0
  95. data/test/functional/compute_resources_controller_test.rb +48 -0
  96. data/test/test_plugin_helper.rb +19 -0
  97. data/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb +204 -0
  98. data/test/unit/foreman_fog_proxmox/helpers/proxmox_server_helper_test.rb +149 -0
  99. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_helper_test.rb +216 -0
  100. data/test/unit/foreman_fog_proxmox/proxmox_compute_attributes_test.rb +124 -0
  101. data/test/unit/foreman_fog_proxmox/proxmox_images_test.rb +52 -0
  102. data/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb +117 -0
  103. data/test/unit/foreman_fog_proxmox/proxmox_test.rb +58 -0
  104. data/test/unit/foreman_fog_proxmox/proxmox_version_test.rb +94 -0
  105. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_container_test.rb +280 -0
  106. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_create_test.rb +110 -0
  107. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_test.rb +222 -0
  108. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_volumes_test.rb +308 -0
  109. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_test.rb +43 -0
  110. data/test/unit/foreman_fog_proxmox/proxmox_vm_new_test.rb +71 -0
  111. data/test/unit/foreman_fog_proxmox/proxmox_vm_queries_test.rb +68 -0
  112. data/test/unit/foreman_fog_proxmox/semver_test.rb +111 -0
  113. metadata +249 -0
@@ -0,0 +1,110 @@
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 'active_support/core_ext/hash/indifferent_access'
25
+
26
+ module ForemanFogProxmox
27
+ class ProxmoxVmCommandsServerCreateTest < ActiveSupport::TestCase
28
+ include ComputeResourceTestHelpers
29
+ include ProxmoxNodeMockFactory
30
+ include ProxmoxServerMockFactory
31
+ include ProxmoxVmHelper
32
+
33
+ describe 'create_vm' do
34
+ it 'raises Foreman::Exception when vmid <= 100 and vmid > 0' do
35
+ args = { vmid: '100' }
36
+ servers = mock('servers')
37
+ servers.stubs(:id_valid?).returns(false)
38
+ cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
39
+ err = assert_raises Foreman::Exception do
40
+ cr.create_vm(args)
41
+ end
42
+ assert err.message.end_with?('invalid vmid=100')
43
+ end
44
+
45
+ it 'computes next vmid when vmid == 0 and creates server' do
46
+ args = { vmid: '0', type: 'qemu', node_id: 'pve', start_after_create: '0' }
47
+ servers = mock('servers')
48
+ servers.stubs(:id_valid?).returns(true)
49
+ servers.stubs(:next_id).returns('101')
50
+ cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
51
+ cr.stubs(:convert_sizes).with(args)
52
+ cr.stubs(:parse_server_vm).with(args).returns(args)
53
+ servers.stubs(:create).with(args)
54
+ vm = mock('vm')
55
+ cr.stubs(:find_vm_by_uuid).with((args[:vmid]).to_s).returns(vm)
56
+ cr.create_vm(args)
57
+ end
58
+
59
+ it 'creates server without bootstart' do
60
+ args = { vmid: '100', type: 'qemu', node_id: 'pve', start_after_create: '0' }
61
+ servers = mock('servers')
62
+ servers.stubs(:id_valid?).returns(true)
63
+ cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
64
+ cr.stubs(:convert_sizes).with(args)
65
+ cr.stubs(:parse_server_vm).with(args).returns(args)
66
+ servers.stubs(:create).with(args)
67
+ vm = mock('vm')
68
+ cr.stubs(:find_vm_by_uuid).with((args[:vmid]).to_s).returns(vm)
69
+ cr.create_vm(args)
70
+ end
71
+
72
+ it 'creates server with bootstart' do
73
+ args = { vmid: '100', type: 'qemu', node_id: 'pve', start_after_create: '1' }
74
+ servers = mock('servers')
75
+ servers.stubs(:id_valid?).returns(true)
76
+ cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
77
+ cr.stubs(:convert_sizes).with(args)
78
+ cr.stubs(:parse_server_vm).with(args).returns(args)
79
+ vm = mock('vm')
80
+ servers.stubs(:create).with(args).returns(vm)
81
+ cr.stubs(:find_vm_by_uuid).with((args[:vmid]).to_s).returns(vm)
82
+ cr.stubs(:start_on_boot).with(vm, args).returns(vm)
83
+ cr.create_vm(args)
84
+ end
85
+
86
+ it 'creates server within pool' do
87
+ args = { vmid: '100', type: 'qemu', node_id: 'pve', start_after_create: '0', pool: 'pool1' }
88
+ servers = mock('servers')
89
+ servers.stubs(:id_valid?).returns(true)
90
+ cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
91
+ cr.stubs(:convert_sizes).with(args)
92
+ cr.stubs(:parse_server_vm).with(args).returns(args)
93
+ vm = mock('vm')
94
+ servers.stubs(:create).with(args).returns(vm)
95
+ cr.stubs(:find_vm_by_uuid).with((args[:vmid]).to_s).returns(vm)
96
+ cr.stubs(:start_on_boot).with(vm, args).returns(vm)
97
+ cr.create_vm(args)
98
+ end
99
+
100
+ it 'clones server' do
101
+ args = { vmid: '100', type: 'qemu', image_id: '999', name: 'name' }
102
+ servers = mock('servers')
103
+ servers.stubs(:id_valid?).returns(true)
104
+ cr = mock_node_servers(ForemanFogProxmox::Proxmox.new, servers)
105
+ cr.expects(:clone_from_image).with('999', args, 100)
106
+ cr.create_vm(args)
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,222 @@
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 'active_support/core_ext/hash/indifferent_access'
25
+
26
+ module ForemanFogProxmox
27
+ class ProxmoxVmCommandsServerUpdateTest < ActiveSupport::TestCase
28
+ include ComputeResourceTestHelpers
29
+ include ProxmoxNodeMockFactory
30
+ include ProxmoxServerMockFactory
31
+ include ProxmoxVmHelper
32
+
33
+ describe 'save_vm' do
34
+ before do
35
+ @cr = FactoryBot.build_stubbed(:proxmox_cr)
36
+ end
37
+
38
+ it 'migrates server from node to another one in the cluster' do
39
+ uuid = '100'
40
+ config = mock('config')
41
+ config.stubs(:attributes).returns(:cores => '')
42
+ vm = mock('vm')
43
+ vm.stubs(:config).returns(config)
44
+ vm.stubs(:container?).returns(false)
45
+ vm.stubs(:templated?).returns(false)
46
+ vm.stubs(:type).returns('qemu')
47
+ vm.stubs(:node_id).returns('pve')
48
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
49
+ attr = { 'templated' => '0', 'node_id' => 'pve2' }
50
+ vm.expects(:migrate)
51
+ @cr.save_vm(uuid, attr)
52
+ end
53
+
54
+ it 'saves modified server config with same volumes' do
55
+ uuid = '100'
56
+ config = mock('config')
57
+ config.stubs(:attributes).returns(:cores => '')
58
+ vm = mock('vm')
59
+ vm.stubs(:config).returns(config)
60
+ vm.stubs(:container?).returns(false)
61
+ vm.stubs(:templated?).returns(false)
62
+ vm.stubs(:type).returns('qemu')
63
+ vm.stubs(:node_id).returns('pve')
64
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
65
+ attr = { 'templated' => '0', 'node_id' => 'pve', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0' } }.with_indifferent_access
66
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0')
67
+ expected_attr = { :cores => '1', :cpulimit => '1' }.with_indifferent_access
68
+ vm.expects(:update, expected_attr)
69
+ @cr.save_vm(uuid, attr)
70
+ end
71
+
72
+ it 'saves server as template' do
73
+ uuid = '100'
74
+ config = mock('config')
75
+ config.stubs(:attributes).returns(:cores => '')
76
+ vm = mock('vm')
77
+ vm.stubs(:config).returns(config)
78
+ vm.stubs(:container?).returns(false)
79
+ vm.stubs(:templated?).returns(false)
80
+ vm.stubs(:type).returns('qemu')
81
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
82
+ attr = { 'templated' => '1' }
83
+ vm.expects(:create_template)
84
+ @cr.save_vm(uuid, attr)
85
+ end
86
+
87
+ it 'saves modified server config with removed interfaces' do
88
+ uuid = '100'
89
+ config = mock('config')
90
+ interfaces = mock('interfaces')
91
+ interface = mock('interface')
92
+ interface.stubs(:id).returns('net0')
93
+ interfaces.stubs(:get).returns(interface)
94
+ config.stubs(:interfaces).returns(interfaces)
95
+ config.stubs(:attributes).returns(:cores => '')
96
+ vm = mock('vm')
97
+ vm.stubs(:config).returns(config)
98
+ vm.stubs(:container?).returns(false)
99
+ vm.stubs(:type).returns('qemu')
100
+ vm.stubs(:node_id).returns('pve')
101
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
102
+ new_attributes = {
103
+ 'templated' => '0',
104
+ 'node_id' => 'pve',
105
+ 'config_attributes' => {
106
+ 'cores' => '1',
107
+ 'cpulimit' => '1'
108
+ },
109
+ 'interfaces_attributes' => {
110
+ '0' => {
111
+ '_delete' => '1',
112
+ 'id' => 'net0'
113
+ }
114
+ }
115
+ }.with_indifferent_access
116
+ @cr.stubs(:parse_server_vm).returns(
117
+ 'vmid' => '100',
118
+ 'node_id' => 'pve',
119
+ 'type' => 'qemu',
120
+ 'cores' => '1',
121
+ 'cpulimit' => '1',
122
+ 'delete' => 'net0',
123
+ 'onboot' => '0'
124
+ )
125
+ expected_config_attr = { :cores => '1', :cpulimit => '1', :delete => 'net0' }
126
+ vm.expects(:update, expected_config_attr)
127
+ @cr.save_vm(uuid, new_attributes)
128
+ end
129
+
130
+ it 'saves server config with modified pool' do
131
+ uuid = '100'
132
+ config = mock('config')
133
+ config.stubs(:pool).returns('pool1')
134
+ config.expects(:pool=).with('pool1')
135
+ vm = mock('vm')
136
+ vm.stubs(:config).returns(config)
137
+ vm.stubs(:container?).returns(false)
138
+ vm.stubs(:type).returns('qemu')
139
+ vm.stubs(:node_id).returns('pve')
140
+ vm.stubs(:vmid).returns(uuid)
141
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
142
+ identity_client = mock('identity_client')
143
+ pools = mock('pools')
144
+ pool1 = mock('pool1')
145
+ pool1.stubs(:poolid).returns('pool1')
146
+ pool1.stubs(:has_server?).with('100').returns(true)
147
+ pool1.expects(:remove_server).with(uuid)
148
+ pool2 = mock('pool2')
149
+ pool2.stubs(:poolid).returns('pool2')
150
+ pool2.stubs(:has_server?).with('100').returns(false)
151
+ pool2.expects(:add_server).with(uuid)
152
+ pools.stubs(:all).returns([pool1, pool2])
153
+ pools.expects(:get).with('pool1').returns(pool1)
154
+ pools.expects(:get).with('pool2').returns(pool2)
155
+ identity_client.stubs(:pools).returns(pools)
156
+ @cr.stubs(:identity_client).returns(identity_client)
157
+ attr = { 'templated' => '0', 'node_id' => 'pve', 'pool' => 'pool2', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0' } }.with_indifferent_access
158
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0', 'pool' => 'pool2')
159
+ vm.expects(:update).with({ 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0' }.with_indifferent_access)
160
+ @cr.save_vm(uuid, attr)
161
+ end
162
+
163
+ it 'saves server config with removed pool' do
164
+ uuid = '100'
165
+ config = mock('config')
166
+ config.expects(:pool=).with('pool1')
167
+ config.stubs(:pool).returns('pool1')
168
+ vm = mock('vm')
169
+ vm.stubs(:config).returns(config)
170
+ vm.stubs(:container?).returns(false)
171
+ vm.stubs(:type).returns('qemu')
172
+ vm.stubs(:node_id).returns('pve')
173
+ vm.stubs(:vmid).returns(uuid)
174
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
175
+ identity_client = mock('identity_client')
176
+ pools = mock('pools')
177
+ pool1 = mock('pool1')
178
+ pool1.stubs(:has_server?).with('100').returns(true)
179
+ pool1.stubs(:poolid).returns('pool1')
180
+ pool1.expects(:remove_server).with(uuid)
181
+ pools.stubs(:all).returns([pool1])
182
+ pools.expects(:get).with('pool1').returns(pool1)
183
+ pools.expects(:get).with('').returns(nil)
184
+ identity_client.stubs(:pools).returns(pools)
185
+ @cr.stubs(:identity_client).returns(identity_client)
186
+ attr = { 'templated' => '0', 'node_id' => 'pve', 'pool' => '', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0' } }.with_indifferent_access
187
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0', 'pool' => '')
188
+ vm.expects(:update).with({ 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0' }.with_indifferent_access)
189
+ @cr.save_vm(uuid, attr)
190
+ end
191
+
192
+ it 'saves server config with added pool' do
193
+ uuid = '100'
194
+ config = mock('config')
195
+ config.expects(:pool=).with(nil)
196
+ config.stubs(:pool).returns(nil)
197
+ vm = mock('vm')
198
+ vm.stubs(:config).returns(config)
199
+ vm.stubs(:container?).returns(false)
200
+ vm.stubs(:type).returns('qemu')
201
+ vm.stubs(:node_id).returns('pve')
202
+ vm.stubs(:vmid).returns(uuid)
203
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
204
+ identity_client = mock('identity_client')
205
+ pools = mock('pools')
206
+ pool2 = mock('pool2')
207
+ pool2.stubs(:has_server?).with('100').returns(false)
208
+ pool2.stubs(:poolid).returns('pool2')
209
+ pool2.expects(:add_server).with(uuid)
210
+ pools.stubs(:all).returns([pool2])
211
+ pools.expects(:get).with('pool2').returns(pool2)
212
+ pools.expects(:get).with('').returns(nil)
213
+ identity_client.stubs(:pools).returns(pools)
214
+ @cr.stubs(:identity_client).returns(identity_client)
215
+ attr = { 'templated' => '0', 'node_id' => 'pve', 'pool' => 'pool2', 'config_attributes' => { 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0' } }.with_indifferent_access
216
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0', 'pool' => 'pool2')
217
+ vm.expects(:update).with({ 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0' }.with_indifferent_access)
218
+ @cr.save_vm(uuid, attr)
219
+ end
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,308 @@
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 'active_support/core_ext/hash/indifferent_access'
25
+
26
+ module ForemanFogProxmox
27
+ class ProxmoxVmCommandsServerUpdateTest < ActiveSupport::TestCase
28
+ include ComputeResourceTestHelpers
29
+ include ProxmoxNodeMockFactory
30
+ include ProxmoxServerMockFactory
31
+ include ProxmoxVmHelper
32
+
33
+ describe 'save_vm' do
34
+ before do
35
+ @cr = FactoryBot.build_stubbed(:proxmox_cr)
36
+ end
37
+
38
+ it 'saves modified server config with added volumes' do
39
+ uuid = '100'
40
+ config = mock('config')
41
+ disks = mock('disks')
42
+ disk = mock('disk')
43
+ disk.stubs(:size).returns(1_073_741_824)
44
+ disk.stubs(:storage).returns('local-lvm')
45
+ disk.stubs(:id).returns('scsi0')
46
+ disks.stubs(:get).returns
47
+ config.stubs(:disks).returns(disks)
48
+ config.stubs(:attributes).returns(:cores => '')
49
+ vm = mock('vm')
50
+ vm.stubs(:config).returns(config)
51
+ vm.stubs(:container?).returns(false)
52
+ vm.stubs(:type).returns('qemu')
53
+ vm.stubs(:node_id).returns('pve')
54
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
55
+ new_attributes = {
56
+ 'templated' => '0',
57
+ 'node_id' => 'pve',
58
+ 'config_attributes' => {
59
+ 'cores' => '1',
60
+ 'cpulimit' => '1'
61
+ },
62
+ 'volumes_attributes' => {
63
+ '0' => {
64
+ 'id' => 'scsi0',
65
+ '_delete' => '',
66
+ 'device' => '0',
67
+ 'controller' => 'scsi',
68
+ 'storage' => 'local-lvm',
69
+ 'size' => '2147483648',
70
+ 'cache' => 'none'
71
+ }
72
+ }
73
+ }.with_indifferent_access
74
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'onboot' => '0')
75
+ expected_config_attr = { :cores => '1', :cpulimit => '1' }
76
+ expected_volume_attr = { id: 'scsi0', storage: 'local:lvm', size: (2_147_483_648 / GIGA).to_s }
77
+ vm.expects(:attach, expected_volume_attr)
78
+ vm.expects(:update, expected_config_attr)
79
+ @cr.save_vm(uuid, new_attributes)
80
+ end
81
+
82
+ it 'saves modified server config with removed volumes' do
83
+ uuid = '100'
84
+ config = mock('config')
85
+ disks = mock('disks')
86
+ disk = mock('disk')
87
+ disk.stubs(:size).returns(1_073_741_824)
88
+ disk.stubs(:storage).returns('local-lvm')
89
+ disk.stubs(:id).returns('virtio0')
90
+ disks.stubs(:get).returns(disk)
91
+ config.stubs(:disks).returns(disks)
92
+ config.stubs(:attributes).returns(:cores => '')
93
+ vm = mock('vm')
94
+ vm.stubs(:config).returns(config)
95
+ vm.stubs(:container?).returns(false)
96
+ vm.stubs(:templated?).returns(false)
97
+ vm.stubs(:type).returns('qemu')
98
+ vm.stubs(:node_id).returns('pve')
99
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
100
+ new_attributes = {
101
+ 'templated' => '0',
102
+ 'node_id' => 'pve',
103
+ 'config_attributes' => {
104
+ 'cores' => '1',
105
+ 'cpulimit' => '1'
106
+ },
107
+ 'volumes_attributes' => {
108
+ '0' => {
109
+ '_delete' => '1',
110
+ 'id' => 'scsi0',
111
+ 'volid' => 'local-lvm:vm-100-disk-0',
112
+ 'device' => '0',
113
+ 'controller' => 'scsi',
114
+ 'storage' => 'local-lvm',
115
+ 'size' => '2147483648',
116
+ 'cache' => 'none'
117
+ }
118
+ }
119
+ }.with_indifferent_access
120
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1', 'config_attributes' => { 'onboot' => '0' })
121
+ expected_config_attr = { :cores => '1', :cpulimit => '1' }
122
+ expected_volume_attr = 'scsi0'
123
+ vm.expects(:detach, expected_volume_attr)
124
+ vm.expects(:detach, 'unused0')
125
+ vm.expects(:update, expected_config_attr)
126
+ @cr.save_vm(uuid, new_attributes)
127
+ end
128
+
129
+ it 'saves modified server config with resized volumes' do
130
+ uuid = '100'
131
+ config = mock('config')
132
+ disks = mock('disks')
133
+ disk = mock('disk')
134
+ disk.stubs(:size).returns(1_073_741_824)
135
+ disk.stubs(:storage).returns('local-lvm')
136
+ disks.stubs(:get).returns(disk)
137
+ config.stubs(:disks).returns(disks)
138
+ config.stubs(:attributes).returns(:cores => '')
139
+ vm = mock('vm')
140
+ vm.stubs(:config).returns(config)
141
+ vm.stubs(:container?).returns(false)
142
+ vm.stubs(:templated?).returns(false)
143
+ vm.stubs(:type).returns('qemu')
144
+ vm.stubs(:node_id).returns('pve')
145
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
146
+ new_attributes = {
147
+ 'templated' => '0',
148
+ 'node_id' => 'pve',
149
+ 'config_attributes' => {
150
+ 'cores' => '1',
151
+ 'cpulimit' => '1'
152
+ },
153
+ 'volumes_attributes' => {
154
+ '0' => {
155
+ 'id' => 'scsi0',
156
+ '_delete' => '',
157
+ 'volid' => 'local-lvm:vm-100-disk-0',
158
+ 'device' => '0',
159
+ 'controller' => 'scsi',
160
+ 'storage' => 'local-lvm',
161
+ 'size' => '2147483648',
162
+ 'cache' => 'none'
163
+ }
164
+ }
165
+ }.with_indifferent_access
166
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1')
167
+ expected_config_attr = { :cores => '1', :cpulimit => '1' }
168
+ expected_volume_attr = ['scsi0', '+1G']
169
+ vm.expects(:extend, expected_volume_attr)
170
+ vm.expects(:update, expected_config_attr)
171
+ @cr.save_vm(uuid, new_attributes)
172
+ end
173
+
174
+ it 'raises error unable to shrink volumes' do
175
+ uuid = '100'
176
+ config = mock('config')
177
+ disks = mock('disks')
178
+ disk = mock('disk')
179
+ disk.stubs(:size).returns(1_073_741_824)
180
+ disk.stubs(:storage).returns('local-lvm')
181
+ disks.stubs(:get).returns(disk)
182
+ config.stubs(:disks).returns(disks)
183
+ config.stubs(:attributes).returns(:cores => '')
184
+ vm = mock('vm')
185
+ vm.stubs(:config).returns(config)
186
+ vm.stubs(:container?).returns(false)
187
+ vm.stubs(:type).returns('qemu')
188
+ vm.stubs(:node_id).returns('pve')
189
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
190
+ new_attributes = {
191
+ 'templated' => '0',
192
+ 'node_id' => 'pve',
193
+ 'config_attributes' => {
194
+ 'cores' => '1',
195
+ 'cpulimit' => '1'
196
+ },
197
+ 'volumes_attributes' => {
198
+ '0' => {
199
+ 'id' => 'scsi0',
200
+ '_delete' => '',
201
+ 'volid' => 'local-lvm:vm-100-disk-0',
202
+ 'device' => '0',
203
+ 'controller' => 'scsi',
204
+ 'storage' => 'local-lvm',
205
+ 'size' => '2',
206
+ 'cache' => 'none'
207
+ }
208
+ }
209
+ }.with_indifferent_access
210
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1')
211
+ err = assert_raises Foreman::Exception do
212
+ @cr.save_vm(uuid, new_attributes)
213
+ end
214
+ assert err.message.end_with?('Unable to shrink scsi0 size. Proxmox allows only increasing size.')
215
+ end
216
+
217
+ it 'saves modified server config with moved volumes' do
218
+ uuid = '100'
219
+ config = mock('config')
220
+ disks = mock('disks')
221
+ disk = mock('disk')
222
+ disk.stubs(:size).returns(1_073_741_824)
223
+ disk.stubs(:storage).returns('local-lvm')
224
+ disks.stubs(:get).returns(disk)
225
+ config.stubs(:disks).returns(disks)
226
+ config.stubs(:attributes).returns(:cores => '')
227
+ vm = mock('vm')
228
+ vm.stubs(:config).returns(config)
229
+ vm.stubs(:container?).returns(false)
230
+ vm.stubs(:type).returns('qemu')
231
+ vm.stubs(:node_id).returns('pve')
232
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
233
+ new_attributes = {
234
+ 'templated' => '0',
235
+ 'node_id' => 'pve',
236
+ 'config_attributes' => {
237
+ 'cores' => '1',
238
+ 'cpulimit' => '1'
239
+ },
240
+ 'volumes_attributes' => {
241
+ '0' => {
242
+ 'id' => 'scsi0',
243
+ '_delete' => '',
244
+ 'volid' => 'local-lvm:vm-100-disk-0',
245
+ 'device' => '0',
246
+ 'controller' => 'scsi',
247
+ 'storage' => 'local-lvm2',
248
+ 'size' => '1073741824',
249
+ 'cache' => 'none'
250
+ }
251
+ }
252
+ }.with_indifferent_access
253
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1')
254
+ expected_config_attr = { :cores => '1', :cpulimit => '1' }
255
+ expected_volume_attr = ['scsi0', 'local-lvm2']
256
+ vm.expects(:move, expected_volume_attr)
257
+ vm.expects(:update, expected_config_attr)
258
+ @cr.save_vm(uuid, new_attributes)
259
+ end
260
+
261
+ it 'saves modified server config with modified volumes options' do
262
+ uuid = '100'
263
+ config = mock('config')
264
+ disks = mock('disks')
265
+ disk = mock('disk')
266
+ disk.stubs(:size).returns(1_073_741_824)
267
+ disk.stubs(:storage).returns('local-lvm')
268
+ disk.stubs(:volid).returns('local-lvm:vm-100-disk-0')
269
+ disk.stubs(:id).returns('scsi0')
270
+ disks.stubs(:get).returns(disk)
271
+ config.stubs(:disks).returns(disks)
272
+ config.stubs(:attributes).returns(:cores => '')
273
+ vm = mock('vm')
274
+ vm.stubs(:config).returns(config)
275
+ vm.stubs(:container?).returns(false)
276
+ vm.stubs(:type).returns('qemu')
277
+ vm.stubs(:node_id).returns('pve')
278
+ @cr.stubs(:find_vm_by_uuid).returns(vm)
279
+ new_attributes = {
280
+ 'templated' => '0',
281
+ 'node_id' => 'pve',
282
+ 'config_attributes' => {
283
+ 'cores' => '1',
284
+ 'cpulimit' => '1'
285
+ },
286
+ 'volumes_attributes' => {
287
+ '0' => {
288
+ 'id' => 'scsi0',
289
+ '_delete' => '',
290
+ 'volid' => 'local-lvm:vm-100-disk-0',
291
+ 'device' => '0',
292
+ 'controller' => 'scsi',
293
+ 'storage' => 'local-lvm',
294
+ 'size' => '1073741824',
295
+ 'cache' => 'directsync'
296
+ }
297
+ }
298
+ }.with_indifferent_access
299
+ @cr.stubs(:parse_server_vm).returns('vmid' => '100', 'node_id' => 'pve', 'type' => 'qemu', 'cores' => '1', 'cpulimit' => '1')
300
+ expected_config_attr = { :cores => '1', :cpulimit => '1' }
301
+ expected_volume_attr = { :id => 'scsi0', :volid => 'local-lvm:vm-100-disk-0', :size => 1_073_741_824 }, { :cache => 'directsync' }
302
+ vm.expects(:attach, expected_volume_attr)
303
+ vm.expects(:update, expected_config_attr)
304
+ @cr.save_vm(uuid, new_attributes)
305
+ end
306
+ end
307
+ end
308
+ end