Module: RSpec::Puppet::Support
- Included in:
- ApplicationExampleGroup, ClassExampleGroup, DefineExampleGroup, FunctionExampleGroup, HostExampleGroup, TypeAliasExampleGroup, TypeExampleGroup
- Defined in:
- lib/rspec-puppet/support.rb
Constant Summary collapse
- @@cache =
RSpec::Puppet::Cache.new
Instance Attribute Summary collapse
-
#adapter ⇒ Class < RSpec::Puppet::Adapters::Base]
private
Class < RSpec::Puppet::Adapters::Base].
Instance Method Summary collapse
- #build_catalog(*args) ⇒ Object
- #build_catalog_without_cache(nodename, facts_val, trusted_facts_val, hiera_config_val, code, exported, node_params, *_) ⇒ Object
- #build_code(type, manifest_opts) ⇒ Object
- #class_name ⇒ Object
- #environment ⇒ Object
- #escape_special_chars(string) ⇒ Object
- #facts_hash(node) ⇒ Object
- #guess_type_from_path(path) ⇒ Object
- #import_str ⇒ Object
- #load_catalogue(type, exported = false, manifest_opts = {}) ⇒ Object
- #munge_facts(facts) ⇒ Object
- #node_params_hash ⇒ Object
- #nodename(type) ⇒ Object
- #param_str(params) ⇒ Object
- #param_str_from_hash(params_hash) ⇒ Object
- #post_cond ⇒ Object
- #pre_cond ⇒ Object
-
#ref(type, title) ⇒ RSpec::Puppet::RawString
Helper to return a resource/node reference, so it gets translated in params to a raw string without quotes.
- #rspec_compatibility ⇒ Object
- #setup_puppet ⇒ Object
- #site_pp_str ⇒ Object
- #str_from_value(value) ⇒ Object
- #stub_facts!(facts) ⇒ Object
- #stub_file_consts(example) ⇒ Object
- #subject ⇒ Object
- #test_manifest(type, opts = {}) ⇒ Object
- #trusted_facts_hash(node_name) ⇒ Object
- #with_vardir ⇒ Object
Instance Attribute Details
#adapter ⇒ Class < RSpec::Puppet::Adapters::Base]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Class < RSpec::Puppet::Adapters::Base].
417 418 419 |
# File 'lib/rspec-puppet/support.rb', line 417 def adapter @adapter end |
Instance Method Details
#build_catalog(*args) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/rspec-puppet/support.rb', line 362 def build_catalog(*args) build_facts = args[1] ['operatingsystem', 'osfamily'].each do |os_fact| if build_facts.key?(os_fact) if build_facts[os_fact].to_s.downcase == 'windows' Puppet::Util::Platform.pretend_to_be :windows else Puppet::Util::Platform.pretend_to_be :posix end end end @@cache.get(*args) do |*args| build_catalog_without_cache(*args) end end |
#build_catalog_without_cache(nodename, facts_val, trusted_facts_val, hiera_config_val, code, exported, node_params, *_) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/rspec-puppet/support.rb', line 325 def build_catalog_without_cache(nodename, facts_val, trusted_facts_val, hiera_config_val, code, exported, node_params, *_) # If we're going to rebuild the catalog, we should clear the cached instance # of Hiera that Puppet is using. This opens the possibility of the catalog # now being rebuilt against a differently configured Hiera (i.e. :hiera_config # set differently in one example group vs. another). # It would be nice if Puppet offered a public API for invalidating their # cached instance of Hiera, but que sera sera. We will go directly against # the implementation out of absolute necessity. HieraPuppet.instance_variable_set('@hiera', nil) if defined? HieraPuppet Puppet[:code] = code stub_facts! facts_val node_facts = Puppet::Node::Facts.new(nodename, facts_val.dup) node_params = facts_val.merge(node_params) node_obj = Puppet::Node.new(nodename, { :parameters => node_params, :facts => node_facts }) if Puppet::Util::Package.versioncmp(Puppet.version, '4.3.0') >= 0 Puppet.push_context( { :trusted_information => Puppet::Context::TrustedInformation.new('remote', nodename, trusted_facts_val) }, "Context for spec trusted hash" ) end adapter.catalog(node_obj, exported) end |
#build_code(type, manifest_opts) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rspec-puppet/support.rb', line 17 def build_code(type, manifest_opts) if Puppet.version.to_f >= 4.0 or Puppet[:parser] == 'future' [site_pp_str, pre_cond, test_manifest(type, manifest_opts), post_cond].compact.join("\n") else [import_str, pre_cond, test_manifest(type, manifest_opts), post_cond].compact.join("\n") end end |
#class_name ⇒ Object
166 167 168 |
# File 'lib/rspec-puppet/support.rb', line 166 def class_name self.class.top_level_description.downcase end |
#environment ⇒ Object
13 14 15 |
# File 'lib/rspec-puppet/support.rb', line 13 def environment 'rp_env' end |
#escape_special_chars(string) ⇒ Object
391 392 393 394 |
# File 'lib/rspec-puppet/support.rb', line 391 def escape_special_chars(string) string.gsub!(/\$/, "\\$") string end |
#facts_hash(node) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rspec-puppet/support.rb', line 194 def facts_hash(node) base_facts = { 'clientversion' => Puppet::PUPPETVERSION, 'environment' => environment.to_s, } node_facts = { 'hostname' => node.split('.').first, 'fqdn' => node, 'domain' => node.split('.', 2).last, 'clientcert' => node, } networking_facts = { 'hostname' => node_facts['hostname'], 'fqdn' => node_facts['fqdn'], 'domain' => node_facts['domain'], } result_facts = if RSpec.configuration.default_facts.any? munge_facts(RSpec.configuration.default_facts) else {} end # Merge in node facts so they always exist by default, but only if they # haven't been defined in `RSpec.configuration.default_facts` result_facts.merge!(munge_facts(node_facts)) { |_key, old_val, new_val| old_val.nil? ? new_val : old_val } (result_facts['networking'] ||= {}).merge!(networking_facts) { |_key, old_val, new_val| old_val.nil? ? new_val : old_val } # Merge in `let(:facts)` facts result_facts.merge!(munge_facts(base_facts)) result_facts.merge!(munge_facts(facts)) if self.respond_to?(:facts) # Merge node facts again on top of `let(:facts)` facts, but only if # a node name is given with `let(:node)` if respond_to?(:node) && RSpec.configuration.derive_node_facts_from_nodename result_facts.merge!(munge_facts(node_facts)) (result_facts['networking'] ||= {}).merge!(networking_facts) end # Facter currently supports lower case facts. Bug FACT-777 has been submitted to support case sensitive # facts. downcase_facts = Hash[result_facts.map { |k, v| [k.downcase, v] }] downcase_facts end |
#guess_type_from_path(path) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rspec-puppet/support.rb', line 25 def guess_type_from_path(path) case path when /spec\/defines/ :define when /spec\/classes/ :class when /spec\/functions/ :function when /spec\/hosts/ :host when /spec\/types/ :type when /spec\/type_aliases/ :type_alias when /spec\/provider/ :provider when /spec\/applications/ :application else :unknown end end |
#import_str ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rspec-puppet/support.rb', line 90 def import_str import_str = "" adapter.modulepath.each { |d| if File.exists?(File.join(d, 'manifests', 'init.pp')) path_to_manifest = File.join([ d, 'manifests', class_name.split('::')[1..-1] ].flatten) import_str = [ "import '#{d}/manifests/init.pp'", "import '#{path_to_manifest}.pp'", '', ].join("\n") break elsif File.exists?(d) import_str = "import '#{adapter.manifest}'\n" break end } import_str end |
#load_catalogue(type, exported = false, manifest_opts = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rspec-puppet/support.rb', line 68 def load_catalogue(type, exported = false, manifest_opts = {}) with_vardir do node_name = nodename(type) hiera_config_value = self.respond_to?(:hiera_config) ? hiera_config : nil hiera_data_value = self.respond_to?(:hiera_data) ? hiera_data : nil catalogue = build_catalog(node_name, facts_hash(node_name), trusted_facts_hash(node_name), hiera_config_value, build_code(type, manifest_opts), exported, node_params_hash, hiera_data_value) test_module = type == :host ? nil : class_name.split('::').first if type == :define RSpec::Puppet::Coverage.add_filter(class_name, title) else RSpec::Puppet::Coverage.add_filter(type.to_s, class_name) end RSpec::Puppet::Coverage.add_from_catalog(catalogue, test_module) catalogue end end |
#munge_facts(facts) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/rspec-puppet/support.rb', line 379 def munge_facts(facts) return facts.reduce({}) do | memo, (k, v)| memo.tap { |m| m[k.to_s] = munge_facts(v) } end if facts.is_a? Hash return facts.reduce([]) do |memo, v| memo << munge_facts(v); memo end if facts.is_a? Array facts end |
#node_params_hash ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/rspec-puppet/support.rb', line 241 def node_params_hash params = RSpec.configuration.default_node_params if respond_to?(:node_params) params.merge(node_params) else params.dup end end |
#nodename(type) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/rspec-puppet/support.rb', line 157 def nodename(type) return node if self.respond_to?(:node) if [:class, :define, :function, :application].include? type Puppet[:certname] else class_name end end |
#param_str(params) ⇒ Object
250 251 252 |
# File 'lib/rspec-puppet/support.rb', line 250 def param_str(params) param_str_from_hash(params) end |
#param_str_from_hash(params_hash) ⇒ Object
290 291 292 293 294 295 296 |
# File 'lib/rspec-puppet/support.rb', line 290 def param_str_from_hash(params_hash) # the param_str has special quoting rules, because the top-level keys are the Puppet # params, which may not be quoted params_hash.collect do |k,v| "#{k.to_s} => #{str_from_value(v)}" end.join(', ') end |
#post_cond ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/rspec-puppet/support.rb', line 182 def post_cond if self.respond_to?(:post_condition) && !post_condition.nil? if post_condition.is_a? Array post_condition.compact.join("\n") else post_condition end else nil end end |
#pre_cond ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rspec-puppet/support.rb', line 170 def pre_cond if self.respond_to?(:pre_condition) && !pre_condition.nil? if pre_condition.is_a? Array pre_condition.compact.join("\n") else pre_condition end else nil end end |
#ref(type, title) ⇒ RSpec::Puppet::RawString
Helper to return a resource/node reference, so it gets translated in params to a raw string without quotes.
410 411 412 |
# File 'lib/rspec-puppet/support.rb', line 410 def ref(type, title) return RSpec::Puppet::RawString.new("#{type}['#{title}']") end |
#rspec_compatibility ⇒ Object
396 397 398 399 400 401 402 |
# File 'lib/rspec-puppet/support.rb', line 396 def rspec_compatibility if RSpec::Version::STRING < '3' # RSpec 2 compatibility: alias_method :failure_message_for_should, :failure_message alias_method :failure_message_for_should_not, :failure_message_when_negated end end |
#setup_puppet ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/rspec-puppet/support.rb', line 298 def setup_puppet vardir = Dir.mktmpdir Puppet[:vardir] = vardir # Enable app_management by default for Puppet versions that support it if Puppet::Util::Package.versioncmp(Puppet.version, '4.3.0') >= 0 && Puppet.version.to_i < 5 Puppet[:app_management] = true end adapter.modulepath.map do |d| Dir["#{d}/*/lib"].entries end.flatten.each do |lib| $LOAD_PATH << lib end vardir end |
#site_pp_str ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/rspec-puppet/support.rb', line 114 def site_pp_str site_pp_str = '' filepath = adapter.manifest if (!filepath.nil?) && File.file?(filepath) site_pp_str = File.open(filepath).read end site_pp_str end |
#str_from_value(value) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/rspec-puppet/support.rb', line 267 def str_from_value(value) case value when Hash kvs = value.collect do |k,v| "#{str_from_value(k)} => #{str_from_value(v)}" end.join(", ") "{ #{kvs} }" when Array vals = value.map do |v| str_from_value(v) end.join(", ") "[ #{vals} ]" when :default 'default' # verbatim default keyword when :undef 'undef' # verbatim undef keyword when Symbol str_from_value(value.to_s) else escape_special_chars(value.inspect) end end |
#stub_facts!(facts) ⇒ Object
357 358 359 360 |
# File 'lib/rspec-puppet/support.rb', line 357 def stub_facts!(facts) Puppet.settings[:autosign] = false facts.each { |k, v| Facter.add(k) { setcode { v } } } end |
#stub_file_consts(example) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rspec-puppet/support.rb', line 48 def stub_file_consts(example) if example.respond_to?(:metadata) type = example.[:type] else type = guess_type_from_path(example.example.[:file_path]) end munged_facts = facts_hash(nodename(type)) ['operatingsystem', 'osfamily'].each do |os_fact| if munged_facts.key?(os_fact) if munged_facts[os_fact].to_s.downcase == 'windows' RSpec::Puppet::Consts.stub_consts_for(:windows) else RSpec::Puppet::Consts.stub_consts_for(:posix) end end end end |
#subject ⇒ Object
9 10 11 |
# File 'lib/rspec-puppet/support.rb', line 9 def subject lambda { catalogue } end |
#test_manifest(type, opts = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rspec-puppet/support.rb', line 124 def test_manifest(type, opts = {}) opts[:params] = params if self.respond_to?(:params) if type == :class if opts[:params].nil? || opts[:params] == {} "include #{class_name}" else "class { '#{class_name}': #{param_str(opts[:params])} }" end elsif type == :application if opts.has_key?(:params) "site { #{class_name} { '#{title}': #{param_str(opts[:params])} } }" else raise ArgumentError, "You need to provide params for an application" end elsif type == :define title_str = if title.is_a?(Array) '[' + title.map { |r| "'#{r}'" }.join(', ') + ']' else "'#{title}'" end if opts.has_key?(:params) "#{class_name} { #{title_str}: #{param_str(opts[:params])} }" else "#{class_name} { #{title_str}: }" end elsif type == :host nil elsif type == :type_alias "$test = #{str_from_value(opts[:test_value])}\nassert_type(#{self.class.top_level_description}, $test)" end end |
#trusted_facts_hash(node_name) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rspec-puppet/support.rb', line 254 def trusted_facts_hash(node_name) return {} unless Puppet::Util::Package.versioncmp(Puppet.version, '4.3.0') >= 0 extensions = {} if RSpec.configuration.default_trusted_facts.any? extensions.merge!(munge_facts(RSpec.configuration.default_trusted_facts)) end extensions.merge!(munge_facts(trusted_facts)) if self.respond_to?(:trusted_facts) extensions end |
#with_vardir ⇒ Object
316 317 318 319 320 321 322 323 |
# File 'lib/rspec-puppet/support.rb', line 316 def with_vardir begin vardir = setup_puppet return yield(vardir) if block_given? ensure FileUtils.rm_rf(vardir) if vardir && File.directory?(vardir) end end |