-
Notifications
You must be signed in to change notification settings - Fork 988
/
host_related.rb
633 lines (546 loc) · 20.8 KB
/
host_related.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# Helpers
def deferred_nic_attrs
[:ip, :ip6, :mac, :subnet, :domain]
end
def set_nic_attributes(host, attributes, evaluator)
attributes.each do |nic_attribute|
next unless evaluator.send(nic_attribute).present?
host.primary_interface.send(:"#{nic_attribute}=", evaluator.send(nic_attribute))
end
host
end
FactoryBot.define do
factory :ptable do
sequence(:name) { |n| "ptable#{n}" }
layout { "zerombr\nclearpart --all --initlabel\npart /boot --fstype ext3 --size=<%= 10 * 10 %> --asprimary\npart / --fstype ext3 --size=1024 --grow\npart swap --recommended" }
os_family { 'Redhat' }
organizations { [Organization.find_by_name('Organization 1')] }
locations { [Location.find_by_name('Location 1')] }
trait :ubuntu do
sequence(:name) { |n| "ubuntu default#{n}" }
layout { "d-i partman-auto/disk string /dev/sda\nd-i partman-auto/method string regular..." }
os_family { 'Debian' }
end
trait :ubuntu_autoinstall do
sequence(:name) { |n| "ubuntu default autoinstall#{n}" }
layout { "storage:\n layout:\n name: lvm\n" }
os_family { 'Debian' }
end
trait :suse do
sequence(:name) { |n| "suse default#{n}" }
layout { "<partitioning config:type=\"list\">\n <drive>\n <device>/dev/hda</device>\n <use>all</use>\n </drive>\n</partitioning>" }
os_family { 'Suse' }
end
trait :windows do
sequence(:name) { |n| "windows default#{n}" }
layout { "select disk 0\nclean\nconvert gpt\ncreate partition efi size=200\nformat quick fs=fat32 label=\"System\"\nassign letter=\"S\"\ncreate partition msr size=16\ncreate partition primary\nformat quick fs=ntfs label=\"Windows\"\nassign letter=\"W\"\nlist volume\nexit" }
os_family { 'Windows' }
end
end
factory :parameter do
sequence(:name) { |n| "parameter#{n}" }
sequence(:value) { |n| "parameter value #{n}" }
type { 'CommonParameter' }
end
factory :host_parameter, :parent => :parameter, :class => HostParameter do
type { 'HostParameter' }
end
factory :hostgroup_parameter, :parent => :parameter, :class => GroupParameter do
type { 'GroupParameter' }
end
factory :nic_base, :class => Nic::Base do
type { 'Nic::Base' }
sequence(:identifier) { |n| "eth#{n}" }
sequence(:mac) { |n| "00:23:45:ab:" + n.to_s(16).rjust(4, '0').insert(2, ':') }
trait :with_subnet do
subnet do
FactoryBot.build(:subnet_ipv4,
:organizations => host ? [host.organization] : [],
:locations => host ? [host.location] : [])
end
end
end
factory :nic_interface, :class => Nic::Interface, :parent => :nic_base do
type { 'Nic::Interface' }
end
factory :nic_managed, :class => Nic::Managed, :parent => :nic_interface do
type { 'Nic::Managed' }
sequence(:mac) { |n| "00:33:45:ab:" + n.to_s(16).rjust(4, '0').insert(2, ':') }
sequence(:ip) { |n| IPAddr.new((subnet.present? ? subnet.ipaddr.to_i : 0) + n, Socket::AF_INET).to_s }
trait :without_ipv4 do
ip { nil }
end
trait :with_ipv6 do
sequence(:ip6) { |n| Array.new(4) { '%x' % rand(16**4) }.join(':') + '::' + n.to_s }
end
end
factory :nic_bmc, :class => Nic::BMC, :parent => :nic_managed do
type { 'Nic::BMC' }
sequence(:mac) { |n| "00:43:56:cd:" + n.to_s(16).rjust(4, '0').insert(2, ':') }
sequence(:ip) { |n| IPAddr.new((subnet.present? ? subnet.ipaddr.to_i : 256 * 256 * 256) + n, Socket::AF_INET).to_s }
provider { 'IPMI' }
username { 'admin' }
password { 'admin' }
end
factory :nic_bond, :class => Nic::Bond, :parent => :nic_managed do
type { 'Nic::Bond' }
mode { 'balance-rr' }
end
factory :nic_bridge, :class => Nic::Bridge, :parent => :nic_managed do
type { 'Nic::Bridge' }
end
factory :nic_primary_and_provision, :parent => :nic_managed, :class => Nic::Managed do
primary { true }
provision { true }
sequence(:mac) { |n| "00:53:67:ab:" + n.to_s(16).rjust(4, '0').insert(2, ':') }
sequence(:ip) { |n| IPAddr.new(n, Socket::AF_INET).to_s }
end
factory :model do
sequence(:name) { |n| "hal900#{n}" }
end
factory :host do
sequence(:name) { |n| "host#{n}" }
sequence(:hostname) { |n| "host#{n}" }
root_pass { 'xybxa6JUkz63w' }
organization { Organization.find_by_name('Organization 1') }
location { Location.find_by_name('Location 1') }
# This allows a test to declare build/create(:host, :ip => '1.2.3.4') and
# have the primary interface correctly updated with the specified attrs
after(:build) do |host, evaluator|
unless host.primary_interface.present?
opts = {
:primary => true,
:domain => FactoryBot.build(:domain),
}
host.interfaces << FactoryBot.build(:nic_managed, opts)
end
set_nic_attributes(host, deferred_nic_attrs, evaluator)
end
trait :with_build do
build { true }
end
trait :with_model do
model
end
trait :with_medium do
medium
end
trait :with_hostgroup do
hostgroup { FactoryBot.create(:hostgroup, :with_domain, :with_os) }
end
trait :with_config_group do
config_groups { [FactoryBot.create(:config_group)] }
end
trait :with_parameter do
after(:create) do |host, evaluator|
FactoryBot.create(:host_parameter, :host => host)
end
end
trait :with_facts do
transient do
fact_count { 20 }
end
after(:create) do |host, evaluator|
evaluator.fact_count.times do
FactoryBot.create(:fact_value, :host => host)
end
end
end
trait :with_reports do
transient do
report_count { 5 }
end
after(:create) do |host, evaluator|
evaluator.report_count.times do |i|
FactoryBot.create(:report, :host => host, :reported_at => (evaluator.report_count - i).minutes.ago)
end
host.update_attribute(:last_report, host.reports.last.reported_at)
end
end
trait :on_compute_resource do
sequence :uuid do |n|
Foreman.uuid
end
compute_resource do
taxonomies = {}
# add taxonomy overrides in case it's set in the host object
taxonomies[:locations] = [location] unless location.nil?
taxonomies[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:ec2_cr, taxonomies)
end
before(:create) { |host| host.expects(:queue_compute) }
end
trait :with_compute_profile do
after(:build) do |host|
host.compute_profile = FactoryBot.create(:compute_profile, :with_compute_attribute, :compute_resource => host.compute_resource)
end
end
trait :with_subnet do
after(:build) do |host|
overrides = {}
overrides[:locations] = [host.location] unless host.location.nil?
overrides[:organizations] = [host.organization] unless host.organization.nil?
host.subnet = FactoryBot.build(:subnet_ipv4, overrides)
host.ip = IPAddr.new(IPAddr.new(host.subnet.network).to_i + 1, Socket::AF_INET).to_s
end
end
trait :with_ipv6_subnet do
after(:build) do |host|
overrides = {}
overrides[:locations] = [host.location] unless host.location.nil?
overrides[:organizations] = [host.organization] unless host.organization.nil?
host.subnet6 = FactoryBot.build(:subnet_ipv6, overrides)
end
end
trait :with_operatingsystem do
operatingsystem
end
trait :with_puppet_ca do
puppet_ca_proxy do
FactoryBot.create(:smart_proxy, :features => [FactoryBot.create(:feature, :puppetca)])
end
end
trait :managed do
managed { true }
pxe_loader { "Grub2 UEFI" }
architecture { operatingsystem.try(:architectures).try(:first) }
medium { operatingsystem.try(:media).try(:first) }
ptable { operatingsystem.try(:ptables).try(:first) }
domain
interfaces { [FactoryBot.build(:nic_primary_and_provision)] }
association :operatingsystem, :with_associations
end
trait :debian do
operatingsystem { FactoryBot.build(:debian7_0, :with_associations) }
end
trait :suse do
operatingsystem { FactoryBot.build(:suse, :with_associations) }
end
trait :redhat do
operatingsystem { FactoryBot.build(:rhel7_5, :with_associations) }
end
trait :with_ipv6 do
subnet6 do
overrides = {:dns => FactoryBot.create(:dns_smart_proxy)}
# add taxonomy overrides in case it's set in the host object
overrides[:locations] = [location] unless location.nil?
overrides[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:subnet_ipv6, :dns, overrides)
end
interfaces do
[FactoryBot.build(:nic_managed,
:primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:ip6 => IPAddr.new(subnet6.ipaddr.to_i + 1, subnet6.family).to_s)]
end
end
trait :dualstack do
with_ipv6
subnet do
overrides = {:dns => FactoryBot.create(:dns_smart_proxy)}
# add taxonomy overrides in case it's set in the host object
overrides[:locations] = [location] unless location.nil?
overrides[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:subnet_ipv4, overrides)
end
interfaces do
[FactoryBot.build(:nic_managed,
:primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:ip => subnet.network.sub(/0\Z/, '1'),
:ip6 => IPAddr.new(subnet6.ipaddr.to_i + 1, subnet6.family).to_s)]
end
end
factory :host_for_snapshots_base do
name { 'snapshot-ipv4-dhcp-el7' }
hostname { name }
managed { true }
domain { FactoryBot.build(:domain_for_snapshots) }
subnet { FactoryBot.build(:subnet_ipv4_dhcp_for_snapshots) }
pxe_loader { "PXELinux BIOS" }
architecture { operatingsystem.try(:architectures).try(:first) }
medium { operatingsystem.try(:media).try(:first) }
ptable { operatingsystem.try(:ptables).try(:first) }
root_pass { '$1$rtd8Ub7R$5Ohzuy8WXlkaK9cA2T1wb0' }
certname { name }
factory :host_for_snapshots_ipv4_dhcp_el7 do
operatingsystem { FactoryBot.build(:for_snapshots_centos_7_0) }
end
factory :host_for_snapshots_ipv4_dhcp_deb10 do
operatingsystem { FactoryBot.build(:for_snapshots_debian_10) }
end
factory :host_for_snapshots_ipv4_dhcp_ubuntu18 do
operatingsystem { FactoryBot.build(:for_snapshots_ubuntu_18) }
end
factory :host_for_snapshots_ipv4_dhcp_ubuntu20 do
ptable { FactoryBot.build(:ptable, :ubuntu_autoinstall) }
operatingsystem { FactoryBot.build(:for_snapshots_ubuntu_20) }
end
factory :host_for_snapshots_ipv4_dhcp_rhel9 do
operatingsystem { FactoryBot.build(:for_snapshots_rhel9) }
end
factory :host_for_snapshots_ipv4_dhcp_rocky8 do
operatingsystem { FactoryBot.build(:for_snapshots_rocky8) }
end
factory :host_for_snapshots_ipv4_dhcp_rocky9 do
operatingsystem { FactoryBot.build(:for_snapshots_rocky9) }
end
factory :host_for_snapshots_ipv4_dhcp_windows10 do
operatingsystem { FactoryBot.build(:for_snapshots_windows10) }
end
end
trait :with_dhcp_orchestration do
managed
compute_resource do
taxonomies = {}
# add taxonomy overrides in case it's set in the host object
taxonomies[:locations] = [location] unless location.nil?
taxonomies[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:libvirt_cr, taxonomies)
end
domain
subnet do
overrides = {
:dhcp => FactoryBot.create(:dhcp_smart_proxy),
}
# add taxonomy overrides in case it's set in the host object
overrides[:locations] = [location] unless location.nil?
overrides[:organizations] = [organization] unless organization.nil?
FactoryBot.create(
:subnet_ipv4_with_bmc,
overrides
)
end
interfaces do
[FactoryBot.build(:nic_primary_and_provision, :ip => subnet.network.sub(/0\Z/, '1'))]
end
end
trait :with_dns_orchestration do
managed
compute_resource do
taxonomies = {}
# add taxonomy overrides in case it's set in the host object
taxonomies[:locations] = [location] unless location.nil?
taxonomies[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:libvirt_cr, taxonomies)
end
subnet do
overrides = {:dns => FactoryBot.create(:dns_smart_proxy)}
# add taxonomy overrides in case it's set in the host object
overrides[:locations] = [location] unless location.nil?
overrides[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:subnet_ipv4, overrides)
end
domain do
FactoryBot.create(:domain,
:dns => FactoryBot.create(:smart_proxy,
:features => [FactoryBot.create(:feature, :dns)])
)
end
interfaces do
[FactoryBot.build(:nic_managed, :primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:ip => subnet.network.sub(/0\Z/, '1'))]
end
end
trait :with_ipv6_dns_orchestration do
managed
compute_resource do
taxonomies = {}
# add taxonomy overrides in case it's set in the host object
taxonomies[:locations] = [location] unless location.nil?
taxonomies[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:libvirt_cr, taxonomies)
end
subnet6 do
overrides = {:dns => FactoryBot.create(:dns_smart_proxy)}
# add taxonomy overrides in case it's set in the host object
overrides[:locations] = [location] unless location.nil?
overrides[:organizations] = [organization] unless organization.nil?
FactoryBot.create(:subnet_ipv6, :dns, overrides)
end
domain do
FactoryBot.create(:domain,
:dns => FactoryBot.create(:smart_proxy,
:features => [FactoryBot.create(:feature, :dns)])
)
end
interfaces do
[FactoryBot.build(:nic_managed, :without_ipv4,
:primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:ip6 => IPAddr.new(subnet6.ipaddr.to_i + 1, subnet6.family).to_s)]
end
end
trait :with_dual_stack_dns_orchestration do
with_dns_orchestration
with_ipv6_dns_orchestration
interfaces do
[FactoryBot.build(:nic_managed,
:primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:ip => subnet.network.sub(/0\Z/, '1'),
:ip6 => IPAddr.new(subnet6.ipaddr.to_i + 1, subnet6.family).to_s)]
end
end
trait :with_tftp_subnet do
subnet { FactoryBot.build(:subnet_ipv4, :tftp, locations: [location], organizations: [organization]) }
end
trait :with_tftp_and_dhcp_subnet do
subnet { FactoryBot.build(:subnet_ipv4, :tftp, :dhcp, locations: [location], organizations: [organization]) }
end
trait :with_tftp_and_httpboot_subnet do
subnet { FactoryBot.build(:subnet_ipv4, :tftp, :httpboot, locations: [location], organizations: [organization]) }
end
trait :with_templates_subnet do
subnet { FactoryBot.build(:subnet_ipv4, :template, locations: [location], organizations: [organization]) }
end
trait :with_separate_provision_interface do
interfaces do
[FactoryBot.build(:nic_managed,
:primary => true,
:provision => false,
:domain => FactoryBot.build(:domain)),
FactoryBot.build(:nic_managed,
:primary => false,
:provision => true,
:domain => FactoryBot.build(:domain),
:subnet => FactoryBot.build(:subnet_ipv4, :tftp, locations: [location], organizations: [organization]))]
end
end
trait :with_separate_provision_interface_dualstack do
interfaces do
[
FactoryBot.build(
:nic_managed,
:primary => true,
:provision => false,
:domain => FactoryBot.build(:domain)
),
FactoryBot.build(
:nic_managed,
:primary => false,
:provision => true,
:domain => FactoryBot.build(:domain),
:subnet => FactoryBot.build(:subnet_ipv4, :tftp, locations: [location], organizations: [organization]),
:subnet6 => FactoryBot.build(:subnet_ipv6, :tftp, locations: [location], organizations: [organization])
),
]
end
end
trait :with_tftp_v6_subnet do
subnet6 { FactoryBot.build(:subnet_ipv6, :tftp, locations: [location], organizations: [organization]) }
end
trait :with_tftp_orchestration do
managed
with_tftp_and_dhcp_subnet
interfaces do
[FactoryBot.build(:nic_managed, :primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:subnet => subnet,
:ip => subnet.network.sub(/0\Z/, '2'))]
end
end
trait :with_tftp_orchestration_and_httpboot do
managed
with_tftp_and_httpboot_subnet
interfaces do
[FactoryBot.build(:nic_managed, :primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:subnet => subnet,
:ip => subnet.network.sub(/0\Z/, '2'))]
end
end
trait :with_tftp_v6_orchestration do
managed
with_tftp_v6_subnet
interfaces do
[FactoryBot.build(:nic_managed, :primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:subnet6 => subnet6,
:ip6 => IPAddr.new(subnet6.ipaddr.to_i + 1, subnet6.family).to_s)]
end
end
trait :with_tftp_dual_stack_orchestration do
managed
with_tftp_subnet
with_tftp_v6_subnet
interfaces do
[FactoryBot.build(:nic_managed, :primary => true,
:provision => true,
:domain => FactoryBot.build(:domain),
:subnet => subnet,
:subnet6 => subnet6,
:ip => subnet.network.sub(/0\Z/, '2'),
:ip6 => IPAddr.new(subnet6.ipaddr.to_i + 1, subnet6.family).to_s)]
end
end
trait :with_realm do
realm
end
trait :without_owner do
owner { nil }
end
trait :with_registration_facet do
association :registration_facet, factory: :registration_facet, strategy: :build
end
trait :with_infrastructure_facet do
association :infrastructure_facet, factory: :infrastructure_facet, strategy: :build
end
end
factory :hostgroup do
sequence(:name) { |n| "hostgroup#{n}" }
organizations { [Organization.find_by_name('Organization 1')] }
locations { [Location.find_by_name('Location 1')] }
trait :with_parent do
association :parent, :factory => :hostgroup
end
trait :with_compute_resource do
compute_resource { FactoryBot.create(:compute_resource, :libvirt) }
end
trait :with_config_group do
config_groups { [FactoryBot.create(:config_group)] }
end
trait :with_parameter do
after(:create) do |hg, evaluator|
FactoryBot.create(:hostgroup_parameter, :hostgroup => hg)
hg.group_parameters.reload
end
end
trait :with_subnet do
association :subnet, :factory => :subnet_ipv4
end
trait :with_rootpass do
sequence(:root_pass) { |n| "xybxa6JUkz63#{n}" }
end
trait :with_os do
architecture { operatingsystem.try(:architectures).try(:first) }
medium { operatingsystem.try(:media).try(:first) }
ptable { operatingsystem.try(:ptables).try(:first) }
association :operatingsystem, :with_associations
end
trait :with_domain do
domain
end
trait :with_puppet_ca do
puppet_ca_proxy do
FactoryBot.create(:smart_proxy, :features => [FactoryBot.create(:feature, :puppetca)])
end
end
trait :with_orchestration do
architecture
ptable
operatingsystem do
FactoryBot.create(:operatingsystem, :architectures => [architecture], :ptables => [ptable])
end
end
end
end