diff --git a/spec/integration/application/apply_spec.rb b/spec/integration/application/apply_spec.rb index 7342c212f..3ce1277d3 100755 --- a/spec/integration/application/apply_spec.rb +++ b/spec/integration/application/apply_spec.rb @@ -1,134 +1,115 @@ -#! /usr/bin/env ruby require 'spec_helper' require 'puppet_spec/files' -require 'puppet/application/apply' describe "apply" do include PuppetSpec::Files before :each do Puppet[:reports] = "none" end - def tmpfile_with_content(name, content) - result = tmpfile(name) - File.open(result, "w") do |f| - f.puts [ content ].flatten * "\n" - end - result - end - describe "when applying provided catalogs" do it "can apply catalogs provided in a file in pson" do file_to_create = tmpfile("pson_catalog") catalog = Puppet::Resource::Catalog.new('mine', Puppet.lookup(:environments).get(Puppet[:environment])) resource = Puppet::Resource.new(:file, file_to_create, :parameters => {:content => "my stuff"}) catalog.add_resource resource - manifest = tmpfile("manifest") + manifest = file_containing("manifest", catalog.to_pson) - File.open(manifest, "w") { |f| f.print catalog.to_pson } puppet = Puppet::Application[:apply] puppet.options[:catalog] = manifest puppet.apply expect(Puppet::FileSystem.exist?(file_to_create)).to be_true expect(File.read(file_to_create)).to eq("my stuff") end end it "applies a given file even when a directory environment is specified" do - manifest = tmpfile_with_content("manifest.pp", "notice('it was applied')") + manifest = file_containing("manifest.pp", "notice('it was applied')") special = Puppet::Node::Environment.create(:special, []) Puppet.override(:current_environment => special) do Puppet[:environment] = 'special' puppet = Puppet::Application[:apply] puppet.stubs(:command_line).returns(stub('command_line', :args => [manifest])) expect { puppet.run_command }.to exit_with(0) end expect(@logs.map(&:to_s)).to include('it was applied') end it "applies a given file even when an ENC is configured", :if => !Puppet.features.microsoft_windows? do - manifest = tmpfile_with_content("manifest.pp", "notice('specific manifest applied')") - - site_manifest = tmpfile_with_content("site_manifest.pp", "notice('the site manifest was applied instead')") - - enc = tmpfile_with_content("enc_script", ["#!/bin/sh", "echo 'classes: []'"]) + manifest = file_containing("manifest.pp", "notice('specific manifest applied')") + site_manifest = file_containing("site_manifest.pp", "notice('the site manifest was applied instead')") + enc = file_containing("enc_script", "#!/bin/sh\necho 'classes: []'") File.chmod(0755, enc) special = Puppet::Node::Environment.create(:special, []) Puppet.override(:current_environment => special) do Puppet[:environment] = 'special' Puppet[:node_terminus] = 'exec' Puppet[:external_nodes] = enc Puppet[:manifest] = site_manifest puppet = Puppet::Application[:apply] puppet.stubs(:command_line).returns(stub('command_line', :args => [manifest])) expect { puppet.run_command }.to exit_with(0) end expect(@logs.map(&:to_s)).to include('specific manifest applied') end context "with a module" do let(:modulepath) { tmpdir('modulepath') } let(:execute) { 'include amod' } let(:args) { ['-e', execute, '--modulepath', modulepath] } before(:each) do - Puppet::FileSystem.mkpath("#{modulepath}/amod/manifests") - File.open("#{modulepath}/amod/manifests/init.pp", "w") do |f| - f.puts <<-EOF - class amod{ - notice('amod class included') + dir_contained_in(modulepath, { + "amod" => { + "manifests" => { + "init.pp" => "class amod{ notice('amod class included') }" + } } - EOF - end - environmentdir = Dir.mktmpdir('environments') - Puppet[:environmentpath] = environmentdir - create_default_directory_environment - end + }) - def create_default_directory_environment - Puppet::FileSystem.mkpath("#{Puppet[:environmentpath]}/#{Puppet[:environment]}") + Puppet[:environmentpath] = dir_containing("environments", { Puppet[:environment] => {} }) end def init_cli_args_and_apply_app(args, execute) Puppet.initialize_settings(args) puppet = Puppet::Application.find(:apply).new(stub('command_line', :subcommand_name => :apply, :args => args)) puppet.options[:code] = execute return puppet end it "looks in --modulepath even when the default directory environment exists" do apply = init_cli_args_and_apply_app(args, execute) expect do expect { apply.run }.to exit_with(0) end.to have_printed('amod class included') end it "looks in --modulepath even when given a specific directory --environment" do args << '--environment' << 'production' apply = init_cli_args_and_apply_app(args, execute) expect do expect { apply.run }.to exit_with(0) end.to have_printed('amod class included') end it "looks in --modulepath when given multiple paths in --modulepath" do args = ['-e', execute, '--modulepath', [tmpdir('notmodulepath'), modulepath].join(File::PATH_SEPARATOR)] apply = init_cli_args_and_apply_app(args, execute) expect do expect { apply.run }.to exit_with(0) end.to have_printed('amod class included') end end end diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb index 312c4fc95..2d0d95275 100755 --- a/spec/lib/puppet_spec/files.rb +++ b/spec/lib/puppet_spec/files.rb @@ -1,88 +1,89 @@ require 'fileutils' require 'tempfile' require 'tmpdir' require 'pathname' # A support module for testing files. module PuppetSpec::Files def self.cleanup $global_tempfiles ||= [] while path = $global_tempfiles.pop do begin Dir.unstub(:entries) FileUtils.rm_rf path, :secure => true rescue Errno::ENOENT # nothing to do end end end def make_absolute(path) PuppetSpec::Files.make_absolute(path) end def self.make_absolute(path) path = File.expand_path(path) path[0] = 'c' if Puppet.features.microsoft_windows? path end def tmpfile(name, dir = nil) PuppetSpec::Files.tmpfile(name, dir) end def self.tmpfile(name, dir = nil) # Generate a temporary file, just for the name... source = dir ? Tempfile.new(name, dir) : Tempfile.new(name) path = source.path source.close! record_tmp(File.expand_path(path)) path end def file_containing(name, contents) PuppetSpec::Files.file_containing(name, contents) end def self.file_containing(name, contents) file = tmpfile(name) File.open(file, 'wb') { |f| f.write(contents) } file end def tmpdir(name) PuppetSpec::Files.tmpdir(name) end def self.tmpdir(name) dir = Dir.mktmpdir(name) record_tmp(dir) dir end def dir_containing(name, contents_hash) PuppetSpec::Files.dir_containing(name, contents_hash) end def self.dir_containing(name, contents_hash) dir_contained_in(tmpdir(name), contents_hash) end + def dir_contained_in(dir, contents_hash) PuppetSpec::Files.dir_contained_in(dir, contents_hash) end def self.dir_contained_in(dir, contents_hash) contents_hash.each do |k,v| if v.is_a?(Hash) Dir.mkdir(tmp = File.join(dir,k)) dir_contained_in(tmp, v) else file = File.join(dir, k) File.open(file, 'wb') {|f| f.write(v) } end end dir end def self.record_tmp(tmp) # ...record it for cleanup, $global_tempfiles ||= [] $global_tempfiles << tmp end def expect_file_mode(file, mode) actual_mode = "%o" % Puppet::FileSystem.stat(file).mode target_mode = if Puppet.features.microsoft_windows? mode else "10" + "%04i" % mode.to_i end actual_mode.should == target_mode end end