diff --git a/spec/unit/application/device_spec.rb b/spec/unit/application/device_spec.rb index df8cd3eaf..464827e3c 100755 --- a/spec/unit/application/device_spec.rb +++ b/spec/unit/application/device_spec.rb @@ -1,342 +1,341 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../spec_helper' +#!/usr/bin/env rspec +require 'spec_helper' require 'puppet/application/device' require 'puppet/util/network_device/config' require 'ostruct' require 'puppet/configurer' describe Puppet::Application::Device do before :each do @device = Puppet::Application[:device] @device.preinit Puppet::Util::Log.stubs(:newdestination) Puppet::Node.indirection.stubs(:terminus_class=) Puppet::Node.indirection.stubs(:cache_class=) Puppet::Node::Facts.indirection.stubs(:terminus_class=) end it "should operate in agent run_mode" do @device.class.run_mode.name.should == :agent end it "should ask Puppet::Application to parse Puppet configuration file" do @device.should_parse_config?.should be_true end it "should declare a main command" do @device.should respond_to(:main) end it "should declare a preinit block" do @device.should respond_to(:preinit) end describe "in preinit" do before :each do @device.stubs(:trap) end it "should catch INT" do @device.expects(:trap).with { |arg,block| arg == :INT } @device.preinit end end describe "when handling options" do before do @device.command_line.stubs(:args).returns([]) end [:centrallogging, :debug, :verbose,].each do |option| it "should declare handle_#{option} method" do @device.should respond_to("handle_#{option}".to_sym) end it "should store argument value when calling handle_#{option}" do @device.options.expects(:[]=).with(option, 'arg') @device.send("handle_#{option}".to_sym, 'arg') end end it "should set waitforcert to 0 with --onetime and if --waitforcert wasn't given" do Puppet[:onetime] = true Puppet::SSL::Host.any_instance.expects(:wait_for_cert).with(0) @device.setup_host end it "should use supplied waitforcert when --onetime is specified" do Puppet[:onetime] = true @device.handle_waitforcert(60) Puppet::SSL::Host.any_instance.expects(:wait_for_cert).with(60) @device.setup_host end it "should use a default value for waitforcert when --onetime and --waitforcert are not specified" do Puppet::SSL::Host.any_instance.expects(:wait_for_cert).with(120) @device.setup_host end it "should set the log destination with --logdest" do @device.options.stubs(:[]=).with { |opt,val| opt == :setdest } Puppet::Log.expects(:newdestination).with("console") @device.handle_logdest("console") end it "should put the setdest options to true" do @device.options.expects(:[]=).with(:setdest,true) @device.handle_logdest("console") end it "should parse the log destination from the command line" do @device.command_line.stubs(:args).returns(%w{--logdest /my/file}) Puppet::Util::Log.expects(:newdestination).with("/my/file") @device.parse_options end it "should store the waitforcert options with --waitforcert" do @device.options.expects(:[]=).with(:waitforcert,42) @device.handle_waitforcert("42") end it "should set args[:Port] with --port" do @device.handle_port("42") @device.args[:Port].should == "42" end end describe "during setup" do before :each do @device.options.stubs(:[]) Puppet.stubs(:info) FileTest.stubs(:exists?).returns(true) Puppet[:libdir] = "/dev/null/lib" Puppet::SSL::Host.stubs(:ca_location=) Puppet::Transaction::Report.indirection.stubs(:terminus_class=) Puppet::Resource::Catalog.indirection.stubs(:terminus_class=) Puppet::Resource::Catalog.indirection.stubs(:cache_class=) Puppet::Node::Facts.indirection.stubs(:terminus_class=) @host = stub_everything 'host' Puppet::SSL::Host.stubs(:new).returns(@host) Puppet.stubs(:settraps) end it "should call setup_logs" do @device.expects(:setup_logs) @device.setup end describe "when setting up logs" do before :each do Puppet::Util::Log.stubs(:newdestination) end it "should set log level to debug if --debug was passed" do @device.options.stubs(:[]).with(:debug).returns(true) @device.setup_logs Puppet::Util::Log.level.should == :debug end it "should set log level to info if --verbose was passed" do @device.options.stubs(:[]).with(:verbose).returns(true) @device.setup_logs Puppet::Util::Log.level.should == :info end [:verbose, :debug].each do |level| it "should set console as the log destination with level #{level}" do @device.options.stubs(:[]).with(level).returns(true) Puppet::Util::Log.expects(:newdestination).with(:console) @device.setup_logs end end it "should set syslog as the log destination if no --logdest" do @device.options.stubs(:[]).with(:setdest).returns(false) Puppet::Util::Log.expects(:newdestination).with(:syslog) @device.setup_logs end end it "should set a central log destination with --centrallogs" do @device.options.stubs(:[]).with(:centrallogs).returns(true) Puppet[:server] = "puppet.reductivelabs.com" Puppet::Util::Log.stubs(:newdestination).with(:syslog) Puppet::Util::Log.expects(:newdestination).with("puppet.reductivelabs.com") @device.setup end it "should use :main, :agent, :device and :ssl config" do Puppet.settings.expects(:use).with(:main, :agent, :device, :ssl) @device.setup end it "should install a remote ca location" do Puppet::SSL::Host.expects(:ca_location=).with(:remote) @device.setup end it "should tell the report handler to use REST" do Puppet::Transaction::Report.indirection.expects(:terminus_class=).with(:rest) @device.setup end it "should change the catalog_terminus setting to 'rest'" do Puppet[:catalog_terminus] = :foo @device.setup Puppet[:catalog_terminus].should == :rest end it "should tell the catalog handler to use cache" do Puppet::Resource::Catalog.indirection.expects(:cache_class=).with(:yaml) @device.setup end it "should change the facts_terminus setting to 'network_device'" do Puppet[:facts_terminus] = :foo @device.setup Puppet[:facts_terminus].should == :network_device end end describe "when initializing each devices SSL" do before(:each) do @host = stub_everything 'host' Puppet::SSL::Host.stubs(:new).returns(@host) end it "should create a new ssl host" do Puppet::SSL::Host.expects(:new).returns(@host) @device.setup_host end it "should wait for a certificate" do @device.options.stubs(:[]).with(:waitforcert).returns(123) @host.expects(:wait_for_cert).with(123) @device.setup_host end end describe "when running" do before :each do @device.options.stubs(:[]).with(:fingerprint).returns(false) Puppet.stubs(:notice) @device.options.stubs(:[]).with(:client) Puppet::Util::NetworkDevice::Config.stubs(:devices).returns({}) end it "should dispatch to main" do @device.stubs(:main) @device.run_command end it "should get the device list" do device_hash = stub_everything 'device hash' Puppet::Util::NetworkDevice::Config.expects(:devices).returns(device_hash) @device.main end it "should exit if the device list is empty" do expect { @device.main }.to exit_with 1 end describe "for each device" do before(:each) do Puppet[:vardir] = "/dummy" Puppet[:confdir] = "/dummy" Puppet[:certname] = "certname" @device_hash = { "device1" => OpenStruct.new(:name => "device1", :url => "url", :provider => "cisco"), "device2" => OpenStruct.new(:name => "device2", :url => "url", :provider => "cisco"), } Puppet::Util::NetworkDevice::Config.stubs(:devices).returns(@device_hash) Puppet.settings.stubs(:set_value) Puppet.settings.stubs(:use) @device.stubs(:setup_host) Puppet::Util::NetworkDevice.stubs(:init) @configurer = stub_everything 'configurer' Puppet::Configurer.stubs(:new).returns(@configurer) end it "should set vardir to the device vardir" do Puppet.settings.expects(:set_value).with(:vardir, "/dummy/devices/device1", :cli) @device.main end it "should set confdir to the device confdir" do Puppet.settings.expects(:set_value).with(:confdir, "/dummy/devices/device1", :cli) @device.main end it "should set certname to the device certname" do Puppet.settings.expects(:set_value).with(:certname, "device1", :cli) Puppet.settings.expects(:set_value).with(:certname, "device2", :cli) @device.main end it "should make sure all the required folders and files are created" do Puppet.settings.expects(:use).with(:main, :agent, :ssl).twice @device.main end it "should initialize the device singleton" do Puppet::Util::NetworkDevice.expects(:init).with(@device_hash["device1"]).then.with(@device_hash["device2"]) @device.main end it "should setup the SSL context" do @device.expects(:setup_host).twice @device.main end it "should launch a configurer for this device" do @configurer.expects(:run).twice @device.main end [:vardir, :confdir].each do |setting| it "should cleanup the #{setting} setting after the run" do configurer = states('configurer').starts_as('notrun') Puppet.settings.expects(:set_value).with(setting, "/dummy/devices/device1", :cli).when(configurer.is('notrun')) @configurer.expects(:run).twice.then(configurer.is('run')) Puppet.settings.expects(:set_value).with(setting, "/dummy", :cli).when(configurer.is('run')) @device.main end end it "should cleanup the certname setting after the run" do configurer = states('configurer').starts_as('notrun') Puppet.settings.expects(:set_value).with(:certname, "device1", :cli).when(configurer.is('notrun')) @configurer.expects(:run).twice.then(configurer.is('run')) Puppet.settings.expects(:set_value).with(:certname, "certname", :cli).when(configurer.is('run')) @device.main end end end end diff --git a/spec/unit/indirector/facts/network_device_spec.rb b/spec/unit/indirector/facts/network_device_spec.rb old mode 100644 new mode 100755 index 302a810e8..93cd35d77 --- a/spec/unit/indirector/facts/network_device_spec.rb +++ b/spec/unit/indirector/facts/network_device_spec.rb @@ -1,89 +1,85 @@ -#!/usr/bin/env ruby -# -# Created by Luke Kanies on 2007-9-23. -# Copyright (c) 2007. All rights reserved. - -require File.dirname(__FILE__) + '/../../../spec_helper' +#!/usr/bin/env rspec +require 'spec_helper' require 'puppet/indirector/facts/network_device' describe Puppet::Node::Facts::NetworkDevice do it "should be a subclass of the Code terminus" do Puppet::Node::Facts::NetworkDevice.superclass.should equal(Puppet::Indirector::Code) end it "should have documentation" do Puppet::Node::Facts::NetworkDevice.doc.should_not be_nil end it "should be registered with the configuration store indirection" do indirection = Puppet::Indirector::Indirection.instance(:facts) Puppet::Node::Facts::NetworkDevice.indirection.should equal(indirection) end it "should have its name set to :facter" do Puppet::Node::Facts::NetworkDevice.name.should == :network_device end end describe Puppet::Node::Facts::NetworkDevice do before :each do @remote_device = stub 'remote_device', :facts => {} Puppet::Util::NetworkDevice.stubs(:current).returns(@remote_device) @device = Puppet::Node::Facts::NetworkDevice.new @name = "me" @request = stub 'request', :key => @name end describe Puppet::Node::Facts::NetworkDevice, " when finding facts" do it "should return a Facts instance" do @device.find(@request).should be_instance_of(Puppet::Node::Facts) end it "should return a Facts instance with the provided key as the name" do @device.find(@request).name.should == @name end it "should return the device facts as the values in the Facts instance" do @remote_device.expects(:facts).returns("one" => "two") facts = @device.find(@request) facts.values["one"].should == "two" end it "should add local facts" do facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:add_local_facts) @device.find(@request) end it "should convert all facts into strings" do facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:stringify) @device.find(@request) end it "should call the downcase hook" do facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:downcase_if_necessary) @device.find(@request) end end describe Puppet::Node::Facts::NetworkDevice, " when saving facts" do it "should fail" do proc { @device.save(@facts) }.should raise_error(Puppet::DevError) end end describe Puppet::Node::Facts::NetworkDevice, " when destroying facts" do it "should fail" do proc { @device.destroy(@facts) }.should raise_error(Puppet::DevError) end end end diff --git a/spec/unit/provider/cisco_spec.rb b/spec/unit/provider/cisco_spec.rb old mode 100644 new mode 100755 index 08320731c..0696221c4 --- a/spec/unit/provider/cisco_spec.rb +++ b/spec/unit/provider/cisco_spec.rb @@ -1,16 +1,15 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../spec_helper' +#!/usr/bin/env rspec +require 'spec_helper' require 'puppet/provider/cisco' describe Puppet::Provider::Cisco do it "should implement a device class method" do Puppet::Provider::Cisco.should respond_to(:device) end it "should create a cisco device instance" do Puppet::Util::NetworkDevice::Cisco::Device.expects(:new).returns :device Puppet::Provider::Cisco.device(:url).should == :device end -end \ No newline at end of file +end diff --git a/spec/unit/provider/interface/cisco_spec.rb b/spec/unit/provider/interface/cisco_spec.rb index c18f87cf8..3d400ea4b 100755 --- a/spec/unit/provider/interface/cisco_spec.rb +++ b/spec/unit/provider/interface/cisco_spec.rb @@ -1,58 +1,57 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../spec_helper' +require 'spec_helper' require 'puppet/provider/interface/cisco' provider_class = Puppet::Type.type(:interface).provider(:cisco) describe provider_class do before do @device = stub_everything 'device' @resource = stub("resource", :name => "Fa0/1") @provider = provider_class.new(@device, @resource) end it "should have a parent of Puppet::Provider::Cisco" do provider_class.should < Puppet::Provider::Cisco end it "should have an instances method" do provider_class.should respond_to(:instances) end describe "when looking up instances at prefetch" do before do @device.stubs(:command).yields(@device) end it "should delegate to the device interface fetcher" do @device.expects(:interface) provider_class.lookup(@device, "Fa0/1") end it "should return the given interface data" do @device.expects(:interface).returns({ :description => "thisone", :mode => :access}) provider_class.lookup(@device, "Fa0").should == {:description => "thisone", :mode => :access } end end describe "when an instance is being flushed" do it "should call the device interface update method with current and past properties" do @instance = provider_class.new(@device, :ensure => :present, :name => "Fa0/1", :description => "myinterface") @instance.description = "newdesc" @instance.resource = @resource @resource.stubs(:[]).with(:name).returns("Fa0/1") device = stub_everything 'device' @instance.stubs(:device).returns(device) device.expects(:command).yields(device) interface = stub 'interface' device.expects(:new_interface).with("Fa0/1").returns(interface) interface.expects(:update).with( {:ensure => :present, :name => "Fa0/1", :description => "myinterface"}, {:ensure => :present, :name => "Fa0/1", :description => "newdesc"}) @instance.flush end end end diff --git a/spec/unit/provider/network_device_spec.rb b/spec/unit/provider/network_device_spec.rb index e2a87cf4e..aae6ad68a 100755 --- a/spec/unit/provider/network_device_spec.rb +++ b/spec/unit/provider/network_device_spec.rb @@ -1,153 +1,152 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../spec_helper' +require 'spec_helper' require 'puppet/provider/network_device' require 'ostruct' Puppet::Type.type(:vlan).provide :test, :parent => Puppet::Provider::NetworkDevice do mk_resource_methods def self.lookup(device, name) end def self.device(url) :device end end provider_class = Puppet::Type.type(:vlan).provider(:test) describe provider_class do before do @resource = stub("resource", :name => "test") @provider = provider_class.new(@resource) end it "should be able to prefetch instances from the device" do provider_class.should respond_to(:prefetch) end it "should have an instances method" do provider_class.should respond_to(:instances) end describe "when prefetching" do before do @resource = stub_everything 'resource' @resources = {"200" => @resource} provider_class.stubs(:lookup) end it "should lookup an entry for each passed resource" do provider_class.expects(:lookup).with{ |device,value| value == "200" }.returns nil provider_class.stubs(:new) @resource.stubs(:provider=) provider_class.prefetch(@resources) end describe "resources that do not exist" do it "should create a provider with :ensure => :absent" do provider_class.stubs(:lookup).returns(nil) provider_class.expects(:new).with(:device, :ensure => :absent).returns "myprovider" @resource.expects(:provider=).with("myprovider") provider_class.prefetch(@resources) end end describe "resources that exist" do it "should create a provider with the results of the find and ensure at present" do provider_class.stubs(:lookup).returns({ :name => "200", :description => "myvlan"}) provider_class.expects(:new).with(:device, :name => "200", :description => "myvlan", :ensure => :present).returns "myprovider" @resource.expects(:provider=).with("myprovider") provider_class.prefetch(@resources) end end end describe "when being initialized" do describe "with a hash" do before do @resource_class = mock 'resource_class' provider_class.stubs(:resource_type).returns @resource_class @property_class = stub 'property_class', :array_matching => :all, :superclass => Puppet::Property @resource_class.stubs(:attrclass).with(:one).returns(@property_class) @resource_class.stubs(:valid_parameter?).returns true end it "should store a copy of the hash as its vlan_properties" do instance = provider_class.new(:device, :one => :two) instance.former_properties.should == {:one => :two} end end end describe "when an instance" do before do @instance = provider_class.new(:device) @property_class = stub 'property_class', :array_matching => :all, :superclass => Puppet::Property @resource_class = stub 'resource_class', :attrclass => @property_class, :valid_parameter? => true, :validproperties => [:description] provider_class.stubs(:resource_type).returns @resource_class end it "should have a method for creating the instance" do @instance.should respond_to(:create) end it "should have a method for removing the instance" do @instance.should respond_to(:destroy) end it "should indicate when the instance already exists" do @instance = provider_class.new(:device, :ensure => :present) @instance.exists?.should be_true end it "should indicate when the instance does not exist" do @instance = provider_class.new(:device, :ensure => :absent) @instance.exists?.should be_false end describe "is being flushed" do it "should flush properties" do @instance = provider_class.new(:ensure => :present, :name => "200", :description => "myvlan") @instance.flush @instance.properties.should be_empty end end describe "is being created" do before do @rclass = mock 'resource_class' @rclass.stubs(:validproperties).returns([:description]) @resource = stub_everything 'resource' @resource.stubs(:class).returns @rclass @resource.stubs(:should).returns nil @instance.stubs(:resource).returns @resource end it "should set its :ensure value to :present" do @instance.create @instance.properties[:ensure].should == :present end it "should set all of the other attributes from the resource" do @resource.expects(:should).with(:description).returns "myvlan" @instance.create @instance.properties[:description].should == "myvlan" end end describe "is being destroyed" do it "should set its :ensure value to :absent" do @instance.destroy @instance.properties[:ensure].should == :absent end end end end diff --git a/spec/unit/provider/package/pkgutil_spec.rb b/spec/unit/provider/package/pkgutil_spec.rb index 5549b3f6d..dcae21250 100755 --- a/spec/unit/provider/package/pkgutil_spec.rb +++ b/spec/unit/provider/package/pkgutil_spec.rb @@ -1,182 +1,181 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../spec_helper' +#!/usr/bin/env rspec +require 'spec_helper' provider = Puppet::Type.type(:package).provider(:pkgutil) describe provider do before(:each) do @resource = Puppet::Type.type(:package).new( :name => "TESTpkg", :ensure => :present, :provider => :pkgutil ) @provider = provider.new(@resource) # Stub all file and config tests provider.stubs(:healthcheck) end it "should have an install method" do @provider.should respond_to(:install) end it "should have a latest method" do @provider.should respond_to(:uninstall) end it "should have an update method" do @provider.should respond_to(:update) end it "should have a latest method" do @provider.should respond_to(:latest) end describe "when installing" do it "should use a command without versioned package" do @resource[:ensure] = :latest @provider.expects(:pkguti).with('-y', '-i', 'TESTpkg') @provider.install end end describe "when updating" do it "should use a command without versioned package" do @provider.expects(:pkguti).with('-y', '-u', 'TESTpkg') @provider.update end end describe "when uninstalling" do it "should call the remove operation" do @provider.expects(:pkguti).with('-y', '-r', 'TESTpkg') @provider.uninstall end end describe "when getting latest version" do it "should return TESTpkg's version string" do fake_data = " noisy output here TESTpkg 1.4.5,REV=2007.11.18 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.latest.should == "1.4.5,REV=2007.11.20" end it "should handle TESTpkg's 'SAME' version string" do fake_data = " noisy output here TESTpkg 1.4.5,REV=2007.11.18 SAME" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.latest.should == "1.4.5,REV=2007.11.18" end it "should handle a non-existent package" do fake_data = "noisy output here Not in catalog" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.latest.should == nil end it "should warn on unknown pkgutil noise" do provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns("testingnoise") @provider.latest.should == nil end it "should ignore pkgutil noise/headers to find TESTpkg" do fake_data = "# stuff => Fetching new catalog and descriptions (http://mirror.opencsw.org/opencsw/unstable/i386/5.11) if available ... 2011-02-19 23:05:46 URL:http://mirror.opencsw.org/opencsw/unstable/i386/5.11/catalog [534635/534635] -> \"/var/opt/csw/pkgutil/catalog.mirror.opencsw.org_opencsw_unstable_i386_5.11.tmp\" [1] Checking integrity of /var/opt/csw/pkgutil/catalog.mirror.opencsw.org_opencsw_unstable_i386_5.11 with gpg. gpg: Signature made February 17, 2011 05:27:53 PM GMT using DSA key ID E12E9D2F gpg: Good signature from \"Distribution Manager \" ==> 2770 packages loaded from /var/opt/csw/pkgutil/catalog.mirror.opencsw.org_opencsw_unstable_i386_5.11 package installed catalog TESTpkg 1.4.5,REV=2007.11.18 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.latest.should == "1.4.5,REV=2007.11.20" end it "should find REALpkg via an alias (TESTpkg)" do fake_data = " noisy output here REALpkg 1.4.5,REV=2007.11.18 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.query[:name].should == "TESTpkg" end end describe "when querying current version" do it "should return TESTpkg's version string" do fake_data = "TESTpkg 1.4.5,REV=2007.11.18 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.query[:ensure].should == "1.4.5,REV=2007.11.18" end it "should handle a package that isn't installed" do fake_data = "TESTpkg notinst 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.query[:ensure].should == :absent end it "should handle a non-existent package" do fake_data = "noisy output here Not in catalog" provider.expects(:pkguti).with(['-c', '--single', 'TESTpkg']).returns fake_data @provider.query[:ensure].should == :absent end end describe "when querying current instances" do it "should warn on unknown pkgutil noise" do provider.expects(:pkguti).with(['-a']).returns("testingnoise") provider.expects(:pkguti).with(['-c']).returns("testingnoise") Puppet.expects(:warning).times(2) provider.expects(:new).never provider.instances.should == [] end it "should return TESTpkg's version string" do fake_data = "TESTpkg TESTpkg 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-a']).returns fake_data fake_data = "TESTpkg 1.4.5,REV=2007.11.18 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c']).returns fake_data testpkg = mock 'pkg1' provider.expects(:new).with(:ensure => "1.4.5,REV=2007.11.18", :name => "TESTpkg", :provider => :pkgutil).returns testpkg provider.instances.should == [testpkg] end it "should also return both TESTpkg and mypkg alias instances" do fake_data = "mypkg TESTpkg 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-a']).returns fake_data fake_data = "TESTpkg 1.4.5,REV=2007.11.18 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c']).returns fake_data testpkg = mock 'pkg1' provider.expects(:new).with(:ensure => "1.4.5,REV=2007.11.18", :name => "TESTpkg", :provider => :pkgutil).returns testpkg aliaspkg = mock 'pkg2' provider.expects(:new).with(:ensure => "1.4.5,REV=2007.11.18", :name => "mypkg", :provider => :pkgutil).returns aliaspkg provider.instances.should == [testpkg,aliaspkg] end it "shouldn't mind noise in the -a output" do fake_data = "noisy output here" provider.expects(:pkguti).with(['-a']).returns fake_data fake_data = "TESTpkg 1.4.5,REV=2007.11.18 1.4.5,REV=2007.11.20" provider.expects(:pkguti).with(['-c']).returns fake_data testpkg = mock 'pkg1' provider.expects(:new).with(:ensure => "1.4.5,REV=2007.11.18", :name => "TESTpkg", :provider => :pkgutil).returns testpkg provider.instances.should == [testpkg] end end end diff --git a/spec/unit/provider/vlan/cisco_spec.rb b/spec/unit/provider/vlan/cisco_spec.rb index a67290eb4..4753cea21 100755 --- a/spec/unit/provider/vlan/cisco_spec.rb +++ b/spec/unit/provider/vlan/cisco_spec.rb @@ -1,56 +1,55 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../spec_helper' +require 'spec_helper' require 'puppet/provider/vlan/cisco' provider_class = Puppet::Type.type(:vlan).provider(:cisco) describe provider_class do before do @device = stub_everything 'device' @resource = stub("resource", :name => "200") @provider = provider_class.new(@device, @resource) end it "should have a parent of Puppet::Provider::Cisco" do provider_class.should < Puppet::Provider::Cisco end it "should have an instances method" do provider_class.should respond_to(:instances) end describe "when looking up instances at prefetch" do before do @device.stubs(:command).yields(@device) end it "should delegate to the device vlans" do @device.expects(:parse_vlans) provider_class.lookup(@device, "200") end it "should return only the given vlan" do @device.expects(:parse_vlans).returns({"200" => { :description => "thisone" }, "1" => { :description => "nothisone" }}) provider_class.lookup(@device, "200").should == {:description => "thisone" } end end describe "when an instance is being flushed" do it "should call the device update_vlan method with its vlan id, current attributes, and desired attributes" do @instance = provider_class.new(@device, :ensure => :present, :name => "200", :description => "myvlan") @instance.description = "myvlan2" @instance.resource = @resource @resource.stubs(:[]).with(:name).returns("200") device = stub_everything 'device' @instance.stubs(:device).returns(device) device.expects(:command).yields(device) device.expects(:update_vlan).with(@instance.name, {:ensure => :present, :name => "200", :description => "myvlan"}, {:ensure => :present, :name => "200", :description => "myvlan2"}) @instance.flush end end end diff --git a/spec/unit/type/interface_spec.rb b/spec/unit/type/interface_spec.rb index 12ba225d9..74e3257f6 100755 --- a/spec/unit/type/interface_spec.rb +++ b/spec/unit/type/interface_spec.rb @@ -1,98 +1,97 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../spec_helper' +require 'spec_helper' describe Puppet::Type.type(:interface) do it "should have a 'name' parameter'" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1")[:name].should == "FastEthernet 0/1" end it "should have a 'device_url' parameter'" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :device_url => :device)[:device_url].should == :device end it "should have an ensure property" do Puppet::Type.type(:interface).attrtype(:ensure).should == :property end it "should be applied on device" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1").should be_appliable_to_device end [:description, :speed, :duplex, :native_vlan, :encapsulation, :mode, :allowed_trunk_vlans, :etherchannel, :ipaddress].each do |p| it "should have a #{p} property" do Puppet::Type.type(:interface).attrtype(p).should == :property end end describe "when validating attribute values" do before do @provider = stub 'provider', :class => Puppet::Type.type(:interface).defaultprovider, :clear => nil Puppet::Type.type(:interface).defaultprovider.stubs(:new).returns(@provider) end it "should support :present as a value to :ensure" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ensure => :present) end it "should support :shutdown as a value to :ensure" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ensure => :shutdown) end it "should support :no_shutdown as a value to :ensure" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ensure => :no_shutdown) end describe "especially speed" do it "should allow a number" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :speed => "100") end it "should allow :auto" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :speed => :auto) end end describe "especially duplex" do it "should allow :half" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :duplex => :half) end it "should allow :full" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :duplex => :full) end it "should allow :auto" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :duplex => :auto) end end describe "especially ipaddress" do it "should allow ipv4 addresses" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ipaddress => "192.168.0.1/24") end it "should allow arrays of ipv4 addresses" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ipaddress => ["192.168.0.1/24", "192.168.1.0/24"]) end it "should allow ipv6 addresses" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ipaddress => "f0e9::/64") end it "should allow ipv6 options" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ipaddress => "f0e9::/64 link-local") Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ipaddress => "f0e9::/64 eui-64") end it "should allow a mix of ipv4 and ipv6" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ipaddress => ["192.168.0.1/24", "f0e9::/64 link-local"]) end it "should munge ip addresses to a computer format" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1", :ipaddress => "192.168.0.1/24")[:ipaddress].should == [[24, IPAddr.new('192.168.0.1'), nil]] end end end end diff --git a/spec/unit/type/vlan_spec.rb b/spec/unit/type/vlan_spec.rb index 3bee14bbd..7d7a0b178 100755 --- a/spec/unit/type/vlan_spec.rb +++ b/spec/unit/type/vlan_spec.rb @@ -1,45 +1,44 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../spec_helper' +require 'spec_helper' describe Puppet::Type.type(:vlan) do it "should have a 'name' parameter'" do Puppet::Type.type(:vlan).new(:name => "200")[:name].should == "200" end it "should have a 'device_url' parameter'" do Puppet::Type.type(:vlan).new(:name => "200", :device_url => :device)[:device_url].should == :device end it "should be applied on device" do Puppet::Type.type(:vlan).new(:name => "200").should be_appliable_to_device end it "should have an ensure property" do Puppet::Type.type(:vlan).attrtype(:ensure).should == :property end it "should have a description property" do Puppet::Type.type(:vlan).attrtype(:description).should == :property end describe "when validating attribute values" do before do @provider = stub 'provider', :class => Puppet::Type.type(:vlan).defaultprovider, :clear => nil Puppet::Type.type(:vlan).defaultprovider.stubs(:new).returns(@provider) end it "should support :present as a value to :ensure" do Puppet::Type.type(:vlan).new(:name => "200", :ensure => :present) end it "should support :absent as a value to :ensure" do Puppet::Type.type(:vlan).new(:name => "200", :ensure => :absent) end it "should fail if vlan name is not a number" do lambda { Puppet::Type.type(:vlan).new(:name => "notanumber", :ensure => :present) }.should raise_error end end end diff --git a/spec/unit/util/network_device/cisco/device_spec.rb b/spec/unit/util/network_device/cisco/device_spec.rb index 8971205d3..1c5a1a6c5 100755 --- a/spec/unit/util/network_device/cisco/device_spec.rb +++ b/spec/unit/util/network_device/cisco/device_spec.rb @@ -1,408 +1,407 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../../spec_helper' +require 'spec_helper' require 'puppet/util/network_device/cisco/device' describe Puppet::Util::NetworkDevice::Cisco::Device do before(:each) do @transport = stub_everything 'transport', :is_a? => true, :command => "" @cisco = Puppet::Util::NetworkDevice::Cisco::Device.new("telnet://user:password@localhost:23/") @cisco.transport = @transport end describe "when creating the device" do it "should find the enable password from the url" do cisco = Puppet::Util::NetworkDevice::Cisco::Device.new("telnet://user:password@localhost:23/?enable=enable_password") cisco.enable_password.should == "enable_password" end it "should find the enable password from the options" do cisco = Puppet::Util::NetworkDevice::Cisco::Device.new("telnet://user:password@localhost:23/?enable=enable_password", :enable_password => "mypass") cisco.enable_password.should == "mypass" end end describe "when connecting to the physical device" do it "should connect to the transport" do @transport.expects(:connect) @cisco.command end it "should attempt to login" do @cisco.expects(:login) @cisco.command end it "should tell the device to not page" do @transport.expects(:command).with("terminal length 0") @cisco.command end it "should enter the enable password if returned prompt is not privileged" do @transport.stubs(:command).yields("Switch>").returns("") @cisco.expects(:enable) @cisco.command end it "should find device capabilities" do @cisco.expects(:find_capabilities) @cisco.command end it "should execute given command" do @transport.expects(:command).with("mycommand") @cisco.command("mycommand") end it "should yield to the command block if one is provided" do @transport.expects(:command).with("mycommand") @cisco.command do |c| c.command("mycommand") end end it "should close the device transport" do @transport.expects(:close) @cisco.command end describe "when login in" do it "should not login if transport handles login" do @transport.expects(:handles_login?).returns(true) @transport.expects(:command).never @transport.expects(:expect).never @cisco.login end it "should send username if one has been provided" do @transport.expects(:command).with("user", :prompt => /^Password:/) @cisco.login end it "should send password after the username" do @transport.expects(:command).with("user", :prompt => /^Password:/) @transport.expects(:command).with("password") @cisco.login end it "should expect the Password: prompt if no user was sent" do @cisco.url.user = '' @transport.expects(:expect).with(/^Password:/) @transport.expects(:command).with("password") @cisco.login end end describe "when entering enable password" do it "should raise an error if no enable password has been set" do @cisco.enable_password = nil lambda{ @cisco.enable }.should raise_error end it "should send the enable command and expect an enable prompt" do @cisco.enable_password = 'mypass' @transport.expects(:command).with("enable", :prompt => /^Password:/) @cisco.enable end it "should send the enable password" do @cisco.enable_password = 'mypass' @transport.stubs(:command).with("enable", :prompt => /^Password:/) @transport.expects(:command).with("mypass") @cisco.enable end end end describe "when finding network device capabilities" do it "should try to execute sh vlan brief" do @transport.expects(:command).with("sh vlan brief").returns("") @cisco.find_capabilities end it "should detect errors" do @transport.stubs(:command).with("sh vlan brief").returns(< "FastEthernet0/1", "Fa0/1" => "FastEthernet0/1", "FastEth 0/1" => "FastEthernet0/1", "Gi1" => "GigEthernet1", "Di9" => "Dialer9", "Ethernet 0/0/1" => "Ethernet0/0/1", "E0" => "Ethernet0", "ATM 0/1.1" => "ATM0/1.1", "VLAN99" => "VLAN99" }.each do |input,expected| it "should canonicalize #{input} to #{expected}", :'fails_on_ruby_1.9.2' => true do @cisco.canonalize_ifname(input).should == expected end end describe "when updating device vlans" do describe "when removing a vlan" do it "should issue the no vlan command" do @transport.expects(:command).with("no vlan 200") @cisco.update_vlan("200", {:ensure => :present, :name => "200"}, { :ensure=> :absent}) end end describe "when updating a vlan" do it "should issue the vlan command to enter global vlan modifications" do @transport.expects(:command).with("vlan 200") @cisco.update_vlan("200", {:ensure => :present, :name => "200"}, { :ensure=> :present, :name => "200"}) end it "should issue the name command to modify the vlan description" do @transport.expects(:command).with("name myvlan") @cisco.update_vlan("200", {:ensure => :present, :name => "200"}, { :ensure=> :present, :name => "200", :description => "myvlan"}) end end end describe "when parsing interface" do it "should parse interface output" do @cisco.expects(:parse_interface).returns({ :ensure => :present }) @cisco.interface("FastEthernet0/1").should == { :ensure => :present } end it "should parse trunking and merge results" do @cisco.stubs(:parse_interface).returns({ :ensure => :present }) @cisco.expects(:parse_trunking).returns({ :native_vlan => "100" }) @cisco.interface("FastEthernet0/1").should == { :ensure => :present, :native_vlan => "100" } end it "should return an absent interface if parse_interface returns nothing" do @cisco.stubs(:parse_interface).returns({}) @cisco.interface("FastEthernet0/1").should == { :ensure => :absent } end it "should parse ip address information and merge results" do @cisco.stubs(:parse_interface).returns({ :ensure => :present }) @cisco.expects(:parse_interface_config).returns({ :ipaddress => [24,IPAddr.new('192.168.0.24'), nil] }) @cisco.interface("FastEthernet0/1").should == { :ensure => :present, :ipaddress => [24,IPAddr.new('192.168.0.24'), nil] } end it "should parse the sh interface command" do @transport.stubs(:command).with("sh interface FastEthernet0/1").returns(< :absent, :duplex => :auto, :speed => :auto } end it "should be able to parse the sh vlan brief command output", :'fails_on_ruby_1.9.2' => true do @cisco.stubs(:support_vlan_brief?).returns(true) @transport.stubs(:command).with("sh vlan brief").returns(<{:status=>"active", :interfaces=>["FastEthernet0/1", "FastEthernet0/2"], :description=>"management", :name=>"100"}, "1"=>{:status=>"active", :interfaces=>["FastEthernet0/3", "FastEthernet0/4", "FastEthernet0/5", "FastEthernet0/6", "FastEthernet0/7", "FastEthernet0/8", "FastEthernet0/9", "FastEthernet0/10", "FastEthernet0/11", "FastEthernet0/12", "FastEthernet0/13", "FastEthernet0/14", "FastEthernet0/15", "FastEthernet0/16", "FastEthernet0/17", "FastEthernet0/18", "FastEthernet0/23", "FastEthernet0/24"], :description=>"default", :name=>"1"}, "10"=>{:status=>"active", :interfaces=>[], :description=>"VLAN0010", :name=>"10"}} end it "should parse trunk switchport information" do @transport.stubs(:command).with("sh interface FastEthernet0/21 switchport").returns(< :trunk, :encapsulation => :dot1q, :allowed_trunk_vlans=>:all, } end it "should parse trunk switchport information with allowed vlans" do @transport.stubs(:command).with("sh interface GigabitEthernet 0/1 switchport").returns(< :trunk, :encapsulation => :dot1q, :allowed_trunk_vlans=>"1,99", } end it "should parse access switchport information" do @transport.stubs(:command).with("sh interface FastEthernet0/1 switchport").returns(< :access, :native_vlan => "100" } end it "should parse ip addresses" do @transport.stubs(:command).with("sh running-config interface Vlan 1 | begin interface").returns(<[[24, IPAddr.new('192.168.0.24'), 'secondary'], [24, IPAddr.new('192.168.0.1'), nil], [64, IPAddr.new('2001:07a8:71c1::'), "eui-64"]]} end it "should parse etherchannel membership" do @transport.stubs(:command).with("sh running-config interface Gi0/17 | begin interface").returns(<"1"} end end describe "when finding device facts" do it "should delegate to the cisco facts entity" do facts = stub 'facts' Puppet::Util::NetworkDevice::Cisco::Facts.expects(:new).returns(facts) facts.expects(:retrieve).returns(:facts) @cisco.facts.should == :facts end end end diff --git a/spec/unit/util/network_device/cisco/facts_spec.rb b/spec/unit/util/network_device/cisco/facts_spec.rb old mode 100644 new mode 100755 index bb29ac292..66842fdf0 --- a/spec/unit/util/network_device/cisco/facts_spec.rb +++ b/spec/unit/util/network_device/cisco/facts_spec.rb @@ -1,63 +1,62 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../../spec_helper' +#!/usr/bin/env rspec +require 'spec_helper' require 'puppet/util/network_device' require 'puppet/util/network_device/cisco/facts' describe Puppet::Util::NetworkDevice::Cisco::Facts do before(:each) do @transport = stub_everything 'transport' @facts = Puppet::Util::NetworkDevice::Cisco::Facts.new(@transport) end { "cisco WS-C2924C-XL (PowerPC403GA) processor (revision 0x11) with 8192K/1024K bytes of memory." => {:hardwaremodel => "WS-C2924C-XL", :memorysize => "8192K", :processor => "PowerPC403GA", :hardwarerevision => "0x11" }, "Cisco 1841 (revision 5.0) with 355328K/37888K bytes of memory." => {:hardwaremodel=>"1841", :memorysize => "355328K", :hardwarerevision => "5.0" }, "Cisco 877 (MPC8272) processor (revision 0x200) with 118784K/12288K bytes of memory." => {:hardwaremodel=>"877", :memorysize => "118784K", :processor => "MPC8272", :hardwarerevision => "0x200" }, "cisco WS-C2960G-48TC-L (PowerPC405) processor (revision C0) with 61440K/4088K bytes of memory." => {:hardwaremodel=>"WS-C2960G-48TC-L", :memorysize => "61440K", :processor => "PowerPC405", :hardwarerevision => "C0" }, "cisco WS-C2950T-24 (RC32300) processor (revision R0) with 19959K bytes of memory." => {:hardwaremodel=>"WS-C2950T-24", :memorysize => "19959K", :processor => "RC32300", :hardwarerevision => "R0" } }.each do |ver, expected| it "should parse show ver output for hardware device facts" do @transport.stubs(:command).with("sh ver").returns(<sh ver #{ver} Switch> eos @facts.parse_show_ver.should == expected end end { "Switch uptime is 1 year, 12 weeks, 6 days, 22 hours, 32 minutes" => { :hostname => "Switch", :uptime => "1 year, 12 weeks, 6 days, 22 hours, 32 minutes", :uptime_seconds => 39393120, :uptime_days => 455 }, "c2950 uptime is 3 weeks, 1 day, 23 hours, 36 minutes" => { :hostname => "c2950", :uptime => "3 weeks, 1 day, 23 hours, 36 minutes", :uptime_days => 22, :uptime_seconds => 1985760}, "router uptime is 5 weeks, 1 day, 3 hours, 30 minutes" => { :hostname => "router", :uptime => "5 weeks, 1 day, 3 hours, 30 minutes", :uptime_days => 36, :uptime_seconds => 3123000 }, "c2950 uptime is 1 minute" => { :hostname => "c2950", :uptime => "1 minute", :uptime_days => 0, :uptime_seconds => 60 } }.each do |ver, expected| it "should parse show ver output for device uptime facts" do @transport.stubs(:command).with("sh ver").returns(<sh ver #{ver} Switch> eos @facts.parse_show_ver.should == expected end end { "IOS (tm) C2900XL Software (C2900XL-C3H2S-M), Version 12.0(5)WC10, RELEASE SOFTWARE (fc1)"=> { :operatingsystem => "IOS", :operatingsystemrelease => "12.0(5)WC10", :operatingsystemmajrelease => "12.0WC", :operatingsystemfeature => "C3H2S"}, "IOS (tm) C2950 Software (C2950-I6K2L2Q4-M), Version 12.1(22)EA8a, RELEASE SOFTWARE (fc1)"=> { :operatingsystem => "IOS", :operatingsystemrelease => "12.1(22)EA8a", :operatingsystemmajrelease => "12.1EA", :operatingsystemfeature => "I6K2L2Q4"}, "Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(44)SE, RELEASE SOFTWARE (fc1)"=>{ :operatingsystem => "IOS", :operatingsystemrelease => "12.2(44)SE", :operatingsystemmajrelease => "12.2SE", :operatingsystemfeature => "LANBASEK9"}, "Cisco IOS Software, C870 Software (C870-ADVIPSERVICESK9-M), Version 12.4(11)XJ4, RELEASE SOFTWARE (fc2)"=>{ :operatingsystem => "IOS", :operatingsystemrelease => "12.4(11)XJ4", :operatingsystemmajrelease => "12.4XJ", :operatingsystemfeature => "ADVIPSERVICESK9"}, "Cisco IOS Software, 1841 Software (C1841-ADVSECURITYK9-M), Version 12.4(24)T4, RELEASE SOFTWARE (fc2)" =>{ :operatingsystem => "IOS", :operatingsystemrelease => "12.4(24)T4", :operatingsystemmajrelease => "12.4T", :operatingsystemfeature => "ADVSECURITYK9"}, }.each do |ver, expected| it "should parse show ver output for device software version facts" do @transport.stubs(:command).with("sh ver").returns(<sh ver #{ver} Switch> eos @facts.parse_show_ver.should == expected end end end diff --git a/spec/unit/util/network_device/cisco/interface_spec.rb b/spec/unit/util/network_device/cisco/interface_spec.rb index 24217750c..b0561c6fd 100755 --- a/spec/unit/util/network_device/cisco/interface_spec.rb +++ b/spec/unit/util/network_device/cisco/interface_spec.rb @@ -1,89 +1,88 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../../spec_helper' +require 'spec_helper' require 'puppet/util/network_device' require 'puppet/util/network_device/cisco/interface' describe Puppet::Util::NetworkDevice::Cisco::Interface do before(:each) do @transport = stub_everything 'transport' @interface = Puppet::Util::NetworkDevice::Cisco::Interface.new("FastEthernet0/1",@transport) end it "should include IPCalc" do @interface.class.include?(Puppet::Util::NetworkDevice::IPCalc) end describe "when updating the physical device" do it "should enter global configuration mode" do @transport.expects(:command).with("conf t") @interface.update end it "should enter interface configuration mode" do @transport.expects(:command).with("interface FastEthernet0/1") @interface.update end it "should 'execute' all differing properties" do @interface.expects(:execute).with(:description, "b") @interface.expects(:execute).with(:mode, :access).never @interface.update({ :description => "a", :mode => :access }, { :description => "b", :mode => :access }) end it "should execute in cisco ios defined order" do speed = states('speed').starts_as('notset') @interface.expects(:execute).with(:speed, :auto).then(speed.is('set')) @interface.expects(:execute).with(:duplex, :auto).when(speed.is('set')) @interface.update({ :duplex => :half, :speed => "10" }, { :duplex => :auto, :speed => :auto }) end it "should execute absent properties with a no prefix" do @interface.expects(:execute).with(:description, "a", "no ") @interface.update({ :description => "a"}, { }) end it "should exit twice" do @transport.expects(:command).with("exit").twice @interface.update end end describe "when executing commands" do it "should execute string commands directly" do @transport.expects(:command).with("speed auto") @interface.execute(:speed, :auto) end it "should execute string commands with the given prefix" do @transport.expects(:command).with("no speed auto") @interface.execute(:speed, :auto, "no ") end it "should stop at executing the first command that works for array" do @transport.expects(:command).with("channel-group 1").yields("% Invalid command") @transport.expects(:command).with("port group 1") @interface.execute(:etherchannel, "1") end it "should execute the block for block commands" do @transport.expects(:command).with("ip address 192.168.0.1 255.255.255.0") @interface.execute(:ipaddress, [[24, IPAddr.new('192.168.0.1'), nil]]) end it "should execute the block for block commands" do @transport.expects(:command).with("ipv6 address fe08::/76 link-local") @interface.execute(:ipaddress, [[76, IPAddr.new('fe08::'), 'link-local']]) end end describe "when sending commands to the device" do it "should detect errors" do Puppet.expects(:err) @transport.stubs(:command).yields("% Invalid Command") @interface.command("sh ver") end end end diff --git a/spec/unit/util/network_device/config_spec.rb b/spec/unit/util/network_device/config_spec.rb old mode 100644 new mode 100755 index 52796f30b..d69358a92 --- a/spec/unit/util/network_device/config_spec.rb +++ b/spec/unit/util/network_device/config_spec.rb @@ -1,102 +1,101 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../spec_helper' +#!/usr/bin/env rspec +require 'spec_helper' require 'puppet/util/network_device/config' describe Puppet::Util::NetworkDevice::Config do before(:each) do Puppet[:deviceconfig] = "/dummy" FileTest.stubs(:exists?).with("/dummy").returns(true) end describe "when initializing" do before :each do Puppet::Util::NetworkDevice::Config.any_instance.stubs(:read) end it "should use the deviceconfig setting as pathname" do Puppet.expects(:[]).with(:deviceconfig).returns("/dummy") Puppet::Util::NetworkDevice::Config.new end it "should raise an error if no file is defined finally" do Puppet.expects(:[]).with(:deviceconfig).returns(nil) lambda { Puppet::Util::NetworkDevice::Config.new }.should raise_error(Puppet::DevError) end it "should read and parse the file" do Puppet::Util::NetworkDevice::Config.any_instance.expects(:read) Puppet::Util::NetworkDevice::Config.new end end describe "when parsing device" do before :each do @config = Puppet::Util::NetworkDevice::Config.new @config.stubs(:changed?).returns(true) @fd = stub 'fd' File.stubs(:open).yields(@fd) end it "should skip comments" do @fd.stubs(:each).yields(' # comment') OpenStruct.expects(:new).never @config.read end it "should increment line number even on commented lines" do @fd.stubs(:each).multiple_yields(' # comment','[router.puppetlabs.com]') @config.read @config.devices.should be_include('router.puppetlabs.com') end it "should skip blank lines" do @fd.stubs(:each).yields(' ') @config.read @config.devices.should be_empty end it "should produce the correct line number" do @fd.stubs(:each).multiple_yields(' ', '[router.puppetlabs.com]') @config.read @config.devices['router.puppetlabs.com'].line.should == 2 end it "should throw an error if the current device already exists" do @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', '[router.puppetlabs.com]') lambda { @config.read }.should raise_error end it "should create a new device for each found device line" do @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', '[swith.puppetlabs.com]') @config.read @config.devices.size.should == 2 end it "should parse the device type" do @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', 'type cisco') @config.read @config.devices['router.puppetlabs.com'].provider.should == 'cisco' end it "should parse the device url" do @fd.stubs(:each).multiple_yields('[router.puppetlabs.com]', 'type cisco', 'url ssh://test/') @config.read @config.devices['router.puppetlabs.com'].url.should == 'ssh://test/' end end -end \ No newline at end of file +end diff --git a/spec/unit/util/network_device/ipcalc_spec.rb b/spec/unit/util/network_device/ipcalc_spec.rb index 0418c6a84..82c5390a4 100755 --- a/spec/unit/util/network_device/ipcalc_spec.rb +++ b/spec/unit/util/network_device/ipcalc_spec.rb @@ -1,63 +1,62 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../spec_helper' +require 'spec_helper' require 'puppet/util/network_device/ipcalc' describe Puppet::Util::NetworkDevice::IPCalc do class TestIPCalc include Puppet::Util::NetworkDevice::IPCalc end before(:each) do @ipcalc = TestIPCalc.new end describe "when parsing ip/prefix" do it "should parse ipv4 without prefixes" do @ipcalc.parse('127.0.0.1').should == [32,IPAddr.new('127.0.0.1')] end it "should parse ipv4 with prefixes" do @ipcalc.parse('127.0.1.2/8').should == [8,IPAddr.new('127.0.1.2')] end it "should parse ipv6 without prefixes" do @ipcalc.parse('FE80::21A:2FFF:FE30:ECF0').should == [128,IPAddr.new('FE80::21A:2FFF:FE30:ECF0')] end it "should parse ipv6 with prefixes" do @ipcalc.parse('FE80::21A:2FFF:FE30:ECF0/56').should == [56,IPAddr.new('FE80::21A:2FFF:FE30:ECF0')] end end describe "when building netmask" do it "should produce the correct ipv4 netmask from prefix length" do @ipcalc.netmask(Socket::AF_INET, 27).should == IPAddr.new('255.255.255.224') end it "should produce the correct ipv6 netmask from prefix length" do @ipcalc.netmask(Socket::AF_INET6, 56).should == IPAddr.new('ffff:ffff:ffff:ff00::0') end end describe "when building wildmask" do it "should produce the correct ipv4 wildmask from prefix length" do @ipcalc.wildmask(Socket::AF_INET, 27).should == IPAddr.new('0.0.0.31') end it "should produce the correct ipv6 wildmask from prefix length" do @ipcalc.wildmask(Socket::AF_INET6, 126).should == IPAddr.new('::3') end end describe "when computing prefix length from netmask" do it "should produce the correct ipv4 prefix length" do @ipcalc.prefix_length(IPAddr.new('255.255.255.224')).should == 27 end it "should produce the correct ipv6 prefix length" do @ipcalc.prefix_length(IPAddr.new('fffe::0')).should == 15 end end end diff --git a/spec/unit/util/network_device/transport/base_spec.rb b/spec/unit/util/network_device/transport/base_spec.rb index c186d72e5..f05a62fbb 100755 --- a/spec/unit/util/network_device/transport/base_spec.rb +++ b/spec/unit/util/network_device/transport/base_spec.rb @@ -1,42 +1,41 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../../spec_helper' +require 'spec_helper' require 'puppet/util/network_device/transport/base' describe Puppet::Util::NetworkDevice::Transport::Base do class TestTransport < Puppet::Util::NetworkDevice::Transport::Base end before(:each) do @transport = TestTransport.new end describe "when sending commands" do it "should send the command to the telnet session" do @transport.expects(:send).with("line") @transport.command("line") end it "should expect an output matching the given prompt" do @transport.expects(:expect).with(/prompt/) @transport.command("line", :prompt => /prompt/) end it "should expect an output matching the default prompt" do @transport.default_prompt = /defprompt/ @transport.expects(:expect).with(/defprompt/) @transport.command("line") end it "should yield telnet output to the given block" do @transport.expects(:expect).yields("output") @transport.command("line") { |out| out.should == "output" } end it "should return telnet output to the caller" do @transport.expects(:expect).returns("output") @transport.command("line").should == "output" end end end diff --git a/spec/unit/util/network_device/transport/ssh_spec.rb b/spec/unit/util/network_device/transport/ssh_spec.rb index 8fc357db3..04a86ba3f 100755 --- a/spec/unit/util/network_device/transport/ssh_spec.rb +++ b/spec/unit/util/network_device/transport/ssh_spec.rb @@ -1,219 +1,218 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../../spec_helper' +require 'spec_helper' require 'puppet/util/network_device/transport/ssh' describe Puppet::Util::NetworkDevice::Transport::Ssh, :if => Puppet.features.ssh?, :'fails_on_ruby_1.9.2' => true do before(:each) do @transport = Puppet::Util::NetworkDevice::Transport::Ssh.new() end it "should handle login through the transport" do @transport.should be_handles_login end it "should connect to the given host and port" do Net::SSH.expects(:start).with { |host, user, args| host == "localhost" && args[:port] == 22 }.returns stub_everything @transport.host = "localhost" @transport.port = 22 @transport.connect end it "should connect using the given username and password" do Net::SSH.expects(:start).with { |host, user, args| user == "user" && args[:password] == "pass" }.returns stub_everything @transport.user = "user" @transport.password = "pass" @transport.connect end it "should raise a Puppet::Error when encountering an authentication failure" do Net::SSH.expects(:start).raises Net::SSH::AuthenticationFailed @transport.host = "localhost" @transport.user = "user" lambda { @transport.connect }.should raise_error Puppet::Error end describe "when connected" do before(:each) do @ssh = stub_everything 'ssh' @channel = stub_everything 'channel' Net::SSH.stubs(:start).returns @ssh @ssh.stubs(:open_channel).yields(@channel) @transport.stubs(:expect) end it "should open a channel" do @ssh.expects(:open_channel) @transport.connect end it "should request a pty" do @channel.expects(:request_pty) @transport.connect end it "should create a shell channel" do @channel.expects(:send_channel_request).with("shell") @transport.connect end it "should raise an error if shell channel creation fails" do @channel.expects(:send_channel_request).with("shell").yields(@channel, false) lambda { @transport.connect }.should raise_error end it "should register an on_data and on_extended_data callback" do @channel.expects(:send_channel_request).with("shell").yields(@channel, true) @channel.expects(:on_data) @channel.expects(:on_extended_data) @transport.connect end it "should accumulate data to the buffer on data" do @channel.expects(:send_channel_request).with("shell").yields(@channel, true) @channel.expects(:on_data).yields(@channel, "data") @transport.connect @transport.buf.should == "data" end it "should accumulate data to the buffer on extended data" do @channel.expects(:send_channel_request).with("shell").yields(@channel, true) @channel.expects(:on_extended_data).yields(@channel, 1, "data") @transport.connect @transport.buf.should == "data" end it "should mark eof on close" do @channel.expects(:send_channel_request).with("shell").yields(@channel, true) @channel.expects(:on_close).yields(@channel) @transport.connect @transport.should be_eof end it "should expect output to conform to the default prompt" do @channel.expects(:send_channel_request).with("shell").yields(@channel, true) @transport.expects(:default_prompt).returns("prompt") @transport.expects(:expect).with("prompt") @transport.connect end it "should start the ssh loop" do @ssh.expects(:loop) @transport.connect end end describe "when closing" do before(:each) do @ssh = stub_everything 'ssh' @channel = stub_everything 'channel' Net::SSH.stubs(:start).returns @ssh @ssh.stubs(:open_channel).yields(@channel) @channel.stubs(:send_channel_request).with("shell").yields(@channel, true) @transport.stubs(:expect) @transport.connect end it "should close the channel" do @channel.expects(:close) @transport.close end it "should close the ssh session" do @ssh.expects(:close) @transport.close end end describe "when sending commands" do before(:each) do @ssh = stub_everything 'ssh' @channel = stub_everything 'channel' Net::SSH.stubs(:start).returns @ssh @ssh.stubs(:open_channel).yields(@channel) @channel.stubs(:send_channel_request).with("shell").yields(@channel, true) @transport.stubs(:expect) @transport.connect end it "should send data to the ssh channel" do @channel.expects(:send_data).with("data\n") @transport.command("data") end it "should expect the default prompt afterward" do @transport.expects(:default_prompt).returns("prompt") @transport.expects(:expect).with("prompt") @transport.command("data") end it "should expect the given prompt" do @transport.expects(:expect).with("myprompt") @transport.command("data", :prompt => "myprompt") end it "should yield the buffer output to given block" do @transport.expects(:expect).yields("output") @transport.command("data") do |out| out.should == "output" end end it "should return buffer output" do @transport.expects(:expect).returns("output") @transport.command("data").should == "output" end end describe "when expecting output" do before(:each) do @connection = stub_everything 'connection' @socket = stub_everything 'socket' transport = stub 'transport', :socket => @socket @ssh = stub_everything 'ssh', :transport => transport @channel = stub_everything 'channel', :connection => @connection @transport.ssh = @ssh @transport.channel = @channel end it "should process the ssh event loop" do IO.stubs(:select) @transport.buf = "output" @transport.expects(:process_ssh) @transport.expect(/output/) end it "should return the output" do IO.stubs(:select) @transport.buf = "output" @transport.stubs(:process_ssh) @transport.expect(/output/).should == "output" end it "should return the output" do IO.stubs(:select) @transport.buf = "output" @transport.stubs(:process_ssh) @transport.expect(/output/).should == "output" end describe "when processing the ssh loop" do it "should advance one tick in the ssh event loop and exit on eof" do @transport.buf = '' @connection.expects(:process).then.raises(EOFError) @transport.process_ssh end end end end diff --git a/spec/unit/util/network_device/transport/telnet_spec.rb b/spec/unit/util/network_device/transport/telnet_spec.rb index 7528e0740..cea5ab79a 100755 --- a/spec/unit/util/network_device/transport/telnet_spec.rb +++ b/spec/unit/util/network_device/transport/telnet_spec.rb @@ -1,76 +1,75 @@ #!/usr/bin/env rspec - -require File.dirname(__FILE__) + '/../../../../spec_helper' +require 'spec_helper' require 'puppet/util/network_device/transport/telnet' describe Puppet::Util::NetworkDevice::Transport::Telnet do before(:each) do @transport = Puppet::Util::NetworkDevice::Transport::Telnet.new() end it "should not handle login through the transport" do @transport.should_not be_handles_login end it "should connect to the given host and port" do Net::Telnet.expects(:new).with { |args| args["Host"] == "localhost" && args["Port"] == 23 }.returns stub_everything @transport.host = "localhost" @transport.port = 23 @transport.connect end it "should connect and specify the default prompt" do @transport.default_prompt = "prompt" Net::Telnet.expects(:new).with { |args| args["Prompt"] == "prompt" }.returns stub_everything @transport.host = "localhost" @transport.port = 23 @transport.connect end describe "when connected" do before(:each) do @telnet = stub_everything 'telnet' Net::Telnet.stubs(:new).returns(@telnet) @transport.connect end it "should send line to the telnet session" do @telnet.expects(:puts).with("line") @transport.send("line") end describe "when expecting output" do it "should waitfor output on the telnet session" do @telnet.expects(:waitfor).with(/regex/) @transport.expect(/regex/) end it "should return telnet session output" do @telnet.expects(:waitfor).returns("output") @transport.expect(/regex/).should == "output" end it "should yield telnet session output to the given block" do @telnet.expects(:waitfor).yields("output") @transport.expect(/regex/) { |out| out.should == "output" } end end end describe "when closing" do before(:each) do @telnet = stub_everything 'telnet' Net::Telnet.stubs(:new).returns(@telnet) @transport.connect end it "should close the telnet session" do @telnet.expects(:close) @transport.close end end end