diff --git a/spec/unit/environments_spec.rb b/spec/unit/environments_spec.rb index 7499ecd30..00ae65f3e 100644 --- a/spec/unit/environments_spec.rb +++ b/spec/unit/environments_spec.rb @@ -1,382 +1,383 @@ require 'spec_helper' require 'puppet/environments' require 'puppet/file_system' require 'matchers/include' +module PuppetEnvironments describe Puppet::Environments do include Matchers::Include - include PuppetSpec::Files FS = Puppet::FileSystem describe "directories loader" do before(:each) do Puppet.settings.initialize_global_settings end it "lists environments" do global_path_1_location = File.expand_path("global_path_1") global_path_2_location = File.expand_path("global_path_2") global_path_1 = FS::MemoryFile.a_directory(global_path_1_location) global_path_2 = FS::MemoryFile.a_directory(global_path_2_location) envdir = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_missing_file("environment.conf"), FS::MemoryFile.a_directory("modules"), FS::MemoryFile.a_directory("manifests"), ]), FS::MemoryFile.a_directory("env2", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), ]) loader_from(:filesystem => [envdir, global_path_1, global_path_2], :directory => envdir, :modulepath => [global_path_1_location, global_path_2_location]) do |loader| expect(loader.list).to include_in_any_order( environment(:env1). with_manifest("#{FS.path_string(envdir)}/env1/manifests"). with_modulepath(["#{FS.path_string(envdir)}/env1/modules", global_path_1_location, global_path_2_location]), environment(:env2)) end end it "does not list files" do envdir = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_regular_file_containing("foo", ''), FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), FS::MemoryFile.a_directory("env2", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), ]) loader_from(:filesystem => [envdir], :directory => envdir) do |loader| expect(loader.list).to include_in_any_order(environment(:env1), environment(:env2)) end end it "ignores directories that are not valid env names (alphanumeric and _)" do envdir = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory(".foo"), FS::MemoryFile.a_directory("bar-thing"), FS::MemoryFile.a_directory("with spaces"), FS::MemoryFile.a_directory("some.thing"), FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), FS::MemoryFile.a_directory("env2", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), ]) loader_from(:filesystem => [envdir], :directory => envdir) do |loader| expect(loader.list).to include_in_any_order(environment(:env1), environment(:env2)) end end it "gets a particular environment" do directory_tree = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), FS::MemoryFile.a_directory("env2", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), ]) loader_from(:filesystem => [directory_tree], :directory => directory_tree) do |loader| expect(loader.get("env1")).to environment(:env1) end end it "returns nil if an environment can't be found" do directory_tree = FS::MemoryFile.a_directory("envdir", []) loader_from(:filesystem => [directory_tree], :directory => directory_tree) do |loader| expect(loader.get("env_not_in_this_list")).to be_nil end end context "with an environment.conf" do let(:envdir) do FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_regular_file_containing("environment.conf", content), ]), ]) end let(:manifestdir) { FS::MemoryFile.a_directory(File.expand_path("/some/manifest/path")) } let(:modulepath) do [ FS::MemoryFile.a_directory(File.expand_path("/some/module/path")), FS::MemoryFile.a_directory(File.expand_path("/some/other/path")), ] end let(:content) do <<-EOF manifest=#{manifestdir} modulepath=#{modulepath.join(File::PATH_SEPARATOR)} config_version=/some/script EOF end it "reads environment.conf settings" do loader_from(:filesystem => [envdir, manifestdir, modulepath].flatten, :directory => envdir) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(manifestdir.path). with_modulepath(modulepath.map(&:path)) end end it "does not append global_module_path to environment.conf modulepath setting" do global_path_location = File.expand_path("global_path") global_path = FS::MemoryFile.a_directory(global_path_location) loader_from(:filesystem => [envdir, manifestdir, modulepath, global_path].flatten, :directory => envdir, :modulepath => [global_path]) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(manifestdir.path). with_modulepath(modulepath.map(&:path)) end end it "reads config_version setting" do loader_from(:filesystem => [envdir, manifestdir, modulepath].flatten, :directory => envdir) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(manifestdir.path). with_modulepath(modulepath.map(&:path)). with_config_version(File.expand_path('/some/script')) end end it "accepts an empty environment.conf without warning" do content = nil envdir = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_regular_file_containing("environment.conf", content), ]), ]) manifestdir = FS::MemoryFile.a_directory(File.join(envdir, "env1", "manifests")) modulesdir = FS::MemoryFile.a_directory(File.join(envdir, "env1", "modules")) global_path_location = File.expand_path("global_path") global_path = FS::MemoryFile.a_directory(global_path_location) loader_from(:filesystem => [envdir, manifestdir, modulesdir, global_path].flatten, :directory => envdir, :modulepath => [global_path]) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest("#{FS.path_string(envdir)}/env1/manifests"). with_modulepath(["#{FS.path_string(envdir)}/env1/modules", global_path_location]). with_config_version(nil) end expect(@logs).to be_empty end it "logs a warning, but processes the main settings if there are extraneous sections" do content << "[foo]" loader_from(:filesystem => [envdir, manifestdir, modulepath].flatten, :directory => envdir) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(manifestdir.path). with_modulepath(modulepath.map(&:path)). with_config_version(File.expand_path('/some/script')) end expect(@logs.map(&:to_s).join).to match(/Invalid.*at.*#{envdir}\/env1.*may not have sections.*ignored: 'foo'/) end it "logs a warning, but processes the main settings if there are any extraneous settings" do content << "dog=arf\n" content << "cat=mew\n" content << "[ignored]\n" content << "cow=moo\n" loader_from(:filesystem => [envdir, manifestdir, modulepath].flatten, :directory => envdir) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(manifestdir.path). with_modulepath(modulepath.map(&:path)). with_config_version(File.expand_path('/some/script')) end expect(@logs.map(&:to_s).join).to match(/Invalid.*at.*#{envdir}\/env1.*unknown setting.*dog, cat/) end it "interpretes relative paths from the environment's directory" do content = <<-EOF manifest=relative/manifest modulepath=relative/modules config_version=relative/script EOF envdir = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_regular_file_containing("environment.conf", content), FS::MemoryFile.a_missing_file("modules"), FS::MemoryFile.a_directory('relative', [ FS::MemoryFile.a_directory('modules'), ]), ]), ]) loader_from(:filesystem => [envdir], :directory => envdir) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(File.join(envdir, 'env1', 'relative', 'manifest')). with_modulepath([File.join(envdir, 'env1', 'relative', 'modules')]). with_config_version(File.join(envdir, 'env1', 'relative', 'script')) end end it "interpolates other setting values correctly" do modulepath = [ File.expand_path('/some/absolute'), '$basemodulepath', 'modules' ].join(File::PATH_SEPARATOR) content = <<-EOF manifest=$confdir/whackymanifests modulepath=#{modulepath} config_version=$vardir/random/scripts EOF some_absolute_dir = FS::MemoryFile.a_directory(File.expand_path('/some/absolute')) base_module_dirs = Puppet[:basemodulepath].split(File::PATH_SEPARATOR).map do |path| FS::MemoryFile.a_directory(path) end envdir = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_regular_file_containing("environment.conf", content), FS::MemoryFile.a_directory("modules"), ]), ]) loader_from(:filesystem => [envdir, some_absolute_dir, base_module_dirs].flatten, :directory => envdir) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(File.join(Puppet[:confdir], 'whackymanifests')). with_modulepath([some_absolute_dir.path, base_module_dirs.map { |d| d.path }, File.join(envdir, 'env1', 'modules')].flatten). with_config_version(File.join(Puppet[:vardir], 'random', 'scripts')) end end it "uses environment.conf settings regardless of existence of modules and manifests subdirectories" do envdir = FS::MemoryFile.a_directory(File.expand_path("envdir"), [ FS::MemoryFile.a_directory("env1", [ FS::MemoryFile.a_regular_file_containing("environment.conf", content), FS::MemoryFile.a_directory("modules"), FS::MemoryFile.a_directory("manifests"), ]), ]) loader_from(:filesystem => [envdir, manifestdir, modulepath].flatten, :directory => envdir) do |loader| expect(loader.get("env1")).to environment(:env1). with_manifest(manifestdir.path). with_modulepath(modulepath.map(&:path)). with_config_version(File.expand_path('/some/script')) end end end end describe "static loaders" do let(:static1) { Puppet::Node::Environment.create(:static1, []) } let(:static2) { Puppet::Node::Environment.create(:static2, []) } let(:loader) { Puppet::Environments::Static.new(static1, static2) } it "lists environments" do expect(loader.list).to eq([static1, static2]) end it "gets an environment" do expect(loader.get(:static2)).to eq(static2) end it "returns nil if env not found" do expect(loader.get(:doesnotexist)).to be_nil end it "gets a basic conf" do conf = loader.get_conf(:static1) expect(conf.modulepath).to eq('') expect(conf.manifest).to eq(:no_manifest) expect(conf.config_version).to be_nil end it "returns nil if you request a configuration from an env that doesn't exist" do expect(loader.get_conf(:doesnotexist)).to be_nil end context "that are private" do let(:private_env) { Puppet::Node::Environment.create(:private, []) } let(:loader) { Puppet::Environments::StaticPrivate.new(private_env) } it "lists nothing" do expect(loader.list).to eq([]) end end end RSpec::Matchers.define :environment do |name| match do |env| env.name == name && (!@manifest || @manifest == env.manifest) && (!@modulepath || @modulepath == env.modulepath) && (!@config_version || @config_version == env.config_version) end chain :with_manifest do |manifest| @manifest = manifest end chain :with_modulepath do |modulepath| @modulepath = modulepath end chain :with_config_version do |config_version| @config_version = config_version end description do "environment #{expected}" + (@manifest ? " with manifest #{@manifest}" : "") + (@modulepath ? " with modulepath [#{@modulepath.join(', ')}]" : "") + (@config_version ? " with config_version #{@config_version}" : "") end failure_message_for_should do |env| "expected <#{env.name}: modulepath = [#{env.modulepath.join(', ')}], manifest = #{env.manifest}, config_version = #{env.config_version}> to be #{description}" end end def loader_from(options, &block) FS.overlay(*options[:filesystem]) do environments = Puppet::Environments::Directories.new( options[:directory], options[:modulepath] || [] ) Puppet.override(:environments => environments) do yield environments end end end end +end diff --git a/spec/unit/face/config_spec.rb b/spec/unit/face/config_spec.rb index 0cbffe2cd..f42844827 100755 --- a/spec/unit/face/config_spec.rb +++ b/spec/unit/face/config_spec.rb @@ -1,144 +1,146 @@ #! /usr/bin/env ruby require 'spec_helper' require 'puppet/face' +module PuppetFaceSpecs describe Puppet::Face[:config, '0.0.1'] do FS = Puppet::FileSystem it "prints a single setting without the name" do Puppet[:trace] = true expect { subject.print("trace") }.to have_printed('true') end it "prints multiple settings with the names" do Puppet[:trace] = true Puppet[:syslogfacility] = "file" expect { subject.print("trace", "syslogfacility") }.to have_printed(<<-OUTPUT) trace = true syslogfacility = file OUTPUT end it "prints the setting from the selected section" do Puppet.settings.parse_config(<<-CONF) [other] syslogfacility = file CONF expect { subject.print("syslogfacility", :section => "other") }.to have_printed('file') end it "defaults to all when no arguments are given" do subject.expects(:puts).times(Puppet.settings.to_a.length) subject.print end it "prints out all of the settings when asked for 'all'" do subject.expects(:puts).times(Puppet.settings.to_a.length) subject.print('all') end shared_examples_for :config_printing_a_section do |section| def add_section_option(args, section) args << { :section => section } if section args end it "prints directory env settings for an env that exists" do FS.overlay( FS::MemoryFile.a_directory(File.expand_path("/dev/null/environments"), [ FS::MemoryFile.a_directory("production", [ FS::MemoryFile.a_missing_file("environment.conf"), ]), ]) ) do args = "environmentpath","manifest","modulepath","environment","basemodulepath" expect { subject.print(*add_section_option(args, section)) }.to have_printed(<<-OUTPUT) environmentpath = #{File.expand_path("/dev/null/environments")} manifest = #{File.expand_path("/dev/null/environments/production/manifests")} modulepath = #{File.expand_path("/dev/null/environments/production/modules")}#{File::PATH_SEPARATOR}#{File.expand_path("/some/base")} environment = production basemodulepath = #{File.expand_path("/some/base")} OUTPUT end end it "interpolates settings in environment.conf" do FS.overlay( FS::MemoryFile.a_directory(File.expand_path("/dev/null/environments"), [ FS::MemoryFile.a_directory("production", [ FS::MemoryFile.a_regular_file_containing("environment.conf", <<-CONTENT), modulepath=/custom/modules#{File::PATH_SEPARATOR}$basemodulepath CONTENT ]), ]) ) do args = "environmentpath","manifest","modulepath","environment","basemodulepath" expect { subject.print(*add_section_option(args, section)) }.to have_printed(<<-OUTPUT) environmentpath = #{File.expand_path("/dev/null/environments")} manifest = #{File.expand_path("/dev/null/environments/production/manifests")} modulepath = #{File.expand_path("/custom/modules")}#{File::PATH_SEPARATOR}#{File.expand_path("/some/base")} environment = production basemodulepath = #{File.expand_path("/some/base")} OUTPUT end end it "prints the default configured env settings for an env that does not exist" do Puppet[:environment] = 'doesnotexist' FS.overlay( FS::MemoryFile.a_directory(File.expand_path("/dev/null/environments"), [ FS::MemoryFile.a_missing_file("doesnotexist") ]) ) do args = "environmentpath","manifest","modulepath","environment","basemodulepath" expect { subject.print(*add_section_option(args, section)) }.to have_printed(<<-OUTPUT) environmentpath = #{File.expand_path("/dev/null/environments")} manifest = no_manifest modulepath = environment = doesnotexist basemodulepath = #{File.expand_path("/some/base")} OUTPUT end end end context "when printing environment settings" do before(:each) do Puppet.settings.stubs(:global_defaults_initialized?).returns(:true) end context "from main section" do before(:each) do Puppet.settings.parse_config(<<-CONF) [main] environmentpath=$confdir/environments basemodulepath=/some/base CONF end it_behaves_like :config_printing_a_section end context "from master section" do before(:each) do Puppet.settings.parse_config(<<-CONF) [master] environmentpath=$confdir/environments basemodulepath=/some/base CONF Puppet.settings.stubs(:global_defaults_initialized?).returns(:true) end it_behaves_like :config_printing_a_section, :master end end end +end diff --git a/spec/unit/indirector/facts/facter_spec.rb b/spec/unit/indirector/facts/facter_spec.rb index 5e86465b2..cb90fb7c3 100755 --- a/spec/unit/indirector/facts/facter_spec.rb +++ b/spec/unit/indirector/facts/facter_spec.rb @@ -1,201 +1,203 @@ #! /usr/bin/env ruby require 'spec_helper' require 'puppet/indirector/facts/facter' +module PuppetNodeFactsFacter describe Puppet::Node::Facts::Facter do FS = Puppet::FileSystem it "should be a subclass of the Code terminus" do Puppet::Node::Facts::Facter.superclass.should equal(Puppet::Indirector::Code) end it "should have documentation" do Puppet::Node::Facts::Facter.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::Facter.indirection.should equal(indirection) end it "should have its name set to :facter" do Puppet::Node::Facts::Facter.name.should == :facter end describe "when reloading Facter" do before do @facter_class = Puppet::Node::Facts::Facter Facter.stubs(:clear) Facter.stubs(:load) Facter.stubs(:loadfacts) end it "should clear Facter" do Facter.expects(:clear) @facter_class.reload_facter end it "should load all Facter facts" do Facter.expects(:loadfacts) @facter_class.reload_facter end end end describe Puppet::Node::Facts::Facter do before :each do Puppet::Node::Facts::Facter.stubs(:reload_facter) @facter = Puppet::Node::Facts::Facter.new Facter.stubs(:to_hash).returns({}) @name = "me" @request = stub 'request', :key => @name @environment = stub 'environment' @request.stubs(:environment).returns(@environment) @request.environment.stubs(:modules).returns([]) end describe Puppet::Node::Facts::Facter, " when finding facts" do it "should reset and load facts" do clear = sequence 'clear' Puppet::Node::Facts::Facter.expects(:reload_facter).in_sequence(clear) Puppet::Node::Facts::Facter.expects(:load_fact_plugins).in_sequence(clear) @facter.find(@request) end it "should include external facts when feature is present" do clear = sequence 'clear' Puppet.features.stubs(:external_facts?).returns(:true) Puppet::Node::Facts::Facter.expects(:setup_external_facts).in_sequence(clear) Puppet::Node::Facts::Facter.expects(:reload_facter).in_sequence(clear) Puppet::Node::Facts::Facter.expects(:load_fact_plugins).in_sequence(clear) @facter.find(@request) end it "should return a Facts instance" do @facter.find(@request).should be_instance_of(Puppet::Node::Facts) end it "should return a Facts instance with the provided key as the name" do @facter.find(@request).name.should == @name end it "should return the Facter facts as the values in the Facts instance" do Facter.expects(:to_hash).returns("one" => "two") facts = @facter.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) @facter.find(@request) end it "should convert facts into strings when stringify_facts is true" do Puppet[:stringify_facts] = true facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:stringify) @facter.find(@request) end it "should sanitize facts when stringify_facts is false" do Puppet[:stringify_facts] = false facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:sanitize) @facter.find(@request) end end describe Puppet::Node::Facts::Facter, " when saving facts" do it "should fail" do proc { @facter.save(@facts) }.should raise_error(Puppet::DevError) end end describe Puppet::Node::Facts::Facter, " when destroying facts" do it "should fail" do proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError) end end it "should skip files when asked to load a directory" do FileTest.expects(:directory?).with("myfile").returns false Puppet::Node::Facts::Facter.load_facts_in_dir("myfile") end it "should load each ruby file when asked to load a directory" do FileTest.expects(:directory?).with("mydir").returns true Dir.expects(:chdir).with("mydir").yields Dir.expects(:glob).with("*.rb").returns %w{a.rb b.rb} Puppet::Node::Facts::Facter.expects(:load).with("a.rb") Puppet::Node::Facts::Facter.expects(:load).with("b.rb") Puppet::Node::Facts::Facter.load_facts_in_dir("mydir") end it "should include pluginfactdest when loading external facts", :if => (Puppet.features.external_facts? and not Puppet.features.microsoft_windows?) do Puppet[:pluginfactdest] = "/plugin/dest" @facter.find(@request) Facter.search_external_path.include?("/plugin/dest") end it "should include pluginfactdest when loading external facts", :if => (Puppet.features.external_facts? and Puppet.features.microsoft_windows?) do Puppet[:pluginfactdest] = "/plugin/dest" @facter.find(@request) Facter.search_external_path.include?("C:/plugin/dest") end describe "when loading fact plugins from disk" do let(:one) { File.expand_path("one") } let(:two) { File.expand_path("two") } it "should load each directory in the Fact path" do Puppet[:factpath] = [one, two].join(File::PATH_SEPARATOR) Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with(one) Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with(two) Puppet::Node::Facts::Facter.load_fact_plugins end it "should load all facts from the modules" do Puppet::Node::Facts::Facter.stubs(:load_facts_in_dir) Dir.stubs(:glob).returns [] Dir.expects(:glob).with("#{one}/*/lib/facter").returns %w{oneA oneB} Dir.expects(:glob).with("#{two}/*/lib/facter").returns %w{twoA twoB} Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneA") Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneB") Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoA") Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoB") FS.overlay(FS::MemoryFile.a_directory(one), FS::MemoryFile.a_directory(two)) do Puppet.override(:current_environment => Puppet::Node::Environment.create(:testing, [one, two], "")) do Puppet::Node::Facts::Facter.load_fact_plugins end end end it "should include module plugin facts when present", :if => Puppet.features.external_facts? do mod = Puppet::Module.new("mymodule", "#{one}/mymodule", @request.environment) @request.environment.stubs(:modules).returns([mod]) @facter.find(@request) Facter.search_external_path.include?("#{one}/mymodule/facts.d") end end end +end