-
Notifications
You must be signed in to change notification settings - Fork 580
Add beaker tests for functions. #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'abs function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should accept a string' do | ||
pp = <<-EOS | ||
$input = '-34.56' | ||
$output = abs($input) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: 34.56/) | ||
end | ||
end | ||
|
||
it 'should accept a float' do | ||
pp = <<-EOS | ||
$input = -34.56 | ||
$output = abs($input) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: 34.56/) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'any2array function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should create an empty array' do | ||
pp = <<-EOS | ||
$input = '' | ||
$output = any2array($input) | ||
validate_array($output) | ||
notify { "Output: ${output}": } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: Output: /) | ||
end | ||
end | ||
|
||
it 'should leave arrays modified' do | ||
pp = <<-EOS | ||
$input = ['test', 'array'] | ||
$output = any2array($input) | ||
validate_array($output) | ||
notify { "Output: ${output}": } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: Output: testarray/) | ||
end | ||
end | ||
|
||
it 'should turn a hash into an array' do | ||
pp = <<-EOS | ||
$input = {'test' => 'array'} | ||
$output = any2array($input) | ||
|
||
validate_array($output) | ||
# Check each element of the array is a plain string. | ||
validate_string($output[0]) | ||
validate_string($output[1]) | ||
notify { "Output: ${output}": } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: Output: testarray/) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'base64 function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should encode then decode a string' do | ||
pp = <<-EOS | ||
$encodestring = base64('encode', 'thestring') | ||
$decodestring = base64('decode', $encodestring) | ||
notify { $decodestring: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/thestring/) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'bool2num function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
['false', 'f', '0', 'n', 'no'].each do |bool| | ||
it 'should convert a given boolean, #{bool}, to 0' do | ||
pp = <<-EOS | ||
$input = #{bool} | ||
$output = bool2num($input) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: 0/) | ||
end | ||
end | ||
end | ||
|
||
['true', 't', '1', 'y', 'yes'].each do |bool| | ||
it 'should convert a given boolean, #{bool}, to 1' do | ||
pp = <<-EOS | ||
$input = #{bool} | ||
$output = bool2num($input) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: 1/) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env ruby | ||
# vim: set sw=2 sts=2 et tw=80 : | ||
require 'rspec' | ||
require 'pry' | ||
|
||
#XXX Super ugly hack to keep from starting beaker nodes | ||
module Kernel | ||
# make an alias of the original require | ||
alias_method :original_require, :require | ||
# rewrite require | ||
def require name | ||
original_require name if name != 'spec_helper_acceptance' | ||
end | ||
end | ||
UNSUPPORTED_PLATFORMS = [] | ||
def fact(*args) [] end | ||
#XXX End hax | ||
|
||
# Get a list of functions for test coverage | ||
function_list = Dir[File.join(File.dirname(__FILE__),"..","..","lib","puppet","parser","functions","*.rb")].collect do |function_rb| | ||
File.basename(function_rb,".rb") | ||
end | ||
|
||
## Configure rspec to parse tests | ||
options = RSpec::Core::ConfigurationOptions.new(['spec/acceptance']) | ||
configuration = RSpec::configuration | ||
world = RSpec::world | ||
options.parse_options | ||
options.configure(configuration) | ||
configuration.load_spec_files | ||
|
||
## Collect up tests and example groups into a hash | ||
def get_tests(children) | ||
children.inject({}) do |memo,c| | ||
memo[c.description] = Hash.new | ||
memo[c.description]["groups"] = get_tests(c.children) unless c.children.empty? | ||
memo[c.description]["tests"] = c.examples.collect { |e| | ||
e.description unless e.pending? | ||
}.compact unless c.examples.empty? | ||
memo[c.description]["pending_tests"] = c.examples.collect { |e| | ||
e.description if e.pending? | ||
}.compact unless c.examples.empty? | ||
memo | ||
end | ||
end | ||
|
||
# Convert tests hash to csv format | ||
def to_csv(function_list,tests) | ||
function_list.collect do |function_name| | ||
if v = tests["#{function_name} function"] | ||
positive_tests = v["groups"]["success"] ? v["groups"]["success"]["tests"].length : 0 | ||
negative_tests = v["groups"]["failure"] ? v["groups"]["failure"]["tests"].length : 0 | ||
pending_tests = | ||
(v["groups"]["failure"] ? v["groups"]["success"]["pending_tests"].length : 0) + | ||
(v["groups"]["failure"] ? v["groups"]["failure"]["pending_tests"].length : 0) | ||
else | ||
positive_tests = 0 | ||
negative_tests = 0 | ||
pending_tests = 0 | ||
end | ||
sprintf("%-25s, %-9d, %-9d, %-9d", function_name,positive_tests,negative_tests,pending_tests) | ||
end.compact | ||
end | ||
|
||
tests = get_tests(world.example_groups) | ||
csv = to_csv(function_list,tests) | ||
percentage_tested = "#{tests.count*100/function_list.count}%" | ||
printf("%-25s, %-9s, %-9s, %-9s\n","#{percentage_tested} have tests.","Positive","Negative","Pending") | ||
puts csv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'capitalize function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should capitalize the first letter of a string' do | ||
pp = <<-EOS | ||
$input = 'this is a string' | ||
$output = capitalize($input) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: This is a string/) | ||
end | ||
end | ||
|
||
it 'should capitalize the first letter of an array of strings' do | ||
pp = <<-EOS | ||
$input = ['this', 'is', 'a', 'string'] | ||
$output = capitalize($input) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: This/) | ||
expect(r.stdout).to match(/Notice: Is/) | ||
expect(r.stdout).to match(/Notice: A/) | ||
expect(r.stdout).to match(/Notice: String/) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'chomp function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should eat the newline' do | ||
pp = <<-EOS | ||
$input = "test\n" | ||
if size($input) != 5 { | ||
fail("Size of ${input} is not 5.") | ||
} | ||
$output = chomp($input) | ||
if size($output) != 4 { | ||
fail("Size of ${input} is not 4.") | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'chop function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should eat the last character' do | ||
pp = <<-EOS | ||
$input = "test" | ||
if size($input) != 4 { | ||
fail("Size of ${input} is not 4.") | ||
} | ||
$output = chop($input) | ||
if size($output) != 3 { | ||
fail("Size of ${input} is not 3.") | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) | ||
end | ||
|
||
it 'should eat the last two characters of \r\n' do | ||
pp = <<-EOS | ||
$input = "test\r\n" | ||
if size($input) != 6 { | ||
fail("Size of ${input} is not 6.") | ||
} | ||
$output = chop($input) | ||
if size($output) != 4 { | ||
fail("Size of ${input} is not 4.") | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) | ||
end | ||
|
||
it 'should not fail on empty strings' do | ||
pp = <<-EOS | ||
$input = "" | ||
$output = chop($input) | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should concat one array to another' do | ||
pp = <<-EOS | ||
$output = concat(['1','2','3'],['4','5','6']) | ||
validate_array($output) | ||
if size($output) != 6 { | ||
fail("${output} should have 6 elements.") | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'count function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should count elements in an array' do | ||
pp = <<-EOS | ||
$input = [1,2,3,4] | ||
$output = count($input) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: 4/) | ||
end | ||
end | ||
|
||
it 'should count elements in an array that match a second argument' do | ||
pp = <<-EOS | ||
$input = [1,1,1,2] | ||
$output = count($input, 1) | ||
notify { $output: } | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: 3/) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'deep_merge function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should deep merge two hashes' do | ||
pp = <<-EOS | ||
$hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } } | ||
$hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } } | ||
$merged_hash = deep_merge($hash1, $hash2) | ||
|
||
if $merged_hash != { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } } { | ||
fail("Hash was incorrectly merged.") | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'defined_with_params function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do | ||
describe 'success' do | ||
it 'should successfully notify' do | ||
pp = <<-EOS | ||
user { 'dan': | ||
ensure => present, | ||
} | ||
|
||
if defined_with_params(User[dan], {'ensure' => 'present' }) { | ||
notify { 'User defined with ensure=>present': } | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) do |r| | ||
expect(r.stdout).to match(/Notice: User defined with ensure=>present/) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what, exactly, does this Super ugly hack do?
Why are we not supposed to start beaker nodes for this test?