diff options
| author | Luke Kanies <luke@puppetlabs.com> | 2011-02-22 11:59:19 -0800 |
|---|---|---|
| committer | Luke Kanies <luke@puppetlabs.com> | 2011-02-22 11:59:19 -0800 |
| commit | 04fb6de5e2108799e47a081e5331d932fcf53109 (patch) | |
| tree | 5df833d9972067992d361accca4729c3073c1fc4 /spec | |
| parent | 0cbdbce0f518d43f0d0160a58dd5ec7253a5af87 (diff) | |
| download | puppet-04fb6de5e2108799e47a081e5331d932fcf53109.tar.gz puppet-04fb6de5e2108799e47a081e5331d932fcf53109.tar.xz puppet-04fb6de5e2108799e47a081e5331d932fcf53109.zip | |
Switching Interfaces to be instances
They were previously classes, which made a lot of things stupider
than they needed to be.
This will likely involve some porting, but not much.
Signed-off-by: Luke Kanies <luke@puppetlabs.com>
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/README.markdown | 7 | ||||
| -rw-r--r-- | spec/spec.opts | 6 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 17 | ||||
| -rw-r--r-- | spec/unit/interface/action_manager_spec.rb | 142 | ||||
| -rw-r--r-- | spec/unit/interface/facts_spec.rb | 26 | ||||
| -rw-r--r-- | spec/unit/interface/indirector_spec.rb | 61 | ||||
| -rw-r--r-- | spec/unit/interface_spec.rb | 99 | ||||
| -rw-r--r-- | spec/unit/puppet/provider/README.markdown | 4 | ||||
| -rw-r--r-- | spec/unit/puppet/type/README.markdown | 4 | ||||
| -rw-r--r-- | spec/watchr.rb | 124 |
10 files changed, 490 insertions, 0 deletions
diff --git a/spec/README.markdown b/spec/README.markdown new file mode 100644 index 000000000..286d3417d --- /dev/null +++ b/spec/README.markdown @@ -0,0 +1,7 @@ +Specs +===== + +The Puppet project uses RSpec for testing. + +For more information on RSpec, see http://rspec.info/ + diff --git a/spec/spec.opts b/spec/spec.opts new file mode 100644 index 000000000..91cd6427e --- /dev/null +++ b/spec/spec.opts @@ -0,0 +1,6 @@ +--format +s +--colour +--loadby +mtime +--backtrace diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 000000000..242ef0a40 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,17 @@ +require 'pathname' +dir = Pathname.new(__FILE__).parent +$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib') + +require 'mocha' +require 'puppet' +require 'rspec' + +RSpec.configure do |config| + config.mock_with :mocha +end + +# We need this because the RAL uses 'should' as a method. This +# allows us the same behaviour but with a different method name. +class Object + alias :must :should +end diff --git a/spec/unit/interface/action_manager_spec.rb b/spec/unit/interface/action_manager_spec.rb new file mode 100644 index 000000000..b71aecaa2 --- /dev/null +++ b/spec/unit/interface/action_manager_spec.rb @@ -0,0 +1,142 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +# This is entirely an internal class for Interface, so we have to load it instead of our class. +require 'puppet/interface' + +class ActionManagerTester + include Puppet::Interface::ActionManager +end + +describe Puppet::Interface::ActionManager do + before do + @tester = ActionManagerTester.new + end + + describe "when included in a class" do + it "should be able to define an action" do + @tester.action(:foo) { "something "} + end + + it "should be able to list defined actions" do + @tester.action(:foo) { "something" } + @tester.action(:bar) { "something" } + + @tester.actions.should be_include(:bar) + @tester.actions.should be_include(:foo) + end + + it "should be able to indicate when an action is defined" do + @tester.action(:foo) { "something" } + @tester.should be_action(:foo) + end + end + + describe "when used to extend a class" do + before do + @tester = Class.new + @tester.extend(Puppet::Interface::ActionManager) + end + + it "should be able to define an action" do + @tester.action(:foo) { "something "} + end + + it "should be able to list defined actions" do + @tester.action(:foo) { "something" } + @tester.action(:bar) { "something" } + + @tester.actions.should be_include(:bar) + @tester.actions.should be_include(:foo) + end + + it "should be able to indicate when an action is defined" do + @tester.action(:foo) { "something" } + @tester.should be_action(:foo) + end + end + + describe "when used both at the class and instance level" do + before do + @klass = Class.new do + include Puppet::Interface::ActionManager + extend Puppet::Interface::ActionManager + end + @instance = @klass.new + end + + it "should be able to define an action at the class level" do + @klass.action(:foo) { "something "} + end + + it "should create an instance method when an action is defined at the class level" do + @klass.action(:foo) { "something" } + @instance.foo.should == "something" + end + + it "should be able to define an action at the instance level" do + @instance.action(:foo) { "something "} + end + + it "should create an instance method when an action is defined at the instance level" do + @instance.action(:foo) { "something" } + @instance.foo.should == "something" + end + + it "should be able to list actions defined at the class level" do + @klass.action(:foo) { "something" } + @klass.action(:bar) { "something" } + + @klass.actions.should be_include(:bar) + @klass.actions.should be_include(:foo) + end + + it "should be able to list actions defined at the instance level" do + @instance.action(:foo) { "something" } + @instance.action(:bar) { "something" } + + @instance.actions.should be_include(:bar) + @instance.actions.should be_include(:foo) + end + + it "should be able to list actions defined at both instance and class level" do + @klass.action(:foo) { "something" } + @instance.action(:bar) { "something" } + + @instance.actions.should be_include(:bar) + @instance.actions.should be_include(:foo) + end + + it "should be able to indicate when an action is defined at the class level" do + @klass.action(:foo) { "something" } + @instance.should be_action(:foo) + end + + it "should be able to indicate when an action is defined at the instance level" do + @klass.action(:foo) { "something" } + @instance.should be_action(:foo) + end + + it "should list actions defined in superclasses" do + @subclass = Class.new(@klass) + @instance = @subclass.new + + @klass.action(:parent) { "a" } + @subclass.action(:sub) { "a" } + @instance.action(:instance) { "a" } + + @instance.should be_action(:parent) + @instance.should be_action(:sub) + @instance.should be_action(:instance) + end + + it "should create an instance method when an action is defined in a superclass" do + @subclass = Class.new(@klass) + @instance = @subclass.new + + @klass.action(:foo) { "something" } + @instance.foo.should == "something" + end + end +end diff --git a/spec/unit/interface/facts_spec.rb b/spec/unit/interface/facts_spec.rb new file mode 100644 index 000000000..03d6410f9 --- /dev/null +++ b/spec/unit/interface/facts_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/interface/facts' + +describe Puppet::Interface.interface(:facts) do + before do + @interface = Puppet::Interface.interface(:facts) + end + + it "should define an 'upload' fact" do + @interface.should be_action(:upload) + end + + it "should set its default format to :yaml" do + @interface.default_format.should == :yaml + end + + describe "when uploading" do + it "should set the terminus_class to :facter" + + it "should set the cach_eclass to :rest" + + it "should find the current certname" + end +end diff --git a/spec/unit/interface/indirector_spec.rb b/spec/unit/interface/indirector_spec.rb new file mode 100644 index 000000000..1e5ee3068 --- /dev/null +++ b/spec/unit/interface/indirector_spec.rb @@ -0,0 +1,61 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'puppet/interface/indirector' + +describe Puppet::Interface::Indirector do + before do + @instance = Puppet::Interface::Indirector.new(:test) + + @indirection = stub 'indirection', :name => :stub_indirection + + @instance.stubs(:indirection).returns @indirection + end + + after do + Puppet::Interface.unload_interface(:test) + end + + it "should be a subclass of Interface" do + Puppet::Interface::Indirector.superclass.should equal(Puppet::Interface) + end + + it "should be able to return a list of indirections" do + Puppet::Interface::Indirector.indirections.should be_include("catalog") + end + + it "should be able to return a list of terminuses for a given indirection" do + Puppet::Interface::Indirector.terminus_classes(:catalog).should be_include("compiler") + end + + describe "as an instance" do + after { Puppet::Interface.unload_interface(:catalog) } + + it "should be able to determine its indirection" do + # Loading actions here an get, um, complicated + Puppet::Interface.stubs(:load_actions) + Puppet::Interface::Indirector.new(:catalog).indirection.should equal(Puppet::Resource::Catalog.indirection) + end + end + + [:find, :search, :save, :destroy].each do |method| + it "should define a '#{method}' action" do + Puppet::Interface::Indirector.should be_action(method) + end + + it "should just call the indirection method when the '#{method}' action is invoked" do + @instance.indirection.expects(method).with(:test, "myargs") + @instance.send(method, :test, "myargs") + end + + it "should be able to override its indirection name" do + @instance.set_indirection_name :foo + @instance.indirection_name.should == :foo + end + + it "should be able to set its terminus class" do + @instance.indirection.expects(:terminus_class=).with(:myterm) + @instance.set_terminus(:myterm) + end + end +end diff --git a/spec/unit/interface_spec.rb b/spec/unit/interface_spec.rb new file mode 100644 index 000000000..4fe797b6b --- /dev/null +++ b/spec/unit/interface_spec.rb @@ -0,0 +1,99 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') +require 'puppet/interface' + +describe Puppet::Interface do + after do + Puppet::Interface.unload_interface(:me) + end + + describe "at initialization" do + it "should require a name" do + Puppet::Interface.new(:me).name.should == :me + end + + it "should register itself" do + Puppet::Interface.expects(:register_interface).with { |name, inst| name == :me and inst.is_a?(Puppet::Interface) } + Puppet::Interface.new(:me) + end + + it "should load actions" do + Puppet::Interface.expects(:load_actions).with(:me) + Puppet::Interface.new(:me) + end + + it "should instance-eval any provided block" do + face = Puppet::Interface.new(:me) do + action(:something) { "foo" } + end + + face.should be_action(:something) + end + end + + it "should allow overriding of the default format" do + face = Puppet::Interface.new(:me) + face.set_default_format :foo + face.default_format.should == :foo + end + + it "should default to :pson for its format" do + Puppet::Interface.new(:me).default_format.should == :pson + end + + it "should create a class-level autoloader" do + Puppet::Interface.autoloader.should be_instance_of(Puppet::Util::Autoload) + end + + it "should define a class-level 'showconfig' action" do + Puppet::Interface.should be_action(:showconfig) + end + + it "should set any provided options" do + Puppet::Interface.new(:me, :verb => "foo").verb.should == "foo" + end + + it "should be able to register and return interfaces" do + $stderr.stubs(:puts) + face = Puppet::Interface.new(:me) + Puppet::Interface.unload_interface(:me) # to remove from the initial registration + Puppet::Interface.register_interface(:me, face) + Puppet::Interface.interface(:me).should equal(face) + end + + it "should create an associated constant when registering an interface" do + $stderr.stubs(:puts) + face = Puppet::Interface.new(:me) + Puppet::Interface.unload_interface(:me) # to remove from the initial registration + Puppet::Interface.register_interface(:me, face) + Puppet::Interface::Me.should equal(face) + end + + it "should be able to unload interfaces" do + $stderr.stubs(:puts) + face = Puppet::Interface.new(:me) + Puppet::Interface.unload_interface(:me) + Puppet::Interface.interface(:me).should be_nil + end + + it "should remove the associated constant when an interface is unregistered" do + $stderr.stubs(:puts) + face = Puppet::Interface.new(:me) + Puppet::Interface.unload_interface(:me) + lambda { Puppet::Interface.const_get("Me") }.should raise_error(NameError) + end + + it "should try to require interfaces that are not known" do + Puppet::Interface.expects(:require).with "puppet/interface/foo" + Puppet::Interface.interface(:foo) + end + + it "should not fail when requiring an interface fails" do + $stderr.stubs(:puts) + Puppet::Interface.expects(:require).with("puppet/interface/foo").raises LoadError + lambda { Puppet::Interface.interface(:foo) }.should_not raise_error + end + + it "should be able to load all actions in all search paths" +end diff --git a/spec/unit/puppet/provider/README.markdown b/spec/unit/puppet/provider/README.markdown new file mode 100644 index 000000000..702585021 --- /dev/null +++ b/spec/unit/puppet/provider/README.markdown @@ -0,0 +1,4 @@ +Provider Specs +============== + +Define specs for your providers under this directory. diff --git a/spec/unit/puppet/type/README.markdown b/spec/unit/puppet/type/README.markdown new file mode 100644 index 000000000..1ee19ac84 --- /dev/null +++ b/spec/unit/puppet/type/README.markdown @@ -0,0 +1,4 @@ +Resource Type Specs +=================== + +Define specs for your resource types in this directory. diff --git a/spec/watchr.rb b/spec/watchr.rb new file mode 100644 index 000000000..476176fd3 --- /dev/null +++ b/spec/watchr.rb @@ -0,0 +1,124 @@ +ENV["WATCHR"] = "1" +ENV['AUTOTEST'] = 'true' + +def run_comp(cmd) + puts cmd + results = [] + old_sync = $stdout.sync + $stdout.sync = true + line = [] + begin + open("| #{cmd}", "r") do |f| + until f.eof? do + c = f.getc + putc c + line << c + if c == ?\n + results << if RUBY_VERSION >= "1.9" then + line.join + else + line.pack "c*" + end + line.clear + end + end + end + ensure + $stdout.sync = old_sync + end + results.join +end + +def clear + #system("clear") +end + +def growl(message, status) + # Strip the color codes + message.gsub!(/\[\d+m/, '') + + growlnotify = `which growlnotify`.chomp + return if growlnotify.empty? + title = "Watchr Test Results" + image = status == :pass ? "autotest/images/pass.png" : "autotest/images/fail.png" + options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'" + system %(#{growlnotify} #{options} &) +end + +def file2specs(file) + %w{spec/unit spec/integration}.collect { |d| + file.sub('lib/puppet', d).sub('.rb', '_spec.rb') + }.find_all { |f| + FileTest.exist?(f) + } +end + +def run_spec(command) + clear + result = run_comp(command).split("\n").last + status = result.include?('0 failures') ? :pass : :fail + growl result, status +end + +def run_spec_files(files) + files = Array(files) + return if files.empty? + opts = File.readlines('spec/spec.opts').collect { |l| l.chomp }.join(" ") + begin + run_spec("rspec #{files.join(' ')}") + rescue => detail + puts detail.backtrace + warn "Failed to run #{files.join(', ')}: #{detail}" + end +end + +def run_suite + files = files("unit") + files("integration") + run_spec("rspec #{files.join(' ')}") +end + +def files(dir) + require 'find' + + result = [] + Find.find(File.join("spec", dir)) do |path| + result << path if path =~ /\.rb/ + end + + result +end + +watch('spec/spec_helper.rb') { run_suite } +watch(%r{^spec/(unit|integration)/.*\.rb$}) { |md| run_spec_files(md[0]) } +watch(%r{^lib/puppet/(.*)\.rb$}) { |md| + run_spec_files(file2specs(md[0])) +} +watch(%r{^spec/lib/spec.*}) { |md| run_suite } +watch(%r{^spec/lib/monkey_patches/.*}) { |md| run_suite } + +# Ctrl-\ +Signal.trap 'QUIT' do + puts " --- Running all tests ---\n\n" + run_suite +end + +@interrupted = false + +# Ctrl-C +Signal.trap 'INT' do + if @interrupted + @wants_to_quit = true + abort("\n") + else + puts "Interrupt a second time to quit; wait for rerun of tests" + @interrupted = true + Kernel.sleep 1.5 + # raise Interrupt, nil # let the run loop catch it + begin + run_suite + rescue => detail + puts detail.backtrace + puts "Could not run suite: #{detail}" + end + end +end |
