Skip to content

Commit f8bfe46

Browse files
author
Ashley Penney
committed
Merge pull request #243 from hunner/add_beaker
Add beaker tests for functions.
2 parents 0b59dfe + 9022295 commit f8bfe46

36 files changed

+1309
-4
lines changed

Gemfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ group :development, :test do
1414
gem 'rake', '~> 10.1.0', :require => false
1515
gem 'rspec-puppet', :require => false
1616
gem 'puppetlabs_spec_helper', :require => false
17-
gem 'rspec-system', :require => false
18-
gem 'rspec-system-puppet', :require => false
19-
gem 'rspec-system-serverspec', :require => false
2017
gem 'serverspec', :require => false
2118
gem 'puppet-lint', :require => false
2219
gem 'pry', :require => false

spec/acceptance/abs_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'abs function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should accept a string' do
6+
pp = <<-EOS
7+
$input = '-34.56'
8+
$output = abs($input)
9+
notify { $output: }
10+
EOS
11+
12+
apply_manifest(pp, :catch_failures => true) do |r|
13+
expect(r.stdout).to match(/Notice: 34.56/)
14+
end
15+
end
16+
17+
it 'should accept a float' do
18+
pp = <<-EOS
19+
$input = -34.56
20+
$output = abs($input)
21+
notify { $output: }
22+
EOS
23+
24+
apply_manifest(pp, :catch_failures => true) do |r|
25+
expect(r.stdout).to match(/Notice: 34.56/)
26+
end
27+
end
28+
end
29+
end

spec/acceptance/any2array_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'any2array function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should create an empty array' do
6+
pp = <<-EOS
7+
$input = ''
8+
$output = any2array($input)
9+
validate_array($output)
10+
notify { "Output: ${output}": }
11+
EOS
12+
13+
apply_manifest(pp, :catch_failures => true) do |r|
14+
expect(r.stdout).to match(/Notice: Output: /)
15+
end
16+
end
17+
18+
it 'should leave arrays modified' do
19+
pp = <<-EOS
20+
$input = ['test', 'array']
21+
$output = any2array($input)
22+
validate_array($output)
23+
notify { "Output: ${output}": }
24+
EOS
25+
26+
apply_manifest(pp, :catch_failures => true) do |r|
27+
expect(r.stdout).to match(/Notice: Output: testarray/)
28+
end
29+
end
30+
31+
it 'should turn a hash into an array' do
32+
pp = <<-EOS
33+
$input = {'test' => 'array'}
34+
$output = any2array($input)
35+
36+
validate_array($output)
37+
# Check each element of the array is a plain string.
38+
validate_string($output[0])
39+
validate_string($output[1])
40+
notify { "Output: ${output}": }
41+
EOS
42+
43+
apply_manifest(pp, :catch_failures => true) do |r|
44+
expect(r.stdout).to match(/Notice: Output: testarray/)
45+
end
46+
end
47+
end
48+
end

spec/acceptance/base64_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'base64 function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should encode then decode a string' do
6+
pp = <<-EOS
7+
$encodestring = base64('encode', 'thestring')
8+
$decodestring = base64('decode', $encodestring)
9+
notify { $decodestring: }
10+
EOS
11+
12+
apply_manifest(pp, :catch_failures => true) do |r|
13+
expect(r.stdout).to match(/thestring/)
14+
end
15+
end
16+
end
17+
end

spec/acceptance/bool2num_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'bool2num function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
['false', 'f', '0', 'n', 'no'].each do |bool|
6+
it 'should convert a given boolean, #{bool}, to 0' do
7+
pp = <<-EOS
8+
$input = #{bool}
9+
$output = bool2num($input)
10+
notify { $output: }
11+
EOS
12+
13+
apply_manifest(pp, :catch_failures => true) do |r|
14+
expect(r.stdout).to match(/Notice: 0/)
15+
end
16+
end
17+
end
18+
19+
['true', 't', '1', 'y', 'yes'].each do |bool|
20+
it 'should convert a given boolean, #{bool}, to 1' do
21+
pp = <<-EOS
22+
$input = #{bool}
23+
$output = bool2num($input)
24+
notify { $output: }
25+
EOS
26+
27+
apply_manifest(pp, :catch_failures => true) do |r|
28+
expect(r.stdout).to match(/Notice: 1/)
29+
end
30+
end
31+
end
32+
end
33+
end

spec/acceptance/build_csv.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env ruby
2+
# vim: set sw=2 sts=2 et tw=80 :
3+
require 'rspec'
4+
require 'pry'
5+
6+
#XXX Super ugly hack to keep from starting beaker nodes
7+
module Kernel
8+
# make an alias of the original require
9+
alias_method :original_require, :require
10+
# rewrite require
11+
def require name
12+
original_require name if name != 'spec_helper_acceptance'
13+
end
14+
end
15+
UNSUPPORTED_PLATFORMS = []
16+
def fact(*args) [] end
17+
#XXX End hax
18+
19+
# Get a list of functions for test coverage
20+
function_list = Dir[File.join(File.dirname(__FILE__),"..","..","lib","puppet","parser","functions","*.rb")].collect do |function_rb|
21+
File.basename(function_rb,".rb")
22+
end
23+
24+
## Configure rspec to parse tests
25+
options = RSpec::Core::ConfigurationOptions.new(['spec/acceptance'])
26+
configuration = RSpec::configuration
27+
world = RSpec::world
28+
options.parse_options
29+
options.configure(configuration)
30+
configuration.load_spec_files
31+
32+
## Collect up tests and example groups into a hash
33+
def get_tests(children)
34+
children.inject({}) do |memo,c|
35+
memo[c.description] = Hash.new
36+
memo[c.description]["groups"] = get_tests(c.children) unless c.children.empty?
37+
memo[c.description]["tests"] = c.examples.collect { |e|
38+
e.description unless e.pending?
39+
}.compact unless c.examples.empty?
40+
memo[c.description]["pending_tests"] = c.examples.collect { |e|
41+
e.description if e.pending?
42+
}.compact unless c.examples.empty?
43+
memo
44+
end
45+
end
46+
47+
# Convert tests hash to csv format
48+
def to_csv(function_list,tests)
49+
function_list.collect do |function_name|
50+
if v = tests["#{function_name} function"]
51+
positive_tests = v["groups"]["success"] ? v["groups"]["success"]["tests"].length : 0
52+
negative_tests = v["groups"]["failure"] ? v["groups"]["failure"]["tests"].length : 0
53+
pending_tests =
54+
(v["groups"]["failure"] ? v["groups"]["success"]["pending_tests"].length : 0) +
55+
(v["groups"]["failure"] ? v["groups"]["failure"]["pending_tests"].length : 0)
56+
else
57+
positive_tests = 0
58+
negative_tests = 0
59+
pending_tests = 0
60+
end
61+
sprintf("%-25s, %-9d, %-9d, %-9d", function_name,positive_tests,negative_tests,pending_tests)
62+
end.compact
63+
end
64+
65+
tests = get_tests(world.example_groups)
66+
csv = to_csv(function_list,tests)
67+
percentage_tested = "#{tests.count*100/function_list.count}%"
68+
printf("%-25s, %-9s, %-9s, %-9s\n","#{percentage_tested} have tests.","Positive","Negative","Pending")
69+
puts csv

spec/acceptance/capitalize_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'capitalize function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should capitalize the first letter of a string' do
6+
pp = <<-EOS
7+
$input = 'this is a string'
8+
$output = capitalize($input)
9+
notify { $output: }
10+
EOS
11+
12+
apply_manifest(pp, :catch_failures => true) do |r|
13+
expect(r.stdout).to match(/Notice: This is a string/)
14+
end
15+
end
16+
17+
it 'should capitalize the first letter of an array of strings' do
18+
pp = <<-EOS
19+
$input = ['this', 'is', 'a', 'string']
20+
$output = capitalize($input)
21+
notify { $output: }
22+
EOS
23+
24+
apply_manifest(pp, :catch_failures => true) do |r|
25+
expect(r.stdout).to match(/Notice: This/)
26+
expect(r.stdout).to match(/Notice: Is/)
27+
expect(r.stdout).to match(/Notice: A/)
28+
expect(r.stdout).to match(/Notice: String/)
29+
end
30+
end
31+
end
32+
end

spec/acceptance/chomp_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'chomp function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should eat the newline' do
6+
pp = <<-EOS
7+
$input = "test\n"
8+
if size($input) != 5 {
9+
fail("Size of ${input} is not 5.")
10+
}
11+
$output = chomp($input)
12+
if size($output) != 4 {
13+
fail("Size of ${input} is not 4.")
14+
}
15+
EOS
16+
17+
apply_manifest(pp, :catch_failures => true)
18+
end
19+
end
20+
end

spec/acceptance/chop_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'chop function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should eat the last character' do
6+
pp = <<-EOS
7+
$input = "test"
8+
if size($input) != 4 {
9+
fail("Size of ${input} is not 4.")
10+
}
11+
$output = chop($input)
12+
if size($output) != 3 {
13+
fail("Size of ${input} is not 3.")
14+
}
15+
EOS
16+
17+
apply_manifest(pp, :catch_failures => true)
18+
end
19+
20+
it 'should eat the last two characters of \r\n' do
21+
pp = <<-EOS
22+
$input = "test\r\n"
23+
if size($input) != 6 {
24+
fail("Size of ${input} is not 6.")
25+
}
26+
$output = chop($input)
27+
if size($output) != 4 {
28+
fail("Size of ${input} is not 4.")
29+
}
30+
EOS
31+
32+
apply_manifest(pp, :catch_failures => true)
33+
end
34+
35+
it 'should not fail on empty strings' do
36+
pp = <<-EOS
37+
$input = ""
38+
$output = chop($input)
39+
EOS
40+
41+
apply_manifest(pp, :catch_failures => true)
42+
end
43+
end
44+
end

spec/acceptance/concat_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
4+
describe 'success' do
5+
it 'should concat one array to another' do
6+
pp = <<-EOS
7+
$output = concat(['1','2','3'],['4','5','6'])
8+
validate_array($output)
9+
if size($output) != 6 {
10+
fail("${output} should have 6 elements.")
11+
}
12+
EOS
13+
14+
apply_manifest(pp, :catch_failures => true)
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)