-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtesting.rb
902 lines (811 loc) · 26.8 KB
/
testing.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
require sprintf('%s/../../%s', File.dirname(File.expand_path(__FILE__)), 'path_helper')
require 'rouster/deltas'
# TODO better document keys :constrain and :version
class Rouster
##
# validate_cron
#
# given the name of the user who owns crontab, the cron's command to execute and a hash of expectations, returns true|false whether cron matches expectations
#
# parameters
# * <user> - name of user who owns crontab
# * <name> - the cron's command to execute
# * <expectations> - hash of expectations, see examples
# * <fail_fast> - return false immediately on any failure (default is false)
#
# example expectations:
# 'username',
# '/home/username/test.pl', {
# :ensure => 'present',
# :minute => 1,
# :hour => 0,
# :dom => '*',
# :mon => '*',
# :dow => '*',
# }
#
# 'root',
# 'printf > /var/log/apache/error_log', {
# :minute => 59,
# :hour => [8, 12],
# :dom => '*',
# :mon => '*',
# :dow => '*',
# }
#
# supported keys:
# * :exists|:ensure -- defaults to present if not specified
# * :minute
# * :hour
# * :dom -- day of month
# * :mon -- month
# * :dow -- day of week
# * :constrain
def validate_cron(user, name, expectations, fail_fast=false)
if user.nil?
raise InternalError.new('no user specified constraint')
end
crontabs = self.get_crontab(user)
if expectations[:ensure].nil? and expectations[:exists].nil?
expectations[:ensure] = 'present'
end
if expectations.has_key?(:constrain)
expectations[:constrain] = expectations[:constrain].class.eql?(Array) ? expectations[:constrain] : [expectations[:constrain]]
expectations[:constrain].each do |constraint|
fact, expectation = constraint.split("\s")
unless meets_constraint?(fact, expectation)
@logger.info(sprintf('returning true for expectation [%s], did not meet constraint[%s/%s]', name, fact, expectation))
return true
end
end
expectations.delete(:constrain)
end
results = Hash.new()
local = nil
expectations.each do |k,v|
case k
when :ensure, :exists
if crontabs.has_key?(name)
if v.to_s.match(/absent|false/).nil?
local = true
else
local = false
end
else
local = v.to_s.match(/absent|false/).nil? ? false : true
end
when :minute, :hour, :dom, :mon, :dow
if crontabs.has_key?(name) and crontabs[name].has_key?(k) and crontabs[name][k].to_s.eql?(v.to_s)
local = true
else
local = false
end
else
raise InternalError.new(sprintf('unknown expectation[%s / %s]', k, v))
end
return false if local.eql?(false) and fail_fast.eql?(true)
results[k] = local
end
@logger.info("#{name} [#{expectations}] => #{results}")
results.find{|k,v| v.false? }.nil?
end
##
# validate_file
#
# given a filename and a hash of expectations, returns true|false whether file matches expectations
#
# parameters
# * <name> - full file name or relative (to ~vagrant)
# * <expectations> - hash of expectations, see examples
# * <fail_fast> - return false immediately on any failure (default is false)
#
# example expectations:
# '/sys/kernel/mm/redhat_transparent_hugepage/enabled', {
# :contains => 'never',
# },
#
# '/etc/fstab', {
# :contains => '/dev/fioa*/iodata*xfs',
# :constrain => 'is_virtual false' # syntax is '<fact> <expected>', file is only tested if <expected> matches <actual>
# :exists => 'file',
# :mode => '0644'
# },
#
# '/etc/hosts', {
# :constrain => ['! is_virtual true', 'is_virtual false'],
# :mode => '0644'
# }
#
# '/etc/nrpe.cfg', {
# :ensure => 'file',
# :contains => ['dont_blame_nrpe=1', 'allowed_hosts=' ]
# }
#
# supported keys:
# * :exists|:ensure -- defaults to file if not specified
# * :file
# * :directory
# * :contains (string or array)
# * :mode/:permissions
# * :size
# * :owner
# * :group
# * :constrain
def validate_file(name, expectations, fail_fast=false, cache=false)
if expectations[:ensure].nil? and expectations[:exists].nil? and expectations[:directory].nil? and expectations[:file?].nil?
expectations[:ensure] = 'file'
end
if expectations.has_key?(:constrain)
expectations[:constrain] = expectations[:constrain].class.eql?(Array) ? expectations[:constrain] : [expectations[:constrain]]
expectations[:constrain].each do |constraint|
valid = constraint.match(/^(\S+?)\s(.*)$/)
if valid.nil?
raise InternalError.new(sprintf('invalid constraint[%s] specified', constraint))
end
fact = $1
expectation = $2
unless meets_constraint?(fact, expectation)
@logger.info(sprintf('returning true for expectation [%s], did not meet constraint[%s/%s]', name, fact, expectation))
return true
end
end
expectations.delete(:constrain)
end
properties = (expectations[:ensure].eql?('file')) ? self.file(name, cache) : self.dir(name, cache)
results = Hash.new()
local = nil
expectations.each do |k,v|
case k
when :ensure, :exists
if properties.nil? and v.to_s.match(/absent|false/)
local = true
elsif properties.nil?
local = false
elsif v.to_s.match(/symlink|link/)
if expectations[:target].nil?
# don't validate the link path, just check whether we're a link
local = properties[:symlink?]
else
# validate the link path
local = properties[:target].eql?(expectations[:target])
end
else
case v
when 'dir', 'directory'
local = properties[:directory?]
else
local = properties[:file?]
end
end
when :file
if properties.nil?
if v.to_s.match(/absent|false/)
local = true
else
local = false
end
elsif properties[:file?].true?
local = ! v.to_s.match(/absent|false/)
else
false
end
when :dir, :directory
if properties.nil?
if v.to_s.match(/absent|false/)
local = true
else
local = false
end
elsif properties.has_key?(:directory?)
if properties[:directory?]
local = v.to_s.match(/absent|false/).nil?
else
local = ! v.to_s.match(/absent|false/).nil?
end
else
local = false
end
when :contains
v = v.class.eql?(Array) ? v : [v]
v.each do |regex|
local = true
begin
self.run(sprintf("grep -c '%s' %s", regex, name))
rescue => e
local = false
end
break if local.false?
end
when :notcontains, :doesntcontain # TODO determine the appropriate attribute title here
v = v.class.eql?(Array) ? v : [v]
v.each do |regex|
local = true
begin
self.run(sprintf("grep -c '%s' %s", regex, name))
local = false
rescue => e
local = true
end
break if local.false?
end
when :mode, :permissions
if properties.nil?
local = false
elsif v.to_s.match(/#{properties[:mode].to_s}/)
local = true
else
local = false
end
when :size
if properties.nil?
local = false
else
local = v.to_i.eql?(properties[:size].to_i)
end
when :owner
if properties.nil?
local = false
elsif v.to_s.match(/#{properties[:owner].to_s}/)
local = true
else
local = false
end
when :group
if properties.nil?
local = false
elsif v.match(/#{properties[:group]}/)
local = true
else
local = false
end
when :type
# noop allowing parse_catalog() output to be passed directly
when :target
# noop allowing ensure => 'link' / 'symlink' to specify their .. target
else
raise InternalError.new(sprintf('unknown expectation[%s / %s]', k, v))
end
return false if local.eql?(false) and fail_fast.eql?(true)
results[k] = local
end
@logger.info("#{name} [#{expectations}] => #{results}")
results.find{|k,v| v.false? }.nil?
end
##
# validate_group
#
# given a group and a hash of expectations, returns true|false whether group matches expectations
#
# paramaters
# * <name> - group name
# * <expectations> - hash of expectations, see examples
# * <fail_fast> - return false immediately on any failure (default is false)
#
# example expectations:
# 'root', {
# # if ensure is not specified, 'present' is implied
# :gid => 0,
# :user => 'root'
# }
# 'sys', {
# :ensure => 'present',
# :user => ['root', 'bin', 'daemon']
# },
#
# 'fizz', {
# :exists => false
# },
#
# supported keys:
# * :exists|:ensure
# * :gid
# * :user|:users (string or array)
# * :constrain
def validate_group(name, expectations, fail_fast=false)
groups = self.get_groups(true)
if expectations[:ensure].nil? and expectations[:exists].nil?
expectations[:ensure] = 'present'
end
if expectations.has_key?(:constrain)
expectations[:constrain] = expectations[:constrain].class.eql?(Array) ? expectations[:constrain] : [expectations[:constrain]]
expectations[:constrain].each do |constraint|
fact, expectation = constraint.split("\s")
unless meets_constraint?(fact, expectation)
@logger.info(sprintf('returning true for expectation [%s], did not meet constraint[%s/%s]', name, fact, expectation))
return true
end
end
expectations.delete(:constrain)
end
results = Hash.new()
local = nil
expectations.each do |k,v|
case k
when :ensure, :exists
if groups.has_key?(name)
if v.to_s.match(/absent|false/).nil?
local = true
else
local = false
end
else
local = v.to_s.match(/absent|false/).nil? ? false : true
end
when :gid
if groups[name].is_a?(Hash) and groups[name].has_key?(:gid)
local = v.to_s.eql?(groups[name][:gid].to_s)
else
local = false
end
when :user, :users
v = v.class.eql?(Array) ? v : [v]
v.each do |user|
if groups[name].is_a?(Hash) and groups[name].has_key?(:users)
local = groups[name][:users].member?(user)
else
local = false
end
break unless local.true? # need to make the return value smarter if we want to store data on which user failed
end
when :type
# noop allowing parse_catalog() output to be passed directly
else
raise InternalError.new(sprintf('unknown expectation[%s / %s]', k, v))
end
return false if local.eql?(false) and fail_fast.eql?(true)
results[k] = local
end
@logger.info("#{name} [#{expectations}] => #{results}")
results.find{|k,v| v.false? }.nil?
end
##
# validate_package
#
# given a package name and a hash of expectations, returns true|false whether package meets expectations
#
# parameters
# * <name> - package name
# * <expectations> - hash of expectations, see examples
# * <fail_fast> - return false immediately on any failure (default is false)
#
# example expectations:
# 'perl-Net-SNMP', {
# :ensure => 'absent'
# },
#
# 'pixman', {
# :ensure => 'present',
# :version => '1.0',
# },
#
# 'rrdtool', {
# # if ensure is not specified, 'present' is implied
# :version => '> 2.1',
# :constrain => 'is_virtual false',
# },
# supported keys:
# * :exists|ensure
# * :version (literal or basic comparison)
# * :constrain
def validate_package(name, expectations, fail_fast=false)
packages = self.get_packages(true)
if expectations[:ensure].nil? and expectations[:exists].nil?
expectations[:ensure] = 'present'
end
if expectations.has_key?(:constrain)
expectations[:constrain] = expectations[:constrain].class.eql?(Array) ? expectations[:constrain] : [expectations[:constrain]]
expectations[:constrain].each do |constraint|
fact, expectation = constraint.split("\s")
unless meets_constraint?(fact, expectation)
@logger.info(sprintf('returning true for expectation [%s], did not meet constraint[%s/%s]', name, fact, expectation))
return true
end
end
expectations.delete(:constrain)
end
results = Hash.new()
local = nil
expectations.each do |k,v|
case k
when :ensure, :exists
if packages.has_key?(name)
if v.to_s.match(/absent|false/).nil?
local = true
else
local = false
end
else
local = v.to_s.match(/absent|false/).nil? ? false : true
end
when :version
# TODO support determination based on multiple versions of the same package installed (?)
if packages.has_key?(name)
lps = packages[name].is_a?(Array) ? packages[name] : [ packages[name] ]
lps.each do |lp|
if v.split("\s").size > 1
## generic comparator functionality
comp, expectation = v.split("\s")
local = generic_comparator(lp[:version], comp, expectation)
break unless local.eql?(true)
else
local = ! v.to_s.match(/#{lp[:version]}/).nil?
break unless local.eql?(true)
end
end
else
local = false
end
when :arch, :architecture
if packages.has_key?(name)
archs = []
lps = packages[name].is_a?(Array) ? packages[name] : [ packages[name] ]
lps.each { |p| archs << p[:arch] }
if v.is_a?(Array)
v.each do |arch|
local = archs.member?(arch)
break unless local.eql?(true) # fail fast - if we are looking for an arch that DNE, bail out
end
else
local = archs.member?(v)
end
end
when :type
# noop allowing parse_catalog() output to be passed directly
else
raise InternalError.new(sprintf('unknown expectation[%s / %s]', k, v))
end
return false if local.eql?(false) and fail_fast.eql?(true)
results[k] = local
end
# TODO figure out a good way to allow access to the entire hash, not just boolean -- for now just print at an info level
@logger.info("#{name} [#{expectations}] => #{results}")
results.find{|k,v| v.false? }.nil?
end
# given a port nnumber and a hash of expectations, returns true|false whether port meets expectations
#
# parameters
# * <number> - port number
# * <expectations> - hash of expectations, see examples
#
# example expectations:
# '22', {
# :ensure => 'active',
# :protocol => 'tcp',
# :address => '0.0.0.0'
# },
#
# '1234', {
# :ensure => 'open',
# :address => '*',
# :constrain => 'is_virtual false'
# }
#
# supported keys:
# * :exists|ensure|state
# * :address
# * :protocol|proto
# * :constrain
def validate_port(number, expectations, fail_fast=false)
number = number.to_s
ports = self.get_ports(true)
if expectations[:ensure].nil? and expectations[:exists].nil? and expectations[:state].nil?
expectations[:ensure] = 'present'
end
if expectations[:protocol].nil? and expectations[:proto].nil?
expectations[:protocol] = 'tcp'
elsif ! expectations[:proto].nil?
expectations[:protocol] = expectations[:proto]
end
if expectations.has_key?(:constrain)
expectations[:constrain] = expectations[:constrain].class.eql?(Array) ? expectations[:constrain] : [expectations[:constrain]]
expectations[:constrain].each do |constraint|
fact, expectation = constraint.split("\s")
unless meets_constraint?(fact, expectation)
@logger.info(sprintf('returning true for expectation [%s], did not meet constraint[%s/%s]', name, fact, expectation))
return true
end
end
expectations.delete(:constrain)
end
results = Hash.new()
local = nil
expectations.each do |k,v|
case k
when :ensure, :exists, :state
if v.to_s.match(/absent|false|open/)
local = ports[expectations[:protocol]][number].nil?
else
local = ! ports[expectations[:protocol]][number].nil?
end
when :protocol, :proto
# TODO rewrite this in a less hacky way
if expectations[:ensure].to_s.match(/absent|false|open/) or expectations[:exists].to_s.match(/absent|false|open/) or expectations[:state].to_s.match(/absent|false|open/)
local = true
else
local = ports[v].has_key?(number)
end
when :address
lr = Array.new
if ports[expectations[:protocol]][number]
addresses = ports[expectations[:protocol]][number][:address]
addresses.each_key do |address|
lr.push(address.eql?(v.to_s))
end
local = ! lr.find{|e| e.true? }.nil? # this feels jankity
else
# this port isn't open in the first place, won't match any addresses we expect to see it on
local = false
end
else
raise InternalError.new(sprintf('unknown expectation[%s / %s]', k, v))
end
return false if local.eql?(false) and fail_fast.eql?(true)
results[k] = local
end
@logger.info("#{name} [#{expectations}] => #{results}")
results.find{|k,v| v.false? }.nil?
end
##
# validate_service
#
# given a service name and a hash of expectations, returns true|false whether package meets expectations
#
# parameters
# * <name> - service name
# * <expectations> - hash of expectations, see examples
# * <fail_fast> - return false immediately on any failure (default is false)
#
# example expectations:
# 'ntp', {
# :ensure => 'present',
# :state => 'started'
# },
#
# 'ypbind', {
# :state => 'stopped',
# }
#
# supported keys:
# * :exists|:ensure
# * :state,:status
# * :constrain
def validate_service(name, expectations, fail_fast=false)
services = self.get_services(true)
if expectations[:ensure].nil? and expectations[:exists].nil?
expectations[:ensure] = 'present'
end
if expectations.has_key?(:constrain)
expectations[:constrain] = expectations[:constrain].class.eql?(Array) ? expectations[:constrain] : [expectations[:constrain]]
expectations[:constrain].each do |constraint|
fact, expectation = constraint.split("\s")
unless meets_constraint?(fact, expectation)
@logger.info(sprintf('returning true for expectation [%s], did not meet constraint[%s/%s]', name, fact, expectation))
return true
end
end
expectations.delete(:constrain)
end
results = Hash.new()
local = nil
expectations.each do |k,v|
case k
when :ensure, :exists
if services.has_key?(name)
if v.to_s.match(/absent|false/)
local = false
else
local = true
end
else
local = v.to_s.match(/absent|false/).nil? ? false : true
end
when :state, :status
if services.has_key?(name)
local = ! v.match(/#{services[name]}/).nil?
else
local = false
end
when :type
# noop allowing parse_catalog() output to be passed directly
else
raise InternalError.new(sprintf('unknown expectation[%s / %s]', k, v))
end
return false if local.eql?(false) and fail_fast.eql?(true)
results[k] = local
end
@logger.info("#{name} [#{expectations}] => #{results}")
results.find{|k,v| v.false? }.nil?
end
##
# validate_user
#
# given a user name and a hash of expectations, returns true|false whether user meets expectations
#
# parameters
# * <name> - user name
# * <expectations> - hash of expectations, see examples
# * <fail_fast> - return false immediately on any failure (default is false)
#
# example expectations:
# 'root' => {
# :uid => 0
# },
#
# 'ftp' => {
# :exists => true,
# :home => '/var/ftp',
# :shell => 'nologin'
# },
#
# 'developer' => {
# :exists => 'false',
# :constrain => 'environment != production'
# }
#
# supported keys:
# * :exists|ensure
# * :home
# * :group
# * :shell
# * :uid
# * :gid
# * :constrain
def validate_user(name, expectations, fail_fast=false)
users = self.get_users(true)
if expectations[:ensure].nil? and expectations[:exists].nil?
expectations[:ensure] = 'present'
end
if expectations.has_key?(:constrain)
expectations[:constrain] = expectations[:constrain].class.eql?(Array) ? expectations[:constrain] : [expectations[:constrain]]
expectations[:constrain].each do |constraint|
fact, expectation = constraint.split("\s")
unless meets_constraint?(fact, expectation)
@logger.info(sprintf('returning true for expectation [%s], did not meet constraint[%s/%s]', name, fact, expectation))
return true
end
end
expectations.delete(:constrain)
end
results = Hash.new()
local = nil
expectations.each do |k,v|
case k
when :ensure, :exists
if users.has_key?(name)
if v.to_s.match(/absent|false/).nil?
local = true
else
local = false
end
else
local = v.to_s.match(/absent|false/).nil? ? false : true
end
when :group
v = v.class.eql?(Array) ? v : [v]
v.each do |group|
local = is_user_in_group?(name, group)
break unless local.true?
end
when :gid
if users[name].is_a?(Hash) and users[name].has_key?(:gid)
local = v.to_i.eql?(users[name][:gid].to_i)
else
local = false
end
when :home
if users[name].is_a?(Hash) and users[name].has_key?(:home)
local = ! v.match(/#{users[name][:home]}/).nil?
else
local = false
end
when :home_exists
if users[name].is_a?(Hash) and users[name].has_key?(:home_exists)
local = ! v.to_s.match(/#{users[name][:home_exists].to_s}/).nil?
else
local = false
end
when :shell
if users[name].is_a?(Hash) and users[name].has_key?(:shell)
local = ! v.match(/#{users[name][:shell]}/).nil?
else
local = false
end
when :uid
if users[name].is_a?(Hash) and users[name].has_key?(:uid)
local = v.to_i.eql?(users[name][:uid].to_i)
else
local = false
end
when :type
# noop allowing parse_catalog() output to be passed directly
else
raise InternalError.new(sprintf('unknown expectation[%s / %s]', k, v))
end
return false if local.eql?(false) and fail_fast.eql?(true)
results[k] = local
end
@logger.info("#{name} [#{expectations}] => #{results}")
results.find{|k,v| v.false? }.nil?
end
## internal methods
private
##
# meets_constraint?
#
# powers the :constrain value in expectations passed to validate_*
# gets facts from node, and if fact expectation regex matches actual fact, returns true
#
# parameters
# * <key> - fact/hiera key to look up (actual value)
# * <expectation> -
# * [cache] - boolean controlling whether facter lookups are cached
def meets_constraint?(key, expectation, cache=true)
expectation = expectation.to_s
unless self.respond_to?('facter') or self.respond_to?('hiera')
# if we haven't loaded puppet.rb, we won't have access to facts/hiera lookups
@logger.warn('using constraints without loading [rouster/puppet] will not work, forcing no-op')
return false
end
facts = self.facter(cache)
actual = nil
if facts[key]
actual = facts[key]
else
# value is not a fact, lets try to find it in hiera
# TODO how to handle the fact that this will really only work on the puppetmaster
actual = self.hiera(key, facts)
end
res = nil
if expectation.split("\s").size > 1
## generic comparator functionality
comp, expectation = expectation.split("\s")
res = generic_comparator(actual, comp, expectation)
else
res = ! actual.to_s.match(/#{expectation}/).nil?
end
@logger.debug(sprintf('meets_constraint?(%s, %s): %s', key, expectation, res.nil?))
res
end
##
# generic_comparator
#
# powers the 3 argument form of constraint (i.e. 'is_virtual != true', '<package_version> > 3.0', etc)
#
# should really be an eval{} of some sort (or would be in the perl world)
#
# parameters
# * <comparand1> - left side of the comparison
# * <comparator> - comparison to make
# * <comparand2> - right side of the comparison
def generic_comparator(comparand1, comparator, comparand2)
# TODO rewrite this as an eval so we don't have to support everything..
# TODO come up with mechanism to determine when is it appropriate to call .to_i vs. otherwise -- comparisons will mainly be numerical (?), but need to support text matching too
case comparator
when '!='
# ugh
if comparand1.to_s.match(/\d/) or comparand2.to_s.match(/\d/)
res = ! comparand1.to_i.eql?(comparand2.to_i)
else
res = ! comparand1.eql?(comparand2)
end
when '<'
res = comparand1.to_i < comparand2.to_i
when '<='
res = comparand1.to_i <= comparand2.to_i
when '>'
res = comparand1.to_i > comparand2.to_i
when '>='
res = comparand1.to_i >= comparand2.to_i
when '=='
# ugh ugh
if comparand1.to_s.match(/\d/) or comparand2.to_s.match(/\d/)
res = comparand1.to_i.eql?(comparand2.to_i)
else
res = comparand1.eql?(comparand2)
end
else
raise NotImplementedError.new(sprintf('unknown comparator[%s]', comparator))
end
res
end
end