diff options
author | Andrew Parker <andy@puppetlabs.com> | 2014-01-27 14:23:20 -0800 |
---|---|---|
committer | Andrew Parker <andy@puppetlabs.com> | 2014-01-29 10:29:20 -0800 |
commit | 5c988b988840eddd4081c60f48e924dffe56d6d7 (patch) | |
tree | 2823daf3b8098d2b0066916504783781eadcd4d7 | |
parent | d474c2eebbaf642d21a4b14102b6b5b5d85f1194 (diff) | |
download | puppet-5c988b988840eddd4081c60f48e924dffe56d6d7.tar.gz |
(PUP-1118) Stop using Environment.new
Instead of creating environments directly, we need to go through the
configured environment loaders, otherwise environments can't come from
other places.
60 files changed, 406 insertions, 459 deletions
diff --git a/acceptance/tests/environment/use_environment_from_environmentdir.rb b/acceptance/tests/environment/use_environment_from_environmentdir.rb index dcf12e40c..4f2a95c40 100644 --- a/acceptance/tests/environment/use_environment_from_environmentdir.rb +++ b/acceptance/tests/environment/use_environment_from_environmentdir.rb @@ -2,7 +2,7 @@ test_name "Use environments from the environmentdir" testdir = master.tmpdir('use_environmentdir') -apply_manifest_on(master, <<-MANIFEST) +apply_manifest_on(master, <<-MANIFEST, :catch_failures => true) File { ensure => directory, owner => puppet, @@ -10,9 +10,10 @@ File { } file { + "#{testdir}":; "#{testdir}/environments":; "#{testdir}/environments/special":; - "#{testdir}/environments/special/manifest":; + "#{testdir}/environments/special/manifests":; "#{testdir}/environments/special/modules":; "#{testdir}/environments/special/modules/amod":; "#{testdir}/environments/special/modules/amod/manifests":; @@ -23,7 +24,10 @@ file { "#{testdir}/environments/special/modules/amod/manifests/init.pp": ensure => file, - content => 'class amod { notify { template: message => template("amod/our_template.erb") } file { "$agent_file_location/file": source => "puppet:///modules/amod/data" }' + content => 'class amod { + notify { template: message => template("amod/our_template.erb") } + file { "$agent_file_location/file": source => "puppet:///modules/amod/data" } + }' ; "#{testdir}/environments/special/modules/amod/lib/facter/environment_fact.rb": ensure => file, @@ -53,9 +57,13 @@ master_opts = { with_puppet_running_on master, master_opts, testdir do agents.each do |agent| atmp = agent.tmpdir('use_environmentdir') - on agent, puppet("agent", "--environment", "special", "-t", "--server", master, "--trace"), :environment => { - "FACTER_agent_file_location" => atmp - } do |result| + on agent, puppet("agent", + "--environment", "special", + "-t", + "--server", master, + "--trace", + 'ENV' => { "FACTER_agent_file_location" => atmp }), + :acceptable_exit_codes => [2] do |result| assert_match(/environment fact/, result.stdout) end diff --git a/api/schemas/environments.json b/api/schemas/environments.json index b9b1f15b7..01cfc4293 100644 --- a/api/schemas/environments.json +++ b/api/schemas/environments.json @@ -36,5 +36,5 @@ } } }, - "required": ["search_path", "environments"] + "required": ["search_paths", "environments"] } diff --git a/lib/puppet/application/doc.rb b/lib/puppet/application/doc.rb index 32a378e6a..99a2ad346 100644 --- a/lib/puppet/application/doc.rb +++ b/lib/puppet/application/doc.rb @@ -171,7 +171,7 @@ HELP exit_code = 0 files = [] unless @manifest - env = Puppet::Node::Environment.new + env = Puppet.lookup(:environments).get(Puppet[:environment]) files += env.modulepath files << ::File.dirname(env[:manifest]) end diff --git a/lib/puppet/context.rb b/lib/puppet/context.rb index 94ffab925..369027599 100644 --- a/lib/puppet/context.rb +++ b/lib/puppet/context.rb @@ -40,7 +40,7 @@ class Puppet::Context elsif block block.call else - raise UndefinedBindingError, "lookup of #{name} in #{@table.inspect} at top of #{@stack.inspect}" + raise UndefinedBindingError, "no '#{name}' in #{@table.inspect} at top of #{@stack.inspect}" end end diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index dfac70309..cb7c3f10a 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -199,7 +199,9 @@ module Puppet :environmentdir => { :default => "$confdir/environments", :desc => "A directory of environments", - :type => :directory + :type => :directory, + :owner => "service", + :group => "service", }, :diff_args => { :default => default_diffargs, diff --git a/lib/puppet/environments.rb b/lib/puppet/environments.rb index 3b805381b..80dec6d69 100644 --- a/lib/puppet/environments.rb +++ b/lib/puppet/environments.rb @@ -122,16 +122,20 @@ module Puppet::Environments def list base = Puppet::FileSystem.path_string(@environment_dir) - Puppet::FileSystem.children(@environment_dir).select do |child| - name = Puppet::FileSystem.basename_string(child) - Puppet::FileSystem.directory?(child) && - Puppet::Node::Environment.valid_name?(name) - end.collect do |child| - name = Puppet::FileSystem.basename_string(child) - Puppet::Node::Environment.create( - name.intern, - [File.join(base, name, "modules")] + @global_module_path, - File.join(base, name, "manifests")) + if Puppet::FileSystem.directory?(@environment_dir) + Puppet::FileSystem.children(@environment_dir).select do |child| + name = Puppet::FileSystem.basename_string(child) + Puppet::FileSystem.directory?(child) && + Puppet::Node::Environment.valid_name?(name) + end.collect do |child| + name = Puppet::FileSystem.basename_string(child) + Puppet::Node::Environment.create( + name.intern, + [File.join(base, name, "modules")] + @global_module_path, + File.join(base, name, "manifests")) + end + else + [] end end diff --git a/lib/puppet/face/node/clean.rb b/lib/puppet/face/node/clean.rb index 20d454e8a..dc1da367e 100644 --- a/lib/puppet/face/node/clean.rb +++ b/lib/puppet/face/node/clean.rb @@ -144,7 +144,7 @@ Puppet::Face.define(:node, '0.0.1') do end def environment - @environment ||= Puppet::Node::Environment.new + @environment ||= Puppet.lookup(:environments).get(Puppet[:environment]) end def type_is_ensurable(resource) diff --git a/lib/puppet/face/parser.rb b/lib/puppet/face/parser.rb index 4ba044f68..cb7aa229b 100644 --- a/lib/puppet/face/parser.rb +++ b/lib/puppet/face/parser.rb @@ -53,7 +53,7 @@ Puppet::Face.define(:parser, '0.0.1') do end def validate_manifest - Puppet::Node::Environment.new(Puppet[:environment]).known_resource_types.clear + Puppet.lookup(:environments).get(Puppet[:environment]).known_resource_types.clear rescue => detail Puppet.log_exception(detail) exit(1) diff --git a/lib/puppet/indirector/node/ldap.rb b/lib/puppet/indirector/node/ldap.rb index 0075b5b4b..37f9c22dd 100644 --- a/lib/puppet/indirector/node/ldap.rb +++ b/lib/puppet/indirector/node/ldap.rb @@ -35,7 +35,7 @@ class Puppet::Node::Ldap < Puppet::Indirector::Ldap next unless info = name2hash(name) merge_parent(info) if info[:parent] - info[:environment] ||= request.environment.to_s + info[:environment] ||= request.environment node = info2node(request.key, info) break end @@ -59,7 +59,7 @@ class Puppet::Node::Ldap < Puppet::Indirector::Ldap return infos.collect do |info| merge_parent(info) if info[:parent] - info[:environment] ||= request.environment.to_s + info[:environment] ||= request.environment info2node(info[:name], info) end end diff --git a/lib/puppet/indirector/request.rb b/lib/puppet/indirector/request.rb index fa40d3a03..cd451745c 100644 --- a/lib/puppet/indirector/request.rb +++ b/lib/puppet/indirector/request.rb @@ -78,14 +78,14 @@ class Puppet::Indirector::Request end def environment - @environment ||= Puppet::Node::Environment.new + @environment ||= Puppet.lookup(:environments).get(Puppet[:environment]) end def environment=(env) @environment = if env.is_a?(Puppet::Node::Environment) env else - Puppet::Node::Environment.new(env) + Puppet.lookup(:environments).get(env) end end diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index ea2eaadcd..fbae86344 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -241,6 +241,6 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus end def environment - Puppet::Node::Environment.new + Puppet.lookup(:environments).get(Puppet[:environment]) end end diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb index f562fb414..09661a85b 100644 --- a/lib/puppet/module.rb +++ b/lib/puppet/module.rb @@ -27,7 +27,8 @@ class Puppet::Module # of +path+, return +nil+ def self.find(modname, environment = nil) return nil unless modname - Puppet::Node::Environment.new(environment).module(modname) + env = Puppet.lookup(:environments).get(environment || Puppet[:environment]) + env.module(modname) end attr_reader :name, :environment, :path diff --git a/lib/puppet/module_tool/applications/upgrader.rb b/lib/puppet/module_tool/applications/upgrader.rb index 36ccb8021..cb9d0fa54 100644 --- a/lib/puppet/module_tool/applications/upgrader.rb +++ b/lib/puppet/module_tool/applications/upgrader.rb @@ -6,7 +6,7 @@ module Puppet::ModuleTool def initialize(name, forge, options) @action = :upgrade - @environment = Puppet::Node::Environment.new(Puppet.settings[:environment]) + @environment = Puppet.lookup(:environments).get(Puppet[:environment]) @module_name = name @options = options @force = options[:force] diff --git a/lib/puppet/network/http/api/v1.rb b/lib/puppet/network/http/api/v1.rb index 28540f0f2..79a7ddcbe 100644 --- a/lib/puppet/network/http/api/v1.rb +++ b/lib/puppet/network/http/api/v1.rb @@ -63,7 +63,8 @@ class Puppet::Network::HTTP::API::V1 method = indirection_method(http_method, indirection) - params[:environment] = Puppet::Node::Environment.new(environment) + params[:environment] = Puppet.lookup(:environments).get(environment) + params.delete(:bucket_path) raise ArgumentError, "No request key specified in #{uri}" if key == "" or key.nil? diff --git a/lib/puppet/network/http/api/v2/environments.rb b/lib/puppet/network/http/api/v2/environments.rb index 2be83bb72..de31a8e8d 100644 --- a/lib/puppet/network/http/api/v2/environments.rb +++ b/lib/puppet/network/http/api/v2/environments.rb @@ -7,7 +7,7 @@ class Puppet::Network::HTTP::API::V2::Environments def call(request, response) response.respond_with(200, "application/json", JSON.dump({ - "search_path" => @env_loader.search_paths, + "search_paths" => @env_loader.search_paths, "environments" => Hash[@env_loader.list.collect do |env| [env.name, { "modules" => Hash[env.modules.collect do |mod| diff --git a/lib/puppet/network/rights.rb b/lib/puppet/network/rights.rb index d81473bd1..d7275babf 100644 --- a/lib/puppet/network/rights.rb +++ b/lib/puppet/network/rights.rb @@ -187,8 +187,8 @@ class Rights @methods << m end - def restrict_environment(env) - env = Puppet::Node::Environment.new(env) + def restrict_environment(environment) + env = Puppet.lookup(:environments).get(environment) raise ArgumentError, "'#{env}' is already in the '#{name}' ACL" if @environment.include?(env) @environment << env diff --git a/lib/puppet/node.rb b/lib/puppet/node.rb index 09b307927..4577ea7e3 100644 --- a/lib/puppet/node.rb +++ b/lib/puppet/node.rb @@ -9,9 +9,6 @@ class Puppet::Node # the node sources. extend Puppet::Indirector - # Adds the environment getter and setter, with some instance/string conversion - include Puppet::Node::Environment::Helper - # Use the node source as the indirection terminus. indirects :node, :terminus_setting => :node_terminus, :doc => "Where to find node information. A node is composed of its name, its facts, and its environment." @@ -53,15 +50,22 @@ class Puppet::Node end def environment - return super if @environment - - if env = parameters["environment"] + if @environment + @environment + elsif env = parameters["environment"] self.environment = env - return super + @environment + else + Puppet.lookup(:environments).get(Puppet[:environment]) end + end - # Else, return the default - Puppet::Node::Environment.new + def environment=(env) + if env.is_a?(String) or env.is_a?(Symbol) + @environment = Puppet.lookup(:environments).get(env) + else + @environment = env + end end def initialize(name, options = {}) diff --git a/lib/puppet/node/environment.rb b/lib/puppet/node/environment.rb index 151ee7dca..163d0d9a4 100644 --- a/lib/puppet/node/environment.rb +++ b/lib/puppet/node/environment.rb @@ -44,26 +44,6 @@ end # when {Puppet::Parser::Functions.reset} is called. class Puppet::Node::Environment - # This defines a mixin for classes that have an environment. It implements - # `environment` and `environment=` that respects the semantics of the - # Puppet::Node::Environment class - # - # @api public - module Helper - - def environment - Puppet::Node::Environment.new(@environment) - end - - def environment=(env) - if env.is_a?(String) or env.is_a?(Symbol) - @environment = env - else - @environment = env.name - end - end - end - include Puppet::Util::Cacher # @api private diff --git a/lib/puppet/parser/files.rb b/lib/puppet/parser/files.rb index 809968547..605bbeb69 100644 --- a/lib/puppet/parser/files.rb +++ b/lib/puppet/parser/files.rb @@ -15,7 +15,7 @@ module Puppet; module Parser; module Files def find_manifests_in_modules(pattern, environment) module_name, file_pattern = split_file_path(pattern) begin - if mod = Puppet::Module.find(module_name, environment) + if mod = environment.module(module_name) return [mod.name, mod.match_manifests(file_pattern)] end rescue Puppet::Module::InvalidName @@ -31,7 +31,9 @@ module Puppet; module Parser; module Files # module. # In all cases, an absolute path is returned, which does not # necessarily refer to an existing file - def find_template(template, environment = nil) + # + # @api private + def find_template(template, environment) if template == File.expand_path(template) return template end @@ -53,7 +55,8 @@ module Puppet; module Parser; module Files nil end - def find_template_in_module(template, environment = nil) + # @api private + def find_template_in_module(template, environment) path, file = split_file_path(template) # Because templates don't have an assumed template name, like manifests do, @@ -61,7 +64,7 @@ module Puppet; module Parser; module Files # directory. return nil unless file - if mod = Puppet::Module.find(path, environment) and t = mod.template(file) + if mod = environment.module(path) and t = mod.template(file) return t end nil @@ -69,8 +72,9 @@ module Puppet; module Parser; module Files # Return an array of paths by splitting the +templatedir+ config # parameter. - def templatepath(environment = nil) - dirs = Puppet.settings.value(:templatedir, environment).split(File::PATH_SEPARATOR) + # @api private + def templatepath(environment) + dirs = Puppet.settings.value(:templatedir, environment.to_s).split(File::PATH_SEPARATOR) dirs.select do |p| File::directory?(p) end @@ -78,6 +82,7 @@ module Puppet; module Parser; module Files # Split the path into the module and the rest of the path, or return # nil if the path is empty or absolute (starts with a /). + # @api private def split_file_path(path) if path == "" or Puppet::Util.absolute_path?(path) nil diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb index 2ceb2d4f0..45928d193 100644 --- a/lib/puppet/parser/functions.rb +++ b/lib/puppet/parser/functions.rb @@ -41,10 +41,7 @@ module Puppet::Parser::Functions # environment # # @api private - def self.environment_module(env = Puppet.lookup(:current_environment)) - if env and ! env.is_a?(Puppet::Node::Environment) - env = Puppet::Node::Environment.new(env) - end + def self.environment_module(env) @modules[env.name] ||= Module.new end @@ -132,10 +129,10 @@ module Puppet::Parser::Functions # the block must be installed as a method because it may use "return", # which is not allowed from procs. real_fname = "real_function_#{name}" - environment_module.send(:define_method, real_fname, &block) + environment_module(environment).send(:define_method, real_fname, &block) fname = "function_#{name}" - environment_module.send(:define_method, fname) do |*args| + environment_module(environment).send(:define_method, fname) do |*args| Puppet::Util::Profiler.profile("Called #{name}") do if args[0].is_a? Array if arity >= 0 and args[0].size != arity diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb index dedae1bab..963e4d106 100644 --- a/lib/puppet/parser/parser_support.rb +++ b/lib/puppet/parser/parser_support.rb @@ -121,8 +121,7 @@ class Puppet::Parser::Parser end def initialize(env) - # The environment is needed to know how to find the resource type collection. - @environment = env.is_a?(String) ? Puppet::Node::Environment.new(env) : env + @environment = env initvars end diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb index fc5721626..e4426cdf9 100644 --- a/lib/puppet/parser/templatewrapper.rb +++ b/lib/puppet/parser/templatewrapper.rb @@ -84,7 +84,7 @@ class Puppet::Parser::TemplateWrapper # @api private def file=(filename) - unless @__file__ = Puppet::Parser::Files.find_template(filename, scope.compiler.environment.to_s) + unless @__file__ = Puppet::Parser::Files.find_template(filename, scope.compiler.environment) raise Puppet::ParseError, "Could not find template '#{filename}'" end diff --git a/lib/puppet/parser/type_loader.rb b/lib/puppet/parser/type_loader.rb index 9e03fd8af..398944996 100644 --- a/lib/puppet/parser/type_loader.rb +++ b/lib/puppet/parser/type_loader.rb @@ -5,7 +5,6 @@ require 'puppet/parser/parser_factory' class Puppet::Parser::TypeLoader extend Forwardable - include Puppet::Node::Environment::Helper # Import manifest files that match a given file glob pattern. # @@ -50,6 +49,18 @@ class Puppet::Parser::TypeLoader self.environment = env end + def environment + @environment + end + + def environment=(env) + if env.is_a?(String) or env.is_a?(Symbol) + @environment = Puppet.lookup(:environments).get(env) + else + @environment = env + end + end + # Try to load the object with the given fully qualified name. def try_load_fqname(type, fqname) return nil if fqname == "" # special-case main. diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb index 22df43a67..bc8625750 100644 --- a/lib/puppet/resource.rb +++ b/lib/puppet/resource.rb @@ -15,9 +15,6 @@ class Puppet::Resource include Puppet::Util::Tagging - require 'puppet/resource/type_collection_helper' - include Puppet::Resource::TypeCollectionHelper - extend Puppet::Util::Pson include Enumerable attr_accessor :file, :line, :catalog, :exported, :virtual, :validate_parameters, :strict @@ -170,23 +167,6 @@ class Puppet::Resource super || parameters.keys.include?( parameter_name(parameter) ) end - # These two methods are extracted into a Helper - # module, but file load order prevents me - # from including them in the class, and I had weird - # behaviour (i.e., sometimes it didn't work) when - # I directly extended each resource with the helper. - def environment - Puppet::Node::Environment.new(@environment) - end - - def environment=(env) - if env.is_a?(String) or env.is_a?(Symbol) - @environment = env - else - @environment = env.name - end - end - %w{exported virtual strict}.each do |m| define_method(m+"?") do self.send(m) @@ -248,13 +228,21 @@ class Puppet::Resource def resource_type @rstype ||= case type - when "Class"; known_resource_types.hostclass(title == :main ? "" : title) - when "Node"; known_resource_types.node(title) + when "Class"; environment.known_resource_types.hostclass(title == :main ? "" : title) + when "Node"; environment.known_resource_types.node(title) else - Puppet::Type.type(type) || known_resource_types.definition(type) + Puppet::Type.type(type) || environment.known_resource_types.definition(type) end end + def environment + @environment ||= Puppet.lookup(:environments).get(Puppet[:environment]) + end + + def environment=(environment) + @environment = environment + end + # Produce a simple hash of our parameters. def to_hash parse_title.merge parameters diff --git a/lib/puppet/resource/type_collection.rb b/lib/puppet/resource/type_collection.rb index d212199dc..21a953eee 100644 --- a/lib/puppet/resource/type_collection.rb +++ b/lib/puppet/resource/type_collection.rb @@ -17,7 +17,7 @@ class Puppet::Resource::TypeCollection end def initialize(env) - @environment = env.is_a?(String) ? Puppet::Node::Environment.new(env) : env + @environment = env @hostclasses = {} @definitions = {} @nodes = {} diff --git a/lib/puppet/test/test_helper.rb b/lib/puppet/test/test_helper.rb index 545bae5d9..55b952c33 100644 --- a/lib/puppet/test/test_helper.rb +++ b/lib/puppet/test/test_helper.rb @@ -1,5 +1,8 @@ require 'puppet/indirector/data_binding/hiera' +require 'tmpdir' +require 'fileutils' + module Puppet::Test # This class is intended to provide an API to be used by external projects # when they are running tests that depend on puppet core. This should @@ -33,12 +36,20 @@ module Puppet::Test # that call Puppet. # @return nil def self.initialize() + owner = Process.pid + @environmentdir = Dir.mktmpdir('environments') Puppet.push_context(Puppet.base_context({ - :environmentdir => "/dev/null", + :environmentdir => @environmentdir, :modulepath => "", :manifest => "/dev/null" }), "Initial for specs") Puppet::Parser::Functions.reset + + ObjectSpace.define_finalizer(Puppet.lookup(:environments), proc { + if Process.pid == owner + FileUtils.rm_rf(@environmentdir) + end + }) end # Call this method once, when beginning a test run--prior to running @@ -192,6 +203,13 @@ module Puppet::Test # below 512 bits. Sad, really, because a 0 bit key would be just fine. Puppet[:req_bits] = 512 Puppet[:keylength] = 512 + + # Although we setup a testing context during initialization, some tests + # will end up creating their own context using the real context objects + # and use the setting for the environments. In order to avoid those tests + # having to deal with a missing environmentdir we can just set it right + # here. + Puppet[:environmentdir] = @environmentdir end private_class_method :initialize_settings_before_each end diff --git a/lib/puppet/util/autoload.rb b/lib/puppet/util/autoload.rb index 04e2af7bc..c0429599b 100644 --- a/lib/puppet/util/autoload.rb +++ b/lib/puppet/util/autoload.rb @@ -43,7 +43,8 @@ class Puppet::Util::Autoload name = cleanpath(name).chomp('.rb') return true unless loaded.include?(name) file, old_mtime = loaded[name] - return true unless file == get_file(name) + environment = Puppet.lookup(:environments).get(Puppet[:environment]) + return true unless file == get_file(name, environment) begin old_mtime.to_i != File.mtime(file).to_i rescue Errno::ENOENT @@ -53,7 +54,7 @@ class Puppet::Util::Autoload # Load a single plugin by name. We use 'load' here so we can reload a # given plugin. - def load_file(name, env=nil) + def load_file(name, env) file = get_file(name.to_s, env) return false unless file begin @@ -73,24 +74,24 @@ class Puppet::Util::Autoload # Load every instance of everything we can find. files_to_load(path).each do |file| name = file.chomp(".rb") - load_file(name) unless loaded?(name) + load_file(name, nil) unless loaded?(name) end end def reload_changed - loaded.keys.each { |file| load_file(file) if changed?(file) } + loaded.keys.each { |file| load_file(file, nil) if changed?(file) } end # Get the correct file to load for a given path # returns nil if no file is found - def get_file(name, env=nil) + def get_file(name, env) name = name + '.rb' unless name =~ /\.rb$/ path = search_directories(env).find { |dir| Puppet::FileSystem.exist?(File.join(dir, name)) } path and File.join(path, name) end def files_to_load(path) - search_directories.map {|dir| files_in_dir(dir, path) }.flatten.uniq + search_directories(nil).map {|dir| files_in_dir(dir, path) }.flatten.uniq end def files_in_dir(dir, path) @@ -100,13 +101,7 @@ class Puppet::Util::Autoload end end - def module_directories(env=nil) - # We have to require this late in the process because otherwise we might - # have load order issues. Since require is much slower than defined?, we - # can skip that - and save some 2,155 invocations of require in my real - # world testing. --daniel 2012-07-10 - require 'puppet/node/environment' unless defined?(Puppet::Node::Environment) - + def module_directories(env) # We're using a per-thread cache of module directories so that we don't # scan the filesystem each time we try to load something. This is reset # at the beginning of compilation and at the end of an agent run. @@ -133,10 +128,10 @@ class Puppet::Util::Autoload # "app_defaults_initialized?" method on the main puppet Settings object. # --cprice 2012-03-16 if Puppet.settings.app_defaults_initialized? - real_env = Puppet::Node::Environment.new(env) + env ||= Puppet.lookup(:environments).get(Puppet[:environment]) # if the app defaults have been initialized then it should be safe to access the module path setting. - $env_module_directories[real_env] ||= real_env.modulepath.collect do |dir| + $env_module_directories[env] ||= env.modulepath.collect do |dir| Dir.entries(dir).reject { |f| f =~ /^\./ }.collect { |f| File.join(dir, f, "lib") } end.flatten.find_all do |d| FileTest.directory?(d) @@ -161,7 +156,7 @@ class Puppet::Util::Autoload gem_source.directories end - def search_directories(env=nil) + def search_directories(env) [gem_directories, module_directories(env), libdirs(), $LOAD_PATH].flatten end @@ -196,7 +191,7 @@ class Puppet::Util::Autoload @wrap = true unless defined?(@wrap) end - def load(name, env=nil) + def load(name, env = nil) self.class.load_file(expand(name), env) end diff --git a/lib/puppet/util/command_line.rb b/lib/puppet/util/command_line.rb index 5713cc7df..35a38f5c3 100644 --- a/lib/puppet/util/command_line.rb +++ b/lib/puppet/util/command_line.rb @@ -126,7 +126,7 @@ module Puppet # we try to restrict to only code that can be autoloaded from the node's # environment. if @subcommand_name != 'master' and @subcommand_name != 'agent' - Puppet::Node::Environment.new.each_plugin_directory do |dir| + Puppet.lookup(:environments).get(Puppet[:environment]).each_plugin_directory do |dir| $LOAD_PATH << dir unless $LOAD_PATH.include?(dir) end end diff --git a/lib/puppet/util/instance_loader.rb b/lib/puppet/util/instance_loader.rb index c6d6b71d4..fe0036b82 100644 --- a/lib/puppet/util/instance_loader.rb +++ b/lib/puppet/util/instance_loader.rb @@ -65,6 +65,7 @@ module Puppet::Util::InstanceLoader name = name.intern return nil unless instances = instance_hash(type) unless instances.include? name +# require 'debugger'; debugger if instance_loader(type).load(name) unless instances.include? name Puppet.warning( diff --git a/lib/puppet/util/rdoc.rb b/lib/puppet/util/rdoc.rb index 49784956b..9119784e7 100644 --- a/lib/puppet/util/rdoc.rb +++ b/lib/puppet/util/rdoc.rb @@ -47,7 +47,7 @@ module Puppet::Util::RDoc def manifestdoc(files) Puppet[:ignoreimport] = true files.select { |f| FileTest.file?(f) }.each do |f| - parser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new(Puppet[:environment])) + parser = Puppet::Parser::Parser.new(Puppet.lookup(:environments).get(Puppet[:environment])) parser.file = f ast = parser.parse output(f, ast) diff --git a/lib/puppet/util/rdoc/parser/puppet_parser_core.rb b/lib/puppet/util/rdoc/parser/puppet_parser_core.rb index 0cd1ee5cc..baff91e98 100644 --- a/lib/puppet/util/rdoc/parser/puppet_parser_core.rb +++ b/lib/puppet/util/rdoc/parser/puppet_parser_core.rb @@ -24,20 +24,20 @@ module RDoc::PuppetParserCore # main entry point def scan - environment = Puppet::Node::Environment.new - @known_resource_types = environment.known_resource_types - unless environment.known_resource_types.watching_file?(@input_file_name) + environment = Puppet.lookup(:environments).get(Puppet[:environment]) + known_resource_types = environment.known_resource_types + unless known_resource_types.watching_file?(@input_file_name) Puppet.info "rdoc: scanning #{@input_file_name}" if @input_file_name =~ /\.pp$/ @parser = Puppet::Parser::Parser.new(environment) @parser.file = @input_file_name @parser.parse.instantiate('').each do |type| - @known_resource_types.add type + known_resource_types.add type end end end - scan_top_level(@top_level) + scan_top_level(@top_level, environment) @top_level end @@ -76,7 +76,7 @@ module RDoc::PuppetParserCore # if it does, it returns the module name, otherwise if we are sure # it is part of the global manifest path, "__site__" is returned. # And finally if this path couldn't be mapped anywhere, nil is returned. - def split_module(path) + def split_module(path, environment) # find a module fullpath = File.expand_path(path) Puppet.debug "rdoc: testing #{fullpath}" @@ -84,7 +84,7 @@ module RDoc::PuppetParserCore modpath = $1 name = $2 Puppet.debug "rdoc: module #{name} into #{modpath} ?" - Puppet::Node::Environment.new.modulepath.each do |mp| + environment.modulepath.each do |mp| if File.identical?(modpath,mp) Puppet.debug "rdoc: found module #{name}" return name @@ -95,7 +95,7 @@ module RDoc::PuppetParserCore # there can be paths we don't want to scan under modules # imagine a ruby or manifest that would be distributed as part as a module # but we don't want those to be hosted under <site> - Puppet::Node::Environment.new.modulepath.each do |mp| + environment.modulepath.each do |mp| # check that fullpath is a descendant of mp dirname = fullpath previous = dirname @@ -111,7 +111,7 @@ module RDoc::PuppetParserCore end # create documentation for the top level +container+ - def scan_top_level(container) + def scan_top_level(container, environment) # use the module README as documentation for the module comment = "" %w{README README.rdoc}.each do |rfile| @@ -121,7 +121,7 @@ module RDoc::PuppetParserCore look_for_directives_in(container, comment) unless comment.empty? # infer module name from directory - name = split_module(@input_file_name) + name = split_module(@input_file_name, environment) if name.nil? # skip .pp files that are not in manifests directories as we can't guarantee they're part # of a module or the global configuration. @@ -140,7 +140,7 @@ module RDoc::PuppetParserCore mod.add_comment(comment, @input_file_name) if @input_file_name =~ /\.pp$/ - parse_elements(mod) + parse_elements(mod, environment.known_resource_types) elsif @input_file_name =~ /\.rb$/ parse_plugins(mod) end @@ -323,10 +323,10 @@ module RDoc::PuppetParserCore # Traverse the AST tree and produce code-objects node # that contains the documentation - def parse_elements(container) + def parse_elements(container, known_resource_types) Puppet.debug "rdoc: scanning manifest" - @known_resource_types.hostclasses.values.sort { |a,b| a.name <=> b.name }.each do |klass| + known_resource_types.hostclasses.values.sort { |a,b| a.name <=> b.name }.each do |klass| name = klass.name if klass.file == @input_file_name unless name.empty? @@ -339,13 +339,13 @@ module RDoc::PuppetParserCore end end - @known_resource_types.definitions.each do |name, define| + known_resource_types.definitions.each do |name, define| if define.file == @input_file_name document_define(name,define,container) end end - @known_resource_types.nodes.each do |name, node| + known_resource_types.nodes.each do |name, node| if node.file == @input_file_name document_node(name.to_s,node,container) end diff --git a/spec/integration/agent/logging_spec.rb b/spec/integration/agent/logging_spec.rb index 284928ed7..c686397c8 100755 --- a/spec/integration/agent/logging_spec.rb +++ b/spec/integration/agent/logging_spec.rb @@ -94,7 +94,7 @@ describe 'agent logging' do # # It's not something we are specifically testing here since it occurs # regardless of user flags. - Puppet::Util::Log.expects(:newdestination).with(instance_of(Puppet::Transaction::Report)).once + Puppet::Util::Log.expects(:newdestination).with(instance_of(Puppet::Transaction::Report)).at_least_once expected[:loggers].each do |logclass| Puppet::Util::Log.expects(:newdestination).with(logclass).at_least_once end diff --git a/spec/integration/indirector/file_content/file_server_spec.rb b/spec/integration/indirector/file_content/file_server_spec.rb index bfa2f2017..d35d60b44 100755 --- a/spec/integration/indirector/file_content/file_server_spec.rb +++ b/spec/integration/indirector/file_content/file_server_spec.rb @@ -28,10 +28,9 @@ describe Puppet::Indirector::FileContent::FileServer, " when finding files" do Puppet.settings[:modulepath] = "/no/such/file" - env = Puppet::Node::Environment.new("foo") - env.stubs(:modulepath).returns [path] + env = Puppet::Node::Environment.create(:foo, [path], '') - result = Puppet::FileServing::Content.indirection.search("plugins", :environment => "foo", :recurse => true) + result = Puppet::FileServing::Content.indirection.search("plugins", :environment => env, :recurse => true) result.should_not be_nil result.length.should == 2 diff --git a/spec/integration/node/environment_spec.rb b/spec/integration/node/environment_spec.rb index c7d9c91a1..105c70944 100755 --- a/spec/integration/node/environment_spec.rb +++ b/spec/integration/node/environment_spec.rb @@ -7,6 +7,13 @@ require 'puppet_spec/scope' describe Puppet::Node::Environment do include PuppetSpec::Files + def a_module_in(name, dir) + Dir.mkdir(dir) + moddir = File.join(dir, name) + Dir.mkdir(moddir) + moddir + end + it "should be able to return each module from its environment with the environment, name, and path set correctly" do base = tmpfile("env_modules") Dir.mkdir(base) @@ -16,15 +23,11 @@ describe Puppet::Node::Environment do %w{1 2}.each do |num| dir = File.join(base, "dir#{num}") dirs << dir - Dir.mkdir(dir) - mod = "mod#{num}" - moddir = File.join(dir, mod) - mods[mod] = moddir - Dir.mkdir(moddir) + + mods["mod#{num}"] = a_module_in("mod#{num}", dir) end - environment = Puppet::Node::Environment.new("foo") - environment.stubs(:modulepath).returns dirs + environment = Puppet::Node::Environment.create(:foo, dirs, '') environment.modules.each do |mod| mod.environment.should == environment @@ -37,19 +40,14 @@ describe Puppet::Node::Environment do Dir.mkdir(base) dirs = [] - mods = {} %w{1 2}.each do |num| dir = File.join(base, "dir#{num}") dirs << dir - Dir.mkdir(dir) - mod = "mod" - moddir = File.join(dir, mod) - mods[mod] = moddir - Dir.mkdir(moddir) + + a_module_in("mod", dir) end - environment = Puppet::Node::Environment.new("foo") - environment.stubs(:modulepath).returns dirs + environment = Puppet::Node::Environment.create(:foo, dirs, '') mods = environment.modules mods.length.should == 1 diff --git a/spec/integration/resource/type_collection_spec.rb b/spec/integration/resource/type_collection_spec.rb index db2612ed0..6349460be 100755 --- a/spec/integration/resource/type_collection_spec.rb +++ b/spec/integration/resource/type_collection_spec.rb @@ -10,11 +10,10 @@ describe Puppet::Resource::TypeCollection do before do @dir = tmpfile("autoload_testing") - Puppet[:modulepath] = @dir - FileUtils.mkdir_p @dir - @code = Puppet::Resource::TypeCollection.new("env") - Puppet::Node::Environment.new("env").stubs(:known_resource_types).returns @code + + environment = Puppet::Node::Environment.create(:env, [@dir], '') + @code = environment.known_resource_types end # Setup a module. diff --git a/spec/lib/puppet_spec/modules.rb b/spec/lib/puppet_spec/modules.rb index 1b75bb23a..6835e4434 100644 --- a/spec/lib/puppet_spec/modules.rb +++ b/spec/lib/puppet_spec/modules.rb @@ -4,7 +4,7 @@ module PuppetSpec::Modules module_dir = File.join(dir, name) FileUtils.mkdir_p(module_dir) - environment = Puppet::Node::Environment.new(options[:environment]) + environment = options[:environment] if metadata = options[:metadata] metadata[:source] ||= 'github' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9b7af8552..ee7b37d8a 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -164,10 +164,10 @@ RSpec.configure do |config| config.instance_variable_get(:@files_to_run).each { |f| logfile.puts f } end end - # Clean up switch of TMPDIR, don't know if needed after this, so needs to reset it - # to old before removing it + + # return to original tmpdir ENV['TMPDIR'] = oldtmpdir - FileUtils.rm_rf(tmpdir) if Puppet::FileSystem.exist?(tmpdir) && tmpdir.to_s.start_with?(oldtmpdir) + FileUtils.rm_rf(tmpdir) end if ENV['PROFILE'] diff --git a/spec/unit/face/module/list_spec.rb b/spec/unit/face/module/list_spec.rb index 8ac7e8523..9fc4e7121 100644 --- a/spec/unit/face/module/list_spec.rb +++ b/spec/unit/face/module/list_spec.rb @@ -39,15 +39,17 @@ describe "puppet module list" do barmod1 = PuppetSpec::Modules.create('bar', @modpath1) foomod2 = PuppetSpec::Modules.create('foo', @modpath2) - env = Puppet::Node::Environment.new + usedenv = Puppet::Node::Environment.create(:useme, [@modpath1, @modpath2], '') - Puppet::Face[:module, :current].list.should == { - @modpath1 => [ - Puppet::Module.new('bar', barmod1.path, env), - Puppet::Module.new('foo', foomod1.path, env) - ], - @modpath2 => [Puppet::Module.new('foo', foomod2.path, env)] - } + Puppet.override(:environments => Puppet::Environments::Static.new(usedenv)) do + Puppet::Face[:module, :current].list(:environment => 'useme').should == { + @modpath1 => [ + Puppet::Module.new('bar', barmod1.path, usedenv), + Puppet::Module.new('foo', foomod1.path, usedenv) + ], + @modpath2 => [Puppet::Module.new('foo', foomod2.path, usedenv)] + } + end end it "should use the specified environment" do diff --git a/spec/unit/indirector/node/active_record_spec.rb b/spec/unit/indirector/node/active_record_spec.rb index 2365dcb3b..ac3c98bc4 100755 --- a/spec/unit/indirector/node/active_record_spec.rb +++ b/spec/unit/indirector/node/active_record_spec.rb @@ -9,7 +9,7 @@ describe "Puppet::Node::ActiveRecord", :if => Puppet.features.rails? && Puppet.f let(:nodename) { "mynode" } let(:fact_values) { {:afact => "a value"} } let(:facts) { Puppet::Node::Facts.new(nodename, fact_values) } - let(:environment) { Puppet::Node::Environment.new("myenv") } + let(:environment) { Puppet::Node::Environment.create(:myenv, [], '') } let(:request) { Puppet::Indirector::Request.new(:node, :find, nodename, nil, :environment => environment) } let(:node_indirection) { Puppet::Node::ActiveRecord.new } diff --git a/spec/unit/indirector/node/ldap_spec.rb b/spec/unit/indirector/node/ldap_spec.rb index 42c08f72b..c19a30150 100755 --- a/spec/unit/indirector/node/ldap_spec.rb +++ b/spec/unit/indirector/node/ldap_spec.rb @@ -6,7 +6,7 @@ require 'puppet/indirector/node/ldap' describe Puppet::Node::Ldap do let(:nodename) { "mynode.domain.com" } let(:node_indirection) { Puppet::Node::Ldap.new } - let(:environment) { Puppet::Node::Environment.new("myenv") } + let(:environment) { Puppet::Node::Environment.create(:myenv, [], '') } let(:fact_values) { {:afact => "a value", "one" => "boo"} } let(:facts) { Puppet::Node::Facts.new(nodename, fact_values) } @@ -176,10 +176,13 @@ describe Puppet::Node::Ldap do end it "should set the node's environment to the environment of the results" do - result_env = Puppet::Node::Environment.new("local_test") + result_env = Puppet::Node::Environment.create(:local_test, [], '') Puppet::Node::Facts.indirection.stubs(:find).with(nodename, :environment => result_env).returns(facts) @result[:environment] = "local_test" - node_indirection.find(request).environment.should == result_env + + Puppet.override(:environments => Puppet::Environments::Static.new(result_env)) do + node_indirection.find(request).environment.should == result_env + end end it "should retain false parameter values" do @@ -256,17 +259,20 @@ describe Puppet::Node::Ldap do end it "should use the parent's environment if the node has none" do - env = Puppet::Node::Environment.new("parent") + env = Puppet::Node::Environment.create(:parent, [], '') @entry[:parent] = "parent" @parent[:environment] = "parent" Puppet::Node::Facts.indirection.stubs(:find).with(nodename, :environment => env).returns(facts) - node_indirection.find(request).environment.should == env + + Puppet.override(:environments => Puppet::Environments::Static.new(env)) do + node_indirection.find(request).environment.should == env + end end it "should prefer the node's environment to the parent's" do - child_env = Puppet::Node::Environment.new("child") + child_env = Puppet::Node::Environment.create(:child, [], '') @entry[:parent] = "parent" @entry[:environment] = "child" @@ -274,7 +280,10 @@ describe Puppet::Node::Ldap do Puppet::Node::Facts.indirection.stubs(:find).with(nodename, :environment => child_env).returns(facts) - node_indirection.find(request).environment.should == child_env + Puppet.override(:environments => Puppet::Environments::Static.new(child_env)) do + + node_indirection.find(request).environment.should == child_env + end end it "should recursively look up parent information" do diff --git a/spec/unit/indirector/node/plain_spec.rb b/spec/unit/indirector/node/plain_spec.rb index 15cef217d..8e1d0decf 100755 --- a/spec/unit/indirector/node/plain_spec.rb +++ b/spec/unit/indirector/node/plain_spec.rb @@ -7,7 +7,7 @@ describe Puppet::Node::Plain do let(:nodename) { "mynode" } let(:fact_values) { {:afact => "a value"} } let(:facts) { Puppet::Node::Facts.new(nodename, fact_values) } - let(:environment) { Puppet::Node::Environment.new("myenv") } + let(:environment) { Puppet::Node::Environment.create(:myenv, [], '') } let(:request) { Puppet::Indirector::Request.new(:node, :find, nodename, nil, :environment => environment) } let(:node_indirection) { Puppet::Node::Plain.new } diff --git a/spec/unit/indirector/request_spec.rb b/spec/unit/indirector/request_spec.rb index 859d5ba0b..e0d9df339 100755 --- a/spec/unit/indirector/request_spec.rb +++ b/spec/unit/indirector/request_spec.rb @@ -218,21 +218,28 @@ describe Puppet::Indirector::Request do Puppet::Indirector::Request.new(:myind, :find, "my key", nil).escaped_key.should == URI.escape("my key") end - it "should have an environment accessor" do - Puppet::Indirector::Request.new(:myind, :find, "my key", nil, :environment => "foo").should respond_to(:environment) - end - it "should set its environment to an environment instance when a string is specified as its environment" do - Puppet::Indirector::Request.new(:myind, :find, "my key", nil, :environment => "foo").environment.should == Puppet::Node::Environment.new("foo") + env = Puppet::Node::Environment.create(:foo, [], '') + + Puppet.override(:environments => Puppet::Environments::Static.new(env)) do + Puppet::Indirector::Request.new(:myind, :find, "my key", nil, :environment => "foo").environment.should == env + end end it "should use any passed in environment instances as its environment" do - env = Puppet::Node::Environment.new("foo") + env = Puppet::Node::Environment.create(:foo, [], '') + Puppet::Indirector::Request.new(:myind, :find, "my key", nil, :environment => env).environment.should equal(env) end - it "should use the default environment when none is provided" do - Puppet::Indirector::Request.new(:myind, :find, "my key", nil ).environment.should equal(Puppet::Node::Environment.new) + it "should use the configured environment when none is provided" do + configured = Puppet::Node::Environment.create(:foo, [], '') + + Puppet[:environment] = "foo" + + Puppet.override(:environments => Puppet::Environments::Static.new(configured)) do + Puppet::Indirector::Request.new(:myind, :find, "my key", nil).environment.should == configured + end end it "should support converting its options to a hash" do diff --git a/spec/unit/module_spec.rb b/spec/unit/module_spec.rb index 2a0945468..8e3c46989 100755 --- a/spec/unit/module_spec.rb +++ b/spec/unit/module_spec.rb @@ -97,7 +97,6 @@ describe Puppet::Module do it "should list modules that are missing" do metadata_file = "#{@modpath}/needy/metadata.json" - Puppet::FileSystem.expects(:exist?).twice.with(metadata_file).returns true mod = PuppetSpec::Modules.create( 'needy', @modpath, @@ -119,7 +118,6 @@ describe Puppet::Module do it "should list modules that are missing and have invalid names" do metadata_file = "#{@modpath}/needy/metadata.json" - Puppet::FileSystem.expects(:exist?).with(metadata_file).twice.returns true mod = PuppetSpec::Modules.create( 'needy', @modpath, @@ -140,42 +138,43 @@ describe Puppet::Module do end it "should list modules with unmet version requirement" do - ['foobar', 'foobaz'].each do |mod_name| - metadata_file = "#{@modpath}/#{mod_name}/metadata.json" - Puppet::FileSystem.stubs(:exist?).with(metadata_file).returns true - end + env = Puppet::Node::Environment.create(:testing, [@modpath], '') + mod = PuppetSpec::Modules.create( - 'foobar', + 'test_gte_req', @modpath, :metadata => { :dependencies => [{ "version_requirement" => ">= 2.2.0", "name" => "baz/foobar" }] - } + }, + :environment => env ) mod2 = PuppetSpec::Modules.create( - 'foobaz', + 'test_specific_req', @modpath, :metadata => { :dependencies => [{ "version_requirement" => "1.0.0", "name" => "baz/foobar" }] - } + }, + :environment => env ) PuppetSpec::Modules.create( 'foobar', @modpath, - :metadata => { :version => '2.0.0', :author => 'baz' } + :metadata => { :version => '2.0.0', :author => 'baz' }, + :environment => env ) mod.unmet_dependencies.should == [{ :reason => :version_mismatch, :name => "baz/foobar", :version_constraint => ">= 2.2.0", - :parent => { :version => "v9.9.9", :name => "puppetlabs/foobar" }, + :parent => { :version => "v9.9.9", :name => "puppetlabs/test_gte_req" }, :mod_details => { :installed_version => "2.0.0" } }] @@ -183,13 +182,15 @@ describe Puppet::Module do :reason => :version_mismatch, :name => "baz/foobar", :version_constraint => "v1.0.0", - :parent => { :version => "v9.9.9", :name => "puppetlabs/foobaz" }, + :parent => { :version => "v9.9.9", :name => "puppetlabs/test_specific_req" }, :mod_details => { :installed_version => "2.0.0" } }] end it "should consider a dependency without a version requirement to be satisfied" do + env = Puppet::Node::Environment.create(:testing, [@modpath], '') + mod = PuppetSpec::Modules.create( 'foobar', @modpath, @@ -197,7 +198,8 @@ describe Puppet::Module do :dependencies => [{ "name" => "baz/foobar" }] - } + }, + :environment => env ) PuppetSpec::Modules.create( 'foobar', @@ -205,15 +207,16 @@ describe Puppet::Module do :metadata => { :version => '2.0.0', :author => 'baz' - } + }, + :environment => env ) mod.unmet_dependencies.should be_empty end it "should consider a dependency without a semantic version to be unmet" do - metadata_file = "#{@modpath}/foobar/metadata.json" - Puppet::FileSystem.expects(:exist?).with(metadata_file).times(3).returns true + env = Puppet::Node::Environment.create(:testing, [@modpath], '') + mod = PuppetSpec::Modules.create( 'foobar', @modpath, @@ -221,7 +224,8 @@ describe Puppet::Module do :dependencies => [{ "name" => "baz/foobar" }] - } + }, + :environment => env ) PuppetSpec::Modules.create( 'foobar', @@ -229,7 +233,8 @@ describe Puppet::Module do :metadata => { :version => '5.1', :author => 'baz' - } + }, + :environment => env ) mod.unmet_dependencies.should == [{ @@ -254,10 +259,8 @@ describe Puppet::Module do end it "should only list unmet dependencies" do - [name, 'satisfied'].each do |mod_name| - metadata_file = "#{@modpath}/#{mod_name}/metadata.json" - Puppet::FileSystem.expects(:exist?).with(metadata_file).twice.returns true - end + env = Puppet::Node::Environment.create(:testing, [@modpath], '') + mod = PuppetSpec::Modules.create( name, @modpath, @@ -272,7 +275,8 @@ describe Puppet::Module do "name" => "baz/notsatisfied" } ] - } + }, + :environment => env ) PuppetSpec::Modules.create( 'satisfied', @@ -280,7 +284,8 @@ describe Puppet::Module do :metadata => { :version => '3.3.0', :author => 'baz' - } + }, + :environment => env ) mod.unmet_dependencies.should == [{ @@ -293,6 +298,8 @@ describe Puppet::Module do end it "should be empty when all dependencies are met" do + env = Puppet::Node::Environment.create(:testing, [@modpath], '') + mod = PuppetSpec::Modules.create( 'mymod2', @modpath, @@ -307,7 +314,8 @@ describe Puppet::Module do "name" => "baz/alsosatisfied" } ] - } + }, + :environment => env ) PuppetSpec::Modules.create( 'satisfied', @@ -315,7 +323,8 @@ describe Puppet::Module do :metadata => { :version => '3.3.0', :author => 'baz' - } + }, + :environment => env ) PuppetSpec::Modules.create( 'alsosatisfied', @@ -323,7 +332,8 @@ describe Puppet::Module do :metadata => { :version => '2.1.0', :author => 'baz' - } + }, + :environment => env ) mod.unmet_dependencies.should be_empty @@ -652,11 +662,13 @@ describe Puppet::Module do end it "should know what other modules require it" do - Puppet.settings[:modulepath] = @modpath + env = Puppet::Node::Environment.create(:testing, [@modpath], '') + dependable = PuppetSpec::Modules.create( 'dependable', @modpath, - :metadata => {:author => 'puppetlabs'} + :metadata => {:author => 'puppetlabs'}, + :environment => env ) PuppetSpec::Modules.create( 'needy', @@ -667,7 +679,8 @@ describe Puppet::Module do "version_requirement" => ">= 2.2.0", "name" => "puppetlabs/dependable" }] - } + }, + :environment => env ) PuppetSpec::Modules.create( 'wantit', @@ -678,7 +691,8 @@ describe Puppet::Module do "version_requirement" => "< 5.0.0", "name" => "puppetlabs/dependable" }] - } + }, + :environment => env ) dependable.required_by.should =~ [ { diff --git a/spec/unit/network/http/api/v2/environments_spec.rb b/spec/unit/network/http/api/v2/environments_spec.rb index adeea57be..394587238 100644 --- a/spec/unit/network/http/api/v2/environments_spec.rb +++ b/spec/unit/network/http/api/v2/environments_spec.rb @@ -8,7 +8,9 @@ describe Puppet::Network::HTTP::API::V2::Environments do include JSONMatchers it "responds with all of the available environments environments" do - handler = Puppet::Network::HTTP::API::V2::Environments.new(TestingEnvironmentLoader.new) + environment = FakeEnvironment.create(:production, [], '') + loader = Puppet::Environments::Static.new(environment) + handler = Puppet::Network::HTTP::API::V2::Environments.new(loader) response = Puppet::Network::HTTP::MemoryResponse.new handler.call(Puppet::Network::HTTP::Request.from_hash(:headers => { 'accept' => 'application/json' }), response) @@ -16,7 +18,7 @@ describe Puppet::Network::HTTP::API::V2::Environments do expect(response.code).to eq(200) expect(response.type).to eq("application/json") expect(JSON.parse(response.body)).to eq({ - "search_path" => ["file:///fake"], + "search_paths" => loader.search_paths, "environments" => { "production" => { "modules" => { @@ -30,7 +32,8 @@ describe Puppet::Network::HTTP::API::V2::Environments do end it "the response conforms to the environments schema" do - handler = Puppet::Network::HTTP::API::V2::Environments.new(TestingEnvironmentLoader.new) + environment = FakeEnvironment.create(:production, [], '') + handler = Puppet::Network::HTTP::API::V2::Environments.new(Puppet::Environments::Static.new(environment)) response = Puppet::Network::HTTP::MemoryResponse.new handler.call(Puppet::Network::HTTP::Request.from_hash(:headers => { 'accept' => 'application/json' }), response) @@ -38,16 +41,6 @@ describe Puppet::Network::HTTP::API::V2::Environments do expect(response.body).to validate_against('api/schemas/environments.json') end - class TestingEnvironmentLoader - def search_paths - ["file:///fake"] - end - - def list - [FakeEnvironment.new(:production)] - end - end - class FakeEnvironment < Puppet::Node::Environment def modules fake_module = Puppet::Module.new('testing', '/somewhere/on/disk', self) diff --git a/spec/unit/node/environment_spec.rb b/spec/unit/node/environment_spec.rb index fbe791972..0953e0349 100755 --- a/spec/unit/node/environment_spec.rb +++ b/spec/unit/node/environment_spec.rb @@ -346,28 +346,6 @@ describe Puppet::Node::Environment do end end - describe Puppet::Node::Environment::Helper do - before do - @helper = Object.new - @helper.extend(Puppet::Node::Environment::Helper) - end - - it "should be able to set and retrieve the environment as a symbol" do - @helper.environment = :foo - @helper.environment.name.should == :foo - end - - it "should accept an environment directly" do - @helper.environment = Puppet::Node::Environment.new(:foo) - @helper.environment.name.should == :foo - end - - it "should accept an environment as a string" do - @helper.environment = 'foo' - @helper.environment.name.should == :foo - end - end - describe "when performing initial import" do def parser_and_environment(name) env = Puppet::Node::Environment.new(name) diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index ae0a33f62..11f24729e 100755 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -5,29 +5,36 @@ require 'matchers/json' describe Puppet::Node do include JSONMatchers + let(:environment) { Puppet::Node::Environment.create(:bar, [], '') } + let(:env_loader) { Puppet::Environments::Static.new(environment) } + it "should register its document type as Node" do PSON.registered_document_types["Node"].should equal(Puppet::Node) end describe "when managing its environment" do it "should use any set environment" do - Puppet::Node.new("foo", :environment => "bar").environment.name.should == :bar + Puppet.override(:environments => env_loader) do + Puppet::Node.new("foo", :environment => "bar").environment.should == environment + end end it "should support providing an actual environment instance" do - Puppet::Node.new("foo", :environment => Puppet::Node::Environment.new(:bar)).environment.name.should == :bar + Puppet::Node.new("foo", :environment => environment).environment.name.should == :bar end it "should determine its environment from its parameters if no environment is set" do - Puppet::Node.new("foo", :parameters => {"environment" => :bar}).environment.name.should == :bar + Puppet.override(:environments => env_loader) do + Puppet::Node.new("foo", :parameters => {"environment" => :bar}).environment.should == environment + end end - it "should use the default environment if no environment is provided" do - Puppet::Node.new("foo").environment.name.should == Puppet::Node::Environment.new.name - end + it "should use the configured environment if no environment is provided" do + Puppet[:environment] = environment.name.to_s - it "should always return an environment instance rather than a string" do - Puppet::Node.new("foo").environment.should be_instance_of(Puppet::Node::Environment) + Puppet.override(:environments => env_loader) do + Puppet::Node.new("foo").environment.should == environment + end end it "should allow the environment to be set after initialization" do @@ -151,8 +158,10 @@ describe Puppet::Node do end it "should include the environment" do - @node.environment = "production" - Puppet::Node.should read_json_attribute('environment').from(@node.to_pson).as(Puppet::Node::Environment.new(:production)) + Puppet.override(:environments => env_loader) do + @node.environment = environment + Puppet::Node.should read_json_attribute('environment').from(@node.to_pson).as(environment) + end end end end diff --git a/spec/unit/parser/ast/collection_spec.rb b/spec/unit/parser/ast/collection_spec.rb index bd4f5f82f..a5e40b2c3 100755 --- a/spec/unit/parser/ast/collection_spec.rb +++ b/spec/unit/parser/ast/collection_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' describe Puppet::Parser::AST::Collection do before :each do @mytype = Puppet::Resource::Type.new(:definition, "mytype") - @environment = Puppet::Node::Environment.new + @environment = Puppet::Node::Environment.create(:testing, [], '') @environment.known_resource_types.add @mytype @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foonode", :environment => @environment)) diff --git a/spec/unit/parser/ast/resource_spec.rb b/spec/unit/parser/ast/resource_spec.rb index abf815c39..00aa263ff 100755 --- a/spec/unit/parser/ast/resource_spec.rb +++ b/spec/unit/parser/ast/resource_spec.rb @@ -97,7 +97,7 @@ describe Puppet::Parser::AST::Resource do describe "when generating qualified resources" do before do @scope = Puppet::Parser::Scope.new Puppet::Parser::Compiler.new(Puppet::Node.new("mynode")) - @parser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new) + @parser = Puppet::Parser::Parser.new(@scope.environment) ["one", "one::two", "three"].each do |name| @parser.environment.known_resource_types.add(Puppet::Resource::Type.new(:definition, name, {})) end diff --git a/spec/unit/parser/compiler_spec.rb b/spec/unit/parser/compiler_spec.rb index 68210a4df..8a6621520 100755 --- a/spec/unit/parser/compiler_spec.rb +++ b/spec/unit/parser/compiler_spec.rb @@ -66,13 +66,15 @@ describe Puppet::Parser::Compiler do now = Time.now Time.stubs(:now).returns(now) - @node = Puppet::Node.new("testnode", :facts => Puppet::Node::Facts.new("facts", {})) - @known_resource_types = Puppet::Resource::TypeCollection.new "development" + environment = Puppet::Node::Environment.create(:testing, [], '') + @node = Puppet::Node.new("testnode", + :facts => Puppet::Node::Facts.new("facts", {}), + :environment => environment) + @known_resource_types = environment.known_resource_types @compiler = Puppet::Parser::Compiler.new(@node) @scope = Puppet::Parser::Scope.new(@compiler, :source => stub('source')) @scope_resource = Puppet::Parser::Resource.new(:file, "/my/file", :scope => @scope) @scope.resource = @scope_resource - @compiler.environment.stubs(:known_resource_types).returns @known_resource_types end it "should have a class method that compiles, converts, and returns a catalog" do diff --git a/spec/unit/parser/files_spec.rb b/spec/unit/parser/files_spec.rb index 3813260b4..02ef3d5cf 100755 --- a/spec/unit/parser/files_spec.rb +++ b/spec/unit/parser/files_spec.rb @@ -6,6 +6,8 @@ require 'puppet/parser/files' describe Puppet::Parser::Files do include PuppetSpec::Files + let(:environment) { Puppet::Node::Environment.create(:testing, [], '') } + before do @basepath = make_absolute("/somepath") end @@ -13,15 +15,15 @@ describe Puppet::Parser::Files do describe "when searching for templates" do it "should return fully-qualified templates directly" do Puppet::Parser::Files.expects(:modulepath).never - Puppet::Parser::Files.find_template(@basepath + "/my/template").should == @basepath + "/my/template" + Puppet::Parser::Files.find_template(@basepath + "/my/template", environment).should == @basepath + "/my/template" end it "should return the template from the first found module" do mod = mock 'module' - Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod - mod.expects(:template).returns("/one/mymod/templates/mytemplate") - Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate" + environment.expects(:module).with("mymod").returns mod + + Puppet::Parser::Files.find_template("mymod/mytemplate", environment).should == "/one/mymod/templates/mytemplate" end it "should return the file in the templatedir if it exists" do @@ -29,101 +31,91 @@ describe Puppet::Parser::Files do Puppet[:modulepath] = "/one:/two" File.stubs(:directory?).returns(true) Puppet::FileSystem.stubs(:exist?).returns(true) - Puppet::Parser::Files.find_template("mymod/mytemplate").should == File.join(Puppet[:templatedir], "mymod/mytemplate") + Puppet::Parser::Files.find_template("mymod/mytemplate", environment).should == File.join(Puppet[:templatedir], "mymod/mytemplate") end it "should not raise an error if no valid templatedir exists and the template exists in a module" do mod = mock 'module' - Puppet::Node::Environment.new.expects(:module).with("mymod").returns mod - mod.expects(:template).returns("/one/mymod/templates/mytemplate") - Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(nil) + environment.expects(:module).with("mymod").returns mod + Puppet::Parser::Files.stubs(:templatepath).with(environment).returns(nil) - Puppet::Parser::Files.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate" + Puppet::Parser::Files.find_template("mymod/mytemplate", environment).should == "/one/mymod/templates/mytemplate" end it "should return unqualified templates if they exist in the template dir" do Puppet::FileSystem.stubs(:exist?).returns true - Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"]) - Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate" + Puppet::Parser::Files.stubs(:templatepath).with(environment).returns(["/my/templates"]) + + Puppet::Parser::Files.find_template("mytemplate", environment).should == "/my/templates/mytemplate" end it "should only return templates if they actually exist" do Puppet::FileSystem.expects(:exist?).with("/my/templates/mytemplate").returns true - Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"]) - Puppet::Parser::Files.find_template("mytemplate").should == "/my/templates/mytemplate" + Puppet::Parser::Files.stubs(:templatepath).with(environment).returns(["/my/templates"]) + Puppet::Parser::Files.find_template("mytemplate", environment).should == "/my/templates/mytemplate" end it "should return nil when asked for a template that doesn't exist" do Puppet::FileSystem.expects(:exist?).with("/my/templates/mytemplate").returns false - Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"]) - Puppet::Parser::Files.find_template("mytemplate").should be_nil - end - - it "should search in the template directories before modules" do - Puppet::FileSystem.stubs(:exist?).returns true - Puppet::Parser::Files.stubs(:templatepath).with(nil).returns(["/my/templates"]) - Puppet::Module.expects(:find).never - Puppet::Parser::Files.find_template("mytemplate") + Puppet::Parser::Files.stubs(:templatepath).with(environment).returns(["/my/templates"]) + Puppet::Parser::Files.find_template("mytemplate", environment).should be_nil end it "should accept relative templatedirs" do Puppet::FileSystem.stubs(:exist?).returns true Puppet[:templatedir] = "my/templates" File.expects(:directory?).with(File.expand_path("my/templates")).returns(true) - Puppet::Parser::Files.find_template("mytemplate").should == File.expand_path("my/templates/mytemplate") + Puppet::Parser::Files.find_template("mytemplate", environment).should == File.expand_path("my/templates/mytemplate") end it "should use the environment templatedir if no module is found and an environment is specified" do Puppet::FileSystem.stubs(:exist?).returns true - Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates"]) - Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate" + Puppet::Parser::Files.stubs(:templatepath).with(environment).returns(["/myenv/templates"]) + Puppet::Parser::Files.find_template("mymod/mytemplate", environment).should == "/myenv/templates/mymod/mytemplate" end it "should use first dir from environment templatedir if no module is found and an environment is specified" do Puppet::FileSystem.stubs(:exist?).returns true - Puppet::Parser::Files.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"]) - Puppet::Parser::Files.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate" + Puppet::Parser::Files.stubs(:templatepath).with(environment).returns(["/myenv/templates", "/two/templates"]) + Puppet::Parser::Files.find_template("mymod/mytemplate", environment).should == "/myenv/templates/mymod/mytemplate" end it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"]) Puppet::FileSystem.expects(:exist?).with("/one/templates/mytemplate").returns(true) - Puppet::Parser::Files.find_template("mytemplate").should == "/one/templates/mytemplate" + Puppet::Parser::Files.find_template("mytemplate", environment).should == "/one/templates/mytemplate" end it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" do Puppet::Parser::Files.stubs(:templatepath).returns(["/one/templates", "/two/templates"]) Puppet::FileSystem.expects(:exist?).with("/one/templates/mytemplate").returns(false) Puppet::FileSystem.expects(:exist?).with("/two/templates/mytemplate").returns(true) - Puppet::Parser::Files.find_template("mytemplate").should == "/two/templates/mytemplate" + Puppet::Parser::Files.find_template("mytemplate", environment).should == "/two/templates/mytemplate" end it "should use the node environment if specified" do mod = mock 'module' - Puppet::Node::Environment.new("myenv").expects(:module).with("mymod").returns mod + environment.expects(:module).with("mymod").returns mod mod.expects(:template).returns("/my/modules/mymod/templates/envtemplate") - Puppet::Parser::Files.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate" + Puppet::Parser::Files.find_template("mymod/envtemplate", environment).should == "/my/modules/mymod/templates/envtemplate" end it "should return nil if no template can be found" do - Puppet::Parser::Files.find_template("foomod/envtemplate", "myenv").should be_nil + Puppet::Parser::Files.find_template("foomod/envtemplate", environment).should be_nil end - - after { Puppet.settings.clear } end describe "when searching for manifests" do it "should ignore invalid modules" do mod = mock 'module' - env = Puppet::Node::Environment.new - env.expects(:module).with("mymod").raises(Puppet::Module::InvalidName, "name is invalid") + environment.expects(:module).with("mymod").raises(Puppet::Module::InvalidName, "name is invalid") Puppet.expects(:value).with(:modulepath).never Dir.stubs(:glob).returns %w{foo} - Puppet::Parser::Files.find_manifests_in_modules("mymod/init.pp", env) + Puppet::Parser::Files.find_manifests_in_modules("mymod/init.pp", environment) end end @@ -134,8 +126,6 @@ describe Puppet::Parser::Files do mod.stubs(:match_manifests).with("init.pp").returns(["/one/#{name}/manifests/init.pp"]) end - let(:environment) { Puppet::Node::Environment.new } - it "returns no files when no module is found" do module_name, files = Puppet::Parser::Files.find_manifests_in_modules("not_here_module/foo", environment) expect(files).to be_empty @@ -150,7 +140,7 @@ describe Puppet::Parser::Files do end it "does not find the module when it is a different environment" do - different_env = Puppet::Node::Environment.new("different") + different_env = Puppet::Node::Environment.create(:different, [], '') a_module_in_environment(environment, "mymod") Puppet::Parser::Files.find_manifests_in_modules("mymod/init.pp", different_env).should_not include("mymod") diff --git a/spec/unit/parser/functions_spec.rb b/spec/unit/parser/functions_spec.rb index 8e3f52616..91013586c 100755 --- a/spec/unit/parser/functions_spec.rb +++ b/spec/unit/parser/functions_spec.rb @@ -8,20 +8,14 @@ describe Puppet::Parser::Functions do let(:function_module) { Puppet::Parser::Functions.environment_module(Puppet.lookup(:current_environment)) } + let(:environment) { Puppet::Node::Environment.create(:myenv, [], '') } + before do Puppet::Parser::Functions.reset end it "should have a method for returning an environment-specific module" do - Puppet::Parser::Functions.environment_module(Puppet::Node::Environment.new("myenv")).should be_instance_of(Module) - end - - it "should use the current default environment if no environment is provided" do - Puppet::Parser::Functions.environment_module.should be_instance_of(Module) - end - - it "should be able to retrieve environment modules asked for by name rather than instance" do - Puppet::Parser::Functions.environment_module(Puppet::Node::Environment.new("myenv")).should equal(Puppet::Parser::Functions.environment_module("myenv")) + Puppet::Parser::Functions.environment_module(environment).should be_instance_of(Module) end describe "when calling newfunction" do diff --git a/spec/unit/parser/parser_spec.rb b/spec/unit/parser/parser_spec.rb index 466bfea13..56062d47e 100755 --- a/spec/unit/parser/parser_spec.rb +++ b/spec/unit/parser/parser_spec.rb @@ -19,18 +19,14 @@ describe Puppet::Parser do end it "should set the environment" do - env = Puppet::Node::Environment.new + env = Puppet::Node::Environment.create(:testing, [], '') Puppet::Parser::Parser.new(env).environment.should == env end - it "should convert the environment into an environment instance if a string is provided" do - env = Puppet::Node::Environment.new("testing") - Puppet::Parser::Parser.new("testing").environment.should == env - end - it "should be able to look up the environment-specific resource type collection" do - rtc = Puppet::Node::Environment.new("development").known_resource_types - parser = Puppet::Parser::Parser.new "development" + env = Puppet::Node::Environment.create(:development, [], '') + rtc = env.known_resource_types + parser = Puppet::Parser::Parser.new env parser.known_resource_types.should equal(rtc) end diff --git a/spec/unit/parser/resource_spec.rb b/spec/unit/parser/resource_spec.rb index 74a66d1c1..f78f83982 100755 --- a/spec/unit/parser/resource_spec.rb +++ b/spec/unit/parser/resource_spec.rb @@ -1,15 +1,11 @@ -#! /usr/bin/env ruby require 'spec_helper' -# LAK: FIXME This is just new tests for resources; I have -# not moved all tests over yet. - describe Puppet::Parser::Resource do before do - @node = Puppet::Node.new("yaynode") - @known_resource_types = Puppet::Resource::TypeCollection.new("env") + environment = Puppet::Node::Environment.create(:testing, [], '') + @node = Puppet::Node.new("yaynode", :environment => environment) + @known_resource_types = environment.known_resource_types @compiler = Puppet::Parser::Compiler.new(@node) - @compiler.environment.stubs(:known_resource_types).returns @known_resource_types @source = newclass "" @scope = @compiler.topscope end @@ -131,8 +127,6 @@ describe Puppet::Parser::Resource do describe "when evaluating" do before do - @node = Puppet::Node.new "test-node" - @compiler = Puppet::Parser::Compiler.new @node @catalog = Puppet::Resource::Catalog.new source = stub('source') source.stubs(:module_name) diff --git a/spec/unit/parser/scope_spec.rb b/spec/unit/parser/scope_spec.rb index 2267294a3..b9b10618f 100755 --- a/spec/unit/parser/scope_spec.rb +++ b/spec/unit/parser/scope_spec.rb @@ -64,7 +64,7 @@ describe Puppet::Parser::Scope do end it "should get its environment from its compiler" do - env = Puppet::Node::Environment.new + env = Puppet::Node::Environment.create(:testing, [], '') compiler = stub 'compiler', :environment => env, :is_a? => true scope = Puppet::Parser::Scope.new(compiler) scope.environment.should equal(env) @@ -87,7 +87,7 @@ describe Puppet::Parser::Scope do end describe "when custom functions are called" do - let(:env) { Puppet::Node::Environment.new('testing') } + let(:env) { Puppet::Node::Environment.create(:testing, [], '') } let(:compiler) { Puppet::Parser::Compiler.new(Puppet::Node.new('foo', :environment => env)) } let(:scope) { Puppet::Parser::Scope.new(compiler) } diff --git a/spec/unit/parser/type_loader_spec.rb b/spec/unit/parser/type_loader_spec.rb index c735db7c2..659ffa942 100755 --- a/spec/unit/parser/type_loader_spec.rb +++ b/spec/unit/parser/type_loader_spec.rb @@ -19,10 +19,6 @@ describe Puppet::Parser::TypeLoader do loader.environment.name.should == :myenv end - it "should include the Environment Helper" do - loader.class.ancestors.should be_include(Puppet::Node::Environment::Helper) - end - it "should delegate its known resource types to its environment" do loader.known_resource_types.should be_instance_of(Puppet::Resource::TypeCollection) end diff --git a/spec/unit/rails/host_spec.rb b/spec/unit/rails/host_spec.rb index 81d048ef1..746c3a075 100755 --- a/spec/unit/rails/host_spec.rb +++ b/spec/unit/rails/host_spec.rb @@ -40,7 +40,7 @@ describe "Puppet::Rails::Host", :if => can_use_scratch_database? do it "should stringify the environment" do host = Puppet::Rails::Host.new - host.environment = Puppet::Node::Environment.new("production") + host.environment = Puppet::Node::Environment.create(:production, [], '') host.environment.class.should == String end diff --git a/spec/unit/resource/type_collection_spec.rb b/spec/unit/resource/type_collection_spec.rb index c3a335ce2..4cd5b339e 100755 --- a/spec/unit/resource/type_collection_spec.rb +++ b/spec/unit/resource/type_collection_spec.rb @@ -7,39 +7,20 @@ require 'puppet/resource/type' describe Puppet::Resource::TypeCollection do include PuppetSpec::Files + let(:environment) { Puppet::Node::Environment.create(:testing, [], '') } + before do @instance = Puppet::Resource::Type.new(:hostclass, "foo") - @code = Puppet::Resource::TypeCollection.new("env") - end - - it "should require an environment at initialization" do - env = Puppet::Node::Environment.new("testing") - Puppet::Resource::TypeCollection.new(env).environment.should equal(env) - end - - it "should convert the environment into an environment instance if a string is provided" do - env = Puppet::Node::Environment.new("testing") - Puppet::Resource::TypeCollection.new("testing").environment.should equal(env) - end - - it "should create a 'loader' at initialization" do - Puppet::Resource::TypeCollection.new("testing").loader.should be_instance_of(Puppet::Parser::TypeLoader) - end - - it "should be able to add a resource type" do - Puppet::Resource::TypeCollection.new("env").should respond_to(:add) + @code = Puppet::Resource::TypeCollection.new(environment) end it "should consider '<<' to be an alias to 'add' but should return self" do - loader = Puppet::Resource::TypeCollection.new("env") - loader.expects(:add).with "foo" - loader.expects(:add).with "bar" - loader << "foo" << "bar" + @code.expects(:add).with "foo" + @code.expects(:add).with "bar" + @code << "foo" << "bar" end it "should set itself as the code collection for added resource types" do - loader = Puppet::Resource::TypeCollection.new("env") - node = Puppet::Resource::Type.new(:node, "foo") @code.add(node) @@ -96,7 +77,7 @@ describe Puppet::Resource::TypeCollection do end it "should remove all nodes, classes, and definitions when cleared" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) loader.add Puppet::Resource::Type.new(:hostclass, "class") loader.add Puppet::Resource::Type.new(:definition, "define") loader.add Puppet::Resource::Type.new(:node, "node") @@ -200,14 +181,14 @@ describe Puppet::Resource::TypeCollection do describe "behavior of add for #{data}" do it "should return the added #{data}" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(data, "foo") loader.add(instance).should equal(instance) end it "should retrieve #{data} insensitive to case" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(data, "Bar") loader.add instance @@ -216,75 +197,75 @@ describe Puppet::Resource::TypeCollection do end it "should return nil when asked for a #{data} that has not been added" do - Puppet::Resource::TypeCollection.new("env").send(data, "foo").should be_nil + Puppet::Resource::TypeCollection.new(environment).send(data, "foo").should be_nil end end end describe "when finding a qualified instance" do it "should return any found instance if the instance name is fully qualified" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar") loader.add instance loader.find_hostclass("namespace", "::foo::bar").should equal(instance) end it "should return nil if the instance name is fully qualified and no such instance exists" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) loader.find_hostclass("namespace", "::foo::bar").should be_nil end it "should be able to find classes in the base namespace" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo") loader.add instance loader.find_hostclass("", "foo").should equal(instance) end it "should return the partially qualified object if it exists in a provided namespace" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz") loader.add instance loader.find_hostclass("foo", "bar::baz").should equal(instance) end it "should be able to find partially qualified objects in any of the provided namespaces" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz") loader.add instance loader.find_hostclass(["nons", "foo", "otherns"], "bar::baz").should equal(instance) end it "should return the unqualified object if it exists in a provided namespace" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar") loader.add instance loader.find_hostclass("foo", "bar").should equal(instance) end it "should return the unqualified object if it exists in the parent namespace" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar") loader.add instance loader.find_hostclass("foo::bar::baz", "bar").should equal(instance) end it "should should return the partially qualified object if it exists in the parent namespace" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz") loader.add instance loader.find_hostclass("foo::bar", "bar::baz").should equal(instance) end it "should return the qualified object if it exists in the root namespace" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz") loader.add instance loader.find_hostclass("foo::bar", "foo::bar::baz").should equal(instance) end it "should return nil if the object cannot be found" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) instance = Puppet::Resource::Type.new(:hostclass, "foo::bar::baz") loader.add instance loader.find_hostclass("foo::bar", "eh").should be_nil @@ -292,7 +273,7 @@ describe Puppet::Resource::TypeCollection do describe "when topscope has a class that has the same name as a local class" do before do - @loader = Puppet::Resource::TypeCollection.new("env") + @loader = Puppet::Resource::TypeCollection.new(environment) [ "foo::bar", "bar" ].each do |name| @loader.add Puppet::Resource::Type.new(:hostclass, name) end @@ -312,7 +293,7 @@ describe Puppet::Resource::TypeCollection do end it "should not look in the local scope for classes when the name is qualified" do - @loader = Puppet::Resource::TypeCollection.new("env") + @loader = Puppet::Resource::TypeCollection.new(environment) @loader.add Puppet::Resource::Type.new(:hostclass, "foo::bar") @loader.find_hostclass("foo", "::bar").should == nil @@ -322,24 +303,24 @@ describe Puppet::Resource::TypeCollection do it "should be able to find nodes" do node = Puppet::Resource::Type.new(:node, "bar") - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) loader.add(node) loader.find_node(stub("ignored"), "bar").should == node end it "should indicate whether any nodes are defined" do - loader = Puppet::Resource::TypeCollection.new("env") + loader = Puppet::Resource::TypeCollection.new(environment) loader.add_node(Puppet::Resource::Type.new(:node, "foo")) loader.should be_nodes end it "should indicate whether no nodes are defined" do - Puppet::Resource::TypeCollection.new("env").should_not be_nodes + Puppet::Resource::TypeCollection.new(environment).should_not be_nodes end describe "when finding nodes" do before :each do - @loader = Puppet::Resource::TypeCollection.new("env") + @loader = Puppet::Resource::TypeCollection.new(environment) end it "should return any node whose name exactly matches the provided node name" do @@ -368,7 +349,7 @@ describe Puppet::Resource::TypeCollection do describe "when managing files" do before do - @loader = Puppet::Resource::TypeCollection.new("env") + @loader = Puppet::Resource::TypeCollection.new(environment) Puppet::Util::WatchedFile.stubs(:new).returns stub("watched_file") end @@ -409,7 +390,7 @@ describe Puppet::Resource::TypeCollection do describe "when determining the configuration version" do before do - @code = Puppet::Resource::TypeCollection.new("env") + @code = Puppet::Resource::TypeCollection.new(environment) end it "should default to the current time" do diff --git a/spec/unit/resource/type_spec.rb b/spec/unit/resource/type_spec.rb index 1ed1ebcd6..1dd0c41db 100755 --- a/spec/unit/resource/type_spec.rb +++ b/spec/unit/resource/type_spec.rb @@ -347,14 +347,15 @@ describe Puppet::Resource::Type do describe "when describing and managing parent classes" do before do - @krt = Puppet::Node::Environment.new.known_resource_types + environment = Puppet::Node::Environment.create(:testing, [], '') + @krt = environment.known_resource_types @parent = Puppet::Resource::Type.new(:hostclass, "bar") @krt.add @parent @child = Puppet::Resource::Type.new(:hostclass, "foo", :parent => "bar") @krt.add @child - @scope = Puppet::Parser::Scope.new(Puppet::Parser::Compiler.new(Puppet::Node.new("foo"))) + @scope = Puppet::Parser::Scope.new(Puppet::Parser::Compiler.new(Puppet::Node.new("foo", :environment => environment))) end it "should be able to define a parent" do diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index b6443865a..1ac2decf1 100755 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -5,7 +5,8 @@ require 'puppet/resource' describe Puppet::Resource do include PuppetSpec::Files - let :basepath do make_absolute("/somepath") end + let(:basepath) { make_absolute("/somepath") } + let(:environment) { Puppet::Node::Environment.create(:testing, [], '') } [:catalog, :file, :line].each do |attr| it "should have an #{attr} attribute" do @@ -120,10 +121,6 @@ describe Puppet::Resource do resource.should be_exported end - it "should support an environment attribute" do - Puppet::Resource.new("file", "/my/file", :environment => :foo).environment.name.should == :foo - end - describe "and munging its type and title" do describe "when modeling a builtin resource" do it "should be able to find the resource type" do @@ -139,19 +136,19 @@ describe Puppet::Resource do describe "that exists" do before do @type = Puppet::Resource::Type.new(:definition, "foo::bar") - Puppet::Node::Environment.new.known_resource_types.add @type + environment.known_resource_types.add @type end it "should set its type to the capitalized type name" do - Puppet::Resource.new("foo::bar", "/my/file").type.should == "Foo::Bar" + Puppet::Resource.new("foo::bar", "/my/file", :environment => environment).type.should == "Foo::Bar" end it "should be able to find the resource type" do - Puppet::Resource.new("foo::bar", "/my/file").resource_type.should equal(@type) + Puppet::Resource.new("foo::bar", "/my/file", :environment => environment).resource_type.should equal(@type) end it "should set its title to the provided title" do - Puppet::Resource.new("foo::bar", "/my/file").title.should == "/my/file" + Puppet::Resource.new("foo::bar", "/my/file", :environment => environment).title.should == "/my/file" end end @@ -179,15 +176,15 @@ describe Puppet::Resource do describe "that exists" do before do @type = Puppet::Resource::Type.new(:hostclass, "foo::bar") - Puppet::Node::Environment.new.known_resource_types.add @type + environment.known_resource_types.add @type end it "should set its title to the capitalized, fully qualified resource type" do - Puppet::Resource.new("class", "foo::bar").title.should == "Foo::Bar" + Puppet::Resource.new("class", "foo::bar", :environment => environment).title.should == "Foo::Bar" end it "should be able to find the resource type" do - Puppet::Resource.new("class", "foo::bar").resource_type.should equal(@type) + Puppet::Resource.new("class", "foo::bar", :environment => environment).resource_type.should equal(@type) end end @@ -207,9 +204,9 @@ describe Puppet::Resource do describe "and a class exists whose name is the empty string" do # this was a bit tough to track down it "should set its title to :main" do @type = Puppet::Resource::Type.new(:hostclass, "") - Puppet::Node::Environment.new.known_resource_types.add @type + environment.known_resource_types.add @type - Puppet::Resource.new("class", "").title.should == :main + Puppet::Resource.new("class", "", :environment => environment).title.should == :main end end end @@ -222,9 +219,9 @@ describe Puppet::Resource do describe "and a class exists whose name is the empty string" do # this was a bit tough to track down it "should set its title to :main" do @type = Puppet::Resource::Type.new(:hostclass, "") - Puppet::Node::Environment.new.known_resource_types.add @type + environment.known_resource_types.add @type - Puppet::Resource.new("class", :main).title.should == :main + Puppet::Resource.new("class", :main, :environment => environment).title.should == :main end end end @@ -237,8 +234,8 @@ describe Puppet::Resource do it "should not fail when an invalid parameter is used and strict mode is disabled" do type = Puppet::Resource::Type.new(:definition, "foobar") - Puppet::Node::Environment.new.known_resource_types.add type - resource = Puppet::Resource.new("foobar", "/my/file") + environment.known_resource_types.add type + resource = Puppet::Resource.new("foobar", "/my/file", :environment => environment) resource[:yay] = true end @@ -267,7 +264,7 @@ describe Puppet::Resource do end describe "when setting default parameters" do - let(:foo_node) { Puppet::Node.new('foo') } + let(:foo_node) { Puppet::Node.new('foo', :environment => environment) } let(:compiler) { Puppet::Parser::Compiler.new(foo_node) } let(:scope) { Puppet::Parser::Scope.new(compiler) } @@ -276,15 +273,15 @@ describe Puppet::Resource do end it "should fail when asked to set default values and it is not a parser resource" do - Puppet::Node::Environment.new.known_resource_types.add( + environment.known_resource_types.add( Puppet::Resource::Type.new(:definition, "default_param", :arguments => {"a" => ast_string("default")}) ) - resource = Puppet::Resource.new("default_param", "name") + resource = Puppet::Resource.new("default_param", "name", :environment => environment) lambda { resource.set_default_parameters(scope) }.should raise_error(Puppet::DevError) end it "should evaluate and set any default values when no value is provided" do - Puppet::Node::Environment.new.known_resource_types.add( + environment.known_resource_types.add( Puppet::Resource::Type.new(:definition, "default_param", :arguments => {"a" => ast_string("a_default_value")}) ) resource = Puppet::Parser::Resource.new("default_param", "name", :scope => scope) @@ -293,7 +290,7 @@ describe Puppet::Resource do end it "should skip attributes with no default value" do - Puppet::Node::Environment.new.known_resource_types.add( + environment.known_resource_types.add( Puppet::Resource::Type.new(:definition, "no_default_param", :arguments => {"a" => ast_string("a_default_value")}) ) resource = Puppet::Parser::Resource.new("no_default_param", "name", :scope => scope) @@ -301,7 +298,7 @@ describe Puppet::Resource do end it "should return the list of default parameters set" do - Puppet::Node::Environment.new.known_resource_types.add( + environment.known_resource_types.add( Puppet::Resource::Type.new(:definition, "default_param", :arguments => {"a" => ast_string("a_default_value")}) ) resource = Puppet::Parser::Resource.new("default_param", "name", :scope => scope) @@ -315,11 +312,10 @@ describe Puppet::Resource do let(:apache) { Puppet::Resource::Type.new(:hostclass, 'apache', :arguments => { 'port' => port }) } before do - environment = Puppet::Node::Environment.new(environment_name) environment.known_resource_types.add(apache) scope.stubs(:host).returns('host') - scope.stubs(:environment).returns(Puppet::Node::Environment.new(environment_name)) + scope.stubs(:environment).returns(environment) scope.stubs(:facts).returns(Puppet::Node::Facts.new("facts", fact_values)) end @@ -398,17 +394,17 @@ describe Puppet::Resource do describe "when validating all required parameters are present" do it "should be able to validate that all required parameters are present" do - Puppet::Node::Environment.new.known_resource_types.add( + environment.known_resource_types.add( Puppet::Resource::Type.new(:definition, "required_param", :arguments => {"a" => nil}) ) - lambda { Puppet::Resource.new("required_param", "name").validate_complete }.should raise_error(Puppet::ParseError) + lambda { Puppet::Resource.new("required_param", "name", :environment => environment).validate_complete }.should raise_error(Puppet::ParseError) end it "should not fail when all required parameters are present" do - Puppet::Node::Environment.new.known_resource_types.add( + environment.known_resource_types.add( Puppet::Resource::Type.new(:definition, "no_required_param") ) - resource = Puppet::Resource.new("no_required_param", "name") + resource = Puppet::Resource.new("no_required_param", "name", :environment => environment) resource["a"] = "meh" lambda { resource.validate_complete }.should_not raise_error end @@ -455,14 +451,14 @@ describe Puppet::Resource do it "should correctly detect when provided parameters are not valid for defined resource types" do type = Puppet::Resource::Type.new(:definition, "foobar") - Puppet::Node::Environment.new.known_resource_types.add type - Puppet::Resource.new("foobar", "/my/file").should_not be_valid_parameter("myparam") + environment.known_resource_types.add type + Puppet::Resource.new("foobar", "/my/file", :environment => environment).should_not be_valid_parameter("myparam") end it "should correctly detect when provided parameters are valid for defined resource types" do type = Puppet::Resource::Type.new(:definition, "foobar", :arguments => {"myparam" => nil}) - Puppet::Node::Environment.new.known_resource_types.add type - Puppet::Resource.new("foobar", "/my/file").should be_valid_parameter("myparam") + environment.known_resource_types.add type + Puppet::Resource.new("foobar", "/my/file", :environment => environment).should be_valid_parameter("myparam") end it "should allow setting and retrieving of parameters" do @@ -603,11 +599,9 @@ describe Puppet::Resource do describe "when serializing a defined type" do before do type = Puppet::Resource::Type.new(:definition, "foo::bar") - Puppet::Node::Environment.new.known_resource_types.add type - end + environment.known_resource_types.add type - before :each do - @resource = Puppet::Resource.new('foo::bar', 'xyzzy') + @resource = Puppet::Resource.new('foo::bar', 'xyzzy', :environment => environment) @resource['one'] = 'test' @resource['two'] = 'other' @resource.resource_type diff --git a/spec/unit/util/autoload_spec.rb b/spec/unit/util/autoload_spec.rb index 3592a63c9..1800dd2dc 100755 --- a/spec/unit/util/autoload_spec.rb +++ b/spec/unit/util/autoload_spec.rb @@ -24,14 +24,9 @@ describe Puppet::Util::Autoload do end it "should collect all of the lib directories that exist in the current environment's module path" do - Puppet.settings.parse_config(<<-CONF) - [foo] - modulepath = #{@dira}#{File::PATH_SEPARATOR}#{@dirb}#{File::PATH_SEPARATOR}#{@dirc} - CONF - - Puppet[:environment] = "foo" - Dir.expects(:entries).with(@dira).returns %w{one two} - Dir.expects(:entries).with(@dirb).returns %w{one two} + environment = Puppet::Node::Environment.create(:foo, [@dira, @dirb, @dirc], '') + Dir.expects(:entries).with(@dira).returns %w{. .. one two} + Dir.expects(:entries).with(@dirb).returns %w{. .. one two} Puppet::FileSystem.expects(:directory?).with(@dira).returns true Puppet::FileSystem.expects(:directory?).with(@dirb).returns true @@ -40,32 +35,14 @@ describe Puppet::Util::Autoload do FileTest.expects(:directory?).with(regexp_matches(%r{two/lib})).times(2).returns true FileTest.expects(:directory?).with(regexp_matches(%r{one/lib})).times(2).returns false - @autoload.class.module_directories.should == ["#{@dira}/two/lib", "#{@dirb}/two/lib"] - end - - it "should not look for lib directories in directories starting with '.'" do - Puppet.settings.parse_config(<<-CONF) - [foo] - modulepath = #{@dira} - CONF - - Puppet[:environment] = "foo" - Dir.expects(:entries).with(@dira).returns %w{. ..} - - Puppet::FileSystem.expects(:directory?).with(@dira).returns true - Puppet::FileSystem.expects(:directory?).with("#{@dira}/./lib").never - Puppet::FileSystem.expects(:directory?).with("#{@dira}/./plugins").never - Puppet::FileSystem.expects(:directory?).with("#{@dira}/../lib").never - Puppet::FileSystem.expects(:directory?).with("#{@dira}/../plugins").never - - @autoload.class.module_directories + @autoload.class.module_directories(environment).should == ["#{@dira}/two/lib", "#{@dirb}/two/lib"] end it "should include the module directories, the Puppet libdir, and all of the Ruby load directories" do Puppet[:libdir] = %w{/libdir1 /lib/dir/two /third/lib/dir}.join(File::PATH_SEPARATOR) @autoload.class.expects(:gem_directories).returns %w{/one /two} @autoload.class.expects(:module_directories).returns %w{/three /four} - @autoload.class.search_directories.should == %w{/one /two /three /four} + Puppet[:libdir].split(File::PATH_SEPARATOR) + $LOAD_PATH + @autoload.class.search_directories(nil).should == %w{/one /two /three /four} + Puppet[:libdir].split(File::PATH_SEPARATOR) + $LOAD_PATH end end |