rspec-puppet 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,8 @@
1
+ require 'tmpdir'
2
+ require 'digest'
3
+ require 'json'
4
+ require 'fileutils'
5
+
1
6
  unless defined?(RSpec::Core::NullReporter)
2
7
  module RSpec::Core
3
8
  class NullReporter
@@ -32,6 +37,29 @@ module RSpec::Puppet
32
37
  @filters = ['Stage[main]', 'Class[Settings]', 'Class[main]', 'Node[default]']
33
38
  end
34
39
 
40
+ def save_results
41
+ slug = "#{Digest::MD5.hexdigest(Dir.pwd)}-#{Process.pid}"
42
+ File.open(File.join(Dir.tmpdir, "rspec-puppet-coverage-#{slug}"), 'w+') do |f|
43
+ f.puts @collection.to_json
44
+ end
45
+ end
46
+
47
+ def merge_results
48
+ pattern = File.join(Dir.tmpdir, "rspec-puppet-coverage-#{Digest::MD5.hexdigest(Dir.pwd)}-*")
49
+ Dir[pattern].each do |result_file|
50
+ load_results(result_file)
51
+ FileUtils.rm(result_file)
52
+ end
53
+ end
54
+
55
+ def load_results(path)
56
+ saved_results = JSON.parse(File.read(path))
57
+ saved_results.each do |resource, data|
58
+ add(resource)
59
+ cover!(resource) if data['touched']
60
+ end
61
+ end
62
+
35
63
  def add(resource)
36
64
  if !exists?(resource) && !filtered?(resource)
37
65
  @collection[resource.to_s] = ResourceWrapper.new(resource)
@@ -70,47 +98,49 @@ module RSpec::Puppet
70
98
  end
71
99
 
72
100
  def report!(coverage_desired = nil)
73
- report = results
74
- puts <<-EOH.gsub(/^ {8}/, '')
75
-
76
- Total resources: #{report[:total]}
77
- Touched resources: #{report[:touched]}
78
- Resource coverage: #{report[:coverage]}%
79
- EOH
80
-
81
- if report[:coverage] != "100.00"
82
- puts <<-EOH.gsub(/^ {10}/, '')
83
- Untouched resources:
84
-
85
- #{
86
- untouched_resources = report[:resources].reject do |_,rsrc|
87
- rsrc[:touched]
88
- end
89
- untouched_resources.inject([]) do |memo, (name,_)|
90
- memo << " #{name}"
91
- end.sort.join("\n")
92
- }
93
- EOH
94
- if coverage_desired
95
- coverage_test(coverage_desired, report[:coverage])
101
+ if parallel_tests?
102
+ require 'parallel_tests'
103
+
104
+ if ParallelTests.first_process?
105
+ ParallelTests.wait_for_other_processes_to_finish
106
+ run_report(coverage_desired)
107
+ else
108
+ save_results
96
109
  end
110
+ else
111
+ run_report(coverage_desired)
97
112
  end
98
113
  end
99
114
 
100
- def coverage_test(coverage_desired, coverage_actual)
115
+ def parallel_tests?
116
+ !!ENV['TEST_ENV_NUMBER']
117
+ end
118
+
119
+ def run_report(coverage_desired = nil)
120
+ merge_results if ENV['TEST_ENV_NUMBER']
121
+
122
+ report = results
123
+
124
+ coverage_test(coverage_desired, report)
125
+
126
+ puts report[:text]
127
+ end
128
+
129
+ def coverage_test(coverage_desired, report)
130
+ coverage_actual = report[:coverage]
131
+ coverage_desired ||= 0
132
+
101
133
  if coverage_desired.is_a?(Numeric) && coverage_desired.to_f <= 100.00 && coverage_desired.to_f >= 0.0
102
- coverage_test = RSpec.describe("Code coverage.")
103
- coverage_results = coverage_test.example("Must be at least #{coverage_desired}% of code coverage") {
134
+ coverage_test = RSpec.describe("Code coverage")
135
+ coverage_results = coverage_test.example("must cover at least #{coverage_desired}% of resources") do
104
136
  expect( coverage_actual.to_f ).to be >= coverage_desired.to_f
105
- }
106
- coverage_test.run(RSpec::Core::NullReporter)
107
- passed = if coverage_results.execution_result.respond_to? :status then
108
- coverage_results.execution_result.status == :passed
109
- else
110
- coverage_results.execution_result[:status] == 'passed'
111
- end
112
-
113
- RSpec.configuration.reporter.example_failed coverage_results unless passed
137
+ end
138
+ coverage_test.run(RSpec.configuration.reporter)
139
+
140
+ # This is not available on RSpec 2.x
141
+ if coverage_results.execution_result.respond_to?(:pending_message)
142
+ coverage_results.execution_result.pending_message = report[:text]
143
+ end
114
144
  else
115
145
  puts "The desired coverage must be 0 <= x <= 100, not '#{coverage_desired.inspect}'"
116
146
  end
@@ -119,6 +149,8 @@ module RSpec::Puppet
119
149
  def results
120
150
  report = {}
121
151
 
152
+ @collection.delete_if { |name, _| filtered?(name) }
153
+
122
154
  report[:total] = @collection.size
123
155
  report[:touched] = @collection.count { |_, resource| resource.touched? }
124
156
  report[:untouched] = report[:total] - report[:touched]
@@ -128,6 +160,19 @@ module RSpec::Puppet
128
160
  [name, wrapper.to_hash]
129
161
  end.flatten]
130
162
 
163
+ text = [
164
+ "Total resources: #{report[:total]}",
165
+ "Touched resources: #{report[:touched]}",
166
+ "Resource coverage: #{report[:coverage]}%",
167
+ ]
168
+
169
+ if report[:untouched] > 0
170
+ text += ['', 'Untouched resources:']
171
+ untouched_resources = report[:resources].reject { |_, r| r[:touched] }
172
+ text += untouched_resources.map { |name, _| " #{name}" }.sort
173
+ end
174
+ report[:text] = text.join("\n")
175
+
131
176
  report
132
177
  end
133
178
 
@@ -206,6 +251,10 @@ module RSpec::Puppet
206
251
  }
207
252
  end
208
253
 
254
+ def to_json(opts)
255
+ to_hash.to_json(opts)
256
+ end
257
+
209
258
  def touch!
210
259
  @touched = true
211
260
  end
@@ -138,7 +138,6 @@ module RSpec::Puppet
138
138
  end
139
139
 
140
140
  def cycles_found?
141
- Puppet::Type.suppress_provider
142
141
  cat = @catalogue.to_ral.relationship_graph
143
142
  cat.write_graph(:resources)
144
143
  if cat.respond_to? :find_cycles_in_graph
@@ -146,7 +145,6 @@ module RSpec::Puppet
146
145
  else
147
146
  find_cycles_legacy(cat)
148
147
  end
149
- Puppet::Type.unsuppress_provider
150
148
 
151
149
  !@cycles.empty?
152
150
  end
@@ -1,6 +1,12 @@
1
1
  module RSpec::Puppet
2
2
  module ManifestMatchers
3
3
  class CountGeneric
4
+ DEFAULT_RESOURCES = [
5
+ 'Class[main]',
6
+ 'Class[Settings]',
7
+ 'Stage[main]',
8
+ ].freeze
9
+
4
10
  def initialize(type, count, *method)
5
11
  if type.nil?
6
12
  @type = method[0].to_s.gsub(/^have_(.+)_resource_count$/, '\1')
@@ -14,22 +20,20 @@ module RSpec::Puppet
14
20
  def matches?(catalogue)
15
21
  @catalogue = catalogue.call
16
22
 
17
- if @type == "resource"
18
- @actual_number = @catalogue.resources.count do |res|
19
- !(['Class', 'Node'].include? res.type)
20
- end
21
-
22
- # Puppet automatically adds Stage[main]
23
- @actual_number = @actual_number - 1
24
- else
25
- @actual_number = @catalogue.resources.count do |res|
26
- res.type == @referenced_type
27
- end
28
-
29
- # Puppet automatically adds Class[main] and Class[Settings]
30
- @actual_number = @actual_number - 2 if @type == "class"
23
+ resources = @catalogue.resources.reject do |res|
24
+ DEFAULT_RESOURCES.include?(res.ref)
31
25
  end
32
26
 
27
+ @actual_number = if @type == 'resource'
28
+ resources.count do |res|
29
+ !['Class', 'Node'].include?(res.type)
30
+ end
31
+ else
32
+ resources.count do |res|
33
+ res.type == @referenced_type
34
+ end
35
+ end
36
+
33
37
  @actual_number == @expected_number
34
38
  end
35
39
 
@@ -30,7 +30,7 @@ module RSpec::Puppet
30
30
 
31
31
  def only_with(*args, &block)
32
32
  params = args.shift
33
- @expected_params_count = (@expected_params_count || 0) + params.size
33
+ @expected_params_count = (@expected_params_count || 0) + params.select { |_, v| !v.nil? }.size
34
34
  self.with(params, &block)
35
35
  end
36
36
 
@@ -294,7 +294,6 @@ module RSpec::Puppet
294
294
  end
295
295
  end
296
296
 
297
- Puppet::Type.suppress_provider
298
297
  # Add autorequires if any
299
298
  if type == :require and resource.resource_type.respond_to? :eachautorequire
300
299
  resource.resource_type.eachautorequire do |t, b|
@@ -307,7 +306,6 @@ module RSpec::Puppet
307
306
  end
308
307
  end
309
308
  end
310
- Puppet::Type.unsuppress_provider
311
309
 
312
310
  results.flatten
313
311
  end
@@ -78,7 +78,7 @@ module RSpec::Puppet
78
78
  end
79
79
 
80
80
  def check_regexp(expected, actual)
81
- !!(actual.to_s.match expected) == @should_match
81
+ !!(munge_line_endings(actual.to_s).match expected) == @should_match
82
82
  end
83
83
 
84
84
  # Ensure that two hashes have the same number of keys, and that for each
@@ -87,6 +87,11 @@ module RSpec::Puppet
87
87
  def check_hash(expected, actual)
88
88
  op = @should_match ? :"==" : :"!="
89
89
 
90
+ unless actual.class.send(op, expected.class)
91
+ @errors << MatchError.new(@parameter, expected, actual, !@should_match)
92
+ return false
93
+ end
94
+
90
95
  unless expected.keys.size.send(op, actual.keys.size)
91
96
  return false
92
97
  end
@@ -99,6 +104,11 @@ module RSpec::Puppet
99
104
  def check_array(expected, actual)
100
105
  op = @should_match ? :"==" : :"!="
101
106
 
107
+ unless actual.class.send(op, expected.class)
108
+ @errors << MatchError.new(@parameter, expected, actual, !@should_match)
109
+ return false
110
+ end
111
+
102
112
  unless expected.size.send(op, actual.size)
103
113
  return false
104
114
  end
@@ -109,7 +119,17 @@ module RSpec::Puppet
109
119
  end
110
120
 
111
121
  def check_string(expected, actual)
112
- (expected.to_s == actual.to_s) == @should_match
122
+ (expected.to_s == munge_line_endings(actual.to_s)) == @should_match
123
+ end
124
+
125
+ def munge_line_endings(value)
126
+ return value unless value.respond_to?(:encode)
127
+
128
+ if Puppet::Util::Platform.windows?
129
+ value.encode(:crlf_newline => true)
130
+ else
131
+ value.encode(:universal_newline => true)
132
+ end
113
133
  end
114
134
  end
115
135
  end
@@ -0,0 +1,79 @@
1
+ module RSpec
2
+ module Puppet
3
+ module Win32
4
+ class Registry
5
+ module Constants
6
+ HKEY_CLASSES_ROOT = 0x80000000
7
+ HKEY_CURRENT_USER = 0x80000001
8
+ HKEY_LOCAL_MACHINE = 0x80000002
9
+ HKEY_USERS = 0x80000003
10
+ HKEY_PERFORMANCE_DATA = 0x80000004
11
+ HKEY_PERFORMANCE_TEXT = 0x80000050
12
+ HKEY_PERFORMANCE_NLSTEXT = 0x80000060
13
+ HKEY_CURRENT_CONFIG = 0x80000005
14
+ HKEY_DYN_DATA = 0x80000006
15
+
16
+ REG_NONE = 0
17
+ REG_SZ = 1
18
+ REG_EXPAND_SZ = 2
19
+ REG_BINARY = 3
20
+ REG_DWORD = 4
21
+ REG_DWORD_LITTLE_ENDIAN = 4
22
+ REG_DWORD_BIG_ENDIAN = 5
23
+ REG_LINK = 6
24
+ REG_MULTI_SZ = 7
25
+ REG_RESOURCE_LIST = 8
26
+ REG_FULL_RESOURCE_DESCRIPTOR = 9
27
+ REG_RESOURCE_REQUIREMENTS_LIST = 10
28
+ REG_QWORD = 11
29
+ REG_QWORD_LITTLE_ENDIAN = 11
30
+
31
+ STANDARD_RIGHTS_READ = 0x00020000
32
+ STANDARD_RIGHTS_WRITE = 0x00020000
33
+ KEY_QUERY_VALUE = 0x0001
34
+ KEY_SET_VALUE = 0x0002
35
+ KEY_CREATE_SUB_KEY = 0x0004
36
+ KEY_ENUMERATE_SUB_KEYS = 0x0008
37
+ KEY_NOTIFY = 0x0010
38
+ KEY_CREATE_LINK = 0x0020
39
+ KEY_READ = STANDARD_RIGHTS_READ |
40
+ KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY
41
+ KEY_WRITE = STANDARD_RIGHTS_WRITE |
42
+ KEY_SET_VALUE | KEY_CREATE_SUB_KEY
43
+ KEY_EXECUTE = KEY_READ
44
+ KEY_ALL_ACCESS = KEY_READ | KEY_WRITE | KEY_CREATE_LINK
45
+
46
+ REG_OPTION_RESERVED = 0x0000
47
+ REG_OPTION_NON_VOLATILE = 0x0000
48
+ REG_OPTION_VOLATILE = 0x0001
49
+ REG_OPTION_CREATE_LINK = 0x0002
50
+ REG_OPTION_BACKUP_RESTORE = 0x0004
51
+ REG_OPTION_OPEN_LINK = 0x0008
52
+ REG_LEGAL_OPTION = REG_OPTION_RESERVED |
53
+ REG_OPTION_NON_VOLATILE | REG_OPTION_CREATE_LINK |
54
+ REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK
55
+
56
+ REG_CREATED_NEW_KEY = 1
57
+ REG_OPENED_EXISTING_KEY = 2
58
+
59
+ REG_WHOLE_HIVE_VOLATILE = 0x0001
60
+ REG_REFRESH_HIVE = 0x0002
61
+ REG_NO_LAZY_FLUSH = 0x0004
62
+ REG_FORCE_RESTORE = 0x0008
63
+
64
+ MAX_KEY_LENGTH = 514
65
+ MAX_VALUE_LENGTH = 32768
66
+ end
67
+ include Constants
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ begin
74
+ require 'win32/registry'
75
+ rescue LoadError
76
+ module Win32
77
+ Registry = RSpec::Puppet::Win32::Registry
78
+ end
79
+ end
@@ -0,0 +1,118 @@
1
+ # Create our dummy Win32::TaskScheduler with the consts defined from
2
+ # https://github.com/djberg96/win32-taskscheduler/blob/ole/lib/win32/taskscheduler.rb
3
+
4
+ require 'rspec-puppet/monkey_patches/windows/taskschedulerconstants'
5
+
6
+ module RSpec
7
+ module Puppet
8
+ module Win32
9
+ class TaskScheduler
10
+ include Windows::TaskSchedulerConstants
11
+
12
+ DAYS_IN_A_MONTH = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
13
+
14
+ IDLE = IDLE_PRIORITY_CLASS
15
+ NORMAL = NORMAL_PRIORITY_CLASS
16
+ HIGH = HIGH_PRIORITY_CLASS
17
+ REALTIME = REALTIME_PRIORITY_CLASS
18
+ BELOW_NORMAL = BELOW_NORMAL_PRIORITY_CLASS
19
+ ABOVE_NORMAL = ABOVE_NORMAL_PRIORITY_CLASS
20
+
21
+ ONCE = TASK_TIME_TRIGGER_ONCE
22
+ DAILY = TASK_TIME_TRIGGER_DAILY
23
+ WEEKLY = TASK_TIME_TRIGGER_WEEKLY
24
+ MONTHLYDATE = TASK_TIME_TRIGGER_MONTHLYDATE
25
+ MONTHLYDOW = TASK_TIME_TRIGGER_MONTHLYDOW
26
+
27
+ ON_IDLE = TASK_EVENT_TRIGGER_ON_IDLE
28
+ AT_SYSTEMSTART = TASK_EVENT_TRIGGER_AT_SYSTEMSTART
29
+ AT_LOGON = TASK_EVENT_TRIGGER_AT_LOGON
30
+ FIRST_WEEK = TASK_FIRST_WEEK
31
+ SECOND_WEEK = TASK_SECOND_WEEK
32
+ THIRD_WEEK = TASK_THIRD_WEEK
33
+ FOURTH_WEEK = TASK_FOURTH_WEEK
34
+ LAST_WEEK = TASK_LAST_WEEK
35
+ SUNDAY = TASK_SUNDAY
36
+ MONDAY = TASK_MONDAY
37
+ TUESDAY = TASK_TUESDAY
38
+ WEDNESDAY = TASK_WEDNESDAY
39
+ THURSDAY = TASK_THURSDAY
40
+ FRIDAY = TASK_FRIDAY
41
+ SATURDAY = TASK_SATURDAY
42
+ JANUARY = TASK_JANUARY
43
+ FEBRUARY = TASK_FEBRUARY
44
+ MARCH = TASK_MARCH
45
+ APRIL = TASK_APRIL
46
+ MAY = TASK_MAY
47
+ JUNE = TASK_JUNE
48
+ JULY = TASK_JULY
49
+ AUGUST = TASK_AUGUST
50
+ SEPTEMBER = TASK_SEPTEMBER
51
+ OCTOBER = TASK_OCTOBER
52
+ NOVEMBER = TASK_NOVEMBER
53
+ DECEMBER = TASK_DECEMBER
54
+
55
+ INTERACTIVE = TASK_FLAG_INTERACTIVE
56
+ DELETE_WHEN_DONE = TASK_FLAG_DELETE_WHEN_DONE
57
+ DISABLED = TASK_FLAG_DISABLED
58
+ START_ONLY_IF_IDLE = TASK_FLAG_START_ONLY_IF_IDLE
59
+ KILL_ON_IDLE_END = TASK_FLAG_KILL_ON_IDLE_END
60
+ DONT_START_IF_ON_BATTERIES = TASK_FLAG_DONT_START_IF_ON_BATTERIES
61
+ KILL_IF_GOING_ON_BATTERIES = TASK_FLAG_KILL_IF_GOING_ON_BATTERIES
62
+ RUN_ONLY_IF_DOCKED = TASK_FLAG_RUN_ONLY_IF_DOCKED
63
+ HIDDEN = TASK_FLAG_HIDDEN
64
+ RUN_IF_CONNECTED_TO_INTERNET = TASK_FLAG_RUN_IF_CONNECTED_TO_INTERNET
65
+ RESTART_ON_IDLE_RESUME = TASK_FLAG_RESTART_ON_IDLE_RESUME
66
+ SYSTEM_REQUIRED = TASK_FLAG_SYSTEM_REQUIRED
67
+ RUN_ONLY_IF_LOGGED_ON = TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
68
+
69
+ FLAG_HAS_END_DATE = TASK_TRIGGER_FLAG_HAS_END_DATE
70
+ FLAG_KILL_AT_DURATION_END = TASK_TRIGGER_FLAG_KILL_AT_DURATION_END
71
+ FLAG_DISABLED = TASK_TRIGGER_FLAG_DISABLED
72
+
73
+ MAX_RUN_TIMES = TASK_MAX_RUN_TIMES
74
+
75
+ FIRST = TASK_FIRST
76
+ SECOND = TASK_SECOND
77
+ THIRD = TASK_THIRD
78
+ FOURTH = TASK_FOURTH
79
+ FIFTH = TASK_FIFTH
80
+ SIXTH = TASK_SIXTH
81
+ SEVENTH = TASK_SEVENTH
82
+ EIGHTH = TASK_EIGHTH
83
+ NINETH = TASK_NINETH
84
+ TENTH = TASK_TENTH
85
+ ELEVENTH = TASK_ELEVENTH
86
+ TWELFTH = TASK_TWELFTH
87
+ THIRTEENTH = TASK_THIRTEENTH
88
+ FOURTEENTH = TASK_FOURTEENTH
89
+ FIFTEENTH = TASK_FIFTEENTH
90
+ SIXTEENTH = TASK_SIXTEENTH
91
+ SEVENTEENTH = TASK_SEVENTEENTH
92
+ EIGHTEENTH = TASK_EIGHTEENTH
93
+ NINETEENTH = TASK_NINETEENTH
94
+ TWENTIETH = TASK_TWENTIETH
95
+ TWENTY_FIRST = TASK_TWENTY_FIRST
96
+ TWENTY_SECOND = TASK_TWENTY_SECOND
97
+ TWENTY_THIRD = TASK_TWENTY_THIRD
98
+ TWENTY_FOURTH = TASK_TWENTY_FOURTH
99
+ TWENTY_FIFTH = TASK_TWENTY_FIFTH
100
+ TWENTY_SIXTH = TASK_TWENTY_SIXTH
101
+ TWENTY_SEVENTH = TASK_TWENTY_SEVENTH
102
+ TWENTY_EIGHTH = TASK_TWENTY_EIGHTH
103
+ TWENTY_NINTH = TASK_TWENTY_NINTH
104
+ THIRTYETH = TASK_THIRTYETH
105
+ THIRTY_FIRST = TASK_THIRTY_FIRST
106
+ LAST = TASK_LAST
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ begin
113
+ require 'win32/taskscheduler'
114
+ rescue LoadError
115
+ module Win32
116
+ TaskScheduler = RSpec::Puppet::Win32::TaskScheduler
117
+ end
118
+ end