Browse Source

Add multiple module path support and test tasks

Change-Id: Ie582146053185809b43b6483346993f0ec7eded5
Dmitry Ilyin 2 years ago
parent
commit
7ce9a11467

+ 10
- 0
demo.rb View File

@@ -0,0 +1,10 @@
1
+#!/usr/bin/env ruby
2
+
3
+require_relative './noop_tests'
4
+
5
+ENV['SPEC_SPEC_DIR'] = './spec/demo-hosts'
6
+
7
+if $0 == __FILE__
8
+  manager = Noop::Manager.new
9
+  manager.main
10
+end

+ 3
- 1
doc/usage.rst View File

@@ -326,7 +326,9 @@ Paths related:
326 326
   it but it should be at the *spec/hosts* from the root folder or the
327 327
   rpsec-puppet will break.
328 328
 - **SPEC_MODULE_PATH** or **SPEC_MODULEPATH** Set the path to the modules
329
-  library.
329
+  library. It can be either a path to a single directory with Puppet
330
+  modules or a string with colon-separated paths to several module
331
+  directories.
330 332
 - **SPEC_TASK_DIR** Set the path to the task manifests folder.
331 333
 - **SPEC_DEPLOYMENT_DIR** Set the path to the *deployment* directory. It's
332 334
   actually use only to find the scripts to update and reset modules.

+ 6
- 10
lib/noop/config/base.rb View File

@@ -50,16 +50,12 @@ module Noop
50 50
       end
51 51
     end
52 52
 
53
-    # @return [Pathname]
54
-    def self.dir_path_modules_local
55
-      return @dir_path_modules_local if @dir_path_modules_local
56
-      @dir_path_modules_local = Noop::Utils.path_from_env 'SPEC_MODULEPATH', 'SPEC_MODULE_PATH'
57
-      @dir_path_modules_local = dir_path_root + 'modules' unless @dir_path_modules_local
58
-      begin
59
-        @dir_path_modules_local = @dir_path_modules_local.realpath
60
-      rescue
61
-        @dir_path_modules_local
62
-      end
53
+    # @return [Array<Pathname>]
54
+    def self.list_path_modules
55
+      return @list_path_modules if @list_path_modules
56
+      @list_path_modules = Noop::Utils.path_list_from_env 'SPEC_MODULEPATH', 'SPEC_MODULE_PATH'
57
+      return @list_path_modules if @list_path_modules.any?
58
+      @list_path_modules = [dir_path_root + 'modules']
63 59
     end
64 60
 
65 61
     # @return [Pathname]

+ 14
- 12
lib/noop/manager/library.rb View File

@@ -82,18 +82,20 @@ module Noop
82 82
     def task_graph_metadata
83 83
       return @task_graph_metadata if @task_graph_metadata
84 84
       @task_graph_metadata = {}
85
-      error "No #{Noop::Config.dir_path_modules_local} directory!" unless Noop::Config.dir_path_modules_local.directory?
86
-      Noop::Config.dir_path_modules_local.find do |task_file|
87
-        next unless task_file.file?
88
-        next unless task_file.to_s.end_with? 'tasks.yaml'
89
-        begin
90
-          tasks = YAML.load_file task_file
91
-        rescue
92
-          next
93
-        end
94
-        tasks.each do |task|
95
-          id = task['id']
96
-          @task_graph_metadata[id] = task
85
+      Noop::Config.list_path_modules.each do |path|
86
+        next unless path.directory?
87
+        path.find do |task_file|
88
+          next unless task_file.file?
89
+          next unless task_file.to_s.end_with? 'tasks.yaml'
90
+          begin
91
+            tasks = YAML.load_file task_file
92
+          rescue
93
+            next
94
+          end
95
+          tasks.each do |task|
96
+            id = task['id']
97
+            @task_graph_metadata[id] = task
98
+          end
97 99
         end
98 100
       end
99 101
 

+ 1
- 1
lib/noop/manager/options.rb View File

@@ -117,7 +117,7 @@ module Noop
117 117
         opts.on('--dir_task_files DIR', 'Path to the folder with task manifest files') do |dir|
118 118
           ENV['SPEC_TASK_DIR'] = dir
119 119
         end
120
-        opts.on('--dir_puppet_modules DIR', 'Path to the puppet modules') do |dir|
120
+        opts.on('--module_path DIR', 'Path to the puppet modules. Can consist of several dirs separated by a colon.') do |dir|
121 121
           ENV['SPEC_MODULE_PATH'] = dir
122 122
         end
123 123
 

+ 6
- 2
lib/noop/manager/report.rb View File

@@ -207,7 +207,6 @@ Total tasks to run:       <%= task_list.count.to_s.colorize :yellow %>
207 207
           :dir_path_root,
208 208
           :dir_path_task_root,
209 209
           :dir_path_task_spec,
210
-          :dir_path_modules_local,
211 210
           :dir_path_tasks_local,
212 211
           :dir_path_deployment,
213 212
           :dir_path_workspace,
@@ -217,12 +216,17 @@ Total tasks to run:       <%= task_list.count.to_s.colorize :yellow %>
217 216
           :dir_path_facts_override,
218 217
           :dir_path_globals,
219 218
           :dir_path_reports,
219
+
220
+          :list_path_modules,
220 221
       ]
221 222
       max_length = paths.map { |p| p.to_s.length }.max
222 223
 
223 224
       paths.each do |path|
224 225
         directory = Noop::Config.send path
225
-        output "#{directory_check_status_string directory} #{path.to_s.ljust max_length} #{directory}"
226
+        directory = [directory] unless directory.is_a? Array
227
+        directory.each do |element|
228
+          output "#{directory_check_status_string element} #{path.to_s.ljust max_length} #{element}"
229
+        end
226 230
       end
227 231
     end
228 232
 

+ 1
- 1
lib/noop/task/overrides.rb View File

@@ -15,7 +15,7 @@ module Noop
15 15
     # to run in this RSpec session
16 16
     def setup_manifest
17 17
       RSpec.configuration.manifest = file_path_manifest.to_s
18
-      RSpec.configuration.module_path = Noop::Config.dir_path_modules_local.to_s
18
+      RSpec.configuration.module_path = Noop::Config.list_path_modules.join ':'
19 19
       RSpec.configuration.manifest_dir = Noop::Config.dir_path_tasks_local.to_s
20 20
 
21 21
       # FIXME: kludge to support calling Puppet function outside of the test context

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

@@ -10,6 +10,7 @@ module Noop
10 10
 Facts:    <%= task.file_path_facts %>
11 11
 Hiera:    <%= task.file_path_hiera %>
12 12
 Spec:     <%= task.file_path_spec %>
13
+Modules:  <%= Noop::Config.list_path_modules.join(':') %>
13 14
 Manifest: <%= task.file_path_manifest %>
14 15
 
15 16
 Node:     <%= task.hiera_lookup 'fqdn' or '?' %>

+ 1
- 1
lib/noop/task/run.rb View File

@@ -68,7 +68,7 @@ module Noop
68 68
           'SPEC_REPORTS_DIR' => Noop::Config.dir_path_reports.to_s,
69 69
           'SPEC_SPEC_DIR' => Noop::Config.dir_path_task_spec.to_s,
70 70
           'SPEC_TASK_DIR' => Noop::Config.dir_path_tasks_local.to_s,
71
-          'SPEC_MODULE_PATH' => Noop::Config.dir_path_modules_local.to_s,
71
+          'SPEC_MODULE_PATH' => Noop::Config.list_path_modules.join(':'),
72 72
       }
73 73
       command = "rspec #{file_path_spec.to_s} #{rspec_options} --format json --out #{file_path_report_json.to_s}"
74 74
       command = "bundle exec #{command}" if ENV['SPEC_BUNDLE_EXEC']

+ 15
- 0
lib/noop/utils.rb View File

@@ -10,6 +10,21 @@ module Noop
10 10
       nil
11 11
     end
12 12
 
13
+    def self.path_list_from_env(*names)
14
+      paths = []
15
+      names.each do |name|
16
+        name = name.to_s
17
+        next unless ENV[name]
18
+        list = ENV[name].split(':')
19
+        list.each do |path|
20
+          path = convert_to_path path
21
+          path = path.realpath if path.exist?
22
+          paths << path
23
+        end
24
+      end
25
+      paths
26
+    end
27
+
13 28
     # @param [Object] value
14 29
     # @return [Pathname]
15 30
     def self.convert_to_path(value)

+ 10
- 0
spec/demo-hosts/test/compute_spec.rb View File

@@ -0,0 +1,10 @@
1
+require 'spec_helper'
2
+require 'shared-examples'
3
+
4
+# ROLE: compute
5
+
6
+manifest = 'test/compute.pp'
7
+
8
+describe manifest, :type => :host do
9
+  run_test manifest
10
+end

+ 10
- 0
spec/demo-hosts/test/controller_spec.rb View File

@@ -0,0 +1,10 @@
1
+require 'spec_helper'
2
+require 'shared-examples'
3
+
4
+# ROLE: controller
5
+
6
+manifest = 'test/controller.pp'
7
+
8
+describe manifest, :type => :host do
9
+  run_test manifest
10
+end

+ 10
- 0
spec/demo-hosts/test/fail_spec.rb View File

@@ -0,0 +1,10 @@
1
+require 'spec_helper'
2
+require 'shared-examples'
3
+
4
+# HIERA: master
5
+
6
+manifest = 'test/fail.pp'
7
+
8
+describe manifest, :type => :host do
9
+  run_test manifest
10
+end

+ 10
- 0
spec/demo-hosts/test/master_spec.rb View File

@@ -0,0 +1,10 @@
1
+require 'spec_helper'
2
+require 'shared-examples'
3
+
4
+# HIERA: master
5
+
6
+manifest = 'test/master.pp'
7
+
8
+describe manifest, :type => :host do
9
+  run_test manifest
10
+end

+ 3
- 3
spec/lib/config_spec.rb View File

@@ -22,9 +22,9 @@ describe Noop::Config do
22 22
       expect(subject.dir_path_task_spec.to_s).to eq "#{root}/spec/hosts"
23 23
     end
24 24
 
25
-    it 'dir_path_modules_local' do
26
-      expect(subject.dir_path_modules_local).to be_a Pathname
27
-      expect(subject.dir_path_modules_local.to_s).to eq "#{root}/modules"
25
+    it 'list_path_modules' do
26
+      expect(subject.list_path_modules).to be_a Array
27
+      expect(subject.list_path_modules.first.to_s).to eq "#{root}/modules"
28 28
     end
29 29
 
30 30
     it 'dir_path_tasks_local' do

+ 4
- 2
spec/spec_helper.rb View File

@@ -9,8 +9,10 @@ require_relative '../lib/noop'
9 9
 
10 10
 # Add fixture lib dirs to LOAD_PATH. Work-around for PUP-3336
11 11
 if Puppet.version < '4.0.0'
12
-  Dir["#{Noop::Config.dir_path_modules_local}/*/lib"].entries.each do |lib_dir|
13
-    $LOAD_PATH << lib_dir
12
+  Noop::Config.list_path_modules.each do |path|
13
+    Dir["#{path}/*/lib"].entries.each do |lib_dir|
14
+      $LOAD_PATH << lib_dir
15
+    end
14 16
   end
15 17
 end
16 18
 

+ 3
- 0
tasks/test/compute.pp View File

@@ -0,0 +1,3 @@
1
+notice("MODULAR: test/compute")
2
+
3
+notify { 'test/compute' :}

+ 3
- 0
tasks/test/controller.pp View File

@@ -0,0 +1,3 @@
1
+notice("MODULAR: test/controller")
2
+
3
+notify { 'test/controller' :}

+ 3
- 0
tasks/test/fail.pp View File

@@ -0,0 +1,3 @@
1
+notice("MODULAR: test/fail")
2
+
3
+fail('some error')

+ 3
- 0
tasks/test/master.pp View File

@@ -0,0 +1,3 @@
1
+notice("MODULAR: test/master")
2
+
3
+notify { 'test/master' :}

Loading…
Cancel
Save