spec/beaker/dsl/helpers_spec.rb in beaker-1.10.0 vs spec/beaker/dsl/helpers_spec.rb in beaker-1.11.0

- old
+ new

@@ -9,10 +9,11 @@ @logger ||= RSpec::Mocks::Mock.new('logger').as_null_object end end describe ClassMixedWithDSLHelpers do + let( :opts ) { Beaker::Options::Presets.env_vars } let( :command ) { 'ls' } let( :host ) { double.as_null_object } let( :result ) { Beaker::Result.new( host, command ) } let( :master ) { make_host( 'master', :roles => %w( master agent ) ) } @@ -125,30 +126,30 @@ before :each do host.should_receive( :exec ).and_return( result ) end it 'yields self' do - subject.on host, command do + subject.on host, command do expect( subject ). to be_an_instance_of( ClassMixedWithDSLHelpers ) end end it 'provides access to stdout' do - subject.on host, command do + subject.on host, command do expect( subject.stdout ).to be == 'stdout' end end it 'provides access to stderr' do - subject.on host, command do + subject.on host, command do expect( subject.stderr ).to be == 'stderr' end end it 'provides access to exit_code' do - subject.on host, command do + subject.on host, command do expect( subject.exit_code ).to be == 0 end end end @@ -511,27 +512,28 @@ end end describe '#stub_forge_on' do it 'stubs forge.puppetlabs.com with the value of `forge`' do - subject.should_receive( :forge ).and_return( 'my_forge.example.com' ) + subject.stub( :options ).and_return( {} ) Resolv.should_receive( :getaddress ). with( 'my_forge.example.com' ).and_return( '127.0.0.1' ) subject.should_receive( :stub_hosts_on ). with( 'my_host', 'forge.puppetlabs.com' => '127.0.0.1' ) subject.should_receive( :stub_hosts_on ). with( 'my_host', 'forgeapi.puppetlabs.com' => '127.0.0.1' ) - subject.stub_forge_on( 'my_host' ) + subject.stub_forge_on( 'my_host', 'my_forge.example.com' ) end end describe "#stub_forge" do it "delegates to #stub_forge_on with the default host" do + subject.stub( :options ).and_return( {} ) subject.stub( :hosts ).and_return( hosts ) - subject.should_receive( :stub_forge_on ).with( master ).once + subject.should_receive( :stub_forge_on ).with( master, nil ).once subject.stub_forge( ) end end @@ -876,6 +878,111 @@ subject.fact('osfamily') end end + + describe 'copy_root_module_to' do + def source_to_scp (source, target, items) + subject.stub(:parse_for_moduleroot).and_return('/totalfake/testmodule') + Dir.stub(:getpwd).and_return('/totalfake/testmodule') + + items = [items] unless items.kind_of?(Array) + File.stub(:exists?).with(any_args()).and_return(false) + File.stub(:directory?).with(any_args()).and_return(false) + items.each do |item| + source_item = File.join(source,item) + File.stub(:exists?).with(source_item).and_return(true) + options = {} + if ['manifests','lib','templates','files'].include? item + File.stub(:directory?).with(source_item).and_return(true) + options = {:mkdir => true} + end + master.should_receive(:do_scp_to).with(source_item,target,options).ordered + end + end + it 'should call scp with the correct info, with only providing host' do + files = ['manifests','lib','templates','metadata.json','Modulefile','files'] + source_to_scp '/totalfake/testmodule',"#{master['puppetpath']}/modules/testmodule",files + subject.stub(:parse_for_modulename).with(any_args()).and_return("testmodule") + subject.copy_root_module_to(master) + end + it 'should call scp with the correct info, when specifying the modulename' do + files = ['manifests','lib','metadata.json','Modulefile'] + source_to_scp '/totalfake/testmodule',"#{master['puppetpath']}/modules/bogusmodule",files + subject.stub(:parse_for_modulename).and_return('testmodule') + subject.copy_root_module_to(master,{:module_name =>"bogusmodule"}) + end + it 'should call scp with the correct info, when specifying the target to a different path' do + files = ['manifests','lib','templates','metadata.json','Modulefile','files'] + target = "/opt/shared/puppet/modules" + source_to_scp '/totalfake/testmodule',"#{target}/testmodule",files + subject.stub(:parse_for_modulename).and_return('testmodule') + subject.copy_root_module_to(master,{:target_module_path => target}) + end + end + + describe 'split_author_modulename' do + it 'should return a correct modulename' do + result = subject.split_author_modulename('myname-test_43_module') + expect(result[:author]).to eq('myname') + expect(result[:module]).to eq('test_43_module') + end + end + + describe 'get_module_name' do + it 'should return a has of author and modulename' do + expect(subject.get_module_name('myname-test_43_module')).to eq('test_43_module') + end + it 'should return nil for invalid names' do + expect(subject.get_module_name('myname-')).to eq(nil) + end + end + + describe 'parse_for_modulename' do + directory = '/testfilepath/myname-testmodule' + it 'should return name from metadata.json' do + File.stub(:exists?).with("#{directory}/metadata.json").and_return(true) + File.stub(:read).with("#{directory}/metadata.json").and_return(" {\"name\":\"myname-testmodule\"} ") + subject.logger.should_receive(:debug).with("Attempting to parse Modulename from metadata.json") + subject.logger.should_not_receive(:debug).with('Unable to determine name, returning null') + subject.parse_for_modulename(directory).should eq('testmodule') + end + it 'should return name from Modulefile' do + File.stub(:exists?).with("#{directory}/metadata.json").and_return(false) + File.stub(:exists?).with("#{directory}/Modulefile").and_return(true) + File.stub(:read).with("#{directory}/Modulefile").and_return("name 'myname-testmodule' \nauthor 'myname'") + subject.logger.should_receive(:debug).with("Attempting to parse Modulename from Modulefile") + subject.logger.should_not_receive(:debug).with("Unable to determine name, returning null") + expect(subject.parse_for_modulename(directory)).to eq('testmodule') + end + end + + describe 'parse_for_module_root' do + directory = '/testfilepath/myname-testmodule' + it 'should recersively go up the directory to find the module files' do + File.stub(:exists?).with("#{directory}/acceptance/Modulefile").and_return(false) + File.stub(:exists?).with("#{directory}/Modulefile").and_return(true) + subject.logger.should_not_receive(:debug).with("At root, can't parse for another directory") + subject.logger.should_receive(:debug).with("No Modulefile found at #{directory}/acceptance, moving up") + expect(subject.parse_for_moduleroot("#{directory}/acceptance")).to eq(directory) + end + it 'should recersively go up the directory to find the module files' do + File.stub(:exists?).and_return(false) + subject.logger.should_receive(:debug).with("No Modulefile found at #{directory}, moving up") + subject.logger.should_receive(:error).with("At root, can't parse for another directory") + expect(subject.parse_for_moduleroot(directory)).to eq(nil) + end + + end +end + +module FakeFS + class File < StringIO + def self.absolute_path(filepath) + RealFile.absolute_path(filepath) + end + def self.expand_path(filepath) + RealFile.expand_path(filepath) + end + end end