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,87 @@
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
+ module ForemanFogProxmox
21
+ module ProxmoxNodeMockFactory
22
+ def mock_node_servers(cr, servers)
23
+ node = mock('node')
24
+ nodes = mock('nodes')
25
+ node.stubs(:node).returns('pve')
26
+ node.stubs(:servers).returns(servers)
27
+ nodes.stubs(:get).returns(node)
28
+ nodes.stubs(:all).returns([node])
29
+ client = mock('client')
30
+ client.stubs(:nodes).returns(nodes)
31
+ cr.stubs(:client).returns(client)
32
+ cr
33
+ end
34
+
35
+ def mock_node_containers(cr, containers)
36
+ node = mock('node')
37
+ nodes = mock('nodes')
38
+ node.stubs(:node).returns('pve')
39
+ node.stubs(:containers).returns(containers)
40
+ nodes.stubs(:get).returns(node)
41
+ nodes.stubs(:all).returns([node])
42
+ client = mock('client')
43
+ client.stubs(:nodes).returns(nodes)
44
+ cr.stubs(:client).returns(client)
45
+ cr
46
+ end
47
+
48
+ def mock_node_servers_containers(cr, servers, containers)
49
+ node = mock('node')
50
+ node.stubs(:node).returns('pve')
51
+ node.stubs(:containers).returns(containers)
52
+ node.stubs(:servers).returns(servers)
53
+ nodes = mock('nodes')
54
+ nodes.stubs(:get).returns(node)
55
+ nodes.stubs(:all).returns([node])
56
+ client = mock('client')
57
+ client.stubs(:nodes).returns(nodes)
58
+ cr.stubs(:client).returns(client)
59
+ cr
60
+ end
61
+
62
+ def mock_cluster_nodes_servers_containers(cr, n1s, n1c, n2s, n2c)
63
+ node1 = mock('node')
64
+ node1.stubs(:node).returns('node1')
65
+ node1.stubs(:servers).returns(n1s)
66
+ node1.stubs(:containers).returns(n1c)
67
+ node2 = mock('node')
68
+ node2.stubs(:node).returns('node2')
69
+ node2.stubs(:servers).returns(n2s)
70
+ node2.stubs(:containers).returns(n2c)
71
+ nodes = mock('nodes')
72
+ nodes.stubs(:get).with('pve').returns(node1)
73
+ nodes.stubs(:get).with('pve2').returns(node2)
74
+ nodes.stubs(:all).returns([node1, node2])
75
+ client = mock('client')
76
+ client.stubs(:nodes).returns(nodes)
77
+ cr.stubs(:client).returns(client)
78
+ identity_client = mock('identity_client')
79
+ pools = mock('pools')
80
+ pools.stubs(:select).returns([])
81
+ pools.stubs(:all).returns([])
82
+ identity_client.stubs(:pools).returns(pools)
83
+ cr.stubs(:identity_client).returns(identity_client)
84
+ cr
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,134 @@
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
+ module ForemanFogProxmox
21
+ module ProxmoxServerMockFactory
22
+ def mock_server_vm
23
+ interface_attributes = {
24
+ id: 'net0',
25
+ macaddr: '36:25:8C:53:0C:50',
26
+ model: 'virtio',
27
+ name: nil,
28
+ ip: nil,
29
+ ip6: nil,
30
+ bridge: 'vmbr0',
31
+ firewall: nil,
32
+ link_down: nil,
33
+ rate: nil,
34
+ queues: nil,
35
+ tag: nil
36
+ }
37
+ interface = mock('interface')
38
+ interface.stubs(:attributes).returns(interface_attributes)
39
+ interfaces = [interface]
40
+ volume_attributes = {
41
+ id: 'scsi0',
42
+ volid: 'local-lvm:vm-100-disk-1',
43
+ size: 8,
44
+ storage: 'local-lvm',
45
+ cache: 'none',
46
+ replicate: nil,
47
+ media: nil,
48
+ format: nil,
49
+ model: 'scsi',
50
+ shared: nil,
51
+ snapshot: nil,
52
+ backup: nil,
53
+ aio: nil
54
+ }
55
+ volume = mock('volume')
56
+ volume.stubs(:attributes).returns(volume_attributes)
57
+ volumes = [volume]
58
+ config = mock('config')
59
+ config_attributes = {
60
+ vmid: 100,
61
+ digest: '0',
62
+ description: '',
63
+ ostype: 'l26',
64
+ smbios1: '0',
65
+ numa: 0,
66
+ kvm: 0,
67
+ vcpus: 1,
68
+ cores: 1,
69
+ bootdisk: 'scsi0',
70
+ onboot: 0,
71
+ boot: 'scsi0',
72
+ agent: 0,
73
+ scsihw: 'scsi',
74
+ sockets: 1,
75
+ memory: 512,
76
+ min_memory: 0,
77
+ shares: 0,
78
+ balloon: 0,
79
+ name: 'test',
80
+ cpu: 1,
81
+ cpulimit: nil,
82
+ cpuunits: nil,
83
+ keyboard: 'fr',
84
+ vga: 'std',
85
+ interfaces: interfaces,
86
+ disks: volumes
87
+ }
88
+ config.stubs(:attributes).returns(config_attributes)
89
+ config.stubs(:disks).returns(volumes)
90
+ config.stubs(:interfaces).returns(interfaces)
91
+ config.stubs(:respond_to?).returns(true)
92
+ vm = mock('vm')
93
+ vm.stubs(:respond_to?).returns(true)
94
+ vm.stubs(:config).returns(config)
95
+ vm.stubs(:type).returns('qemu')
96
+ vm.stubs(:identity).returns(100)
97
+ vm.stubs(:node_id).returns('pve')
98
+ service = mock('service')
99
+ vm_attributes = {
100
+ vmid: 100,
101
+ id: 'qemu/100',
102
+ node_id: 'pve',
103
+ config: config,
104
+ service: service,
105
+ name: 'test',
106
+ type: 'qemu',
107
+ maxdisk: 0,
108
+ disk: 0,
109
+ diskwrite: 0,
110
+ diskread: 0,
111
+ uptime: 0,
112
+ netout: 0,
113
+ netin: 0,
114
+ cpu: 1,
115
+ cpus: 1,
116
+ template: 0,
117
+ status: 'stopped',
118
+ maxcpu: 0,
119
+ mem: 0,
120
+ maxmem: 512,
121
+ qmpstatus: 'stopped',
122
+ ha: {},
123
+ pid: 0,
124
+ blockstat: 0,
125
+ balloon: 0,
126
+ ballooninfo: 0,
127
+ snapshots: []
128
+ }
129
+ vm.stubs(:attributes).returns(vm_attributes)
130
+ vm.stubs(:container?).returns(false)
131
+ [vm, config_attributes, volume_attributes, interface_attributes]
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,79 @@
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 'fog/compute/proxmox/models/node'
21
+
22
+ FactoryBot.define do
23
+ factory :proxmox_resource, :class => ComputeResource do
24
+ sequence(:name) { |n| "compute_resource#{n}" }
25
+ organizations { [Organization.find_by(name: 'Organization 1')] }
26
+ locations { [Location.find_by(name: 'Location 1')] }
27
+
28
+ trait :proxmox do
29
+ provider { 'Proxmox' }
30
+ user { 'root@pam' }
31
+ password { 'proxmox01' }
32
+ url { 'https://192.168.56.101:8006/api2/json' }
33
+ end
34
+
35
+ factory :proxmox_cr, :class => ForemanFogProxmox::Proxmox, :traits => [:proxmox]
36
+ end
37
+
38
+ factory :node, :class => Fog::Proxmox::Compute::Node do
39
+ sequence(:identity) { |n| "node#{n}" }
40
+ trait :pve do
41
+ identity { 'pve' }
42
+ end
43
+ trait :service do
44
+ service { :proxmox_cr }
45
+ end
46
+ factory :pve_node, :class => Fog::Proxmox::Compute::Node, :traits => [:pve, :service]
47
+ end
48
+
49
+ def deferred_nic_attrs
50
+ [:ip, :ip6, :mac, :subnet, :domain]
51
+ end
52
+
53
+ def set_nic_attributes(host, attributes, evaluator)
54
+ attributes.each do |nic_attribute|
55
+ next if evaluator.send(nic_attribute).blank?
56
+
57
+ host.primary_interface.send(:"#{nic_attribute}=", evaluator.send(nic_attribute))
58
+ end
59
+ host
60
+ end
61
+
62
+ factory :nic_base_empty, :class => Nic::Base do
63
+ type { 'Nic::Base' }
64
+ end
65
+
66
+ factory :nic_managed_empty, :class => Nic::Managed, :parent => :nic_base_empty do
67
+ type { 'Nic::Managed' }
68
+ identifier { 'net0' }
69
+ end
70
+
71
+ factory :host_empty, :class => Host do
72
+ sequence(:name) { |n| "host#{n}" }
73
+ sequence(:hostname) { |n| "host#{n}" }
74
+ trait :compute_attributes do
75
+ { 'type' => 'qemu' }
76
+ end
77
+ compute_attributes { { 'type' => 'qemu' } }
78
+ end
79
+ end
@@ -0,0 +1,48 @@
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
+ require 'test_plugin_helper'
20
+
21
+ module ForemanFogProxmox
22
+ class ComputeResourcesControllerTest < ActionController::TestCase
23
+ test 'should get isos by node and storage' do
24
+ get :isos_by_node_and_storage, params: { :node_id => 'pve', :storage => 'local' }
25
+ assert_response :found
26
+ show_response = @response.body
27
+ assert_not show_response.empty?
28
+ end
29
+ test 'should get ostemplates by node and storage' do
30
+ get :ostemplates_by_node_and_storage, params: { :node_id => 'pve', :storage => 'local' }
31
+ assert_response :found
32
+ show_response = @response.body
33
+ assert_not show_response.empty?
34
+ end
35
+ test 'should get isos by node' do
36
+ get :isos_by_node, params: { :node_id => 'pve' }
37
+ assert_response :found
38
+ show_response = @response.body
39
+ assert_not show_response.empty?
40
+ end
41
+ test 'should get ostemplates by node' do
42
+ get :ostemplates_by_node, params: { :node_id => 'pve' }
43
+ assert_response :found
44
+ show_response = @response.body
45
+ assert_not show_response.empty?
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This calls the main test_helper in Foreman-core
4
+
5
+ require 'simplecov'
6
+
7
+ SimpleCov.start do
8
+ add_filter '/test/'
9
+ add_group 'Controllers', 'app/controllers'
10
+ add_group 'Models', 'app/models'
11
+ add_group 'Helpers', 'app/helpers'
12
+ add_group 'Lib', 'lib'
13
+ end
14
+
15
+ require 'test_helper'
16
+
17
+ # Add plugin to FactoryBot's paths
18
+ FactoryBot.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
19
+ FactoryBot.reload
@@ -0,0 +1,204 @@
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
+ include ProxmoxVmHelper
26
+
27
+ describe 'parse' do
28
+ setup { Fog.mock! }
29
+ teardown { Fog.unmock! }
30
+
31
+ let(:host) do
32
+ { 'vmid' => '100',
33
+ 'name' => 'test',
34
+ 'type' => 'lxc',
35
+ 'node_id' => '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' => '' },
56
+ '1' => { 'id' => 'mp0', 'storage' => 'local-lvm', 'size' => '1073741824', 'mp' => '/opt/path' }
57
+ },
58
+ 'interfaces_attributes' => {
59
+ '0' => { 'id' => 'net0', 'name' => 'eth0', 'bridge' => 'vmbr0', 'ip' => 'dhcp', 'ip6' => 'dhcp', 'rate' => '', 'gw' => '192.168.56.100', 'gw6' => '2001:0:1234::c1c0:abcd:876' },
60
+ '1' => { 'id' => 'net1', 'name' => 'eth1', 'bridge' => 'vmbr0', 'ip' => 'dhcp', 'ip6' => 'dhcp', 'gw' => '192.168.56.100', 'gw6' => '2001:0:1234::c1c0:abcd:876' }
61
+ } }
62
+ end
63
+
64
+ let(:container) do
65
+ { 'vmid' => '100',
66
+ :vmid => '100',
67
+ 'name' => 'test',
68
+ 'type' => 'lxc',
69
+ :type => 'lxc',
70
+ 'node_id' => 'pve',
71
+ :node_id => 'pve',
72
+ :memory => 536_870_912,
73
+ 'templated' => 0,
74
+ :onboot => 0,
75
+ :swap => 536_870_912,
76
+ 'cores' => '1',
77
+ :arch => 'amd64',
78
+ :ostype => 'debian',
79
+ 'ostemplate' => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
80
+ 'ostemplate_storage' => 'local',
81
+ 'ostemplate_file' => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
82
+ 'password' => 'proxmox01',
83
+ :rootfs => 'local-lvm:1073741824',
84
+ :mp0 => 'local-lvm:1073741824,mp=/opt/path',
85
+ 'net0' => 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876',
86
+ 'net1' => 'model=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876' }
87
+ end
88
+
89
+ let(:host_delete) do
90
+ { 'vmid' => '100',
91
+ 'name' => 'test',
92
+ 'type' => 'lxc',
93
+ 'node_id' => 'pve',
94
+ 'volumes_attributes' => { '0' => { '_delete' => '1', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824', 'mp' => '/opt/path' } },
95
+ 'interfaces_attributes' => { '0' => { '_delete' => '1', 'id' => 'net0', 'name' => 'eth0' } } }
96
+ end
97
+
98
+ test '#memory' do
99
+ memory = parse_container_memory(host['config_attributes'])
100
+ assert memory.key?(:memory)
101
+ assert_equal 536_870_912, memory[:memory]
102
+ assert memory.key?(:swap)
103
+ assert_equal 536_870_912, memory[:swap]
104
+ end
105
+
106
+ test '#cpu' do
107
+ cpu = parse_container_cpu(host['config_attributes'])
108
+ assert cpu.key?(:cores)
109
+ assert_equal 1, cpu[:cores]
110
+ assert cpu.key?(:arch)
111
+ assert_equal 'amd64', cpu[:arch]
112
+ end
113
+
114
+ test '#ostemplate' do
115
+ ostemplate = parse_container_ostemplate(host)
116
+ expected_ostemplate = {
117
+ :ostemplate => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
118
+ :ostemplate_storage => 'local',
119
+ :ostemplate_file => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz'
120
+ }
121
+ assert_equal expected_ostemplate, ostemplate
122
+ end
123
+
124
+ test '#vm host' do
125
+ vm = parse_container_vm(host)
126
+ assert_equal 536_870_912, vm[:memory]
127
+ assert_equal 'local-lvm:1073741824', vm[:rootfs]
128
+ assert_equal 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876', vm[:net0]
129
+ assert_not vm.key?(:config)
130
+ assert_not vm.key?(:node)
131
+ assert_not vm.key?(:type)
132
+ end
133
+
134
+ test '#vm container' do
135
+ vm = parse_container_vm(host)
136
+ expected_vm = ActiveSupport::HashWithIndifferentAccess.new(
137
+ :vmid => '100',
138
+ :password => 'proxmox01',
139
+ :onboot => '0',
140
+ :ostype => 'debian',
141
+ :arch => 'amd64',
142
+ :cores => 1,
143
+ :memory => 536_870_912,
144
+ :swap => 536_870_912,
145
+ :ostemplate => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
146
+ :ostemplate_file => 'local:vztmpl/alpine-3.7-default_20171211_amd64.tar.xz',
147
+ :ostemplate_storage => 'local',
148
+ :net0 => 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876',
149
+ :net1 => 'name=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876',
150
+ :rootfs => 'local-lvm:1073741824',
151
+ :mp0 => 'local-lvm:1073741824,mp=/opt/path'
152
+ )
153
+ assert_equal expected_vm, vm
154
+ end
155
+
156
+ test '#volume with rootfs 1Gb' do
157
+ volumes = parse_container_volumes(host['volumes_attributes'])
158
+ assert_not volumes.empty?
159
+ assert_equal 2, volumes.size
160
+ assert rootfs = volumes.first
161
+ assert rootfs.key?(:rootfs)
162
+ assert_equal 'local-lvm:1073741824', rootfs[:rootfs]
163
+ assert mp0 = volumes[1]
164
+ assert mp0.key?(:mp0)
165
+ assert_equal 'local-lvm:1073741824,mp=/opt/path', mp0[:mp0]
166
+ end
167
+
168
+ test '#interface with name eth0 and bridge' do
169
+ deletes = []
170
+ nics = []
171
+ add_container_interface(host['interfaces_attributes']['0'], deletes, nics)
172
+ assert 1, nics.length
173
+ assert nics[0].key?(:net0)
174
+ assert_equal 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876', nics[0][:net0]
175
+ end
176
+
177
+ test '#interface with name eth1 and bridge' do
178
+ deletes = []
179
+ nics = []
180
+ add_container_interface(host['interfaces_attributes']['1'], deletes, nics)
181
+ assert 1, nics.length
182
+ assert nics[0].key?(:net1)
183
+ assert_equal 'name=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876', nics[0][:net1]
184
+ end
185
+
186
+ test '#interface delete net0' do
187
+ deletes = []
188
+ nics = []
189
+ add_container_interface(host_delete['interfaces_attributes']['0'], deletes, nics)
190
+ assert_empty nics
191
+ assert_equal 1, deletes.length
192
+ assert_equal 'net0', deletes[0]
193
+ end
194
+
195
+ test '#interfaces' do
196
+ interfaces_to_add, interfaces_to_delete = parse_container_interfaces(host['interfaces_attributes'])
197
+ assert_empty interfaces_to_delete
198
+ assert_equal 2, interfaces_to_add.length
199
+ assert_includes interfaces_to_add, { net0: 'name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876' }
200
+ assert_includes interfaces_to_add, { net1: 'name=eth1,bridge=vmbr0,ip=dhcp,ip6=dhcp,gw=192.168.56.100,gw6=2001:0:1234::c1c0:abcd:876' }
201
+ end
202
+ end
203
+ end
204
+ end