Fixtures for Fuel Library Noop tests framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.rb 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. module Noop
  2. class Task
  3. # Extract the parameter or property of a Puppet resource in the catalog
  4. # @param context [RSpec::ExampleGroup] The 'self' of the RSpec example group
  5. # @param resource_type [String] Name of the resource type
  6. # @param resource_name [String] Title of the resource
  7. # @param parameter [String] Parameter name
  8. # @return [Object]
  9. def resource_parameter_value(context, resource_type, resource_name, parameter)
  10. catalog = context.subject
  11. catalog = catalog.call if catalog.is_a? Proc
  12. resource = catalog.resource resource_type, resource_name
  13. error "No resource type: '#{resource_type}' name: '#{resource_name}' in the catalog!" unless resource
  14. resource[parameter.to_sym]
  15. end
  16. # Save the current puppet scope
  17. # @param value [Puppet::Scope]
  18. def puppet_scope=(value)
  19. @puppet_scope = value
  20. end
  21. # The saved Puppet scope to run functions in
  22. # Or the newly generated scope.
  23. # @return [Puppet::Scope]
  24. def puppet_scope
  25. return @puppet_scope if @puppet_scope
  26. PuppetlabsSpec::PuppetInternals.scope
  27. end
  28. # Load a puppet function if it's not already loaded
  29. # @param name [String] Function name
  30. def puppet_function_load(name)
  31. name = name.to_sym unless name.is_a? Symbol
  32. Puppet::Parser::Functions.autoloader.load name
  33. end
  34. # Call a puppet function and return it's value
  35. # @param name [String] Function name
  36. # @param *args [Object] Function parameters
  37. # @return [Object]
  38. def puppet_function(name, *args)
  39. name = name.to_sym unless name.is_a? Symbol
  40. puppet_function_load name
  41. if puppet4?
  42. puppet_scope.call_function name, args
  43. else
  44. error "Could not load Puppet function '#{name}'!" unless puppet_scope.respond_to? "function_#{name}".to_sym
  45. puppet_scope.send "function_#{name}".to_sym, args
  46. end
  47. end
  48. # Take a variable value from the saved puppet scope
  49. # @param name [String] variable name
  50. def lookupvar(name)
  51. puppet_scope.lookupvar name
  52. end
  53. alias :variable :lookupvar
  54. # Load a class from the Puppet modules into the current scope
  55. # It can be used to extract values from 'params' classes like this:
  56. # Noop.load_class 'nova::params'
  57. # Noop.variable 'nova::params::common_package_name'
  58. # => 'openstack-nova-common'
  59. # These values can be later used in the spec examples.
  60. # Note, that the loaded class will not be found in the spec's catalog
  61. # object, but can be found here: Noop.puppet_scope.catalog
  62. # @param class_name [String]
  63. def puppet_class_include(class_name)
  64. class_name = class_name.to_s
  65. unless puppet_scope.catalog.classes.include? class_name
  66. debug "Dynamically loading class: '#{class_name}'"
  67. puppet_scope.compiler.evaluate_classes [class_name], puppet_scope, false
  68. end
  69. end
  70. # Convert resource catalog to a RAL catalog
  71. # and run both "generate" functions for each resource
  72. # that has it and then add results to the catalog
  73. # @param context [RSpec::ExampleGroup] The 'self' of the RSpec example group
  74. # @return <Lambda>
  75. def create_ral_catalog(context)
  76. catalog = context.catalog
  77. catalog = catalog.call if catalog.is_a? Proc
  78. ral_catalog = catalog.to_ral
  79. ral_catalog.resources.each do |resource|
  80. generate_additional_resources ral_catalog, resource
  81. end
  82. lambda { ral_catalog }
  83. end
  84. # If the resources has one of the generate function
  85. # run it and add the generated resources to the catalog
  86. # if they are not there already. Run generate functions
  87. # recursively for the generated resources too.
  88. # @param catalog [Puppet::Catalog]
  89. # @param resource [Puppet::Type]
  90. def generate_additional_resources(catalog, resource)
  91. generate_functions = [:generate, :eval_generate]
  92. generate_functions.each do |function_name|
  93. next unless resource.respond_to? function_name
  94. debug "Resource: #{resource} run: #{function_name}"
  95. generated = resource.send function_name
  96. next unless generated.is_a? Array
  97. generated.each do |generated_resource|
  98. next unless generated_resource.is_a? Puppet::Type
  99. next if catalog.resource generated_resource.ref
  100. debug "Add resource: #{generated_resource} to the catalog"
  101. catalog.add_resource generated_resource
  102. generate_additional_resources catalog, generated_resource
  103. end
  104. end
  105. catalog
  106. end
  107. # Check if the currently running spec is the given one
  108. # or one of the given ones if an array is provided
  109. # @param spec [String, Array<String>]
  110. # @return [true,false]
  111. def current_spec_is?(spec)
  112. return false unless file_name_spec_set?
  113. spec = [spec] unless spec.is_a? Array
  114. spec = spec.flatten
  115. spec = spec.map do |spec|
  116. Noop::Utils.convert_to_spec spec
  117. end
  118. spec.any? do |spec|
  119. file_name_spec == spec
  120. end
  121. end
  122. # check if we're using Puppet4
  123. # @return [true,false]
  124. def puppet4?
  125. Puppet.version.to_f >= 4.0
  126. end
  127. # convert the values in the nested data structure
  128. # from nil to :undef as they are used in Puppet 4
  129. # modifies the argument object and returns it
  130. # @param data [Array, Hash]
  131. # @return [Array, Hash]
  132. def nil2undef(data)
  133. return :undef if data.nil?
  134. if data.is_a? Array
  135. data.each_with_index do |value, index|
  136. data[index] = nil2undef value
  137. end
  138. data
  139. elsif data.is_a? Hash
  140. data.keys.each do |key|
  141. data[key] = nil2undef data[key]
  142. end
  143. data
  144. end
  145. data
  146. end
  147. end
  148. end