foreman_netbox 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +619 -0
  3. data/README.md +50 -0
  4. data/Rakefile +50 -0
  5. data/app/contracts/foreman_netbox/device_contract.rb +42 -0
  6. data/app/contracts/foreman_netbox/params/interface_params.rb +16 -0
  7. data/app/contracts/foreman_netbox/params/ip_address_params.rb +15 -0
  8. data/app/contracts/foreman_netbox/params/tenant_params.rb +13 -0
  9. data/app/contracts/foreman_netbox/virtual_machine_contract.rb +29 -0
  10. data/app/interactors/foreman_netbox/concerns/facts.rb +11 -0
  11. data/app/interactors/foreman_netbox/concerns/primary_ips.rb +25 -0
  12. data/app/interactors/foreman_netbox/delete_host/delete_device.rb +20 -0
  13. data/app/interactors/foreman_netbox/delete_host/delete_virtual_machine.rb +20 -0
  14. data/app/interactors/foreman_netbox/delete_host/organizer.rb +12 -0
  15. data/app/interactors/foreman_netbox/sync_host/organizer.rb +35 -0
  16. data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +36 -0
  17. data/app/interactors/foreman_netbox/sync_host/sync_device/find.rb +41 -0
  18. data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +30 -0
  19. data/app/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url.rb +29 -0
  20. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create.rb +32 -0
  21. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find.rb +28 -0
  22. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/organizer.rb +20 -0
  23. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +34 -0
  24. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find.rb +28 -0
  25. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/organizer.rb +22 -0
  26. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create.rb +34 -0
  27. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find.rb +40 -0
  28. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/organizer.rb +22 -0
  29. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +33 -0
  30. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +37 -0
  31. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete.rb +36 -0
  32. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find.rb +30 -0
  33. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/organizer.rb +23 -0
  34. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +46 -0
  35. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
  36. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find.rb +32 -0
  37. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
  38. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +39 -0
  39. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +38 -0
  40. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +32 -0
  41. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/find.rb +28 -0
  42. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/organizer.rb +25 -0
  43. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +35 -0
  44. data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +85 -0
  45. data/app/interactors/foreman_netbox/sync_host/sync_device/validate.rb +25 -0
  46. data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +30 -0
  47. data/app/interactors/foreman_netbox/sync_host/sync_tenant/find.rb +26 -0
  48. data/app/interactors/foreman_netbox/sync_host/sync_tenant/organizer.rb +19 -0
  49. data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +33 -0
  50. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +34 -0
  51. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/find.rb +34 -0
  52. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +28 -0
  53. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url.rb +31 -0
  54. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +35 -0
  55. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find.rb +28 -0
  56. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/organizer.rb +22 -0
  57. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create.rb +34 -0
  58. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find.rb +32 -0
  59. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/organizer.rb +22 -0
  60. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +35 -0
  61. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +33 -0
  62. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete.rb +36 -0
  63. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find.rb +30 -0
  64. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/organizer.rb +23 -0
  65. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +46 -0
  66. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
  67. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find.rb +28 -0
  68. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
  69. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +39 -0
  70. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +38 -0
  71. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +82 -0
  72. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/validate.rb +25 -0
  73. data/app/jobs/foreman_netbox/sync_host_job.rb +19 -0
  74. data/app/lib/foreman_netbox/api.rb +12 -0
  75. data/app/models/concerns/foreman_netbox/host_extensions.rb +27 -0
  76. data/app/models/concerns/foreman_netbox/location_extensions.rb +13 -0
  77. data/app/models/concerns/foreman_netbox/nic/base_extensions.rb +23 -0
  78. data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +18 -0
  79. data/app/models/foreman_netbox/netbox_facet.rb +34 -0
  80. data/app/models/orchestration/netbox.rb +64 -0
  81. data/app/models/setting/netbox.rb +32 -0
  82. data/app/services/foreman_netbox/cached_netbox_parameters.rb +103 -0
  83. data/app/services/foreman_netbox/netbox_parameters.rb +185 -0
  84. data/app/services/foreman_netbox/netbox_parameters_comparator.rb +96 -0
  85. data/app/views/hosts/_netbox_tab.html.erb +28 -0
  86. data/config/routes.rb +4 -0
  87. data/db/migrate/20200527135812_create_netbox_facet.rb +12 -0
  88. data/db/migrate/20200604141933_add_sync_info_to_netbox_facet.rb +8 -0
  89. data/db/migrate/20200708093607_add_raw_data_to_netbox_facet.rb +7 -0
  90. data/lib/foreman_netbox.rb +6 -0
  91. data/lib/foreman_netbox/engine.rb +64 -0
  92. data/lib/foreman_netbox/version.rb +5 -0
  93. data/lib/tasks/foreman_netbox_tasks.rake +67 -0
  94. data/test/factories/host.rb +17 -0
  95. data/test/factories/netbox_facet.rb +17 -0
  96. data/test/fixtures/facts/k8s_physical_host.json +328 -0
  97. data/test/fixtures/facts/rhel_physical_host.json +512 -0
  98. data/test/fixtures/facts/rhel_virtual_host.json +455 -0
  99. data/test/fixtures/netbox_device_raw_data.json +330 -0
  100. data/test/fixtures/netbox_virtual_machine_raw_data.json +241 -0
  101. data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +81 -0
  102. data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +81 -0
  103. data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +93 -0
  104. data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +78 -0
  105. data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +65 -0
  106. data/test/interactors/foreman_netbox/sync_host/sync_device/find_test.rb +105 -0
  107. data/test/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url_test.rb +39 -0
  108. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create_test.rb +45 -0
  109. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find_test.rb +58 -0
  110. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +55 -0
  111. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find_test.rb +63 -0
  112. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create_test.rb +48 -0
  113. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find_test.rb +105 -0
  114. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +54 -0
  115. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +51 -0
  116. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete_test.rb +41 -0
  117. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find_test.rb +53 -0
  118. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
  119. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +72 -0
  120. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
  121. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
  122. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +92 -0
  123. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +51 -0
  124. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/find_test.rb +59 -0
  125. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +54 -0
  126. data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +188 -0
  127. data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +50 -0
  128. data/test/interactors/foreman_netbox/sync_host/sync_tenant/find_test.rb +58 -0
  129. data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +59 -0
  130. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +74 -0
  131. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/find_test.rb +77 -0
  132. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url_test.rb +39 -0
  133. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +54 -0
  134. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find_test.rb +58 -0
  135. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create_test.rb +50 -0
  136. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find_test.rb +65 -0
  137. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +54 -0
  138. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +53 -0
  139. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete_test.rb +37 -0
  140. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find_test.rb +53 -0
  141. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
  142. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +73 -0
  143. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
  144. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
  145. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +88 -0
  146. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +171 -0
  147. data/test/jobs/foreman_netbox/sync_host_job_test.rb +14 -0
  148. data/test/models/foreman_netbox/location_test.rb +42 -0
  149. data/test/models/foreman_netbox/netbox_facet_test.rb +219 -0
  150. data/test/models/foreman_netbox/nics/base_test.rb +81 -0
  151. data/test/models/foreman_netbox/usergroup_test.rb +23 -0
  152. data/test/models/host/managed_test.rb +63 -0
  153. data/test/services/foreman_netbox/netbox_attributes_test.rb +278 -0
  154. data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +136 -0
  155. data/test/test_plugin_helper.rb +25 -0
  156. metadata +343 -0
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class UpdateVirtualMachineInterfacesTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::SyncInterfaces::Update.call(
8
+ interfaces: interfaces,
9
+ host: host,
10
+ netbox_params: host.netbox_facet.netbox_params
11
+ )
12
+ end
13
+
14
+ let(:interfaces) { ForemanNetbox::API.client::Virtualization::Interfaces.new }
15
+ let(:old_mac) { 'FE:13:C6:44:29:22' }
16
+ let(:host) do
17
+ FactoryBot.build_stubbed(
18
+ :host,
19
+ interfaces: [
20
+ FactoryBot.build_stubbed(:nic_base, mac: old_mac)
21
+ ]
22
+ )
23
+ end
24
+
25
+ setup do
26
+ setup_default_netbox_settings
27
+ end
28
+
29
+ context 'if the interface has been updated since the last synchronization' do
30
+ let(:new_mac) { 'FE:13:C6:44:29:24' }
31
+
32
+ it 'updates interface in Netbox' do
33
+ get_stub = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/interfaces.json").with(
34
+ query: { limit: 50 }
35
+ ).to_return(
36
+ status: 200, headers: { 'Content-Type': 'application/json' },
37
+ body: {
38
+ count: 1,
39
+ results: [
40
+ {
41
+ id: 1,
42
+ name: host.interfaces.first.netbox_name,
43
+ mac_address: new_mac,
44
+ tags: []
45
+ }
46
+ ]
47
+ }.to_json
48
+ )
49
+ stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/virtualization/interfaces/1.json").with(
50
+ body: {
51
+ mac_address: host.interfaces.first.mac,
52
+ tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
53
+ }.to_json
54
+ ).to_return(
55
+ status: 200, headers: { 'Content-Type': 'application/json' },
56
+ body: { id: 1 }.to_json
57
+ )
58
+
59
+ subject
60
+ assert_requested(get_stub)
61
+ assert_requested(stub_patch)
62
+ end
63
+ end
64
+
65
+ context 'if the interface has not been updated since the last synchronization' do
66
+ it 'does not update interface on Netbox' do
67
+ get_stub = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/interfaces.json").with(
68
+ query: { limit: 50 }
69
+ ).to_return(
70
+ status: 200, headers: { 'Content-Type': 'application/json' },
71
+ body: {
72
+ count: 1,
73
+ results: [
74
+ {
75
+ id: 1,
76
+ name: host.interfaces.first.netbox_name,
77
+ mac_address: old_mac,
78
+ tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
79
+ }
80
+ ]
81
+ }.to_json
82
+ )
83
+
84
+ subject
85
+ assert_requested(get_stub)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class UpdateVirtualMachineTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::Update.call(
8
+ virtual_machine: virtual_machine,
9
+ host: host,
10
+ netbox_params: host.netbox_facet.netbox_params,
11
+ cluster: cluster,
12
+ tenant: tenant,
13
+ ip_addresses: ip_addresses
14
+ )
15
+ end
16
+
17
+ let(:virtual_machine) do
18
+ ForemanNetbox::API.client::Virtualization::VirtualMachine.new(id: 1).tap do |virtual_machine|
19
+ virtual_machine.instance_variable_set(
20
+ :@data,
21
+ {
22
+ 'id' => 1,
23
+ 'name' => virtual_machine_name,
24
+ 'cluster' => { 'id' => 1 },
25
+ 'tenant' => { 'id' => 1 },
26
+ 'vcpus' => 2,
27
+ 'memory' => 128,
28
+ 'disk' => 512,
29
+ 'primary_ip4' => {
30
+ 'id' => 1,
31
+ 'family' => 4,
32
+ 'address' => '10.0.0.1/24'
33
+ },
34
+ 'primary_ip6' => {
35
+ 'id' => 2,
36
+ 'family' => 6,
37
+ 'address' => '1600:0:2d0:201::18/64'
38
+ },
39
+ 'tags' => virtual_machine_tags
40
+ }
41
+ )
42
+ end
43
+ end
44
+
45
+ let(:virtual_machine_name) { 'name.example.com' }
46
+ let(:virtual_machine_tags) { ['tag'] }
47
+ let(:virtual_machine_data) { virtual_machine.instance_variable_get(:@data).deep_symbolize_keys }
48
+ let(:cluster) { OpenStruct.new(id: virtual_machine_data.dig(:cluster, :id)) }
49
+ let(:tenant) { OpenStruct.new(id: virtual_machine_data.dig(:tenant, :id)) }
50
+ let(:primary_ip4) do
51
+ OpenStruct.new(
52
+ id: virtual_machine_data.dig(:primary_ip4, :id),
53
+ address: OpenStruct.new(
54
+ address: virtual_machine_data.dig(:primary_ip4, :address).split('/')[0]
55
+ )
56
+ )
57
+ end
58
+ let(:primary_ip6) do
59
+ OpenStruct.new(
60
+ id: virtual_machine_data.dig(:primary_ip6, :id),
61
+ address: OpenStruct.new(
62
+ address: virtual_machine_data.dig(:primary_ip6, :address).split('/')[0]
63
+ )
64
+ )
65
+ end
66
+ let(:ip_addresses) { ForemanNetbox::API.client.ipam.ip_addresses.filter(virtual_machine_id: virtual_machine.id) }
67
+ let(:host) do
68
+ FactoryBot.build_stubbed(
69
+ :host,
70
+ hostname: virtual_machine_name,
71
+ ip: primary_ip4.address.address,
72
+ ip6: primary_ip6.address.address
73
+ ).tap do |host|
74
+ host.stubs(:compute?).returns(true)
75
+ host.stubs(:compute_object).returns(
76
+ OpenStruct.new(
77
+ cpus: virtual_machine_data[:vcpus],
78
+ memory_mb: virtual_machine_data[:memory],
79
+ volumes: [
80
+ OpenStruct.new(size_gb: virtual_machine_data[:disk])
81
+ ]
82
+ )
83
+ )
84
+ end
85
+ end
86
+
87
+ setup do
88
+ setup_default_netbox_settings
89
+ stub_request(:get, "#{Setting[:netbox_url]}/api/ipam/ip-addresses.json").with(
90
+ query: { limit: 50, virtual_machine_id: virtual_machine.id }
91
+ ).to_return(
92
+ status: 200, headers: { 'Content-Type': 'application/json' },
93
+ body: {
94
+ count: 2,
95
+ results: [
96
+ { id: primary_ip4.id, address: primary_ip4.address.address },
97
+ { id: primary_ip6.id, address: primary_ip6.address.address }
98
+ ]
99
+ }.to_json
100
+ )
101
+ end
102
+
103
+ context 'if the host has not been updated since the last synchronization' do
104
+ let(:virtual_machine_tags) { ForemanNetbox::NetboxParameters::DEFAULT_TAGS }
105
+
106
+ it 'does not update virtual_machine' do
107
+ assert_equal virtual_machine, subject.virtual_machine
108
+ end
109
+ end
110
+
111
+ context 'if the host has been updated since the last synchronization' do
112
+ let(:cluster) { OpenStruct.new(id: 2) }
113
+ let(:tenant) { OpenStruct.new(id: 2) }
114
+ let(:primary_ip4) do
115
+ OpenStruct.new(
116
+ id: 3,
117
+ address: OpenStruct.new(
118
+ address: '10.0.0.2'
119
+ )
120
+ )
121
+ end
122
+ let(:primary_ip6) do
123
+ OpenStruct.new(
124
+ id: 4,
125
+ address: OpenStruct.new(
126
+ address: '1500:0:2d0:201::2'
127
+ )
128
+ )
129
+ end
130
+ let(:host) do
131
+ FactoryBot.build_stubbed(
132
+ :host,
133
+ ip: primary_ip4.address.address,
134
+ ip6: primary_ip6.address.address
135
+ ).tap do |host|
136
+ host.stubs(:compute?).returns(true)
137
+ host.stubs(:compute_object).returns(
138
+ OpenStruct.new(
139
+ cpus: 4,
140
+ memory_mb: 256,
141
+ volumes: [
142
+ OpenStruct.new(size_gb: 1024)
143
+ ]
144
+ )
145
+ )
146
+ end
147
+ end
148
+
149
+ it 'updates virtual_machine' do
150
+ stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/virtualization/virtual-machines/#{virtual_machine.id}.json").with(
151
+ body: {
152
+ name: host.name,
153
+ cluster: cluster.id,
154
+ disk: host.compute_object.volumes.map(&:size_gb).reduce(&:+),
155
+ memory: host.compute_object.memory_mb,
156
+ primary_ip4: primary_ip4.id,
157
+ primary_ip6: primary_ip6.id,
158
+ tenant: tenant.id,
159
+ vcpus: host.compute_object.cpus,
160
+ tags: virtual_machine_tags | ForemanNetbox::NetboxParameters::DEFAULT_TAGS
161
+ }.to_json
162
+ ).to_return(
163
+ status: 200, headers: { 'Content-Type': 'application/json' },
164
+ body: { id: 1 }.to_json
165
+ )
166
+
167
+ subject
168
+ assert_requested(stub_patch)
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class SyncHostJobTest < ActiveJob::TestCase
6
+ let(:host) { FactoryBot.create(:host) }
7
+
8
+ test 'push host to the Netbox' do
9
+ Host::Managed.expects(:find).with(host.id).returns(host)
10
+ host.expects(:push_to_netbox)
11
+
12
+ ForemanNetbox::SyncHostJob.perform_now(host.id)
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ module ForemanNetbox
6
+ class LocationTest < ActiveSupport::TestCase
7
+ describe '#netbox_site_name' do
8
+ context 'with netbox_site parameter' do
9
+ let(:loc) do
10
+ FactoryBot.create(:location).tap do |location|
11
+ location.location_parameters.create!(name: 'netbox_site', value: 'My Site')
12
+ end
13
+ end
14
+
15
+ it { assert_equal loc.parameters['netbox_site'], loc.netbox_site_name }
16
+ end
17
+
18
+ context 'without netbox_site parameter' do
19
+ let(:loc) { FactoryBot.create(:location) }
20
+
21
+ it { assert_equal loc.name, loc.netbox_site_name }
22
+ end
23
+ end
24
+
25
+ describe '#netbox_site_slug' do
26
+ context 'with netbox_site parameter' do
27
+ let(:loc) do
28
+ FactoryBot.create(:location).tap do |location|
29
+ location.location_parameters.create!(name: 'netbox_site', value: 'My Site')
30
+ end
31
+ end
32
+
33
+ it { assert_equal loc.parameters['netbox_site'].parameterize, loc.netbox_site_slug }
34
+ end
35
+ context 'without netbox_site parameter' do
36
+ let(:loc) { FactoryBot.create(:location) }
37
+
38
+ it { assert_equal loc.name.parameterize, loc.netbox_site_slug }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,219 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ module ForemanNetbox
6
+ class NetboxFacetTest < ActiveSupport::TestCase
7
+ describe '#netbox_will_change?' do
8
+ let(:tenant_name) { raw_data.dig(:tenant, :name) }
9
+ let(:interfaces) { raw_data.dig(:interfaces, :results) }
10
+ let(:ip_addresses) { raw_data.dig(:ip_addresses, :results) }
11
+
12
+ context 'when the host is a device' do
13
+ let(:raw_data) { JSON.parse(file_fixture('netbox_device_raw_data.json').read).deep_symbolize_keys }
14
+ let(:device_name) { raw_data.dig(:device, :name) }
15
+ let(:site_name) { raw_data.dig(:site, :name) }
16
+ let(:device_serial) { raw_data.dig(:device, :serial) }
17
+ let(:manufacturer_name) { raw_data.dig(:manufacturer, :name) }
18
+ let(:device_type_model) { raw_data.dig(:device_type, :model) }
19
+ let(:host) do
20
+ FactoryBot.build_stubbed(
21
+ :host,
22
+ :managed,
23
+ :with_device_netbox_facet,
24
+ hostname: device_name,
25
+ owner: FactoryBot.build_stubbed(:usergroup, name: tenant_name),
26
+ location: FactoryBot.build_stubbed(:location, name: site_name),
27
+ interfaces: interfaces.map do |interface|
28
+ ip4 = ip_addresses.find do |ip|
29
+ ip[:interface][:name] == interface[:name] && ip[:family][:value] == 4
30
+ end
31
+ ip6 = ip_addresses.find do |ip|
32
+ ip[:interface][:name] == interface[:name] && ip[:family][:value] == 6
33
+ end
34
+
35
+ FactoryBot.build_stubbed(
36
+ :nic_base,
37
+ identifier: interface[:name],
38
+ mac: interface[:mac_address],
39
+ ip: ip4 && ip4[:address].split('/').first,
40
+ subnet: ip4 && FactoryBot.build_stubbed(
41
+ :subnet_ipv4,
42
+ cidr: ip4[:address].split('/').second
43
+ ),
44
+ ip6: ip6 && ip6[:address].split('/').first,
45
+ subnet6: ip6 && FactoryBot.build_stubbed(
46
+ :subnet_ipv6,
47
+ cidr: ip6[:address].split('/').second
48
+ )
49
+ )
50
+ end
51
+ ).tap do |host|
52
+ host.stubs(:compute?).returns(false)
53
+ host.stubs(:facts).returns(
54
+ serialnumber: device_serial,
55
+ manufacturer: manufacturer_name,
56
+ productname: device_type_model
57
+ )
58
+ end
59
+ end
60
+
61
+ context 'when no changes detected' do
62
+ it { assert_not host.netbox_facet.netbox_will_change? }
63
+ end
64
+
65
+ context 'when interfaces are changed' do
66
+ let(:interfaces) { [] }
67
+
68
+ it { assert host.netbox_facet.netbox_will_change? }
69
+ end
70
+
71
+ context 'when ip_addresses are changed' do
72
+ let(:ip_addresses) { [] }
73
+
74
+ it { assert host.netbox_facet.netbox_will_change? }
75
+ end
76
+
77
+ context 'when tenant is changed' do
78
+ let(:tenant_name) { 'New Tenant Name' }
79
+
80
+ it { assert host.netbox_facet.netbox_will_change? }
81
+ end
82
+
83
+ context 'when device is changed' do
84
+ let(:device_name) { 'new_device_name' }
85
+
86
+ it { assert host.netbox_facet.netbox_will_change? }
87
+ end
88
+
89
+ context 'when device type is changed' do
90
+ let(:device_type_model) { 'New Device Type Model' }
91
+
92
+ it { assert host.netbox_facet.netbox_will_change? }
93
+ end
94
+
95
+ context 'when manufacturer is changed' do
96
+ let(:manufacturer_name) { 'New Manufacturer Name' }
97
+
98
+ it { assert host.netbox_facet.netbox_will_change? }
99
+ end
100
+
101
+ context 'when manufacturer is changed' do
102
+ let(:site_name) { 'New Site Name' }
103
+
104
+ it { assert host.netbox_facet.netbox_will_change? }
105
+ end
106
+ end
107
+
108
+ context 'when the host is a virtual machine' do
109
+ let(:raw_data) { JSON.parse(file_fixture('netbox_virtual_machine_raw_data.json').read).deep_symbolize_keys }
110
+ let(:virtual_machine_name) { raw_data.dig(:virtual_machine, :name) }
111
+ let(:cluster_name) { raw_data.dig(:cluster, :name) }
112
+ let(:vcpus) { raw_data.dig(:virtual_machine, :vcpus) }
113
+ let(:memory) { raw_data.dig(:virtual_machine, :memory) }
114
+ let(:disk) { raw_data.dig(:virtual_machine, :disk) }
115
+ let(:host) do
116
+ FactoryBot.build_stubbed(
117
+ :host,
118
+ :managed,
119
+ :with_virtual_machine_netbox_facet,
120
+ hostname: virtual_machine_name,
121
+ owner: FactoryBot.build_stubbed(:usergroup, name: tenant_name),
122
+ interfaces: interfaces.map do |interface|
123
+ ip4 = ip_addresses.find do |ip|
124
+ ip[:interface][:name] == interface[:name] && ip[:family][:value] == 4
125
+ end
126
+ ip6 = ip_addresses.find do |ip|
127
+ ip[:interface][:name] == interface[:name] && ip[:family][:value] == 6
128
+ end
129
+
130
+ FactoryBot.build_stubbed(
131
+ :nic_base,
132
+ identifier: interface[:name],
133
+ mac: interface[:mac_address],
134
+ ip: ip4 && ip4[:address].split('/').first,
135
+ subnet: ip4 && FactoryBot.build_stubbed(
136
+ :subnet_ipv4,
137
+ cidr: ip4[:address].split('/').second
138
+ ),
139
+ ip6: ip6 && ip6[:address].split('/').first,
140
+ subnet6: ip6 && FactoryBot.build_stubbed(
141
+ :subnet_ipv6,
142
+ cidr: ip6[:address].split('/').second
143
+ )
144
+ )
145
+ end
146
+ ).tap do |host|
147
+ host.stubs(:compute?).returns(true)
148
+ host.stubs(:compute_object).returns(
149
+ OpenStruct.new(
150
+ cluster: cluster_name,
151
+ cpus: vcpus,
152
+ memory_mb: memory,
153
+ volumes: [
154
+ OpenStruct.new(size_gb: disk / 2),
155
+ OpenStruct.new(size_gb: disk / 2)
156
+ ]
157
+ )
158
+ )
159
+ host.stubs(:compute_resource).returns(
160
+ OpenStruct.new(type: 'Foreman::Model::Vmware')
161
+ )
162
+ end
163
+ end
164
+
165
+ context 'when no changes detected' do
166
+ it { assert_not host.netbox_facet.netbox_will_change? }
167
+ end
168
+
169
+ context 'when interfaces are changed' do
170
+ let(:interfaces) { [] }
171
+
172
+ it { assert host.netbox_facet.netbox_will_change? }
173
+ end
174
+
175
+ context 'when ip_addresses are changed' do
176
+ let(:ip_addresses) { [] }
177
+
178
+ it { assert host.netbox_facet.netbox_will_change? }
179
+ end
180
+
181
+ context 'when tenant is changed' do
182
+ let(:tenant_name) { 'New Tenant Name' }
183
+
184
+ it { assert host.netbox_facet.netbox_will_change? }
185
+ end
186
+
187
+ context 'when virtual machine is changed' do
188
+ let(:virtual_machine_name) { 'new_virtual_machine' }
189
+
190
+ it { assert host.netbox_facet.netbox_will_change? }
191
+ end
192
+
193
+ context 'when cpus is changed' do
194
+ let(:vcpus) { raw_data.dig(:virtual_machine, :vcpus) * 2 }
195
+
196
+ it { assert host.netbox_facet.netbox_will_change? }
197
+ end
198
+
199
+ context 'when memory is changed' do
200
+ let(:memory) { raw_data.dig(:virtual_machine, :memory) * 2 }
201
+
202
+ it { assert host.netbox_facet.netbox_will_change? }
203
+ end
204
+
205
+ context 'when disk is changed' do
206
+ let(:disk) { raw_data.dig(:virtual_machine, :disk) * 2 }
207
+
208
+ it { assert host.netbox_facet.netbox_will_change? }
209
+ end
210
+
211
+ context 'when cluster is changed' do
212
+ let(:cluster_name) { 'New Cluster Name' }
213
+
214
+ it { assert host.netbox_facet.netbox_will_change? }
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end