Browse Source

Puppet4 Support: noop fixtures

Change-Id: Ie301faa4585e74f546dff7c845c7d8ef49d87552
Dmitry Ilyin 3 years ago
parent
commit
da6ea6109e
7 changed files with 53 additions and 13 deletions
  1. 1
    1
      Gemfile
  2. 1
    0
      catalogs/.gitignore
  3. 1
    0
      facts/ubuntu.yaml
  4. 1
    0
      lib/noop/task/facts.rb
  5. 34
    3
      lib/noop/task/helpers.rb
  6. 13
    9
      lib/noop/task/overrides.rb
  7. 2
    0
      spec/shared-examples.rb

+ 1
- 1
Gemfile View File

@@ -3,7 +3,7 @@ source 'https://rubygems.org'
3 3
 group :development, :test do
4 4
   gem 'puppetlabs_spec_helper'
5 5
   gem 'puppet-lint', '~> 0.3.2'
6
-  gem 'rspec-puppet', '~> 2.2.0'
6
+  gem 'rspec-puppet', '~> 2.4.0'
7 7
   gem 'rspec-puppet-utils', '~> 2.0.0'
8 8
   gem 'deep_merge'
9 9
   gem 'pry'

+ 1
- 0
catalogs/.gitignore View File

@@ -0,0 +1 @@
1
+*.pp

+ 1
- 0
facts/ubuntu.yaml View File

@@ -18,3 +18,4 @@
18 18
 :lsbdistcodename: 'trusty'
19 19
 :os_package_type: 'debian'
20 20
 :os_service_default: '<SERVICE DEFAULT>'
21
+:libvirt_package_version: '1.2.9'

+ 1
- 0
lib/noop/task/facts.rb View File

@@ -67,6 +67,7 @@ module Noop
67 67
       facts_data[:hostname] = hostname if hostname
68 68
       facts_data[:l3_fqdn_hostname] = hostname if hostname
69 69
       facts_data[:fqdn] = fqdn if fqdn
70
+      facts_data[:puppetversion] = Puppet.version
70 71
     end
71 72
 
72 73
     # @return [Hash]

+ 34
- 3
lib/noop/task/helpers.rb View File

@@ -43,8 +43,12 @@ module Noop
43 43
     def puppet_function(name, *args)
44 44
       name = name.to_sym unless name.is_a? Symbol
45 45
       puppet_function_load name
46
-      error "Could not load Puppet function '#{name}'!" unless puppet_scope.respond_to? "function_#{name}".to_sym
47
-      puppet_scope.send "function_#{name}".to_sym, args
46
+      if puppet4?
47
+        puppet_scope.call_function name, args
48
+      else
49
+        error "Could not load Puppet function '#{name}'!" unless puppet_scope.respond_to? "function_#{name}".to_sym
50
+        puppet_scope.send "function_#{name}".to_sym, args
51
+      end
48 52
     end
49 53
 
50 54
     # Take a variable value from the saved puppet scope
@@ -66,7 +70,7 @@ module Noop
66 70
     def puppet_class_include(class_name)
67 71
       class_name = class_name.to_s
68 72
       unless puppet_scope.catalog.classes.include? class_name
69
-        debug "Dynamicly loading class: '#{class_name}'"
73
+        debug "Dynamically loading class: '#{class_name}'"
70 74
         puppet_scope.compiler.evaluate_classes [class_name], puppet_scope, false
71 75
       end
72 76
     end
@@ -113,5 +117,32 @@ module Noop
113 117
       end
114 118
     end
115 119
 
120
+    # check if we're using Puppet4
121
+    # @return [true,false]
122
+    def puppet4?
123
+      Puppet.version.to_f >= 4.0
124
+    end
125
+
126
+    # convert the values in the nested data structure
127
+    # from nil to :undef as they are used in Puppet 4
128
+    # modifies the argument object and returns it
129
+    # @param data [Array, Hash]
130
+    # @return [Array, Hash]
131
+    def nil2undef(data)
132
+      return :undef if data.nil?
133
+      if data.is_a? Array
134
+        data.each_with_index do |value, index|
135
+          data[index] = nil2undef value
136
+        end
137
+        data
138
+      elsif data.is_a? Hash
139
+        data.keys.each do |key|
140
+          data[key] = nil2undef data[key]
141
+        end
142
+        data
143
+      end
144
+      data
145
+    end
146
+
116 147
   end
117 148
 end

+ 13
- 9
lib/noop/task/overrides.rb View File

@@ -17,6 +17,10 @@ module Noop
17 17
       RSpec.configuration.manifest = file_path_manifest.to_s
18 18
       RSpec.configuration.module_path = Noop::Config.dir_path_modules_local.to_s
19 19
       RSpec.configuration.manifest_dir = Noop::Config.dir_path_tasks_local.to_s
20
+
21
+      # FIXME: kludge to support calling Puppet function outside of the test context
22
+      Puppet.settings[:modulepath] = RSpec.configuration.module_path
23
+      Puppet.settings[:manifest] = RSpec.configuration.manifest_dir
20 24
     end
21 25
 
22 26
     # Override Hiera configuration in the Puppet objects
@@ -82,15 +86,15 @@ module Noop
82 86
     # This results in an rspec failure so we need to initialize the basic
83 87
     # settings up front to prevent issues with test framework. See PUP-5601
84 88
     def puppet_default_settings
85
-      Puppet.settings.initialize_app_defaults(
86
-          {
87
-              :logdir => '/dev/null',
88
-              :confdir => '/dev/null',
89
-              :vardir => '/dev/null',
90
-              :rundir => '/dev/null',
91
-              :hiera_config => '/dev/null',
92
-          }
93
-      )
89
+      defaults = {
90
+          :logdir => '/dev/null',
91
+          :confdir => '/dev/null',
92
+          :vardir => '/dev/null',
93
+          :rundir => '/dev/null',
94
+          :hiera_config => '/dev/null',
95
+      }
96
+      defaults[:codedir] = '/dev/null' if puppet4?
97
+      Puppet.settings.initialize_app_defaults(defaults)
94 98
     end
95 99
 
96 100
     def rspec_coverage_add_override

+ 2
- 0
spec/shared-examples.rb View File

@@ -63,6 +63,8 @@ def run_test(manifest_file, *args)
63 63
 
64 64
   Noop::Config.log.progname = 'noop_spec'
65 65
   Noop::Utils.debug "RSPEC: #{Noop.task.inspect}"
66
+
67
+  # FIXME: kludge to support calling Puppet function outside of the test context
66 68
   Noop.setup_overrides
67 69
 
68 70
   include FuelRelationshipGraphMatchers

Loading…
Cancel
Save