Class: ChefRake::Task::Test

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/chef/raketasks/test.rb

Instance Method Summary collapse

Constructor Details

#initializeTest

Returns a new instance of Test.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/raketasks/test.rb', line 24

def initialize
  super

  namespace :test do
    desc 'Run all integration tests'
    task integration: %i[
      integration:ec2
      integration:physical
      integration:vagrant
      integration:vcenter
    ]

    namespace :integration do
      def kitchen_instances(regexp, config)
        # Gets a collection of instances.
        #
        # @param regexp [String] regular expression to match against instance names.
        # @param config [Hash] configuration values for the `Kitchen::Config` class.
        # @return [Collection<Instance>] all instances.
        active_config = Kitchen::Config.new(config)
        # If we are in CI mode then add formatter options (@refactor)
        kitchen_config = active_config.send(:data).send(:data)

        if ENV['CI']
          kitchen_config[:verifier][:reporter] = ['junit:reports/integration/inspec_%{platform}_%{suite}.xml']
        end

        instances = active_config.instances
        return instances if regexp.nil? || regexp == 'all'

        instances.get_all(Regexp.new(regexp))
      end

      def run_kitchen(action, regexp, loader_config = {})
        # Runs a test kitchen action against some instances.
        #
        # @param action [String] kitchen action to run (defaults to `'test'`).
        # @param regexp [String] regular expression to match against instance names.
        # @param loader_config [Hash] loader configuration options.
        # @return void
        action = 'test' if action.nil?
        require 'kitchen'
        Kitchen.logger = Kitchen.default_file_logger
        config = { loader: Kitchen::Loader::YAML.new(loader_config) }
        kitchen_instances(regexp, config).each { |i| i.send(action) }
      rescue StandardError
        # Clean up on any error
        kitchen_instances(regexp, config).each { |i| i.send('destroy') }
      end

      desc 'Run integration tests on AWS EC2'
      task :ec2, [:regexp, :action] do |_t, args|
        run_kitchen(args.action, args.regexp, local_config: '.kitchen.ec2.yml')
      end

      desc 'Run integration tests using static IPs'
      task :static, [:regexp, :action] do |_t, args|
        run_kitchen(args.action, args.regexp, local_config: '.kitchen.static.yml')
      end

      desc 'Run integration tests locally with vagrant'
      task :vagrant, [:regexp, :action] do |_t, args|
        run_kitchen(args.action, args.regexp, local_config: '.kitchen.vagrant.yml')
      end

      desc 'Run integration tests using vCenter'
      task :vcenter, [:regexp, :action] do |_t, args|
        run_kitchen(args.action, args.regexp, local_config: '.kitchen.vcenter.yml')
      end
    end # namespace integration

    namespace :lint do
      desc 'Run linting tests for cookbook'
      task :cookbook, [:autocorrect] do |_t, args|
        args.with_defaults(autocorrect: false)

        cmd = 'cookstyle'
        cmd << ' --auto-correct' if args.autocorrect
        sh cmd
      end
    end # namespace lint
  end # namespace test
end