-
Notifications
You must be signed in to change notification settings - Fork 982
/
facet_test.rb
348 lines (269 loc) · 11.3 KB
/
facet_test.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
require 'test_helper'
class TestFacet < HostFacets::Base
end
class TestHostgroupFacet < ApplicationRecord
include Facets::HostgroupFacet
end
class TestHostAndHostrgoupFacet < ApplicationRecord
include Facets::HostgroupFacet
include Facets::Base
end
module TestModule
class ModuleTestFacet < HostFacets::Base
end
end
class FacetTest < ActiveSupport::TestCase
setup do
@config = {}
Facets.stubs(:configuration).returns(@config)
end
teardown do
Host::Managed.cloned_parameters[:include].delete(:test_model)
Host::Managed.cloned_parameters[:include].delete(:test_facet)
Host::Managed.cloned_parameters[:include].delete(:namespaced_facet)
Host::Managed.cloned_parameters[:include].delete(:hostgroup_facet)
Host::Managed.cloned_parameters[:include].delete(:same_facet)
Hostgroup.cloned_parameters[:include].delete(:test_model)
Hostgroup.cloned_parameters[:include].delete(:test_facet)
Hostgroup.cloned_parameters[:include].delete(:namespaced_facet)
Hostgroup.cloned_parameters[:include].delete(:hostgroup_facet)
Hostgroup.cloned_parameters[:include].delete(:same_facet)
end
context 'namespaced facets' do
setup do
Facets.register TestModule::ModuleTestFacet, :namespaced_facet
@host = Host::Managed.new
@facet = @host.build_namespaced_facet
end
test 'can create a namespaced facet' do
assert_equal @facet, @host.facets.first
end
test 'returns facets attributes' do
attributes = @host.attributes
assert_not_nil attributes["namespaced_facet_attributes"]
end
end
context 'managed host behavior' do
setup do
Facets.register TestFacet
@host = Host::Managed.new
@facet = @host.build_test_facet
end
test 'registered facets are subscribed properly' do
assert_equal @facet, @host.facets.first
end
test 'facets are cloned to the new host' do
facet_clone = @facet.dup
@facet.stubs(:dup).returns(facet_clone)
cloned_host = @host.clone
assert_equal facet_clone, cloned_host.test_facet
assert_equal facet_clone, cloned_host.facets.first
end
test 'facets are included in attributes' do
attributes = @host.attributes
assert_not_nil attributes["test_facet_attributes"]
end
test 'apply_inherited_attributes is augmented by facets' do
attributes = { 'hostgroup_id' => 1 }
@host.hostgroup = Hostgroup.new(:id => 1)
TestFacet.expects(:inherited_attributes).returns({ :test_attribute => :test_value })
actual_attributes = @host.apply_inherited_attributes(attributes)
assert_equal :test_value, actual_attributes['test_facet_attributes'][:test_attribute]
end
test 'apply_inherited_attributes works facet defaults' do
attributes = { 'hostgroup_id' => 1 }
@host.hostgroup = Hostgroup.new(:id => 1)
@host.apply_inherited_attributes(attributes)
end
test 'facet attributes are passed to Facet.inherited_attributes' do
attributes = { 'hostgroup_id' => 1, 'test_facet_attributes' => { :test_attribute => :test_value }}
@host.hostgroup = Hostgroup.new
TestFacet.expects(:inherited_attributes).with do |hostgroup, facet_attributes|
facet_attributes[:test_attribute] == :test_value
end
@host.apply_inherited_attributes(attributes)
end
test 'facts are parsed by facets too' do
TestFacet.expects(:populate_fields_from_facts)
@host.stubs(:save)
facts_json = read_json_fixture('facts/brslc022.facts.json')
HostFactImporter.new(@host).parse_facts(facts_json['facts'], nil, nil)
end
end
context "managed host facet behavior" do
setup do
TestFacet.class_eval do
def my_attribute
end
def my_attribute=(val)
end
end
Facets.register TestFacet
end
test 'facets are updated without specifying id explicitly' do
saved_host = FactoryBot.build(:host)
saved_host.build_test_facet
saved_host.save!
saved_host.attributes = {'test_facet_attributes' => { 'my_attribute' => 'my_value'}}
assert_not_nil saved_host.test_facet.id
end
test 'facets do not get created for nil attributes and viceversa' do
saved_host = FactoryBot.build(:host)
saved_host.update({'test_facet_attributes' => { 'my_attribute' => nil}})
assert_nil saved_host.test_facet
saved_host.update({'test_facet_attributes' => { 'my_attribute' => 'val'}})
assert_not_nil saved_host.test_facet
end
test 'facets do update with nil and empty attributes' do
saved_host = FactoryBot.build(:host)
saved_host.update({'test_facet_attributes' => { 'my_attribute' => 'val' }})
TestFacet.any_instance.expects(:my_attribute=).twice
saved_host.update({'test_facet_attributes' => { 'my_attribute' => nil }})
saved_host.update({'test_facet_attributes' => { 'my_attribute' => [] }})
end
test 'facet is not removed when associated host is deleted' do
as_admin do
saved_host = FactoryBot.create(:host)
facet = saved_host.build_test_facet
saved_host.save!
assert facet.id
saved_host.destroy!
assert TestFacet.find_by(id: facet.id)
end
end
end
context 'managed host facet dependent destroy behavior' do
setup do
Facets.register TestFacet do
set_dependent_action :destroy
end
end
test 'facet is removed when associated host is deleted' do
as_admin do
saved_host = FactoryBot.create(:host)
facet = saved_host.build_test_facet
saved_host.save!
assert facet.id
saved_host.destroy!
refute TestFacet.find_by(id: facet.id)
end
end
end
context 'inside db:migrate task' do
setup do
Foreman.stubs(:in_setup_db_rake?).returns(true)
end
test 'facet can be registered more than once' do
# register facet twice
Facets.register TestFacet
Facets.register TestFacet
@host = Host::Managed.new
@host.test_facet
end
end
context 'hostgroup facet behavior' do
test 'inherited attributes are defined by inherit_attributes class method' do
TestHostgroupFacet.stubs(:attributes_to_inherit).returns([])
TestHostgroupFacet.inherit_attributes :to_inherit
facet = TestHostgroupFacet.new
facet.stubs(:attributes).returns({:to_inherit => 'val1', :dont_inherit => 'val2'})
actual = facet.inherited_attributes
assert_equal({:to_inherit => 'val1'}, actual)
end
test 'all attributes are inherited for same facet in host and hostgroup' do
Facets.register :same_facet do
configure_hostgroup TestHostAndHostrgoupFacet
configure_host TestHostAndHostrgoupFacet
end
TestHostAndHostrgoupFacet.expects(:attribute_names).returns(['id', 'created_at', 'updated_at', 'attr1', 'attr2'])
actual = TestHostAndHostrgoupFacet.attributes_to_inherit
assert_equal(['attr1', 'attr2'], actual)
end
end
context 'hostgroup behavior' do
setup do
Facets.register :hostgroup_facet do
configure_hostgroup TestHostgroupFacet
configure_host TestFacet
end
@hostgroup = Hostgroup.new
@facet = @hostgroup.build_hostgroup_facet
end
test 'attributes inherited by default' do
grand_parent = Hostgroup.new
grand_parent_facet = grand_parent.build_hostgroup_facet
parent = Hostgroup.new
parent_facet = parent.build_hostgroup_facet
grand_parent_facet.expects(:inherited_attributes).returns(grand_only: 'grand', parent: 'grand', local: 'grand')
parent_facet.expects(:inherited_attributes).returns(grand_only: nil, parent: 'parent', local: 'parent')
@facet.expects(:inherited_attributes).returns(grand_only: nil, parent: nil, local: 'local')
@hostgroup.expects(:hostgroup_ancestry_cache).returns([grand_parent, parent])
actual = @hostgroup.inherited_facet_attributes(Facets.registered_facets[:hostgroup_facet])
assert_equal({grand_only: 'grand', parent: 'parent', local: 'local'}, actual)
end
test 'attributes inherited from leaf without facet' do
parent = Hostgroup.new
parent_facet = parent.build_hostgroup_facet
parent_facet.expects(:inherited_attributes).returns(parent: 'parent', local: 'parent')
@facet.expects(:inherited_attributes).returns(parent: nil, local: 'local')
# Hostgroup without facet attached
child = Hostgroup.new
child.expects(:hostgroup_ancestry_cache).returns([parent, @hostgroup])
actual = child.inherited_facet_attributes(Facets.registered_facets[:hostgroup_facet])
assert_equal({parent: 'parent', local: 'local'}, actual)
end
test 'hostgroup and facet are connected two-way' do
assert_equal @hostgroup, @facet.hostgroup
end
test 'a cloned hostgroup also clones facets' do
@hostgroup.name = "test"
@hostgroup.hostgroup_facet.id = 42
cloned_hg = @hostgroup.clone
cloned_facet = cloned_hg.hostgroup_facet
assert_nil cloned_facet.id
refute_equal cloned_facet.id, @hostgroup.hostgroup_facet.id
assert_equal cloned_facet.attributes.except("id"), @facet.attributes.except("id")
end
end
context 'host and hostgroup relationship' do
test 'host facet is getting attributes from hostgroup facet' do
Facets.register :hostgroup_facet do
configure_host TestFacet
configure_hostgroup TestHostgroupFacet
end
host = Host::Managed.new
hostgroup = FactoryBot.create(:hostgroup, :hostgroup_facet => TestHostgroupFacet.new)
TestHostgroupFacet.stubs(:attributes_to_inherit).returns([])
TestHostgroupFacet.inherit_attributes :to_inherit
TestHostgroupFacet.any_instance.stubs(:attributes).returns({:to_inherit => 'val1', :dont_inherit => 'val2'})
actual = host.apply_inherited_attributes('hostgroup' => hostgroup)
assert_equal({'to_inherit' => 'val1'}, actual['hostgroup_facet_attributes'])
end
test 'hostgroup facet does not override attributes from host' do
Facets.register :hostgroup_facet do
configure_host TestFacet
configure_hostgroup TestHostgroupFacet
end
host = Host::Managed.new
hostgroup = FactoryBot.create(:hostgroup, :hostgroup_facet => TestHostgroupFacet.new)
TestHostgroupFacet.stubs(:attributes_to_inherit).returns([])
TestHostgroupFacet.inherit_attributes :to_inherit
TestHostgroupFacet.any_instance.stubs(:attributes).returns({:to_inherit => 'val1'})
actual = host.apply_inherited_attributes('hostgroup' => hostgroup, 'hostgroup_facet_attributes' => { 'to_inherit' => 'val3' })
assert_equal({'to_inherit' => 'val3'}, actual['hostgroup_facet_attributes'])
end
test 'hostgroup facet does not override nil attributes from host' do
Facets.register :hostgroup_facet do
configure_host TestFacet
configure_hostgroup TestHostgroupFacet
end
host = Host::Managed.new
hostgroup = FactoryBot.create(:hostgroup, :hostgroup_facet => TestHostgroupFacet.new)
TestHostgroupFacet.stubs(:attributes_to_inherit).returns([])
TestHostgroupFacet.inherit_attributes :to_inherit
TestHostgroupFacet.any_instance.stubs(:attributes).returns({:to_inherit => 'val1'})
actual = host.apply_inherited_attributes('hostgroup' => hostgroup, 'hostgroup_facet_attributes' => { 'to_inherit' => nil })
assert_equal({'to_inherit' => nil}, actual['hostgroup_facet_attributes'])
end
end
end