foreman_fog_proxmox 0.4.0 → 0.5.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 (67) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +24 -12
  3. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_compute_resource.js +26 -0
  4. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm.js +92 -0
  5. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm_container.js +60 -0
  6. data/app/assets/javascripts/foreman_fog_proxmox/{proxmox.js → proxmox_vm_server.js} +10 -10
  7. data/app/controllers/concerns/foreman_fog_proxmox/controller/parameters/compute_resource.rb +1 -1
  8. data/app/controllers/foreman_fog_proxmox/compute_resources_controller.rb +9 -1
  9. data/app/helpers/node_dashboard_helper.rb +36 -0
  10. data/app/helpers/proxmox_compute_selectors_helper.rb +23 -0
  11. data/app/helpers/proxmox_container_helper.rb +157 -0
  12. data/app/helpers/proxmox_form_helper.rb +67 -0
  13. data/app/helpers/{proxmox_compute_helper.rb → proxmox_server_helper.rb} +49 -38
  14. data/app/helpers/proxmox_vm_helper.rb +86 -0
  15. data/app/models/concerns/fog_extensions/proxmox/container.rb +57 -0
  16. data/{test/factories/foreman_fog_proxmox_factories.rb → app/models/concerns/fog_extensions/proxmox/container_config.rb} +16 -13
  17. data/app/models/concerns/fog_extensions/proxmox/server.rb +8 -2
  18. data/app/models/foreman_fog_proxmox/proxmox.rb +160 -67
  19. data/app/overrides/compute_resources_vms/form/add_clone_to_new_vm_compute_detail.rb +24 -0
  20. data/app/overrides/compute_resources_vms/form/add_vm_type_to_networks_form.rb +32 -0
  21. data/app/overrides/compute_resources_vms/form/add_vm_type_to_nic_provider_specific_form.rb +25 -0
  22. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_edit.rb +24 -0
  23. data/app/overrides/compute_resources_vms/form/add_vm_type_to_volumes_new_volume.rb +24 -0
  24. data/app/services/foreman_fog_proxmox/node_dashboard/data.rb +37 -0
  25. data/app/views/compute_resources/form/_proxmox.html.erb +7 -4
  26. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_form.html.erb +23 -0
  27. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_networks_new_childs_form.html.erb +25 -0
  28. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_nic_provider_specific_form.html.erb +23 -0
  29. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_volumes_edit.html.erb +73 -0
  30. data/app/views/compute_resources_vms/form/proxmox/_base.html.erb +16 -23
  31. data/app/views/compute_resources_vms/form/proxmox/_general.html.erb +37 -0
  32. data/app/views/compute_resources_vms/form/proxmox/_network.html.erb +2 -7
  33. data/app/views/compute_resources_vms/form/proxmox/container/_advanced.html.erb +31 -0
  34. data/app/views/compute_resources_vms/form/proxmox/container/_config.html.erb +41 -0
  35. data/app/views/compute_resources_vms/form/proxmox/container/_extended.html.erb +26 -0
  36. data/app/views/compute_resources_vms/form/proxmox/container/_network.html.erb +25 -0
  37. data/app/views/compute_resources_vms/form/proxmox/container/_volume_mp.html.erb +25 -0
  38. data/app/views/compute_resources_vms/form/proxmox/container/_volume_rootfs.html.erb +24 -0
  39. data/app/views/compute_resources_vms/form/proxmox/server/_advanced.html.erb +31 -0
  40. data/app/views/compute_resources_vms/form/proxmox/{_config.html.erb → server/_config.html.erb} +11 -8
  41. data/app/views/compute_resources_vms/form/proxmox/server/_network.html.erb +29 -0
  42. data/app/views/compute_resources_vms/form/proxmox/server/_volume.html.erb +27 -0
  43. data/app/views/compute_resources_vms/index/_proxmox.html.erb +4 -2
  44. data/app/views/compute_resources_vms/show/_proxmox.html.erb +9 -2
  45. data/app/views/dashboard/_foreman_fog_proxmox_widget.erb +4 -2
  46. data/app/views/images/form/_proxmox.html.erb +17 -0
  47. data/config/routes.rb +1 -0
  48. data/lib/foreman_fog_proxmox/engine.rb +10 -3
  49. data/lib/foreman_fog_proxmox/value.rb +24 -0
  50. data/lib/foreman_fog_proxmox/version.rb +1 -1
  51. data/locale/en/foreman_fog_proxmox.edit.po +456 -0
  52. data/locale/en/foreman_fog_proxmox.po +327 -11
  53. data/locale/en/foreman_fog_proxmox.po.time_stamp +0 -0
  54. data/locale/foreman_fog_proxmox.pot +515 -8
  55. data/locale/fr/foreman_fog_proxmox.edit.po +456 -0
  56. data/locale/fr/foreman_fog_proxmox.po +327 -11
  57. data/locale/fr/foreman_fog_proxmox.po.time_stamp +0 -0
  58. data/test/factories/proxmox_factory.rb +72 -0
  59. data/test/functional/compute_resources_controller_test.rb +34 -0
  60. data/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb +205 -0
  61. data/test/{helpers/proxmox_compute_helper_test.rb → unit/foreman_fog_proxmox/helpers/proxmox_server_helper_test.rb} +36 -28
  62. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_helper_test.rb +240 -0
  63. data/test/unit/foreman_fog_proxmox/proxmox_test.rb +327 -0
  64. data/test/unit/foreman_fog_proxmox/proxmox_test_helpers.rb +251 -0
  65. metadata +83 -14
  66. data/app/views/compute_resources_vms/form/proxmox/_volume.html.erb +0 -23
  67. data/test/unit/foreman_fog_proxmox_test.rb +0 -11
@@ -0,0 +1,205 @@
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
+
22
+ module ForemanFogProxmox
23
+ class ProxmoxContainerHelperTest < ActiveSupport::TestCase
24
+ include ProxmoxContainerHelper
25
+
26
+ describe 'parse' do
27
+
28
+ setup { Fog.mock! }
29
+ teardown { Fog.unmock! }
30
+
31
+ let(:host) do
32
+ { 'vmid' => '100',
33
+ 'name' => 'test',
34
+ 'type' => 'lxc',
35
+ 'node' => 'pve',
36
+ 'ostemplate_storage' => 'local',
37
+ 'ostemplate_file' => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
38
+ 'password' => 'proxmox01',
39
+ 'config_attributes' => {
40
+ 'onboot' => '0',
41
+ 'description' => '',
42
+ 'memory' => '536870912',
43
+ 'swap' => '536870912',
44
+ 'cores' => '1',
45
+ 'cpulimit' => '',
46
+ 'cpuunits' => '',
47
+ 'arch' => 'amd64',
48
+ 'ostype' => 'debian',
49
+ 'hostname' => '',
50
+ 'nameserver' => '',
51
+ 'searchdomain' => '',
52
+
53
+ },
54
+ 'volumes_attributes' => {
55
+ '0'=> { 'id' => 'rootfs', 'storage' => 'local-lvm', 'size' => '1073741824', 'cache' => nil },
56
+ '1'=> { 'id' => 'mp0', 'storage' => 'local-lvm', 'size' => '1073741824' }
57
+ },
58
+ 'interfaces_attributes' => {
59
+ '0' => { 'id' => 'net0', 'name' => 'eth0', 'bridge' => 'vmbr0', 'ip' => 'dhcp', 'ip6' => 'dhcp', 'rate' => nil },
60
+ '1' => { 'id' => 'net1', 'name' => 'eth1', 'bridge' => 'vmbr0', 'ip' => 'dhcp', 'ip6' => 'dhcp' }
61
+ }
62
+ }
63
+ end
64
+
65
+ let(:container) do
66
+ { 'vmid' => '100',
67
+ :vmid => '100',
68
+ 'name' => 'test',
69
+ 'type' => 'lxc',
70
+ :type => 'lxc',
71
+ 'node' => 'pve',
72
+ :node => {name: 'pve'},
73
+ :memory => 536870912,
74
+ 'templated' => 0,
75
+ :onboot => 0,
76
+ :swap => 536870912,
77
+ 'cores' => '1',
78
+ :arch => 'amd64',
79
+ :ostype => 'debian',
80
+ 'ostemplate' => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
81
+ 'ostemplate_storage' => 'local',
82
+ 'ostemplate_file' => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
83
+ 'password' => 'proxmox01',
84
+ :rootfs => 'local-lvm:1073741824',
85
+ :mp0 => 'local-lvm:1073741824',
86
+ 'net0' => 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp',
87
+ 'net1' => 'model=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp'
88
+ }
89
+ end
90
+
91
+ let(:host_delete) do
92
+ { 'vmid' => '100',
93
+ 'name' => 'test',
94
+ 'type' => 'lxc',
95
+ 'volumes_attributes' => { '0' => { '_delete' => '1', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824' }},
96
+ 'interfaces_attributes' => { '0' => { '_delete' => '1', 'id' => 'net0', 'name' => 'eth0' } }
97
+ }
98
+ end
99
+
100
+ test '#memory' do
101
+ memory = parse_container_memory(host['config_attributes'])
102
+ assert memory.has_key?(:memory)
103
+ assert_equal 536870912, memory[:memory]
104
+ assert memory.has_key?(:swap)
105
+ assert_equal 536870912, memory[:swap]
106
+ end
107
+
108
+ test '#cpu' do
109
+ cpu = parse_container_cpu(host['config_attributes'])
110
+ assert cpu.has_key?(:cores)
111
+ assert_equal 1, cpu[:cores]
112
+ assert cpu.has_key?(:arch)
113
+ assert_equal 'amd64', cpu[:arch]
114
+ end
115
+
116
+ test '#ostemplate' do
117
+ ostemplate = parse_container_ostemplate(host)
118
+ expected_ostemplate = {
119
+ :ostemplate => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
120
+ :ostemplate_storage => 'local',
121
+ :ostemplate_file => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz'
122
+ }
123
+ assert_equal expected_ostemplate, ostemplate
124
+ end
125
+
126
+ test '#vm host' do
127
+ vm = parse_container_vm(host)
128
+ assert_equal 536870912, vm[:memory]
129
+ assert_equal 'local-lvm:1073741824', vm[:rootfs]
130
+ assert_equal 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp', vm[:net0]
131
+ assert !vm.has_key?(:config)
132
+ assert !vm.has_key?(:node)
133
+ assert !vm.has_key?(:type)
134
+ end
135
+
136
+ test '#vm container' do
137
+ vm = parse_container_vm(host)
138
+ expected_vm = ActiveSupport::HashWithIndifferentAccess.new(
139
+ :vmid => '100',
140
+ :password => 'proxmox01',
141
+ :onboot => '0',
142
+ :ostype => 'debian',
143
+ :arch => 'amd64',
144
+ :cores => 1,
145
+ :memory => 536870912,
146
+ :swap => 536870912,
147
+ :ostemplate => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
148
+ :ostemplate_file => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
149
+ :ostemplate_storage => 'local',
150
+ :net0 => 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp',
151
+ :net1 => 'name=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp',
152
+ :rootfs => 'local-lvm:1073741824',
153
+ :mp0 => 'local-lvm:1073741824' )
154
+ assert_equal expected_vm.length, vm.length
155
+ assert_equal expected_vm.keys, vm.keys
156
+ assert_equal expected_vm, vm
157
+ end
158
+
159
+ test '#volume with rootfs 1Gb' do
160
+ volumes = parse_container_volumes(host['volumes_attributes'])
161
+ assert !volumes.empty?
162
+ assert volume = volumes.first
163
+ assert volume.has_key?(:rootfs)
164
+ assert_equal 'local-lvm:1073741824', volume[:rootfs]
165
+ end
166
+
167
+ test '#volume delete mp0' do
168
+ volumes = parse_container_volumes(host_delete['volumes_attributes'])
169
+ assert !volumes.empty?
170
+ assert_equal 1, volumes.length
171
+ assert volume = volumes.first
172
+ assert volume.has_key?(:delete)
173
+ assert_equal 'mp0', volume[:delete]
174
+ end
175
+
176
+ test '#interface with name eth0 and bridge' do
177
+ interface = parse_container_interface(host['interfaces_attributes']['0'])
178
+ assert interface.has_key?(:net0)
179
+ assert_equal 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp', interface[:net0]
180
+ end
181
+
182
+ test '#interface with name eth1 and bridge' do
183
+ interface = parse_container_interface(host['interfaces_attributes']['1'])
184
+ assert interface.has_key?(:net1)
185
+ assert_equal 'name=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp', interface[:net1]
186
+ end
187
+
188
+ test '#interface delete net0' do
189
+ interface = parse_container_interface(host_delete['interfaces_attributes']['0'])
190
+ assert interface.has_key?(:delete)
191
+ assert_equal interface[:delete], 'net0'
192
+ assert_equal 1, interface.length
193
+ end
194
+
195
+ test '#interfaces' do
196
+ interfaces = parse_container_interfaces(host['interfaces_attributes'])
197
+ assert !interfaces.empty?
198
+ assert_equal 2, interfaces.length
199
+ assert interfaces.include?({ net0: 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp'})
200
+ assert interfaces.include?({ net1: 'name=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp'})
201
+ end
202
+
203
+ end
204
+ end
205
+ end
@@ -19,8 +19,9 @@
19
19
 
20
20
  require 'test_plugin_helper'
21
21
 
22
- class ProxmoxComputeHelperTest < ActiveSupport::TestCase
23
- include ProxmoxComputeHelper
22
+ module ForemanFogProxmox
23
+ class ProxmoxServerHelperTest < ActiveSupport::TestCase
24
+ include ProxmoxServerHelper
24
25
 
25
26
  describe 'parse' do
26
27
 
@@ -31,8 +32,9 @@ class ProxmoxComputeHelperTest < ActiveSupport::TestCase
31
32
  { 'vmid' => '100',
32
33
  'name' => 'test',
33
34
  'node' => 'pve',
35
+ 'type' => 'qemu',
34
36
  'config_attributes' => {
35
- 'memory' => '512',
37
+ 'memory' => '536870912',
36
38
  'min_memory' => '',
37
39
  'ballon' => '',
38
40
  'shares' => '',
@@ -42,10 +44,12 @@ class ProxmoxComputeHelperTest < ActiveSupport::TestCase
42
44
  'cores' => '1',
43
45
  'sockets' => '1'
44
46
  },
45
- 'volumes_attributes' => {'0'=> { 'controller' => 'scsi', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824', 'cache' => 'none' }},
47
+ 'volumes_attributes' => {
48
+ '0'=> { 'controller' => 'scsi', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824', 'cache' => 'none' }
49
+ },
46
50
  'interfaces_attributes' => {
47
- '0' => { 'id' => 'net0', 'model' => 'virtio', 'bridge' => 'vmbr0' },
48
- '1' => { 'id' => 'net1', 'model' => 'e1000', 'bridge' => 'vmbr0' }
51
+ '0' => { 'id' => 'net0', 'model' => 'virtio', 'bridge' => 'vmbr0', 'firewall' => '0', 'disconnect' => '0', 'rate' => nil },
52
+ '1' => { 'id' => 'net1', 'model' => 'e1000', 'bridge' => 'vmbr0', 'firewall' => '0', 'disconnect' => '0' }
49
53
  }
50
54
  }
51
55
  end
@@ -53,45 +57,48 @@ class ProxmoxComputeHelperTest < ActiveSupport::TestCase
53
57
  let(:host_delete) do
54
58
  { 'vmid' => '100',
55
59
  'name' => 'test',
60
+ 'type' => 'qemu',
61
+ 'cdrom' => 'image',
62
+ 'cdrom_iso' => 'local-lvm:iso/debian-netinst.iso',
56
63
  'volumes_attributes' => { '0' => { '_delete' => '1', 'controller' => 'scsi', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824' }},
57
64
  'interfaces_attributes' => { '0' => { '_delete' => '1', 'id' => 'net0', 'model' => 'virtio' } }
58
65
  }
59
66
  end
60
67
 
61
68
  test '#memory' do
62
- memory = parse_memory(host['config_attributes'])
69
+ memory = parse_server_memory(host['config_attributes'])
63
70
  assert memory.has_key?(:memory)
64
- assert_equal memory[:memory], 512
71
+ assert_equal 536870912, memory[:memory]
65
72
  end
66
73
 
67
74
  test '#cpu' do
68
- cpu = parse_cpu(host['config_attributes'])
75
+ cpu = parse_server_cpu(host['config_attributes'])
69
76
  assert cpu.has_key?(:cpu)
70
- assert_equal cpu[:cpu], 'cputype=kvm64,flags=+spec-ctrl'
77
+ assert_equal 'cputype=kvm64,flags=+spec-ctrl', cpu[:cpu]
71
78
  end
72
79
 
73
80
  test '#vm' do
74
- vm = parse_vm(host)
75
- assert_equal vm['cores'], '1'
76
- assert_equal vm['sockets'], '1'
77
- assert_equal vm[:cpu], 'cputype=kvm64,flags=+spec-ctrl'
78
- assert_equal vm[:memory], 512
79
- assert_equal vm[:scsi0], 'local-lvm:1073741824,cache=none'
80
- assert_equal vm[:net0], 'model=virtio,bridge=vmbr0'
81
+ vm = parse_server_vm(host)
82
+ assert_equal '1', vm['cores']
83
+ assert_equal '1', vm['sockets']
84
+ assert_equal 'cputype=kvm64,flags=+spec-ctrl', vm[:cpu]
85
+ assert_equal 536870912, vm[:memory]
86
+ assert_equal 'local-lvm:1073741824,cache=none', vm[:scsi0]
87
+ assert_equal 'model=virtio,bridge=vmbr0,firewall=0,link_down=0', vm[:net0]
81
88
  assert !vm.has_key?(:config)
82
89
  assert !vm.has_key?(:node)
83
90
  end
84
91
 
85
92
  test '#volume with scsi 1Gb' do
86
- volumes = parse_volumes(host['volumes_attributes'])
93
+ volumes = parse_server_volumes(host['volumes_attributes'])
87
94
  assert !volumes.empty?
88
95
  assert volume = volumes.first
89
96
  assert volume.has_key?(:scsi0)
90
- assert_equal volume[:scsi0], 'local-lvm:1073741824,cache=none'
97
+ assert_equal 'local-lvm:1073741824,cache=none', volume[:scsi0]
91
98
  end
92
99
 
93
100
  test '#volume delete scsi0' do
94
- volumes = parse_volumes(host_delete['volumes_attributes'])
101
+ volumes = parse_server_volumes(host_delete['volumes_attributes'])
95
102
  assert !volumes.empty?
96
103
  assert_equal volumes.length, 1
97
104
  assert volume = volumes.first
@@ -100,31 +107,32 @@ class ProxmoxComputeHelperTest < ActiveSupport::TestCase
100
107
  end
101
108
 
102
109
  test '#interface with model virtio and bridge' do
103
- interface = parse_interface(host['interfaces_attributes']['0'].merge(device: '0'))
110
+ interface = parse_server_interface(host['interfaces_attributes']['0'])
104
111
  assert interface.has_key?(:net0)
105
- assert_equal interface[:net0], 'model=virtio,bridge=vmbr0'
112
+ assert_equal 'model=virtio,bridge=vmbr0,firewall=0,link_down=0', interface[:net0]
106
113
  end
107
114
 
108
115
  test '#interface with model e1000 and bridge' do
109
- interface = parse_interface(host['interfaces_attributes']['1'].merge(device: '1'))
116
+ interface = parse_server_interface(host['interfaces_attributes']['1'])
110
117
  assert interface.has_key?(:net1)
111
- assert_equal interface[:net1], 'model=e1000,bridge=vmbr0'
118
+ assert_equal 'model=e1000,bridge=vmbr0,firewall=0,link_down=0', interface[:net1]
112
119
  end
113
120
 
114
121
  test '#interface delete net0' do
115
- interface = parse_interface(host_delete['interfaces_attributes']['0'].merge(device: '0'))
122
+ interface = parse_server_interface(host_delete['interfaces_attributes']['0'])
116
123
  assert interface.has_key?(:delete)
117
124
  assert_match(/(scsi0,){0,1}net0(,scsi0){0,1}/, interface[:delete])
118
125
  assert_equal interface.length, 1
119
126
  end
120
127
 
121
128
  test '#interfaces' do
122
- interfaces = parse_interfaces(host['interfaces_attributes'])
129
+ interfaces = parse_server_interfaces(host['interfaces_attributes'])
123
130
  assert !interfaces.empty?
124
131
  assert_equal interfaces.length, 2
125
- assert interfaces.include?({ net0: 'model=virtio,bridge=vmbr0'})
126
- assert interfaces.include?({ net1: 'model=e1000,bridge=vmbr0'})
132
+ assert interfaces.include?({ net0: 'model=virtio,bridge=vmbr0,firewall=0,link_down=0'})
133
+ assert interfaces.include?({ net1: 'model=e1000,bridge=vmbr0,firewall=0,link_down=0'})
127
134
  end
128
135
 
129
136
  end
130
137
  end
138
+ end
@@ -0,0 +1,240 @@
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 'fog/compute/proxmox/models/server'
22
+ require 'fog/compute/proxmox/models/server_config'
23
+ require 'fog/compute/proxmox/models/container'
24
+ require 'fog/compute/proxmox/models/container_config'
25
+ require 'fog/compute/proxmox/models/interface'
26
+ require 'fog/compute/proxmox/models/interfaces'
27
+ require 'fog/compute/proxmox/models/disk'
28
+ require 'fog/compute/proxmox/models/disks'
29
+
30
+ module ForemanFogProxmox
31
+ class ProxmoxVmHelperTest < ActiveSupport::TestCase
32
+ include ProxmoxVmHelper
33
+
34
+ let(:container) do
35
+ Fog::Compute::Proxmox::Container.new(
36
+ { 'vmid' => '100',
37
+ 'hostname' => 'test',
38
+ 'type' => 'lxc',
39
+ 'node' => 'pve',
40
+ 'templated' => '0',
41
+ 'memory' => '536870912',
42
+ 'swap' => '',
43
+ 'cores' => '1',
44
+ 'arch' => 'amd64',
45
+ 'ostype' => 'debian',
46
+ 'rootfs' => 'local-lvm:1073741824',
47
+ 'mp0' => 'local-lvm:1073741824',
48
+ 'net0' => 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp',
49
+ 'net1' => 'name=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp'
50
+ })
51
+ end
52
+
53
+ let(:server) do
54
+ Fog::Compute::Proxmox::Server.new(
55
+ { 'vmid' => '100',
56
+ 'name' => 'test',
57
+ 'node' => 'pve',
58
+ 'type' => 'qemu',
59
+ 'templated' => '0',
60
+ 'ide2' => 'local-lvm:iso/debian-netinst.iso,media=cdrom',
61
+ 'memory' => '536870912',
62
+ 'min_memory' => '',
63
+ 'ballon' => '',
64
+ 'shares' => '',
65
+ 'cpu_type' => 'kvm64',
66
+ 'spectre' => '1',
67
+ 'pcid' => '0',
68
+ 'cores' => '1',
69
+ 'sockets' => '1',
70
+ 'scsi0' => 'local-lvm:1073741824,cache=none',
71
+ 'net0' => 'model=virtio,bridge=vmbr0',
72
+ 'net1' => 'model=e1000,bridge=vmbr0'
73
+ })
74
+ end
75
+
76
+ let(:host_server) do
77
+ { 'vmid' => '100',
78
+ 'name' => 'test',
79
+ 'node' => 'pve',
80
+ 'type' => 'qemu',
81
+ 'config_attributes' => {
82
+ 'memory' => '536870912',
83
+ 'min_memory' => nil,
84
+ 'ballon' => '',
85
+ 'shares' => '',
86
+ 'cpu_type' => 'kvm64',
87
+ 'spectre' => '1',
88
+ 'pcid' => '0',
89
+ 'cores' => '1',
90
+ 'sockets' => '1'
91
+ },
92
+ 'volumes_attributes' => {
93
+ '0'=> { 'controller' => 'scsi', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824', 'cache' => 'none' }
94
+ },
95
+ 'interfaces_attributes' => {
96
+ '0' => { 'id' => 'net0', 'model' => 'virtio', 'bridge' => 'vmbr0', 'firewall' => '0', 'disconnect' => '0' },
97
+ '1' => { 'id' => 'net1', 'model' => 'e1000', 'bridge' => 'vmbr0', 'firewall' => '0', 'disconnect' => '0' }
98
+ }
99
+ }
100
+ end
101
+
102
+ let(:host_container) do
103
+ { 'vmid' => '100',
104
+ 'name' => 'test',
105
+ 'type' => 'lxc',
106
+ 'node' => 'pve',
107
+ 'ostemplate_storage' => 'local',
108
+ 'ostemplate_file' => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
109
+ 'password' => 'proxmox01',
110
+ 'config_attributes' => {
111
+ 'onboot' => '0',
112
+ 'description' => '',
113
+ 'memory' => '536870912',
114
+ 'swap' => '',
115
+ 'cores' => '1',
116
+ 'cpulimit' => '',
117
+ 'cpuunits' => '',
118
+ 'arch' => 'amd64',
119
+ 'ostype' => 'debian',
120
+ 'hostname' => '',
121
+ 'nameserver' => '',
122
+ 'searchdomain' => '',
123
+
124
+ },
125
+ 'volumes_attributes' => {
126
+ '0'=> { 'id' => 'rootfs', 'storage' => 'local-lvm', 'size' => '1073741824' },
127
+ '1'=> { 'id' => 'mp0', 'storage' => 'local-lvm', 'size' => '1073741824' }
128
+ },
129
+ 'interfaces_attributes' => {
130
+ '0' => { 'id' => 'net0', 'name' => 'eth0', 'bridge' => 'vmbr0', 'ip' => 'dhcp', 'ip6' => 'dhcp' },
131
+ '1' => { 'id' => 'net1', 'name' => 'eth1', 'bridge' => 'vmbr0', 'ip' => 'dhcp', 'ip6' => 'dhcp' }
132
+ }
133
+ }
134
+ end
135
+
136
+ describe 'object_to_config_hash' do
137
+
138
+ setup { Fog.mock! }
139
+ teardown { Fog.unmock! }
140
+
141
+ it '#server qemu' do
142
+ config_hash = object_to_config_hash(server,'qemu')
143
+ expected_config_hash = ActiveSupport::HashWithIndifferentAccess.new(server.config.attributes).reject { |key,_value| %w[templated node type ide2 scsi0 net0 net1].include? key }
144
+ assert_equal expected_config_hash, config_hash['config_attributes']
145
+ end
146
+
147
+ it '#server lxc' do
148
+ config_hash = object_to_config_hash(server,'lxc')
149
+ assert config_hash.has_key?('config_attributes')
150
+ expected_config_hash = ActiveSupport::HashWithIndifferentAccess.new(server.config.attributes).reject { |key,_value| %w[templated node type ide2 scsi0 net0 net1].include? key }
151
+ assert_equal expected_config_hash, config_hash['config_attributes']
152
+ end
153
+
154
+ it '#container qemu' do
155
+ config_hash = object_to_config_hash(container,'qemu')
156
+ assert config_hash.has_key?('config_attributes')
157
+ expected_config_hash = ActiveSupport::HashWithIndifferentAccess.new(container.config.attributes).reject { |key,_value| %w[templated node type rootfs mp0 net0 net1].include? key }
158
+ assert_equal expected_config_hash, config_hash['config_attributes']
159
+ end
160
+
161
+ it '#container lxc' do
162
+ config_hash = object_to_config_hash(container,'lxc')
163
+ assert config_hash.has_key?('config_attributes')
164
+ expected_config_hash = ActiveSupport::HashWithIndifferentAccess.new(container.config.attributes).reject { |key,_value| %w[templated node type rootfs mp0 net0 net1].include? key }
165
+ assert_equal expected_config_hash, config_hash['config_attributes']
166
+ end
167
+ end
168
+
169
+
170
+ describe 'convert_sizes' do
171
+
172
+ setup { Fog.mock! }
173
+ teardown { Fog.unmock! }
174
+
175
+ it '#server' do
176
+ convert_sizes(host_server)
177
+ assert_equal '512', host_server['config_attributes']['memory']
178
+ assert_equal '1', host_server['volumes_attributes']['0']['size']
179
+ end
180
+
181
+ it '#container' do
182
+ convert_sizes(host_container)
183
+ assert_equal '512', host_container['config_attributes']['memory']
184
+ assert_equal '1', host_container['volumes_attributes']['0']['size']
185
+ end
186
+ end
187
+
188
+ describe 'convert_memory_size' do
189
+
190
+ setup { Fog.mock! }
191
+ teardown { Fog.unmock! }
192
+
193
+ it '#server memory' do
194
+ convert_memory_size(host_server['config_attributes'],'memory')
195
+ assert_equal '512', host_server['config_attributes']['memory']
196
+ end
197
+
198
+ it '#server min_memory nil' do
199
+ convert_memory_size(host_server['config_attributes'],'min_memory')
200
+ assert_nil host_server['config_attributes']['min_memory']
201
+ end
202
+
203
+ it '#container memory' do
204
+ convert_memory_size(host_container['config_attributes'],'memory')
205
+ assert_equal '512', host_container['config_attributes']['memory']
206
+ end
207
+
208
+ it '#container swap empty' do
209
+ convert_memory_size(host_container['config_attributes'],'swap')
210
+ assert host_container['config_attributes']['swap'].empty?
211
+ end
212
+ end
213
+
214
+ describe 'parse_type_and_vmid' do
215
+
216
+ setup { Fog.mock! }
217
+ teardown { Fog.unmock! }
218
+
219
+ it "raises Foreman::Exception when the uuid does not match" do
220
+ err = assert_raises Foreman::Exception do
221
+ parse_type_and_vmid('100')
222
+ end
223
+ assert err.message.end_with?('Invalid uuid=[100].')
224
+ end
225
+
226
+ it '#server' do
227
+ type, vmid = parse_type_and_vmid('qemu_100')
228
+ assert_equal 'qemu', type
229
+ assert_equal '100', vmid
230
+ end
231
+
232
+ it '#container' do
233
+ type, vmid = parse_type_and_vmid('lxc_100')
234
+ assert_equal 'lxc', type
235
+ assert_equal '100', vmid
236
+ end
237
+ end
238
+
239
+ end
240
+ end