(#10745) Add initial unit tests
This commit is contained in:
parent
41e60f7724
commit
8550774555
4
spec/spec.opts
Normal file
4
spec/spec.opts
Normal file
@ -0,0 +1,4 @@
|
||||
--format
|
||||
s
|
||||
--colour
|
||||
--backtrace
|
80
spec/spec_helper.rb
Normal file
80
spec/spec_helper.rb
Normal file
@ -0,0 +1,80 @@
|
||||
unless defined?(SPEC_HELPER_IS_LOADED)
|
||||
SPEC_HELPER_IS_LOADED = 1
|
||||
|
||||
dir = File.expand_path(File.dirname(__FILE__))
|
||||
|
||||
$LOAD_PATH.unshift("#{dir}/")
|
||||
$LOAD_PATH.unshift("#{dir}/lib") # a spec-specific test lib dir
|
||||
$LOAD_PATH.unshift("#{dir}/../lib")
|
||||
|
||||
# Don't want puppet getting the command line arguments for rake or autotest
|
||||
ARGV.clear
|
||||
|
||||
require 'puppet'
|
||||
require 'puppet/face'
|
||||
require 'mocha'
|
||||
require 'fog'
|
||||
gem 'rspec', '>=2.0.0'
|
||||
|
||||
Fog.credentials_path = File.join(dir, 'fog-stub-configuration')
|
||||
Fog.mock!
|
||||
|
||||
# So everyone else doesn't have to include this base constant.
|
||||
module PuppetSpec
|
||||
FIXTURE_DIR = File.join(dir = File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR)
|
||||
end
|
||||
|
||||
module PuppetTest
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.mock_with :mocha
|
||||
|
||||
config.after :each do
|
||||
Puppet.settings.clear
|
||||
Puppet::Node::Environment.clear
|
||||
Puppet::Util::Storage.clear
|
||||
|
||||
if defined?($tmpfiles)
|
||||
$tmpfiles.each do |file|
|
||||
file = File.expand_path(file)
|
||||
if Puppet.features.posix? and file !~ /^\/tmp/ and file !~ /^\/var\/folders/
|
||||
puts "Not deleting tmpfile #{file} outside of /tmp or /var/folders"
|
||||
next
|
||||
elsif Puppet.features.microsoft_windows?
|
||||
tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp"))
|
||||
if file !~ /^#{tempdir}/
|
||||
puts "Not deleting tmpfile #{file} outside of #{tempdir}"
|
||||
next
|
||||
end
|
||||
end
|
||||
if FileTest.exist?(file)
|
||||
system("chmod -R 755 '#{file}'")
|
||||
system("rm -rf '#{file}'")
|
||||
end
|
||||
end
|
||||
$tmpfiles.clear
|
||||
end
|
||||
|
||||
Puppet::Util::Log.close_all
|
||||
end
|
||||
|
||||
config.before :each do
|
||||
# these globals are set by Application
|
||||
$puppet_application_mode = nil
|
||||
$puppet_application_name = nil
|
||||
|
||||
# Set the confdir and vardir to gibberish so that tests
|
||||
# have to be correctly mocked.
|
||||
Puppet[:confdir] = "/dev/null"
|
||||
Puppet[:vardir] = "/dev/null"
|
||||
|
||||
# Avoid opening ports to the outside world
|
||||
Puppet.settings[:bindaddress] = "127.0.0.1"
|
||||
|
||||
@logs = []
|
||||
Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs))
|
||||
end
|
||||
end
|
||||
|
||||
end
|
50
spec/unit/face/dashboard_spec.rb
Normal file
50
spec/unit/face/dashboard_spec.rb
Normal file
@ -0,0 +1,50 @@
|
||||
require 'spec_helper'
|
||||
require 'puppet'
|
||||
require 'puppet/face'
|
||||
describe Puppet::Face[:dashboard, :current] do
|
||||
let :dashboard_options do
|
||||
{:enc_server => 'enc_server', :enc_port => '3001'}
|
||||
end
|
||||
let :connection do
|
||||
mock('Puppet::Dashboard::Classifier')
|
||||
end
|
||||
|
||||
describe 'action list' do
|
||||
it 'should default enc_server to Puppet[:server] and port to 3000' do
|
||||
defaults = {:enc_server =>'master', :enc_port => 3000}
|
||||
Puppet.expects(:[]).with(:server).returns('master')
|
||||
Puppet::Dashboard::Classifier.expects(:connection).with(defaults).returns connection
|
||||
connection.expects(:list).with('node_classes', 'Listing classes')
|
||||
subject.list('classes', {})
|
||||
end
|
||||
{'classes' => 'node_classes', 'nodes' => 'nodes', 'groups' => 'node_groups'}.each do |k,v|
|
||||
it "should convert the types into their dashboard names" do
|
||||
Puppet::Dashboard::Classifier.expects(:connection).with(dashboard_options).returns connection
|
||||
connection.expects(:list).with(v, "Listing #{k}")
|
||||
subject.list(k, dashboard_options)
|
||||
end
|
||||
end
|
||||
it 'should fail when an invalid type is specified' do
|
||||
expect { subject.list('foo', {} ) }.should raise_error(Puppet::Error, /Invalid type specified/)
|
||||
end
|
||||
end
|
||||
describe 'actions create_class and create_node' do
|
||||
{'node' => 'node', 'class' => 'node_class'}.each do |type,dash_type|
|
||||
it "should require the name option for #{type}" do
|
||||
expect { subject.send("create_#{type}", {}) }.should raise_error(ArgumentError)
|
||||
end
|
||||
it "should accept name option for #{type}" do
|
||||
munged_options = dashboard_options.merge(:name => 'dan')
|
||||
Puppet::Dashboard::Classifier.expects(:connection).with(munged_options).returns connection
|
||||
connection.expects(:create).with(dash_type, "Creating #{type} dan", { dash_type => { 'name' => 'dan' } })
|
||||
subject.send("create_#{type}", munged_options)
|
||||
end
|
||||
end
|
||||
end
|
||||
describe 'action register_module' do
|
||||
|
||||
end
|
||||
describe 'action add_module' do
|
||||
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user