Skip to content

Commit d32d61f

Browse files
committed
Initial commit
0 parents  commit d32d61f

File tree

8 files changed

+308
-0
lines changed

8 files changed

+308
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Puppet Oracle Module
2+
====================
3+
4+
This repository aims to ease the configuration of Oracle
5+
Databases with custom types and providers

spec/lib/puppet_spec/files.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'fileutils'
2+
require 'tempfile'
3+
4+
# A support module for testing files.
5+
module PuppetSpec::Files
6+
# This code exists only to support tests that run as root, pretty much.
7+
# Once they have finally been eliminated this can all go... --daniel 2011-04-08
8+
if Puppet.features.posix? then
9+
def self.in_tmp(path)
10+
path =~ /^\/tmp/ or path =~ /^\/var\/folders/
11+
end
12+
elsif Puppet.features.microsoft_windows?
13+
def self.in_tmp(path)
14+
tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp"))
15+
path =~ /^#{tempdir}/
16+
end
17+
else
18+
fail "Help! Can't find in_tmp for this platform"
19+
end
20+
21+
def self.cleanup
22+
$global_tempfiles ||= []
23+
while path = $global_tempfiles.pop do
24+
fail "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)
25+
26+
begin
27+
FileUtils.rm_r path, :secure => true
28+
rescue Errno::ENOENT
29+
# nothing to do
30+
end
31+
end
32+
end
33+
34+
def tmpfile(name)
35+
# Generate a temporary file, just for the name...
36+
source = Tempfile.new(name)
37+
path = source.path
38+
source.close!
39+
40+
# ...record it for cleanup,
41+
$global_tempfiles ||= []
42+
$global_tempfiles << File.expand_path(path)
43+
44+
# ...and bam.
45+
path
46+
end
47+
48+
def tmpdir(name)
49+
path = tmpfile(name)
50+
FileUtils.mkdir_p(path)
51+
path
52+
end
53+
end

spec/lib/puppet_spec/fixtures.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module PuppetSpec::Fixtures
2+
def fixtures(*rest)
3+
File.join(PuppetSpec::FIXTURE_DIR, *rest)
4+
end
5+
def my_fixture_dir
6+
callers = caller
7+
while line = callers.shift do
8+
next unless found = line.match(%r{/spec/(.*)_spec\.rb:})
9+
return fixtures(found[1])
10+
end
11+
fail "sorry, I couldn't work out your path from the caller stack!"
12+
end
13+
def my_fixture(name)
14+
file = File.join(my_fixture_dir, name)
15+
unless File.readable? file then
16+
fail Puppet::DevError, "fixture '#{name}' for #{my_fixture_dir} is not readable"
17+
end
18+
return file
19+
end
20+
def my_fixtures(glob = '*', flags = 0)
21+
files = Dir.glob(File.join(my_fixture_dir, glob), flags)
22+
unless files.length > 0 then
23+
fail Puppet::DevError, "fixture '#{glob}' for #{my_fixture_dir} had no files!"
24+
end
25+
block_given? and files.each do |file| yield file end
26+
files
27+
end
28+
end

spec/lib/puppet_spec/matchers.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require 'stringio'
2+
3+
########################################################################
4+
# Backward compatibility for Jenkins outdated environment.
5+
module RSpec
6+
module Matchers
7+
module BlockAliases
8+
alias_method :to, :should unless method_defined? :to
9+
alias_method :to_not, :should_not unless method_defined? :to_not
10+
alias_method :not_to, :should_not unless method_defined? :not_to
11+
end
12+
end
13+
end
14+
15+
16+
########################################################################
17+
# Custom matchers...
18+
RSpec::Matchers.define :have_matching_element do |expected|
19+
match do |actual|
20+
actual.any? { |item| item =~ expected }
21+
end
22+
end
23+
24+
25+
RSpec::Matchers.define :exit_with do |expected|
26+
actual = nil
27+
match do |block|
28+
begin
29+
block.call
30+
rescue SystemExit => e
31+
actual = e.status
32+
end
33+
actual and actual == expected
34+
end
35+
failure_message_for_should do |block|
36+
"expected exit with code #{expected} but " +
37+
(actual.nil? ? " exit was not called" : "we exited with #{actual} instead")
38+
end
39+
failure_message_for_should_not do |block|
40+
"expected that exit would not be called with #{expected}"
41+
end
42+
description do
43+
"expect exit with #{expected}"
44+
end
45+
end
46+
47+
48+
RSpec::Matchers.define :have_printed do |expected|
49+
match do |block|
50+
$stderr = $stdout = StringIO.new
51+
52+
begin
53+
block.call
54+
ensure
55+
$stdout.rewind
56+
@actual = $stdout.read
57+
58+
$stdout = STDOUT
59+
$stderr = STDERR
60+
end
61+
62+
if @actual then
63+
case expected
64+
when String
65+
@actual.include? expected
66+
when Regexp
67+
expected.match @actual
68+
else
69+
raise ArgumentError, "No idea how to match a #{@actual.class.name}"
70+
end
71+
end
72+
end
73+
74+
failure_message_for_should do |actual|
75+
if actual.nil? then
76+
"expected #{expected.inspect}, but nothing was printed"
77+
else
78+
"expected #{expected.inspect} to be printed; got:\n#{actual}"
79+
end
80+
end
81+
82+
description do
83+
"expect #{expected.inspect} to be printed"
84+
end
85+
86+
diffable
87+
end

spec/lib/puppet_spec/verbose.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Support code for running stuff with warnings disabled.
2+
module Kernel
3+
def with_verbose_disabled
4+
verbose, $VERBOSE = $VERBOSE, nil
5+
result = yield
6+
$VERBOSE = verbose
7+
return result
8+
end
9+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'rspec'
2+
3+
class Object
4+
# This is necessary because the RAL has a 'should'
5+
# method.
6+
alias :must :should
7+
alias :must_not :should_not
8+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Some monkey-patching to allow us to test private methods.
2+
class Class
3+
def publicize_methods(*methods)
4+
saved_private_instance_methods = methods.empty? ? self.private_instance_methods : methods
5+
6+
self.class_eval { public(*saved_private_instance_methods) }
7+
yield
8+
self.class_eval { private(*saved_private_instance_methods) }
9+
end
10+
end
11+

spec/spec_helper.rb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
dir = File.expand_path(File.dirname(__FILE__))
2+
$LOAD_PATH.unshift File.join(dir, 'lib')
3+
4+
# Don't want puppet getting the command line arguments for rake or autotest
5+
ARGV.clear
6+
7+
require 'puppet'
8+
require 'mocha'
9+
gem 'rspec', '>=2.0.0'
10+
require 'rspec/expectations'
11+
12+
# So everyone else doesn't have to include this base constant.
13+
module PuppetSpec
14+
FIXTURE_DIR = File.join(dir = File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR)
15+
end
16+
17+
require 'pathname'
18+
require 'tmpdir'
19+
20+
require 'puppet_spec/verbose'
21+
require 'puppet_spec/files'
22+
require 'puppet_spec/fixtures'
23+
require 'puppet_spec/matchers'
24+
require 'monkey_patches/alias_should_to_must'
25+
require 'monkey_patches/publicize_methods'
26+
27+
Pathname.glob("#{dir}/shared_behaviours/**/*.rb") do |behaviour|
28+
require behaviour.relative_path_from(Pathname.new(dir))
29+
end
30+
31+
RSpec.configure do |config|
32+
include PuppetSpec::Fixtures
33+
34+
config.mock_with :mocha
35+
36+
config.before :each do
37+
GC.disable
38+
39+
# We need to preserve the current state of all our indirection cache and
40+
# terminus classes. This is pretty important, because changes to these
41+
# are global and lead to order dependencies in our testing.
42+
#
43+
# We go direct to the implementation because there is no safe, sane public
44+
# API to manage restoration of these to their default values. This
45+
# should, once the value is proved, be moved to a standard API on the
46+
# indirector.
47+
#
48+
# To make things worse, a number of the tests stub parts of the
49+
# indirector. These stubs have very specific expectations that what
50+
# little of the public API we could use is, well, likely to explode
51+
# randomly in some tests. So, direct access. --daniel 2011-08-30
52+
$saved_indirection_state = {}
53+
indirections = Puppet::Indirector::Indirection.send(:class_variable_get, :@@indirections)
54+
indirections.each do |indirector|
55+
$saved_indirection_state[indirector.name] = {
56+
:@terminus_class => indirector.instance_variable_get(:@terminus_class),
57+
:@cache_class => indirector.instance_variable_get(:@cache_class)
58+
}
59+
end
60+
61+
# these globals are set by Application
62+
$puppet_application_mode = nil
63+
$puppet_application_name = nil
64+
65+
# REVISIT: I think this conceals other bad tests, but I don't have time to
66+
# fully diagnose those right now. When you read this, please come tell me
67+
# I suck for letting this float. --daniel 2011-04-21
68+
Signal.stubs(:trap)
69+
70+
# Set the confdir and vardir to gibberish so that tests
71+
# have to be correctly mocked.
72+
Puppet[:confdir] = "/dev/null"
73+
Puppet[:vardir] = "/dev/null"
74+
75+
# Avoid opening ports to the outside world
76+
Puppet.settings[:bindaddress] = "127.0.0.1"
77+
78+
@logs = []
79+
Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
80+
81+
@log_level = Puppet::Util::Log.level
82+
end
83+
84+
config.after :each do
85+
Puppet.settings.clear
86+
Puppet::Node::Environment.clear
87+
Puppet::Util::Storage.clear
88+
Puppet::Util::ExecutionStub.reset
89+
90+
PuppetSpec::Files.cleanup
91+
92+
@logs.clear
93+
Puppet::Util::Log.close_all
94+
Puppet::Util::Log.level = @log_level
95+
96+
# Restore the indirector configuration. See before hook.
97+
indirections = Puppet::Indirector::Indirection.send(:class_variable_get, :@@indirections)
98+
indirections.each do |indirector|
99+
$saved_indirection_state.fetch(indirector.name, {}).each do |variable, value|
100+
indirector.instance_variable_set(variable, value)
101+
end
102+
end
103+
$saved_indirection_state = nil
104+
105+
GC.enable
106+
end
107+
end

0 commit comments

Comments
 (0)