diff --git a/acceptance/lib/puppet/acceptance/environment_utils.rb b/acceptance/lib/puppet/acceptance/environment_utils.rb index 97de23d9a..b657ad128 100644 --- a/acceptance/lib/puppet/acceptance/environment_utils.rb +++ b/acceptance/lib/puppet/acceptance/environment_utils.rb @@ -1,297 +1,354 @@ require 'puppet/acceptance/module_utils' module Puppet module Acceptance module EnvironmentUtils include Puppet::Acceptance::ModuleUtils # Generate puppet manifest for the creation of an environment with # the given modulepath and manifest and env_name. The created environment # will have on testing_mod module, and manifest site.pp which includes it. # # @param options [Hash] # @option options [String] :modulepath Modules directory # @option options [String] :manifest Manifest directory # @option options [String] :env_name Environment name # @return [String] Puppet manifest to create the environment files def generate_environment(options) modulepath = options[:modulepath] manifestpath = options[:manifestpath] env_name = options[:env_name] environment = <<-MANIFEST_SNIPPET file { ################################################### # #{env_name} #{generate_module("testing_mod", env_name, modulepath)} "#{manifestpath}":; "#{manifestpath}/site.pp": ensure => file, + mode => 0640, content => ' notify { "in #{env_name} site.pp": } include testing_mod ' ; } MANIFEST_SNIPPET end # Generate one module's manifest code. def generate_module(module_name, env_name, modulepath) module_pp = <<-MANIFEST_SNIPPET "#{modulepath}":; "#{modulepath}/#{module_name}":; "#{modulepath}/#{module_name}/manifests":; "#{modulepath}/#{module_name}/manifests/init.pp": ensure => file, + mode => 0640, content => 'class #{module_name} { notify { "include #{env_name} #{module_name}": } }' ; MANIFEST_SNIPPET end # Default, legacy, dynamic and directory environments # using generate_manifest(), all rooted in testdir. # # @param [String] testdir path to the temp directory which will be the confdir all # the environments live in # @return [String] Puppet manifest to generate all of the environment files. def environment_manifest(testdir) manifest = <<-MANIFEST File { ensure => directory, - owner => puppet, - mode => 0700, + owner => #{master['user']}, + group => #{master['group']}, + mode => 0750, } file { "#{testdir}": } #{generate_environment( :modulepath => "#{testdir}/modules", :manifestpath => "#{testdir}/manifests", :env_name => "default environment")} #{generate_environment( :modulepath => "#{testdir}/testing-modules", :manifestpath => "#{testdir}/testing-manifests", :env_name => "legacy testing environment")} file { "#{testdir}/dynamic":; "#{testdir}/dynamic/testing":; } #{generate_environment( :modulepath => "#{testdir}/dynamic/testing/modules", :manifestpath => "#{testdir}/dynamic/testing/manifests", :env_name => "dynamic testing environment")} file { "#{testdir}/environments":; "#{testdir}/environments/testing":; } #{generate_environment( :modulepath => "#{testdir}/environments/testing/modules", :manifestpath => "#{testdir}/environments/testing/manifests", :env_name => "directory testing environment")} file { "#{testdir}/environments/testing_environment_conf":; } #{generate_environment( :modulepath => "#{testdir}/environments/testing_environment_conf/nonstandard-modules", :manifestpath => "#{testdir}/environments/testing_environment_conf/nonstandard-manifests", :env_name => "directory testing with environment.conf")} file { "#{testdir}/environments/testing_environment_conf/environment.conf": ensure => file, + mode => 0640, content => ' modulepath = nonstandard-modules:$basemodulepath manifest = nonstandard-manifests config_version = local-version.sh ' } file { "#{testdir}/environments/testing_environment_conf/local-version.sh": ensure => file, + mode => 0640, content => '#! /usr/bin/env bash echo "local testing_environment_conf"' ; } ################### # Services file { "#{testdir}/services":; "#{testdir}/services/testing":; #{generate_module('service_mod', "service testing environment", "#{testdir}/services/testing/modules")} } ####################### # Config version script file { "#{testdir}/static-version.sh": ensure => file, + mode => 0640, content => '#! /usr/bin/env bash echo "static"' ; } MANIFEST end + def get_directory_hash_from(host, path) + dir_hash = {} + on(host, "ls #{path}") do |result| + result.stdout.split.inject(dir_hash) do |hash,f| + hash[f] = "#{path}/#{f}" + hash + end + end + dir_hash + end + + def safely_shadow_directory_contents_and_yield(host, original_path, new_path, &block) + original_files = get_directory_hash_from(host, original_path) + new_files = get_directory_hash_from(host, new_path) + conflicts = original_files.keys & new_files.keys + + step "backup original files" do + conflicts.each do |c| + on(host, "mv #{original_files[c]} #{original_files[c]}.bak") + end + end + + step "shadow original files with temporary files" do + new_files.each do |name,full_path_name| + on(host, "cp -R #{full_path_name} #{original_path}/#{name}") + end + end + + step "open permissions to 770 on all temporary files copied into working dir and set ownership" do + file_list = new_files.keys.map { |name| "#{original_path}/#{name}" }.join(' ') + on(host, "chown -R #{host['user']}:#{host['group']} #{file_list}") + on(host, "chmod -R 770 #{file_list}") + end + + yield + + ensure + step "clear out the temporary files" do + files_to_delete = new_files.keys.map { |name| "#{original_path}/#{name}" } + on(host, "rm -rf #{files_to_delete.join(' ')}") + end + step "move the shadowed files back to their original places" do + conflicts.each do |c| + on(host, "mv #{original_files[c]}.bak #{original_files[c]}") + end + end + end + # Stand up a puppet master on the master node with the given master_opts - # using the passed confdir as the --confdir setting, and then run through a - # series of environment tests for the passed environment and return a hashed - # structure of the results. + # using the passed envdir as the source of the puppet environment files, + # and passed confdir as the directory to use for the temporary + # puppet.conf. It then runs through a series of environment tests for the + # passed environment and returns a hashed structure of the results. # # @return [Hash>] Hash of # Beaker::Hosts for each agent run keyed to a hash of Beaker::Result # objects keyed by each subtest that was performed. - def use_an_environment(environment, description, master_opts, confdir, options = {}) - ssldir = on(master, puppet("master --configprint ssldir")).stdout.chomp + def use_an_environment(environment, description, master_opts, envdir, confdir, options = {}) master_puppet_conf = master_opts.dup # shallow clone - master_puppet_conf[:__commandline_args__] = "--confdir=#{confdir} --ssldir=#{ssldir}" - config_print = options[:config_print] - directory_environments = options[:directory_environments] - results = {} - with_puppet_running_on(master, master_puppet_conf, confdir) do - agents.each do |agent| - agent_results = results[agent] = {} - - step "puppet agent using #{description} environment" - args = "-t", "--server", master - args << ["--environment", environment] if environment - # Test agents configured to use directory environments (affects environment - # loading on the agent, especially with regards to requests/node environment) - args << "--environmentpath='$confdir/environments'" if directory_environments && agent != master - on(agent, puppet("agent", *args), :acceptable_exit_codes => (0..255)) do - agent_results[:puppet_agent] = result - end - - if agent == master - args = ["--trace", "--confdir", confdir] - args << ["--environment", environment] if environment + results = {} + safely_shadow_directory_contents_and_yield(master, master['puppetpath'], envdir) do - step "print puppet config for #{description} environment" - on(agent, puppet(*(["config", "print", "basemodulepath", "modulepath", "manifest", "config_version", config_print] + args)), :acceptable_exit_codes => (0..255)) do - agent_results[:puppet_config] = result - end + config_print = options[:config_print] + directory_environments = options[:directory_environments] - step "puppet apply using #{description} environment" - on(agent, puppet(*(["apply", '-e', '"include testing_mod"'] + args)), :acceptable_exit_codes => (0..255)) do - agent_results[:puppet_apply] = result - end + with_puppet_running_on(master, master_puppet_conf, confdir) do + agents.each do |agent| + agent_results = results[agent] = {} - # Be aware that Puppet Module Tool will create the module directory path if it - # does not exist. So these tests should be run last... - step "install a module into environment" - on(agent, puppet(*(["module", "install", "pmtacceptance-nginx"] + args)), :acceptable_exit_codes => (0..255)) do - agent_results[:puppet_module_install] = result + step "puppet agent using #{description} environment" + args = "-t", "--server", master + args << ["--environment", environment] if environment + # Test agents configured to use directory environments (affects environment + # loading on the agent, especially with regards to requests/node environment) + args << "--environmentpath='$confdir/environments'" if directory_environments && agent != master + on(agent, puppet("agent", *args), :acceptable_exit_codes => (0..255)) do + agent_results[:puppet_agent] = result end - step "uninstall a module from #{description} environment" - on(agent, puppet(*(["module", "uninstall", "pmtacceptance-nginx"] + args)), :acceptable_exit_codes => (0..255)) do - agent_results[:puppet_module_uninstall] = result + if agent == master + args = ["--trace"] + args << ["--environment", environment] if environment + + step "print puppet config for #{description} environment" + on(agent, puppet(*(["config", "print", "basemodulepath", "modulepath", "manifest", "config_version", config_print] + args)), :acceptable_exit_codes => (0..255)) do + agent_results[:puppet_config] = result + end + + step "puppet apply using #{description} environment" + on(agent, puppet(*(["apply", '-e', '"include testing_mod"'] + args)), :acceptable_exit_codes => (0..255)) do + agent_results[:puppet_apply] = result + end + + # Be aware that Puppet Module Tool will create the module directory path if it + # does not exist. So these tests should be run last... + step "install a module into environment" + on(agent, puppet(*(["module", "install", "pmtacceptance-nginx"] + args)), :acceptable_exit_codes => (0..255)) do + agent_results[:puppet_module_install] = result + end + + step "uninstall a module from #{description} environment" + on(agent, puppet(*(["module", "uninstall", "pmtacceptance-nginx"] + args)), :acceptable_exit_codes => (0..255)) do + agent_results[:puppet_module_uninstall] = result + end end end end end return results end # For each Beaker::Host in the results Hash, generates a chart, comparing # the expected exit code and regexp matches from expectations to the # Beaker::Result.output for a particular command that was executed in the # environment. Outputs either 'ok' or text highlighting the errors, and # returns false if any errors were found. # # @param [Hash>] results # @param [Hash Integer,Array}>] expectations # @return [Array] Returns an empty array of there were no failures, or an # Array of failed cases. def review_results(results, expectations) failed = [] results.each do |agent, agent_results| divider = "-" * 79 logger.info divider logger.info "For: (#{agent.name}) #{agent}" logger.info divider agent_results.each do |testname, execution_results| expected_exit_code = expectations[testname][:exit_code] match_tests = expectations[testname][:matches] || [] not_match_tests = expectations[testname][:does_not_match] || [] expect_failure = expectations[testname][:expect_failure] notes = expectations[testname][:notes] errors = [] if execution_results.exit_code != expected_exit_code errors << "To exit with an exit code of '#{expected_exit_code}', instead of '#{execution_results.exit_code}'" end match_tests.each do |regexp| if execution_results.output !~ regexp errors << "#{errors.empty? ? "To" : "And"} match: #{regexp}" end end not_match_tests.each do |regexp| if execution_results.output =~ regexp errors << "#{errors.empty? ? "Not to" : "And not"} match: #{regexp}" end end error_msg = "Expected the output:\n#{execution_results.output}\n#{errors.join("\n")}" unless errors.empty? case_failed = case when errors.empty? && expect_failure then 'ok - failed as expected' when errors.empty? && !expect_failure then 'ok' else '*UNEXPECTED FAILURE*' end logger.info "#{testname}: #{case_failed}" if case_failed == 'ok - failed as expected' logger.info divider logger.info "Case is known to fail as follows:\n#{execution_results.output}\n" elsif case_failed == '*UNEXPECTED FAILURE*' failed << "Unexpected failure for #{testname}" logger.info divider logger.info "#{error_msg}" end logger.info("------\nNotes: #{notes}") if notes logger.info divider end end return failed end def assert_review(review) failures = [] review.each do |scenario, failed| if !failed.empty? problems = "Problems in the '#{scenario}' output reported above:\n #{failed.join("\n ")}" logger.warn(problems) failures << problems end end assert failures.empty?, "Failed Review:\n\n#{failures.join("\n")}\n" end end end end diff --git a/acceptance/lib/puppet/acceptance/environment_utils_spec.rb b/acceptance/lib/puppet/acceptance/environment_utils_spec.rb new file mode 100644 index 000000000..596b5dac9 --- /dev/null +++ b/acceptance/lib/puppet/acceptance/environment_utils_spec.rb @@ -0,0 +1,144 @@ +require File.join(File.dirname(__FILE__),'../../acceptance_spec_helper.rb') +require 'puppet/acceptance/environment_utils' + +module EnvironmentUtilsSpec +describe 'EnvironmentUtils' do + class ATestCase + include Puppet::Acceptance::EnvironmentUtils + + def step(str) + yield + end + + def on(host, command, options = nil) + stdout = host.do(command, options) + yield TestResult.new(stdout) if block_given? + end + end + + class TestResult + attr_accessor :stdout + + def initialize(stdout) + self.stdout = stdout + end + end + + class TestHost + attr_accessor :did, :directories, :attributes + + def initialize(directories, attributes = {}) + self.directories = directories + self.did = [] + self.attributes = attributes + end + + def do(command, options) + did << (options.nil? ? command : [command, options]) + case command + when /^ls (.*)/ then directories[$1] + end + end + + def [](param) + attributes[param] + end + end + + let(:testcase) { ATestCase.new } + let(:host) { TestHost.new(directory_contents, 'user' => 'root', 'group' => 'puppet') } + let(:directory_contents) do + { + '/etc/puppet' => 'foo bar baz widget', + '/tmp/dir' => 'foo dingo bar thing', + } + end + + it "runs the block of code" do + ran_code = false + testcase.safely_shadow_directory_contents_and_yield(host, '/etc/puppet', '/tmp/dir') do + ran_code = true + end + expect(ran_code).to be_true + expect(host.did).to eq([ + "ls /etc/puppet", + "ls /tmp/dir", + "mv /etc/puppet/foo /etc/puppet/foo.bak", + "mv /etc/puppet/bar /etc/puppet/bar.bak", + "cp -R /tmp/dir/foo /etc/puppet/foo", + "cp -R /tmp/dir/dingo /etc/puppet/dingo", + "cp -R /tmp/dir/bar /etc/puppet/bar", + "cp -R /tmp/dir/thing /etc/puppet/thing", + "chown -R root:puppet /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + "chmod -R 770 /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + "rm -rf /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + "mv /etc/puppet/foo.bak /etc/puppet/foo", + "mv /etc/puppet/bar.bak /etc/puppet/bar" + ]) + end + + it "backs up the original items that are shadowed by tmp items" do + testcase.safely_shadow_directory_contents_and_yield(host, '/etc/puppet', '/tmp/dir') {} + expect(host.did.grep(%r{mv /etc/puppet/\w+ })).to eq([ + "mv /etc/puppet/foo /etc/puppet/foo.bak", + "mv /etc/puppet/bar /etc/puppet/bar.bak", + ]) + end + + it "copies in all the tmp items into the working dir" do + testcase.safely_shadow_directory_contents_and_yield(host, '/etc/puppet', '/tmp/dir') {} + expect(host.did.grep(%r{cp})).to eq([ + "cp -R /tmp/dir/foo /etc/puppet/foo", + "cp -R /tmp/dir/dingo /etc/puppet/dingo", + "cp -R /tmp/dir/bar /etc/puppet/bar", + "cp -R /tmp/dir/thing /etc/puppet/thing", + ]) + end + + it "opens the permissions on all copied files to 770 and sets ownership based on host settings" do + testcase.safely_shadow_directory_contents_and_yield(host, '/etc/puppet', '/tmp/dir') {} + expect(host.did.grep(%r{ch(mod|own)})).to eq([ + "chown -R root:puppet /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + "chmod -R 770 /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + ]) + end + + it "deletes all the tmp items from the working dir" do + testcase.safely_shadow_directory_contents_and_yield(host, '/etc/puppet', '/tmp/dir') {} + expect(host.did.grep(%r{rm})).to eq([ + "rm -rf /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + ]) + end + + it "replaces the original items that had been shadowed into the working dir" do + testcase.safely_shadow_directory_contents_and_yield(host, '/etc/puppet', '/tmp/dir') {} + expect(host.did.grep(%r{mv /etc/puppet/\w+\.bak})).to eq([ + "mv /etc/puppet/foo.bak /etc/puppet/foo", + "mv /etc/puppet/bar.bak /etc/puppet/bar" + ]) + end + + it "always cleans up, even if the code we yield to raises an error" do + expect do + testcase.safely_shadow_directory_contents_and_yield(host, '/etc/puppet', '/tmp/dir') do + raise 'oops' + end + end.to raise_error('oops') + expect(host.did).to eq([ + "ls /etc/puppet", + "ls /tmp/dir", + "mv /etc/puppet/foo /etc/puppet/foo.bak", + "mv /etc/puppet/bar /etc/puppet/bar.bak", + "cp -R /tmp/dir/foo /etc/puppet/foo", + "cp -R /tmp/dir/dingo /etc/puppet/dingo", + "cp -R /tmp/dir/bar /etc/puppet/bar", + "cp -R /tmp/dir/thing /etc/puppet/thing", + "chown -R root:puppet /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + "chmod -R 770 /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + "rm -rf /etc/puppet/foo /etc/puppet/dingo /etc/puppet/bar /etc/puppet/thing", + "mv /etc/puppet/foo.bak /etc/puppet/foo", + "mv /etc/puppet/bar.bak /etc/puppet/bar" + ]) + end +end +end diff --git a/acceptance/lib/puppet/acceptance/install_utils_spec.rb b/acceptance/lib/puppet/acceptance/install_utils_spec.rb index 344a4e461..3c077f1d2 100644 --- a/acceptance/lib/puppet/acceptance/install_utils_spec.rb +++ b/acceptance/lib/puppet/acceptance/install_utils_spec.rb @@ -1,261 +1,263 @@ require File.join(File.dirname(__FILE__),'../../acceptance_spec_helper.rb') require 'puppet/acceptance/install_utils' +module InstallUtilsSpec describe 'InstallUtils' do class ATestCase include Puppet::Acceptance::InstallUtils end class Platform < String def with_version_codename self end end class TestHost attr_accessor :config def initialize(config = {}) self.config = config end def [](key) config[key] end end let(:host) { TestHost.new } let(:testcase) { ATestCase.new } describe "install_packages_on" do it "raises an error if package_hash has unknown platform keys" do expect do testcase.install_packages_on(host, { :foo => 'bar'}) end.to raise_error(RuntimeError, /Unknown platform 'foo' in package_hash/) end shared_examples_for(:install_packages_on) do |platform,command,package| let(:package_hash) do { :redhat => ['rh_package'], :debian => [['db_command', 'db_package']], } end let(:additional_switches) { platform == 'debian' ? '--allow-unauthenticated' : nil } before do logger = mock('logger', :notify => nil) host.stubs(:logger).returns(logger) host.config['platform'] = Platform.new(platform) end it "installs packages on a host" do host.expects(:check_for_package).never host.expects(:install_package).with(package, additional_switches).once testcase.install_packages_on(host, package_hash) end it "checks and installs packages on a host" do host.expects(:check_for_package).with(command).once host.expects(:install_package).with(package, additional_switches).once testcase.install_packages_on(host, package_hash, :check_if_exists => true) end end it_should_behave_like(:install_packages_on, 'fedora', 'rh_package', 'rh_package') it_should_behave_like(:install_packages_on, 'debian', 'db_command', 'db_package') end describe "fetch" do before do logger = stub('logger', :notify => nil) testcase.stubs(:logger).returns(logger) FileUtils.expects(:makedirs).with('dir') end it "does not fetch if destination file already exists" do File.expects(:exists?).with('dir/file').returns(true) testcase.expects(:open).never testcase.fetch('http://foo', 'file', 'dir') end it "fetches file from url and stores in destination directory as filename" do stream = mock('stream') file = mock('file') testcase.expects(:open).with('http://foo/file').yields(stream) File.expects(:open).with('dir/file', 'w').yields(file) FileUtils.expects(:copy_stream).with(stream, file) testcase.fetch('http://foo', 'file', 'dir') end it "returns path to destination file" do testcase.expects(:open).with('http://foo/file') expect(testcase.fetch('http://foo', 'file', 'dir')).to eql('dir/file') end end describe "fetch_remote_dir" do before do logger = stub('logger', {:notify => nil, :debug => nil}) testcase.stubs(:logger).returns(logger) end it "calls wget with the right amount of cut dirs for url that ends in '/'" do url = 'http://builds.puppetlabs.lan/puppet/7807591405af849da2ad6534c66bd2d4efff604f/repos/el/6/devel/x86_64/' testcase.expects(:`).with("wget -nv -P dir --reject \"index.html*\",\"*.gif\" --cut-dirs=6 -np -nH --no-check-certificate -r #{url} 2>&1").returns("log") expect( testcase.fetch_remote_dir(url, 'dir')).to eql('dir/x86_64') end it "calls wget with the right amount of cut dirs for url that doesn't end in '/'" do url = 'http://builds.puppetlabs.lan/puppet/7807591405af849da2ad6534c66bd2d4efff604f/repos/apt/wheezy' testcase.expects(:`).with("wget -nv -P dir --reject \"index.html*\",\"*.gif\" --cut-dirs=4 -np -nH --no-check-certificate -r #{url}/ 2>&1").returns("log") expect( testcase.fetch_remote_dir(url, 'dir')).to eql('dir/wheezy') end end shared_examples_for :redhat_platforms do |platform,sha,files| before do host.config['platform'] = Platform.new(platform) end it "fetches and installs repo configurations for #{platform}" do platform_configs_dir = "repo-configs/#{platform}" rpm_url = files[:rpm][0] rpm_file = files[:rpm][1] testcase.expects(:fetch).with( "http://yum.puppetlabs.com", rpm_file, platform_configs_dir ).returns("#{platform_configs_dir}/#{rpm_file}") repo_url = files[:repo][0] repo_file = files[:repo][1] testcase.expects(:fetch).with( repo_url, repo_file, platform_configs_dir ).returns("#{platform_configs_dir}/#{repo_file}") repo_dir_url = files[:repo_dir][0] repo_dir = files[:repo_dir][1] testcase.expects(:link_exists?).returns( true ) testcase.expects(:fetch_remote_dir).with( repo_dir_url, platform_configs_dir ).returns("#{platform_configs_dir}/#{repo_dir}") testcase.expects(:link_exists?).returns( true ) testcase.expects(:on).with(host, regexp_matches(/rm.*repo; rm.*rpm; rm.*#{repo_dir}/)) testcase.expects(:scp_to).with(host, "#{platform_configs_dir}/#{rpm_file}", '/root') testcase.expects(:scp_to).with(host, "#{platform_configs_dir}/#{repo_file}", '/root') testcase.expects(:scp_to).with(host, "#{platform_configs_dir}/#{repo_dir}", '/root') testcase.expects(:on).with(host, regexp_matches(%r{mv.*repo /etc/yum.repos.d})) testcase.expects(:on).with(host, regexp_matches(%r{find /etc/yum.repos.d/ -name .*})) testcase.expects(:on).with(host, regexp_matches(%r{rpm.*/root/.*rpm})) testcase.install_repos_on(host, sha, 'repo-configs') end end describe "install_repos_on" do let(:sha) { "abcdef10" } it_should_behave_like(:redhat_platforms, 'el-6-i386', 'abcdef10', { :rpm => [ "http://yum.puppetlabs.com", "puppetlabs-release-el-6.noarch.rpm", ], :repo => [ "http://builds.puppetlabs.lan/puppet/abcdef10/repo_configs/rpm/", "pl-puppet-abcdef10-el-6-i386.repo", ], :repo_dir => [ "http://builds.puppetlabs.lan/puppet/abcdef10/repos/el/6/products/i386/", "i386", ], }, ) it_should_behave_like(:redhat_platforms, 'fedora-20-x86_64', 'abcdef10', { :rpm => [ "http://yum.puppetlabs.com", "puppetlabs-release-fedora-20.noarch.rpm", ], :repo => [ "http://builds.puppetlabs.lan/puppet/abcdef10/repo_configs/rpm/", "pl-puppet-abcdef10-fedora-f20-x86_64.repo", ], :repo_dir => [ "http://builds.puppetlabs.lan/puppet/abcdef10/repos/fedora/f20/products/x86_64/", "x86_64", ], }, ) it_should_behave_like(:redhat_platforms, 'centos-5-x86_64', 'abcdef10', { :rpm => [ "http://yum.puppetlabs.com", "puppetlabs-release-el-5.noarch.rpm", ], :repo => [ "http://builds.puppetlabs.lan/puppet/abcdef10/repo_configs/rpm/", "pl-puppet-abcdef10-el-5-x86_64.repo", ], :repo_dir => [ "http://builds.puppetlabs.lan/puppet/abcdef10/repos/el/5/products/x86_64/", "x86_64", ], }, ) it "installs on a debian host" do host.config['platform'] = platform = Platform.new('ubuntu-precise-x86_64') platform_configs_dir = "repo-configs/#{platform}" deb = "puppetlabs-release-precise.deb" testcase.expects(:fetch).with( "http://apt.puppetlabs.com/", deb, platform_configs_dir ).returns("#{platform_configs_dir}/#{deb}") list = "pl-puppet-#{sha}-precise.list" testcase.expects(:fetch).with( "http://builds.puppetlabs.lan/puppet/#{sha}/repo_configs/deb/", list, platform_configs_dir ).returns("#{platform_configs_dir}/#{list}") testcase.expects(:fetch_remote_dir).with( "http://builds.puppetlabs.lan/puppet/#{sha}/repos/apt/precise", platform_configs_dir ).returns("#{platform_configs_dir}/precise") testcase.expects(:on).with(host, regexp_matches(/rm.*list; rm.*deb; rm.*/)) testcase.expects(:scp_to).with(host, "#{platform_configs_dir}/#{deb}", '/root') testcase.expects(:scp_to).with(host, "#{platform_configs_dir}/#{list}", '/root') testcase.expects(:scp_to).with(host, "#{platform_configs_dir}/precise", '/root') testcase.expects(:on).with(host, regexp_matches(%r{mv.*list /etc/apt/sources.list.d})) testcase.expects(:on).with(host, regexp_matches(%r{find /etc/apt/sources.list.d/ -name .*})) testcase.expects(:on).with(host, regexp_matches(%r{dpkg -i.*/root/.*deb})) testcase.expects(:on).with(host, regexp_matches(%r{apt-get update})) testcase.install_repos_on(host, sha, 'repo-configs') end end end +end diff --git a/acceptance/tests/environment/can_enumerate_environments.rb b/acceptance/tests/environment/can_enumerate_environments.rb index 0223a17c1..6973d8bdb 100644 --- a/acceptance/tests/environment/can_enumerate_environments.rb +++ b/acceptance/tests/environment/can_enumerate_environments.rb @@ -1,68 +1,68 @@ test_name "Can enumerate environments via an HTTP endpoint" def master_port(agent) setting_on(agent, "agent", "masterport") end def setting_on(host, section, name) on(host, puppet("config", "print", name, "--section", section)).stdout.chomp end def full_path(host, path) if host['platform'] =~ /win/ on(host, "cygpath '#{path}'").stdout.chomp else path end end def curl_master_from(agent, path, headers = '', &block) url = "https://#{master}:#{master_port(agent)}#{path}" cert_path = full_path(agent, setting_on(agent, "agent", "hostcert")) key_path = full_path(agent, setting_on(agent, "agent", "hostprivkey")) curl_base = "curl -sg --cert \"#{cert_path}\" --key \"#{key_path}\" -k -H '#{headers}'" on agent, "#{curl_base} '#{url}'", &block end environments_dir = master.tmpdir("environments") apply_manifest_on(master, <<-MANIFEST) File { ensure => directory, owner => #{master['user']}, group => #{master['group']}, - mode => 0750, + mode => 0770, } file { "#{environments_dir}":; "#{environments_dir}/env1":; "#{environments_dir}/env2":; } MANIFEST master_opts = { :master => { :environmentpath => environments_dir } } if master.is_pe? master_opts[:master][:basemodulepath] = master['sitemoduledir'] end with_puppet_running_on(master, master_opts) do agents.each do |agent| step "Ensure that an unauthenticated client cannot access the environments list" do on agent, "curl -ksv https://#{master}:#{master_port(agent)}/v2.0/environments", :acceptable_exit_codes => [0,7] do assert_match(/< HTTP\/1\.\d 403/, stderr) end end step "Ensure that an authenticated client can retrieve the list of environments" do curl_master_from(agent, '/v2.0/environments') do data = JSON.parse(stdout) assert_equal(["env1", "env2", "production"], data["environments"].keys.sort) end end end end diff --git a/acceptance/tests/environment/directory.rb b/acceptance/tests/environment/directory.rb index 287750e5f..e69562858 100644 --- a/acceptance/tests/environment/directory.rb +++ b/acceptance/tests/environment/directory.rb @@ -1,223 +1,226 @@ test_name "directory environments" require 'puppet/acceptance/environment_utils' extend Puppet::Acceptance::EnvironmentUtils step "setup environments" stub_forge_on(master) testdir = master.tmpdir("confdir") +puppet_conf_backup_dir = master.tmpdir("puppet-conf-backup-dir") apply_manifest_on(master, environment_manifest(testdir), :catch_failures => true) results = {} review = {} #################### step "[ Run Tests ]" existing_directory_scenario = "Test a specific, existing directory environment configuration" step existing_directory_scenario master_opts = { 'main' => { 'environmentpath' => '$confdir/environments', 'config_version' => '$confdir/static-version.sh', } } -general = [ master_opts, testdir, { :directory_environments => true } ] +general = [ master_opts, testdir, puppet_conf_backup_dir, { :directory_environments => true } ] results[existing_directory_scenario] = use_an_environment("testing", "directory testing", *general) default_environment_scenario = "Test behavior of default environment" step default_environment_scenario results[default_environment_scenario] = use_an_environment(nil, "default environment", *general) non_existent_environment_scenario = "Test for an environment that does not exist" step non_existent_environment_scenario results[non_existent_environment_scenario] = use_an_environment("doesnotexist", "non existent environment", *general) with_explicit_environment_conf_scenario = "Test a specific, existing directory environment with an explicit environment.conf file" step with_explicit_environment_conf_scenario results[with_explicit_environment_conf_scenario] = use_an_environment("testing_environment_conf", "directory with environment.conf testing", *general) master_environmentpath_scenario = "Test behavior of a directory environment when environmentpath is set in the master section" step master_environmentpath_scenario master_opts = { 'master' => { 'environmentpath' => '$confdir/environments', 'config_version' => '$confdir/static-version.sh', } } -results[master_environmentpath_scenario] = use_an_environment("testing", "master environmentpath", master_opts, testdir, :directory_environments => true, :config_print => '--section=master') +results[master_environmentpath_scenario] = use_an_environment("testing", "master environmentpath", master_opts, testdir, puppet_conf_backup_dir, :directory_environments => true, :config_print => '--section=master') bad_environmentpath_scenario = "Test behavior of directory environments when environmentpath is set to a non-existent directory" step bad_environmentpath_scenario master_opts = { 'main' => { 'environmentpath' => '/doesnotexist', 'config_version' => '$confdir/static-version.sh', } } -results[bad_environmentpath_scenario] = use_an_environment("testing", "bad environmentpath", master_opts, testdir, :directory_environments => true) +results[bad_environmentpath_scenario] = use_an_environment("testing", "bad environmentpath", master_opts, testdir, puppet_conf_backup_dir, :directory_environments => true) ######################################## step "[ Report on Environment Results ]" +testdir = master.tmpdir("confdir") + step "Reviewing: #{existing_directory_scenario}" existing_directory_expectations = lambda do |env| { :puppet_config => { :exit_code => 0, - :matches => [%r{manifest.*/tmp.*/environments/#{env}/manifests$}, - %r{modulepath.*/tmp.*/environments/#{env}/modules:.+}, + :matches => [%r{manifest.*#{master['puppetpath']}/environments/#{env}/manifests$}, + %r{modulepath.*#{master['puppetpath']}/environments/#{env}/modules:.+}, %r{config_version = $}] }, :puppet_module_install => { :exit_code => 0, - :matches => [%r{Preparing to install into /tmp.*/environments/#{env}/modules}, + :matches => [%r{Preparing to install into #{master['puppetpath']}/environments/#{env}/modules}, %r{pmtacceptance-nginx}], }, :puppet_module_uninstall => { :exit_code => 0, - :matches => [%r{Removed.*pmtacceptance-nginx.*from /tmp.*/environments/#{env}/modules}], + :matches => [%r{Removed.*pmtacceptance-nginx.*from #{master['puppetpath']}/environments/#{env}/modules}], }, :puppet_apply => { :exit_code => 0, :matches => [%r{include directory #{env} environment testing_mod}], }, :puppet_agent => { :exit_code => 2, :matches => [%r{Applying configuration version '\d+'}, %r{in directory #{env} environment site.pp}, %r{include directory #{env} environment testing_mod}], }, } end review[existing_directory_scenario] = review_results( results[existing_directory_scenario], existing_directory_expectations.call('testing') ) step "Reviewing: #{default_environment_scenario}" default_environment_expectations = existing_directory_expectations.call('production').merge( :puppet_apply => { :exit_code => 0, :matches => [%r{include default environment testing_mod}], :notes => "The production directory environment is empty, but the inclusion of basemodulepath in the directory environment modulepath picks up the default testing_mod class in $confdir/modules" }, :puppet_agent => { :exit_code => 0, :matches => [ %r{Applying configuration version '\d+'}], :does_not_match => [%r{include.*testing_mod}, %r{Warning.*404}], :notes => "The master automatically creates an empty production env dir." } ) review[default_environment_scenario] = review_results( results[default_environment_scenario], default_environment_expectations ) step "Reviewing: #{non_existent_environment_scenario}" non_existent_environment_expectations = lambda do |env,path| { :puppet_config => { :exit_code => 1, :matches => [%r{Could not find a directory environment named '#{env}' anywhere in the path.*#{path}}], }, :puppet_module_install => { :exit_code => 1, :matches => [%r{Could not find a directory environment named '#{env}' anywhere in the path.*#{path}}], }, :puppet_module_uninstall => { :exit_code => 1, :matches => [%r{Could not find a directory environment named '#{env}' anywhere in the path.*#{path}}], }, :puppet_apply => { :exit_code => 1, :matches => [%r{Could not find a directory environment named '#{env}' anywhere in the path.*#{path}}], }, :puppet_agent => { :exit_code => 1, :matches => [%r{Warning.*404.*Could not find environment '#{env}'}, %r{Could not retrieve catalog; skipping run}], }, } end review[non_existent_environment_scenario] = review_results( results[non_existent_environment_scenario], - non_existent_environment_expectations.call('doesnotexist', '/tmp/.*/environments') + non_existent_environment_expectations.call('doesnotexist', master['puppetpath']) ) existing_directory_with_puppet_conf_expectations = { :puppet_config => { :exit_code => 0, - :matches => [%r{manifest.*/tmp.*/environments/testing_environment_conf/nonstandard-manifests$}, - %r{modulepath.*/tmp.*/environments/testing_environment_conf/nonstandard-modules:.+}, - %r{config_version = /tmp/.*/environments/testing_environment_conf/local-version.sh$}] + :matches => [%r{manifest.*#{master['puppetpath']}/environments/testing_environment_conf/nonstandard-manifests$}, + %r{modulepath.*#{master['puppetpath']}/environments/testing_environment_conf/nonstandard-modules:.+}, + %r{config_version = #{master['puppetpath']}/environments/testing_environment_conf/local-version.sh$}] }, :puppet_module_install => { :exit_code => 0, - :matches => [%r{Preparing to install into /tmp.*/environments/testing_environment_conf/nonstandard-modules}, + :matches => [%r{Preparing to install into #{master['puppetpath']}/environments/testing_environment_conf/nonstandard-modules}, %r{pmtacceptance-nginx}], }, :puppet_module_uninstall => { :exit_code => 0, - :matches => [%r{Removed.*pmtacceptance-nginx.*from /tmp.*/environments/testing_environment_conf/nonstandard-modules}], + :matches => [%r{Removed.*pmtacceptance-nginx.*from #{master['puppetpath']}/environments/testing_environment_conf/nonstandard-modules}], }, :puppet_apply => { :exit_code => 0, :matches => [%r{include directory testing with environment\.conf testing_mod}], }, :puppet_agent => { :exit_code => 2, :matches => [%r{Applying configuration version 'local testing_environment_conf'}, %r{in directory testing with environment\.conf site.pp}, %r{include directory testing with environment\.conf testing_mod}], }, } step "Reviewing: #{with_explicit_environment_conf_scenario}" review[with_explicit_environment_conf_scenario] = review_results( results[with_explicit_environment_conf_scenario], existing_directory_with_puppet_conf_expectations ) master_environmentpath_expectations = existing_directory_expectations.call('testing').merge( :puppet_module_install => { :exit_code => 0, - :matches => [%r{Preparing to install into /tmp.*/confdir.*/modules}, + :matches => [%r{Preparing to install into #{master['puppetpath']}/modules}, %r{pmtacceptance-nginx}], :expect_failure => true, :notes => "Runs in user mode and doesn't see the master environmenetpath setting.", }, :puppet_module_uninstall => { :exit_code => 0, - :matches => [%r{Removed.*pmtacceptance-nginx.*from /tmp.*/confdir.*/modules}], + :matches => [%r{Removed.*pmtacceptance-nginx.*from #{master['puppetpath']}/modules}], :expect_failure => true, :notes => "Runs in user mode and doesn't see the master environmenetpath setting.", }, :puppet_apply => { :exit_code => 0, :matches => [%r{include default environment testing_mod}], :expect_failure => true, :notes => "Runs in user mode and doesn't see the master environmenetpath setting.", } ) step "Reviewing: #{master_environmentpath_scenario}" review[master_environmentpath_scenario] = review_results( results[master_environmentpath_scenario], master_environmentpath_expectations ) bad_environmentpath_expectations = non_existent_environment_expectations.call('testing', '/doesnotexist') step "Reviewing: #{bad_environmentpath_scenario}" review[bad_environmentpath_scenario] = review_results( results[bad_environmentpath_scenario], bad_environmentpath_expectations ) ######################### step "[ Assert Success ]" assert_review(review) diff --git a/acceptance/tests/environment/directory_environment_with_environment_conf.rb b/acceptance/tests/environment/directory_environment_with_environment_conf.rb index f1d75ce2b..e556c2d1e 100644 --- a/acceptance/tests/environment/directory_environment_with_environment_conf.rb +++ b/acceptance/tests/environment/directory_environment_with_environment_conf.rb @@ -1,109 +1,109 @@ test_name 'Use a directory environment from environmentpath with an environment.conf' testdir = master.tmpdir('use-environment-conf') absolute_manifestdir = "#{testdir}/manifests" absolute_modulesdir = "#{testdir}/absolute-modules" absolute_globalsdir = "#{testdir}/global-modules" apply_manifest_on(master, <<-MANIFEST, :catch_failures => true) File { ensure => directory, owner => #{master['user']}, group => #{master['group']}, - mode => 0750, + mode => 0770, } file { "#{testdir}":; "#{testdir}/environments":; "#{testdir}/environments/direnv":; "#{testdir}/environments/direnv/environment.conf": ensure => file, mode => 0640, content => ' manifest=#{absolute_manifestdir} modulepath=relative-modules:#{absolute_modulesdir}:$basemodulepath config_version=version_script.sh ' ; "#{testdir}/environments/direnv/relative-modules":; "#{testdir}/environments/direnv/relative-modules/relmod":; "#{testdir}/environments/direnv/relative-modules/relmod/manifests":; "#{testdir}/environments/direnv/relative-modules/relmod/manifests/init.pp": ensure => file, mode => 0640, content => 'class relmod { notify { "included relmod": } }' ; "#{testdir}/environments/direnv/version_script.sh": ensure => file, mode => 0750, content => '#!/usr/bin/env sh echo "ver123" ' ; "#{absolute_manifestdir}":; "#{absolute_manifestdir}/site.pp": ensure => file, mode => 0640, content => ' notify { "direnv site.pp": } include relmod include absmod include globalmod ' ; "#{absolute_modulesdir}":; "#{absolute_modulesdir}/absmod":; "#{absolute_modulesdir}/absmod/manifests":; "#{absolute_modulesdir}/absmod/manifests/init.pp": ensure => file, mode => 0640, content => 'class absmod { notify { "included absmod": } }' ; "#{absolute_globalsdir}":; "#{absolute_globalsdir}/globalmod":; "#{absolute_globalsdir}/globalmod/manifests":; "#{absolute_globalsdir}/globalmod/manifests/init.pp": ensure => file, mode => 0640, content => 'class globalmod { notify { "included globalmod": } }' ; } MANIFEST master_opts = { 'master' => { 'environmentpath' => "#{testdir}/environments", 'basemodulepath' => "#{absolute_globalsdir}", } } if master.is_pe? master_opts['master']['basemodulepath'] << ":#{master['sitemoduledir']}" end with_puppet_running_on master, master_opts, testdir do agents.each do |agent| on(agent, puppet("agent", "-t", "--server", master, "--environment", "direnv"), :acceptable_exit_codes => [2]) do |result| assert_match(/direnv site.pp/, result.stdout) assert_match(/included relmod/, result.stdout) assert_match(/included absmod/, result.stdout) assert_match(/included globalmod/, result.stdout) assert_match(/Applying.*ver123/, result.stdout) end end end diff --git a/acceptance/tests/environment/dynamic.rb b/acceptance/tests/environment/dynamic.rb index 8707a13bc..060e5426a 100644 --- a/acceptance/tests/environment/dynamic.rb +++ b/acceptance/tests/environment/dynamic.rb @@ -1,118 +1,121 @@ test_name "dynamic environments" require 'puppet/acceptance/environment_utils' extend Puppet::Acceptance::EnvironmentUtils step "setup environments" stub_forge_on(master) testdir = master.tmpdir("confdir") +puppet_conf_backup_dir = master.tmpdir("puppet-conf-backup-dir") apply_manifest_on(master, environment_manifest(testdir), :catch_failures => true) results = {} review = {} #################### step "[ Run Tests ]" existing_dynamic_scenario = "Test a specific, existing dynamic environment configuration" step existing_dynamic_scenario master_opts = { 'main' => { 'manifest' => '$confdir/dynamic/$environment/manifests', 'modulepath' => '$confdir/dynamic/$environment/modules', 'config_version' => '$confdir/static-version.sh', } } -results[existing_dynamic_scenario] = use_an_environment("testing", "dynamic testing", master_opts, testdir) +results[existing_dynamic_scenario] = use_an_environment("testing", "dynamic testing", master_opts, testdir, puppet_conf_backup_dir) default_environment_scenario = "Test behavior of default environment" step default_environment_scenario -results[default_environment_scenario] = use_an_environment(nil, "default environment", master_opts, testdir) +results[default_environment_scenario] = use_an_environment(nil, "default environment", master_opts, testdir, puppet_conf_backup_dir) non_existent_environment_scenario = "Test for an environment that does not exist" step non_existent_environment_scenario -results[non_existent_environment_scenario] = use_an_environment("doesnotexist", "non existent environment", master_opts, testdir) +results[non_existent_environment_scenario] = use_an_environment("doesnotexist", "non existent environment", master_opts, testdir, puppet_conf_backup_dir) ######################################## step "[ Report on Environment Results ]" +confdir = master.puppet['confdir'] + step "Reviewing: #{existing_dynamic_scenario}" review[existing_dynamic_scenario] = review_results(results[existing_dynamic_scenario], :puppet_config => { :exit_code => 0, - :matches => [%r{manifest.*/tmp.*/dynamic/testing/manifests$}, - %r{modulepath.*/tmp.*/dynamic/testing/modules$}, - %r{config_version.*/tmp.*/static-version.sh$}] + :matches => [%r{manifest.*#{confdir}/dynamic/testing/manifests$}, + %r{modulepath.*#{confdir}/dynamic/testing/modules$}, + %r{config_version.*#{confdir}/static-version.sh$}] }, :puppet_module_install => { :exit_code => 0, - :matches => [%r{Preparing to install into /tmp.*/dynamic/testing/modules}, + :matches => [%r{Preparing to install into #{confdir}/dynamic/testing/modules}, %r{pmtacceptance-nginx}], }, :puppet_module_uninstall => { :exit_code => 0, - :matches => [%r{Removed.*pmtacceptance-nginx.*from /tmp.*/dynamic/testing/modules}], + :matches => [%r{Removed.*pmtacceptance-nginx.*from #{confdir}/dynamic/testing/modules}], }, :puppet_apply => { :exit_code => 0, :matches => [%r{include dynamic testing environment testing_mod}], }, :puppet_agent => { :exit_code => 2, :matches => [%r{Applying configuration version 'static'}, %r{in dynamic testing environment site.pp}, %r{include dynamic testing environment testing_mod}], } ) step "Reviewing: #{default_environment_scenario}" default_expectations = lambda do |env| { :puppet_config => { :exit_code => 0, - :matches => [%r{manifest.*/tmp.*/dynamic/#{env}/manifests$}, - %r{modulepath.*/tmp.*/dynamic/#{env}/modules$}, - %r{^config_version.*/tmp.*/static-version.sh$}] + :matches => [%r{manifest.*#{confdir}/dynamic/#{env}/manifests$}, + %r{modulepath.*#{confdir}/dynamic/#{env}/modules$}, + %r{^config_version.*#{confdir}/static-version.sh$}] }, :puppet_module_install => { :exit_code => 0, - :matches => [%r{Preparing to install into /tmp.*/dynamic/#{env}/modules}, + :matches => [%r{Preparing to install into #{confdir}/dynamic/#{env}/modules}, %r{pmtacceptance-nginx}], }, :puppet_module_uninstall => { :exit_code => 0, - :matches => [%r{Removed.*pmtacceptance-nginx.*from /tmp.*/dynamic/#{env}/modules}], + :matches => [%r{Removed.*pmtacceptance-nginx.*from #{confdir}/dynamic/#{env}/modules}], }, :puppet_apply => { :exit_code => 1, :matches => [ENV['PARSER'] == 'future' ? %r{Error:.*Could not find class ::testing_mod} : %r{Error:.*Could not find class testing_mod} ], }, :puppet_agent => { :exit_code => 0, :matches => [%r{Applying configuration version 'static'}], :does_not_match => [%r{in default environment site.pp}, %r{include default environment testing_mod}, %r{Notice: include}], }, } end review[default_environment_scenario] = review_results( results[default_environment_scenario], default_expectations.call('production') ) step "Reviewing: #{non_existent_environment_scenario}" review[non_existent_environment_scenario] = review_results( results[non_existent_environment_scenario], default_expectations.call('doesnotexist') ) ######################### step "[ Assert Success ]" assert_review(review) diff --git a/acceptance/tests/environment/static.rb b/acceptance/tests/environment/static.rb index e591ee55a..e3b1481df 100644 --- a/acceptance/tests/environment/static.rb +++ b/acceptance/tests/environment/static.rb @@ -1,112 +1,115 @@ test_name "legacy environments" require 'puppet/acceptance/environment_utils' extend Puppet::Acceptance::EnvironmentUtils step "setup environments" stub_forge_on(master) testdir = master.tmpdir("confdir") +puppet_conf_backup_dir = master.tmpdir("puppet-conf-backup-dir") apply_manifest_on(master, environment_manifest(testdir), :catch_failures => true) results = {} review = {} #################### step "[ Run Tests ]" existing_legacy_scenario = "Test a specific, existing legacy environment configuration" step existing_legacy_scenario master_opts = { 'testing' => { 'manifest' => "$confdir/testing-manifests", 'modulepath' => "$confdir/testing-modules", 'config_version' => "$confdir/static-version.sh", }, } -results[existing_legacy_scenario] = use_an_environment("testing", "legacy testing", master_opts, testdir) +results[existing_legacy_scenario] = use_an_environment("testing", "legacy testing", master_opts, testdir, puppet_conf_backup_dir) default_environment_scenario = "Test behavior of default environment" step default_environment_scenario -results[default_environment_scenario] = use_an_environment(nil, "default environment", master_opts, testdir) +results[default_environment_scenario] = use_an_environment(nil, "default environment", master_opts, testdir, puppet_conf_backup_dir) non_existent_environment_scenario = "Test for an environment that does not exist" step non_existent_environment_scenario -results[non_existent_environment_scenario] = use_an_environment("doesnotexist", "non existent environment", master_opts, testdir) +results[non_existent_environment_scenario] = use_an_environment("doesnotexist", "non existent environment", master_opts, testdir, puppet_conf_backup_dir) ######################################## step "[ Report on Environment Results ]" +confdir = master.puppet['confdir'] + step "Reviewing: #{existing_legacy_scenario}" review[existing_legacy_scenario] = review_results(results[existing_legacy_scenario], :puppet_config => { :exit_code => 0, - :matches => [%r{manifest.*/tmp.*/testing-manifests$}, - %r{modulepath.*/tmp.*/testing-modules$}, - %r{config_version.*/tmp.*/static-version.sh$}] + :matches => [%r{manifest.*#{confdir}/testing-manifests$}, + %r{modulepath.*#{confdir}/testing-modules$}, + %r{config_version.*#{confdir}/static-version.sh$}] }, :puppet_module_install => { :exit_code => 0, - :matches => [%r{Preparing to install into /tmp.*/testing-modules}, + :matches => [%r{Preparing to install into #{confdir}/testing-modules}, %r{pmtacceptance-nginx}], }, :puppet_module_uninstall => { :exit_code => 0, - :matches => [%r{Removed.*pmtacceptance-nginx.*from /tmp.*/testing-modules}], + :matches => [%r{Removed.*pmtacceptance-nginx.*from #{confdir}/testing-modules}], }, :puppet_apply => { :exit_code => 0, :matches => [%r{include legacy testing environment testing_mod}], }, :puppet_agent => { :exit_code => 2, :matches => [%r{Applying configuration version 'static'}, %r{in legacy testing environment site.pp}, %r{include legacy testing environment testing_mod}], } ) step "Reviewing: #{default_environment_scenario}" default_expectations = { :puppet_config => { :exit_code => 0, - :matches => [%r{manifest.*/tmp.*/manifests/site.pp$}, - %r{modulepath.*/tmp.*/modules:.*}, + :matches => [%r{manifest.*#{confdir}/manifests/site.pp$}, + %r{modulepath.*#{confdir}/modules:.*}, %r{^config_version\s+=\s*$}] }, :puppet_module_install => { :exit_code => 0, - :matches => [%r{Preparing to install into /tmp.*/modules}, + :matches => [%r{Preparing to install into #{confdir}/modules}, %r{pmtacceptance-nginx}], }, :puppet_module_uninstall => { :exit_code => 0, - :matches => [%r{Removed.*pmtacceptance-nginx.*from /tmp.*/modules}], + :matches => [%r{Removed.*pmtacceptance-nginx.*from #{confdir}/modules}], }, :puppet_apply => { :exit_code => 0, :matches => [%r{include default environment testing_mod}], }, :puppet_agent => { :exit_code => 2, :matches => [%r{Applying configuration version '\d+'}, %r{in default environment site.pp}, %r{include default environment testing_mod}], }, } review[default_environment_scenario] = review_results( results[default_environment_scenario], default_expectations ) step "Reviewing: #{non_existent_environment_scenario}" review[non_existent_environment_scenario] = review_results( results[non_existent_environment_scenario], default_expectations ) ######################### step "[ Assert Success ]" assert_review(review) diff --git a/acceptance/tests/environment/use_environment_from_environmentpath.rb b/acceptance/tests/environment/use_environment_from_environmentpath.rb index c83d38e90..ceb673067 100644 --- a/acceptance/tests/environment/use_environment_from_environmentpath.rb +++ b/acceptance/tests/environment/use_environment_from_environmentpath.rb @@ -1,180 +1,180 @@ test_name "Use environments from the environmentpath" testdir = master.tmpdir('use_environmentpath') def generate_environment(path_to_env, environment) env_content = <<-EOS "#{path_to_env}/#{environment}":; "#{path_to_env}/#{environment}/manifests":; "#{path_to_env}/#{environment}/modules":; EOS end def generate_module_content(module_name, options = {}) base_path = options[:base_path] environment = options[:environment] env_path = options[:env_path] path_to_module = [base_path, env_path, environment, "modules"].compact.join("/") module_info = "module-#{module_name}" module_info << "-from-#{environment}" if environment module_content = <<-EOS "#{path_to_module}/#{module_name}":; "#{path_to_module}/#{module_name}/manifests":; "#{path_to_module}/#{module_name}/files":; "#{path_to_module}/#{module_name}/templates":; "#{path_to_module}/#{module_name}/lib":; "#{path_to_module}/#{module_name}/lib/facter":; "#{path_to_module}/#{module_name}/manifests/init.pp": ensure => file, mode => 0640, content => 'class #{module_name} { notify { "template-#{module_name}": message => template("#{module_name}/our_template.erb") } file { "$agent_file_location/file-#{module_info}": source => "puppet:///modules/#{module_name}/data" } }' ; "#{path_to_module}/#{module_name}/lib/facter/environment_fact_#{module_name}.rb": ensure => file, mode => 0640, content => "Facter.add(:environment_fact_#{module_name}) { setcode { 'environment fact from #{module_info}' } }" ; "#{path_to_module}/#{module_name}/files/data": ensure => file, mode => 0640, content => "data file from #{module_info}" ; "#{path_to_module}/#{module_name}/templates/our_template.erb": ensure => file, mode => 0640, content => "<%= @environment_fact_#{module_name} %>" ; EOS end def generate_site_manifest(path_to_manifest, *modules_to_include) manifest_content = <<-EOS "#{path_to_manifest}/site.pp": ensure => file, mode => 0640, content => "#{modules_to_include.map { |m| "include #{m}" }.join("\n")}" ; EOS end apply_manifest_on(master, <<-MANIFEST, :catch_failures => true) File { ensure => directory, owner => #{master['user']}, group => #{master['group']}, - mode => 0750, + mode => 0770, } file { "#{testdir}":; "#{testdir}/base":; "#{testdir}/additional":; "#{testdir}/modules":; #{generate_environment("#{testdir}/base", "shadowed")} #{generate_environment("#{testdir}/base", "onlybase")} #{generate_environment("#{testdir}/additional", "shadowed")} #{generate_module_content("atmp", :base_path => testdir, :env_path => 'base', :environment => 'shadowed')} #{generate_site_manifest("#{testdir}/base/shadowed/manifests", "atmp", "globalmod")} #{generate_module_content("atmp", :base_path => testdir, :env_path => 'base', :environment => 'onlybase')} #{generate_site_manifest("#{testdir}/base/onlybase/manifests", "atmp", "globalmod")} #{generate_module_content("atmp", :base_path => testdir, :env_path => 'additional', :environment => 'shadowed')} #{generate_site_manifest("#{testdir}/additional/shadowed/manifests", "atmp", "globalmod")} # And one global module (--modulepath setting) #{generate_module_content("globalmod", :base_path => testdir)} } MANIFEST def run_with_environment(agent, environment, options = {}) expected_exit_code = options[:expected_exit_code] || 2 expected_strings = options[:expected_strings] step "running an agent in environment '#{environment}'" atmp = agent.tmpdir("use_environmentpath_#{environment}") agent_config = [ "-t", "--server", master, ] agent_config << '--environment' << environment if environment # This to test how the agent behaves when using the directory environment # loaders (which will not load an environment if it does not exist) agent_config << "--environmentpath='$confdir/environments'" if agent != master agent_config << { 'ENV' => { "FACTER_agent_file_location" => atmp }, } on(agent, puppet("agent", *agent_config), :acceptable_exit_codes => [expected_exit_code]) do |result| yield atmp, result end on agent, "rm -rf #{atmp}" end master_opts = { 'master' => { 'environmentpath' => "#{testdir}/additional:#{testdir}/base", 'basemodulepath' => "#{testdir}/modules", } } if master.is_pe? master_opts['master']['basemodulepath'] << ":#{master['sitemoduledir']}" end with_puppet_running_on master, master_opts, testdir do agents.each do |agent| run_with_environment(agent, "shadowed") do |tmpdir,catalog_result| ["module-atmp-from-shadowed", "module-globalmod"].each do |expected| assert_match(/environment fact from #{expected}/, catalog_result.stdout) end ["module-atmp-from-shadowed", "module-globalmod"].each do |expected| on agent, "cat #{tmpdir}/file-#{expected}" do |file_result| assert_match(/data file from #{expected}/, file_result.stdout) end end end run_with_environment(agent, "onlybase") do |tmpdir,catalog_result| ["module-atmp-from-onlybase", "module-globalmod"].each do |expected| assert_match(/environment fact from #{expected}/, catalog_result.stdout) end ["module-atmp-from-onlybase", "module-globalmod"].each do |expected| on agent, "cat #{tmpdir}/file-#{expected}" do |file_result| assert_match(/data file from #{expected}/, file_result.stdout) end end end if master.is_pe? step("This test cannot run if the production environment directory does not exist, because the fallback production environment puppet creates has an empty modulepath and PE cannot run without it's basemodulepath in /opt. PUP-2519, which implicitly creates the production environment directory should allow this to run again") else run_with_environment(agent, nil, :expected_exit_code => 0) do |tmpdir, result| assert_no_match(/module-atmp/, result.stdout, "module-atmp was included despite no environment being loaded") assert_match(/Loading facts.*globalmod/, result.stdout) end end end end