From 7ce9a11467f0857fddf635e03c36c0a6d919498c Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin Date: Fri, 8 Jul 2016 12:16:26 -0500 Subject: [PATCH] Add multiple module path support and test tasks Change-Id: Ie582146053185809b43b6483346993f0ec7eded5 --- demo.rb | 10 ++++++++++ doc/usage.rst | 4 +++- lib/noop/config/base.rb | 16 ++++++--------- lib/noop/manager/library.rb | 26 +++++++++++++------------ lib/noop/manager/options.rb | 2 +- lib/noop/manager/report.rb | 8 ++++++-- lib/noop/task/overrides.rb | 2 +- lib/noop/task/report.rb | 1 + lib/noop/task/run.rb | 2 +- lib/noop/utils.rb | 15 ++++++++++++++ spec/demo-hosts/test/compute_spec.rb | 10 ++++++++++ spec/demo-hosts/test/controller_spec.rb | 10 ++++++++++ spec/demo-hosts/test/fail_spec.rb | 10 ++++++++++ spec/demo-hosts/test/master_spec.rb | 10 ++++++++++ spec/lib/config_spec.rb | 6 +++--- spec/spec_helper.rb | 6 ++++-- tasks/test/compute.pp | 3 +++ tasks/test/controller.pp | 3 +++ tasks/test/fail.pp | 3 +++ tasks/test/master.pp | 3 +++ 20 files changed, 117 insertions(+), 33 deletions(-) create mode 100644 demo.rb create mode 100644 spec/demo-hosts/test/compute_spec.rb create mode 100644 spec/demo-hosts/test/controller_spec.rb create mode 100644 spec/demo-hosts/test/fail_spec.rb create mode 100644 spec/demo-hosts/test/master_spec.rb create mode 100644 tasks/test/compute.pp create mode 100644 tasks/test/controller.pp create mode 100644 tasks/test/fail.pp create mode 100644 tasks/test/master.pp diff --git a/demo.rb b/demo.rb new file mode 100644 index 0000000..66ebe8e --- /dev/null +++ b/demo.rb @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby + +require_relative './noop_tests' + +ENV['SPEC_SPEC_DIR'] = './spec/demo-hosts' + +if $0 == __FILE__ + manager = Noop::Manager.new + manager.main +end diff --git a/doc/usage.rst b/doc/usage.rst index 3055d25..76a6d31 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -326,7 +326,9 @@ Paths related: it but it should be at the *spec/hosts* from the root folder or the rpsec-puppet will break. - **SPEC_MODULE_PATH** or **SPEC_MODULEPATH** Set the path to the modules - library. + library. It can be either a path to a single directory with Puppet + modules or a string with colon-separated paths to several module + directories. - **SPEC_TASK_DIR** Set the path to the task manifests folder. - **SPEC_DEPLOYMENT_DIR** Set the path to the *deployment* directory. It's actually use only to find the scripts to update and reset modules. diff --git a/lib/noop/config/base.rb b/lib/noop/config/base.rb index cf493e3..430c3aa 100644 --- a/lib/noop/config/base.rb +++ b/lib/noop/config/base.rb @@ -50,16 +50,12 @@ module Noop end end - # @return [Pathname] - def self.dir_path_modules_local - return @dir_path_modules_local if @dir_path_modules_local - @dir_path_modules_local = Noop::Utils.path_from_env 'SPEC_MODULEPATH', 'SPEC_MODULE_PATH' - @dir_path_modules_local = dir_path_root + 'modules' unless @dir_path_modules_local - begin - @dir_path_modules_local = @dir_path_modules_local.realpath - rescue - @dir_path_modules_local - end + # @return [Array] + def self.list_path_modules + return @list_path_modules if @list_path_modules + @list_path_modules = Noop::Utils.path_list_from_env 'SPEC_MODULEPATH', 'SPEC_MODULE_PATH' + return @list_path_modules if @list_path_modules.any? + @list_path_modules = [dir_path_root + 'modules'] end # @return [Pathname] diff --git a/lib/noop/manager/library.rb b/lib/noop/manager/library.rb index 99c0fbb..e88f92f 100644 --- a/lib/noop/manager/library.rb +++ b/lib/noop/manager/library.rb @@ -82,18 +82,20 @@ module Noop def task_graph_metadata return @task_graph_metadata if @task_graph_metadata @task_graph_metadata = {} - error "No #{Noop::Config.dir_path_modules_local} directory!" unless Noop::Config.dir_path_modules_local.directory? - Noop::Config.dir_path_modules_local.find do |task_file| - next unless task_file.file? - next unless task_file.to_s.end_with? 'tasks.yaml' - begin - tasks = YAML.load_file task_file - rescue - next - end - tasks.each do |task| - id = task['id'] - @task_graph_metadata[id] = task + Noop::Config.list_path_modules.each do |path| + next unless path.directory? + path.find do |task_file| + next unless task_file.file? + next unless task_file.to_s.end_with? 'tasks.yaml' + begin + tasks = YAML.load_file task_file + rescue + next + end + tasks.each do |task| + id = task['id'] + @task_graph_metadata[id] = task + end end end diff --git a/lib/noop/manager/options.rb b/lib/noop/manager/options.rb index 5db9504..65950d2 100644 --- a/lib/noop/manager/options.rb +++ b/lib/noop/manager/options.rb @@ -117,7 +117,7 @@ module Noop opts.on('--dir_task_files DIR', 'Path to the folder with task manifest files') do |dir| ENV['SPEC_TASK_DIR'] = dir end - opts.on('--dir_puppet_modules DIR', 'Path to the puppet modules') do |dir| + opts.on('--module_path DIR', 'Path to the puppet modules. Can consist of several dirs separated by a colon.') do |dir| ENV['SPEC_MODULE_PATH'] = dir end diff --git a/lib/noop/manager/report.rb b/lib/noop/manager/report.rb index 7978f42..69b4f42 100644 --- a/lib/noop/manager/report.rb +++ b/lib/noop/manager/report.rb @@ -207,7 +207,6 @@ Total tasks to run: <%= task_list.count.to_s.colorize :yellow %> :dir_path_root, :dir_path_task_root, :dir_path_task_spec, - :dir_path_modules_local, :dir_path_tasks_local, :dir_path_deployment, :dir_path_workspace, @@ -217,12 +216,17 @@ Total tasks to run: <%= task_list.count.to_s.colorize :yellow %> :dir_path_facts_override, :dir_path_globals, :dir_path_reports, + + :list_path_modules, ] max_length = paths.map { |p| p.to_s.length }.max paths.each do |path| directory = Noop::Config.send path - output "#{directory_check_status_string directory} #{path.to_s.ljust max_length} #{directory}" + directory = [directory] unless directory.is_a? Array + directory.each do |element| + output "#{directory_check_status_string element} #{path.to_s.ljust max_length} #{element}" + end end end diff --git a/lib/noop/task/overrides.rb b/lib/noop/task/overrides.rb index d650efa..cb13dae 100644 --- a/lib/noop/task/overrides.rb +++ b/lib/noop/task/overrides.rb @@ -15,7 +15,7 @@ module Noop # to run in this RSpec session def setup_manifest RSpec.configuration.manifest = file_path_manifest.to_s - RSpec.configuration.module_path = Noop::Config.dir_path_modules_local.to_s + RSpec.configuration.module_path = Noop::Config.list_path_modules.join ':' RSpec.configuration.manifest_dir = Noop::Config.dir_path_tasks_local.to_s # FIXME: kludge to support calling Puppet function outside of the test context diff --git a/lib/noop/task/report.rb b/lib/noop/task/report.rb index e5a0648..96bf852 100644 --- a/lib/noop/task/report.rb +++ b/lib/noop/task/report.rb @@ -10,6 +10,7 @@ module Noop Facts: <%= task.file_path_facts %> Hiera: <%= task.file_path_hiera %> Spec: <%= task.file_path_spec %> +Modules: <%= Noop::Config.list_path_modules.join(':') %> Manifest: <%= task.file_path_manifest %> Node: <%= task.hiera_lookup 'fqdn' or '?' %> diff --git a/lib/noop/task/run.rb b/lib/noop/task/run.rb index 7acea0e..015b242 100644 --- a/lib/noop/task/run.rb +++ b/lib/noop/task/run.rb @@ -68,7 +68,7 @@ module Noop 'SPEC_REPORTS_DIR' => Noop::Config.dir_path_reports.to_s, 'SPEC_SPEC_DIR' => Noop::Config.dir_path_task_spec.to_s, 'SPEC_TASK_DIR' => Noop::Config.dir_path_tasks_local.to_s, - 'SPEC_MODULE_PATH' => Noop::Config.dir_path_modules_local.to_s, + 'SPEC_MODULE_PATH' => Noop::Config.list_path_modules.join(':'), } command = "rspec #{file_path_spec.to_s} #{rspec_options} --format json --out #{file_path_report_json.to_s}" command = "bundle exec #{command}" if ENV['SPEC_BUNDLE_EXEC'] diff --git a/lib/noop/utils.rb b/lib/noop/utils.rb index c924fdb..463e537 100644 --- a/lib/noop/utils.rb +++ b/lib/noop/utils.rb @@ -10,6 +10,21 @@ module Noop nil end + def self.path_list_from_env(*names) + paths = [] + names.each do |name| + name = name.to_s + next unless ENV[name] + list = ENV[name].split(':') + list.each do |path| + path = convert_to_path path + path = path.realpath if path.exist? + paths << path + end + end + paths + end + # @param [Object] value # @return [Pathname] def self.convert_to_path(value) diff --git a/spec/demo-hosts/test/compute_spec.rb b/spec/demo-hosts/test/compute_spec.rb new file mode 100644 index 0000000..3d28c33 --- /dev/null +++ b/spec/demo-hosts/test/compute_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' +require 'shared-examples' + +# ROLE: compute + +manifest = 'test/compute.pp' + +describe manifest, :type => :host do + run_test manifest +end diff --git a/spec/demo-hosts/test/controller_spec.rb b/spec/demo-hosts/test/controller_spec.rb new file mode 100644 index 0000000..5be3726 --- /dev/null +++ b/spec/demo-hosts/test/controller_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' +require 'shared-examples' + +# ROLE: controller + +manifest = 'test/controller.pp' + +describe manifest, :type => :host do + run_test manifest +end diff --git a/spec/demo-hosts/test/fail_spec.rb b/spec/demo-hosts/test/fail_spec.rb new file mode 100644 index 0000000..6bc3181 --- /dev/null +++ b/spec/demo-hosts/test/fail_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' +require 'shared-examples' + +# HIERA: master + +manifest = 'test/fail.pp' + +describe manifest, :type => :host do + run_test manifest +end diff --git a/spec/demo-hosts/test/master_spec.rb b/spec/demo-hosts/test/master_spec.rb new file mode 100644 index 0000000..6c3befd --- /dev/null +++ b/spec/demo-hosts/test/master_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' +require 'shared-examples' + +# HIERA: master + +manifest = 'test/master.pp' + +describe manifest, :type => :host do + run_test manifest +end diff --git a/spec/lib/config_spec.rb b/spec/lib/config_spec.rb index 77a8d16..6f5301a 100644 --- a/spec/lib/config_spec.rb +++ b/spec/lib/config_spec.rb @@ -22,9 +22,9 @@ describe Noop::Config do expect(subject.dir_path_task_spec.to_s).to eq "#{root}/spec/hosts" end - it 'dir_path_modules_local' do - expect(subject.dir_path_modules_local).to be_a Pathname - expect(subject.dir_path_modules_local.to_s).to eq "#{root}/modules" + it 'list_path_modules' do + expect(subject.list_path_modules).to be_a Array + expect(subject.list_path_modules.first.to_s).to eq "#{root}/modules" end it 'dir_path_tasks_local' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5e41275..3e20689 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,8 +9,10 @@ require_relative '../lib/noop' # Add fixture lib dirs to LOAD_PATH. Work-around for PUP-3336 if Puppet.version < '4.0.0' - Dir["#{Noop::Config.dir_path_modules_local}/*/lib"].entries.each do |lib_dir| - $LOAD_PATH << lib_dir + Noop::Config.list_path_modules.each do |path| + Dir["#{path}/*/lib"].entries.each do |lib_dir| + $LOAD_PATH << lib_dir + end end end diff --git a/tasks/test/compute.pp b/tasks/test/compute.pp new file mode 100644 index 0000000..5369eec --- /dev/null +++ b/tasks/test/compute.pp @@ -0,0 +1,3 @@ +notice("MODULAR: test/compute") + +notify { 'test/compute' :} diff --git a/tasks/test/controller.pp b/tasks/test/controller.pp new file mode 100644 index 0000000..178b2cf --- /dev/null +++ b/tasks/test/controller.pp @@ -0,0 +1,3 @@ +notice("MODULAR: test/controller") + +notify { 'test/controller' :} diff --git a/tasks/test/fail.pp b/tasks/test/fail.pp new file mode 100644 index 0000000..a7e4fcd --- /dev/null +++ b/tasks/test/fail.pp @@ -0,0 +1,3 @@ +notice("MODULAR: test/fail") + +fail('some error') \ No newline at end of file diff --git a/tasks/test/master.pp b/tasks/test/master.pp new file mode 100644 index 0000000..9cca2be --- /dev/null +++ b/tasks/test/master.pp @@ -0,0 +1,3 @@ +notice("MODULAR: test/master") + +notify { 'test/master' :}