puppet-ci-testing 0.11.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.
@@ -0,0 +1,165 @@
1
+ require 'singleton'
2
+ require 'time'
3
+
4
+ module SimpleJUnit
5
+
6
+ class TestSuiteCollection
7
+
8
+ include Singleton
9
+
10
+ attr :testsuites
11
+
12
+ def initialize
13
+ # initialization code needs to be put in reset method for testing
14
+ self.reset
15
+ end
16
+
17
+ def create_testsuite(name)
18
+ @testsuites << SimpleJUnit::TestSuite.new(name)
19
+ @testsuites.last
20
+ end
21
+
22
+ def to_s
23
+ return <<-EOTC
24
+ <?xml version="1.0" encoding="UTF-8"?>
25
+ <testsuites>
26
+ #{(@testsuites.each {|ts| ts.to_s}).join}
27
+ </testsuites>
28
+ EOTC
29
+ end
30
+
31
+ # This is really the initialize method, but for testing we want to be
32
+ # able to reset the singleton to an initialized state
33
+ def reset
34
+ @testsuites = []
35
+ end
36
+ end
37
+
38
+
39
+ class TestSuite
40
+
41
+ attr :name
42
+ attr_accessor :testcases
43
+
44
+ def initialize(name)
45
+ @name = name
46
+ @timestamp = Time.now()
47
+ @testcases = []
48
+ end
49
+
50
+ def add_testcase(testcase)
51
+ unless @testcases.include? testcase
52
+ @testcases << testcase
53
+ end
54
+ end
55
+
56
+ def create_testcase(name, desc=nil)
57
+ @testcases << TestCase.new(name, desc)
58
+ @testcases.last
59
+ end
60
+
61
+ def to_s
62
+ num_testcase = @testcases.count
63
+ num_failures = (@testcases.select {|tc| tc.failed?}).count
64
+
65
+ return <<-EOTS
66
+ <testsuite name="#{@name}" errors="0" tests="#{num_testcase}" failures="#{num_failures}" time="0" timestamp="#{@timestamp.iso8601}">
67
+ <properties/>
68
+ #{(@testcases.collect {|tc| tc.to_s}).join}
69
+ </testsuite>
70
+ EOTS
71
+ end
72
+ end
73
+
74
+
75
+ class TestCase
76
+
77
+ attr :classname
78
+ attr :description
79
+ attr :duration
80
+ attr :status
81
+ attr :errors
82
+ attr :output
83
+
84
+ def initialize(name, desc=nil, duration=nil)
85
+ @classname = name
86
+ @description = desc
87
+ @duration = duration
88
+ @status = :pending
89
+ @errors = nil
90
+ @output = nil
91
+ end
92
+
93
+ def passed(output:nil, error:nil)
94
+ @status = :passed
95
+ unless output.nil?
96
+ @output = output
97
+ end
98
+ unless error.nil?
99
+ @errors = error
100
+ end
101
+ end
102
+
103
+ def passed?
104
+ @status == :passed
105
+ end
106
+
107
+ def failed(type:nil, output:nil, error:nil)
108
+ @status = :failed
109
+ @error_type = type || 'unspecified'
110
+ unless output.nil?
111
+ @output = output
112
+ end
113
+ unless error.nil?
114
+ @errors = error
115
+ end
116
+ end
117
+
118
+ def failed?
119
+ @status == :failed
120
+ end
121
+
122
+ def pending?
123
+ @status == :pending
124
+ end
125
+
126
+ def skip
127
+ @status = :skipped
128
+ end
129
+
130
+ def skipped?
131
+ @status == :skipped
132
+ end
133
+
134
+ def start
135
+ @started = Time.now
136
+ end
137
+
138
+ def finish
139
+ @duration = Time.now - @started
140
+ end
141
+
142
+
143
+ def to_s
144
+ xml = " <testcase classname=\"#{@classname}\" "
145
+ unless @description.nil?
146
+ xml += "name=\"#{@description}\" "
147
+ end
148
+ xml += "time=\"#{@duration}\">\n"
149
+
150
+ case @status
151
+ when :skipped
152
+ xml += " <skipped/>\n"
153
+ when :failed
154
+ xml += " <failure message=\"#{@error_type}\">#{@errors}</failure>\n"
155
+ end
156
+
157
+ xml += " <system-out>#{@output}</system-out>\n" if @output
158
+ xml += " <system-err>#{@errors}</system-err>\n" if @errors
159
+
160
+ xml += " </testcase>\n"
161
+ return xml
162
+ end
163
+
164
+ end
165
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'check_file_syntax'
3
+
4
+ describe 'CheckFileSyntax' do
5
+
6
+ { :json => '.json',
7
+ :yaml => ['.yaml','.yml'],
8
+ :perl => ['.pl','.pm'],
9
+ :bash => ['.sh','.bash','.zsh','.ksh'],
10
+ :ruby => '.rb',
11
+ :python => '.py',
12
+ :erb => '.erb',
13
+ :puppet => '.pp' }.each_pair do |type, exts|
14
+ [exts].flatten.each do |ext|
15
+ it "identifies #{type} with #{ext} extension" do
16
+ expect(CheckFileSyntax::type_of_file("foo#{ext}", type, exts)).to eq true
17
+ end
18
+ end
19
+ end
20
+
21
+ { :json => '.json',
22
+ :yaml => ['.yaml','.yml'],
23
+ :perl => ['.pl','.pm'],
24
+ :bash => ['.sh','.bash','.zsh','.ksh'],
25
+ :ruby => '.rb',
26
+ :python => '.py',
27
+ :erb => '.erb',
28
+ :puppet => '.pp' }.each_pair do |type, exts|
29
+ bad_ext = random_string(8)
30
+ [exts].flatten.each do |ext|
31
+ it "fails identifying #{type} without #{ext} extension" do
32
+ expect(CheckFileSyntax::type_of_file("foo.#{bad_ext}", type, exts)).to eq false
33
+ end
34
+ end
35
+ end
36
+
37
+ CheckFileSyntax::ALL_CHECKS.each do |type|
38
+ bad_ext ||= random_string(8)
39
+ # Puppet, ERB, JSON and YAML files don't have shebang lines
40
+ unless [:puppet, :erb, :json, :yaml].include? type
41
+ it "identifies content as #{type}" do
42
+ filename = eval "generate_#{type.to_s}(:valid, extension:'.#{bad_ext}')"
43
+ expect(CheckFileSyntax::type_of_file(filename, type, '.foo')).to eq true
44
+ File.unlink filename
45
+ end
46
+ end
47
+ end
48
+
49
+ CheckFileSyntax::ALL_CHECKS.each do |type|
50
+ it "identifies valid syntax of #{type}" do
51
+ filename = eval "generate_#{type.to_s}(:valid)"
52
+ CheckFileSyntax::check_file_syntax(filename) { |path, status, errors|
53
+ expect(status).to eq :passed
54
+ }
55
+ File.unlink filename
56
+ File.unlink "#{filename}c" if type == :python
57
+ end
58
+ end
59
+
60
+ CheckFileSyntax::ALL_CHECKS.each do |type|
61
+ it "identifies invalid syntax of #{type}" do
62
+ filename = eval "generate_#{type.to_s}(:invalid)"
63
+ CheckFileSyntax::check_file_syntax(filename) { |path, status, errors|
64
+ expect(status).to eq :failed
65
+ }
66
+ File.unlink filename
67
+ end
68
+ end
69
+
70
+
71
+ end
@@ -0,0 +1,157 @@
1
+ require 'spec_helper'
2
+ require 'simple_junit'
3
+
4
+ describe 'SimpleJUnit' do
5
+ describe 'TestSuiteCollection' do
6
+ it 'creates an empty test suite collection' do
7
+ tc = SimpleJUnit::TestSuiteCollection.instance
8
+ expect(tc.testsuites).to eq []
9
+ tc.reset
10
+ end
11
+
12
+ it 'creates two test suite collections' do
13
+ tc = SimpleJUnit::TestSuiteCollection.instance
14
+ tc.create_testsuite('foo')
15
+ tc.create_testsuite('bar')
16
+ expect(tc.testsuites.count).to eq 2
17
+ tc.reset
18
+ end
19
+
20
+ it 'generates valid JUnit XML output' do
21
+ tc = SimpleJUnit::TestSuiteCollection.instance
22
+ xml = tc.to_s.gsub(/\n\s*/, "")
23
+ expect(xml).to match /<\?xml version="1.0" encoding="UTF-8"\?><testsuites><\/testsuites>/
24
+ tc.reset
25
+ end
26
+
27
+ it 'generates valid JUnit XML output with test suites' do
28
+ tc = SimpleJUnit::TestSuiteCollection.instance
29
+ tc.create_testsuite('foo')
30
+ tc.create_testsuite('bar')
31
+ xml = tc.to_s.gsub(/\n\s*/, "")
32
+ expect(xml).to match /<\?xml version="1.0" encoding="UTF-8"\?>/
33
+ expect(xml).to match /\s*<testsuite name="foo" errors="0" tests="0" failures="0" time="0" timestamp="[^\"]+">/
34
+ expect(xml).to match /\s*<testsuite name="bar" errors="0" tests="0" failures="0" time="0" timestamp="[^\"]+">/
35
+ tc.reset
36
+ end
37
+ end
38
+
39
+
40
+ describe 'TestSuite' do
41
+ it 'generates valid XML' do
42
+ name = random_string(5)
43
+ ts = SimpleJUnit::TestSuite.new(name)
44
+ t1 = random_string(3)
45
+ ts.create_testcase(t1).passed
46
+ t2 = random_string(3)
47
+ ts.create_testcase(t2).failed
48
+ t3 = random_string(3)
49
+ ts.create_testcase(t3).failed
50
+ t4 = random_string(3)
51
+ ts.create_testcase(t4).passed
52
+ t5 = random_string(3)
53
+ ts.create_testcase(t5).skip
54
+ xml = ts.to_s.gsub(/\n\s*/, "")
55
+ expect(xml).to match /<testsuite name="#{name}" errors="0" tests="5" failures="2" time="0" timestamp="[^"]+"><properties\/><testcase classname="#{t1}" time=""><\/testcase><testcase classname="#{t2}" time=""><failure message="unspecified"><\/failure><\/testcase><testcase classname="#{t3}" time=""><failure message="unspecified"><\/failure><\/testcase><testcase classname="#{t4}" time=""><\/testcase><testcase classname="#{t5}" time=""><skipped\/><\/testcase><\/testsuite>/
56
+ end
57
+
58
+ end
59
+
60
+
61
+ describe 'TestCase' do
62
+ it 'passed() sets internal state correctly' do
63
+ t = SimpleJUnit::TestCase.new('foo')
64
+ output = random_string(30)
65
+ error = random_string(20)
66
+ t.passed(output: output, error: error)
67
+ expect(t.status).to eq :passed
68
+ expect(t.output).to eq output
69
+ expect(t.errors).to eq error
70
+ end
71
+
72
+ it 'passed? test correct' do
73
+ t = SimpleJUnit::TestCase.new('foo')
74
+ expect(t.passed?).to eq false
75
+ t.passed
76
+ expect(t.passed?).to eq true
77
+ t.failed
78
+ expect(t.passed?).to eq false
79
+ t.skip
80
+ expect(t.passed?).to eq false
81
+ end
82
+
83
+ it 'failed() sets internal state correctly with error type' do
84
+ t = SimpleJUnit::TestCase.new('foo')
85
+ output = random_string(30)
86
+ error = random_string(20)
87
+ t.failed(type:'bar', output:output, error:error)
88
+ expect(t.status).to eq :failed
89
+ expect(t.output).to eq output
90
+ expect(t.errors).to eq error
91
+ end
92
+
93
+ it 'failed() sets internal state correctly without error type' do
94
+ t = SimpleJUnit::TestCase.new('foo')
95
+ output = random_string(30)
96
+ error = random_string(20)
97
+ t.failed(:output => output, :error => error)
98
+ expect(t.status).to eq :failed
99
+ expect(t.output).to eq output
100
+ expect(t.errors).to eq error
101
+ end
102
+
103
+ it 'failed? test correct' do
104
+ t = SimpleJUnit::TestCase.new('foo')
105
+ expect(t.failed?).to eq false
106
+ t.failed
107
+ expect(t.failed?).to eq true
108
+ t.passed
109
+ expect(t.failed?).to eq false
110
+ t.skip
111
+ expect(t.passed?).to eq false
112
+ end
113
+
114
+ it 'pending? test correct' do
115
+ t = SimpleJUnit::TestCase.new('foo')
116
+ expect(t.pending?).to eq true
117
+ t.failed
118
+ expect(t.pending?).to eq false
119
+ t.passed
120
+ expect(t.pending?).to eq false
121
+ t.skip
122
+ expect(t.pending?).to eq false
123
+ end
124
+
125
+ it 'skipped? test correct' do
126
+ t = SimpleJUnit::TestCase.new('foo')
127
+ expect(t.skipped?).to eq false
128
+ t.failed
129
+ expect(t.skipped?).to eq false
130
+ t.passed
131
+ expect(t.skipped?).to eq false
132
+ t.skip
133
+ expect(t.skipped?).to eq true
134
+ end
135
+
136
+ it 'duration is calculated correctly' do
137
+ t = SimpleJUnit::TestCase.new('foo')
138
+ t.start
139
+ sleep(2)
140
+ t.finish
141
+ expect(t.duration.to_i).to eq 2
142
+ end
143
+
144
+ it 'generates correct XML' do
145
+ name = random_string(5)
146
+ type = random_string(7)
147
+ output = random_string(13)
148
+ error = random_string(11)
149
+ t = SimpleJUnit::TestCase.new(name)
150
+ t.passed(output: output, error: error)
151
+ xml = t.to_s.gsub(/\n\s*/, '')
152
+ expect(xml).to match /<testcase classname="#{name}" time=""><system-out>#{output}<\/system-out><system-err>#{error}<\/system-err><\/testcase>/
153
+ end
154
+ end
155
+ end
156
+
157
+
@@ -0,0 +1,288 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
44
+ # have no way to turn it off -- the option exists only for backwards
45
+ # compatibility in RSpec 3). It causes shared context metadata to be
46
+ # inherited by the metadata hash of host groups and examples, rather than
47
+ # triggering implicit auto-inclusion in groups with matching metadata.
48
+ config.shared_context_metadata_behavior = :apply_to_host_groups
49
+
50
+ # The settings below are suggested to provide a good initial experience
51
+ # with RSpec, but feel free to customize to your heart's content.
52
+ =begin
53
+ # This allows you to limit a spec run to individual examples or groups
54
+ # you care about by tagging them with `:focus` metadata. When nothing
55
+ # is tagged with `:focus`, all examples get run. RSpec also provides
56
+ # aliases for `it`, `describe`, and `context` that include `:focus`
57
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
58
+ config.filter_run_when_matching :focus
59
+
60
+ # Allows RSpec to persist some state between runs in order to support
61
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
62
+ # you configure your source control system to ignore this file.
63
+ config.example_status_persistence_file_path = "spec/examples.txt"
64
+
65
+ # Limits the available syntax to the non-monkey patched syntax that is
66
+ # recommended. For more details, see:
67
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
68
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
69
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
70
+ config.disable_monkey_patching!
71
+
72
+ # This setting enables warnings. It's recommended, but in some cases may
73
+ # be too noisy due to issues in dependencies.
74
+ config.warnings = true
75
+
76
+ # Many RSpec users commonly either run the entire suite or an individual
77
+ # file, and it's useful to allow more verbose output when running an
78
+ # individual spec file.
79
+ if config.files_to_run.one?
80
+ # Use the documentation formatter for detailed output,
81
+ # unless a formatter has already been configured
82
+ # (e.g. via a command-line flag).
83
+ config.default_formatter = 'doc'
84
+ end
85
+
86
+ # Print the 10 slowest examples and example groups at the
87
+ # end of the spec run, to help surface which specs are running
88
+ # particularly slow.
89
+ config.profile_examples = 10
90
+
91
+ # Run specs in random order to surface order dependencies. If you find an
92
+ # order dependency and want to debug it, you can fix the order by providing
93
+ # the seed, which is printed after each run.
94
+ # --seed 1234
95
+ config.order = :random
96
+
97
+ # Seed global randomization in this process using the `--seed` CLI option.
98
+ # Setting this allows you to use `--seed` to deterministically reproduce
99
+ # test failures related to randomization by passing the same `--seed` value
100
+ # as the one that triggered the failure.
101
+ Kernel.srand config.seed
102
+ =end
103
+ end
104
+
105
+ def random_string(size=10)
106
+ (0..size).map { ('a'..'z').to_a[rand(26)] }.join
107
+ end
108
+
109
+
110
+ def generate_json(validity, extension: '.json')
111
+ filebase = random_string(16)
112
+ if validity == :valid
113
+ open(filebase + extension, 'w') do |fh|
114
+ fh.puts <<EOF
115
+ {
116
+ "test": "good JSON",
117
+ "foo": "bar"
118
+ }
119
+ EOF
120
+ end
121
+ elsif validity == :invalid
122
+ open(filebase + extension, 'w') do |fh|
123
+ fh.puts <<EOF
124
+ {
125
+ "test": "bad JSON"
126
+ "foo": "bar"
127
+ }
128
+ EOF
129
+ end
130
+ end
131
+
132
+ return filebase + extension
133
+ end
134
+
135
+
136
+ def generate_yaml(validity, extension: '.yaml')
137
+ filebase = random_string(16)
138
+ if validity == :valid
139
+ open(filebase + extension, 'w') do |fh|
140
+ fh.puts <<EOF
141
+ ---
142
+ test: "good YAML"
143
+ foo: "bar"
144
+ EOF
145
+ end
146
+ elsif validity == :invalid
147
+ open(filebase + extension, 'w') do |fh|
148
+ fh.puts <<EOF
149
+ ===
150
+ test: "bad YAML",
151
+ foo: "bar"
152
+ EOF
153
+ end
154
+ end
155
+
156
+ return filebase + extension
157
+ end
158
+
159
+
160
+ def generate_ruby(validity, extension: '.rb')
161
+ filebase = random_string(16)
162
+ if validity == :valid
163
+ open(filebase + extension, 'w') do |fh|
164
+ fh.puts <<EOF
165
+ #!/usr/bin/env ruby
166
+ puts "valid ruby!"
167
+ EOF
168
+ end
169
+ elsif validity == :invalid
170
+ open(filebase + extension, 'w') do |fh|
171
+ fh.puts <<EOF
172
+ #!/usr/bin/env ruby
173
+ put "invalid ruby!'
174
+ EOF
175
+ end
176
+ end
177
+
178
+ return filebase + extension
179
+ end
180
+
181
+
182
+ def generate_python(validity, extension: '.py')
183
+ filebase = random_string(16)
184
+ if validity == :valid
185
+ open(filebase + extension, 'w') do |fh|
186
+ fh.puts <<EOF
187
+ #!/usr/bin/env python
188
+ print("valid python!")
189
+ EOF
190
+ end
191
+ elsif validity == :invalid
192
+ open(filebase + extension, 'w') do |fh|
193
+ fh.puts <<EOF
194
+ #!/usr/bin/env python
195
+ print("invalid python!')
196
+ EOF
197
+ end
198
+ end
199
+
200
+ return filebase + extension
201
+ end
202
+
203
+
204
+ def generate_perl(validity, extension: '.pl')
205
+ filebase = random_string(16)
206
+ if validity == :valid
207
+ open(filebase + extension, 'w') do |fh|
208
+ fh.puts <<EOF
209
+ #!/usr/bin/env perl
210
+ print "valid ";
211
+ print "perl!"
212
+ EOF
213
+ end
214
+ elsif validity == :invalid
215
+ open(filebase + extension, 'w') do |fh|
216
+ fh.puts <<EOF
217
+ #!/usr/bin/env perl
218
+ print "invalid "
219
+ print "perl!"
220
+ EOF
221
+ end
222
+ end
223
+
224
+ return filebase + extension
225
+ end
226
+
227
+
228
+ def generate_bash(validity, extension: '.sh')
229
+ filebase = random_string(16)
230
+ if validity == :valid
231
+ open(filebase + extension, 'w') do |fh|
232
+ fh.puts <<EOF
233
+ #!/bin/bash
234
+ echo "valid shell!"
235
+ EOF
236
+ end
237
+ elsif validity == :invalid
238
+ open(filebase + extension, 'w') do |fh|
239
+ fh.puts <<EOF
240
+ #!/bin/bash
241
+ echo "invalid shell!'
242
+ EOF
243
+ end
244
+ end
245
+
246
+ return filebase + extension
247
+ end
248
+
249
+
250
+ def generate_erb(validity, extension: '.erb')
251
+ filebase = random_string(16)
252
+ if validity == :valid
253
+ open(filebase + extension, 'w') do |fh|
254
+ fh.puts <<EOF
255
+ <% puts "valid erb!" %>
256
+ EOF
257
+ end
258
+ elsif validity == :invalid
259
+ open(filebase + extension, 'w') do |fh|
260
+ fh.puts <<EOF
261
+ <% puts "invalid " <%= erb!" %>
262
+ EOF
263
+ end
264
+ end
265
+
266
+ return filebase + extension
267
+ end
268
+
269
+
270
+ def generate_puppet(validity, extension: '.pp')
271
+ filebase = random_string(16)
272
+ if validity == :valid
273
+ open(filebase + extension, 'w') do |fh|
274
+ fh.puts <<EOF
275
+ puppet { 'good': }
276
+ EOF
277
+ end
278
+ elsif validity == :invalid
279
+ open(filebase + extension, 'w') do |fh|
280
+ fh.puts <<EOF
281
+ puppet { "bad" }
282
+ EOF
283
+ end
284
+ end
285
+
286
+ return filebase + extension
287
+ end
288
+