class Umami::Runner

Attributes

cookbook_dir[R]

Public Class Methods

new() click to toggle source
# File lib/chef-umami/runner.rb, line 32
def initialize
  @umami_config = umami_config
  @cookbook_dir = Dir.pwd
  ## If we load the pusher or client now, they won't see the updated
  ## Chef config!
  @push = nil
  @chef_client = nil
  @ui = ui
end

Public Instance Methods

chef_client() click to toggle source
# File lib/chef-umami/runner.rb, line 94
def chef_client
  @chef_client ||= Umami::Client.new(policyfile)
end
chef_config() click to toggle source

Convenience to return the Chef::Config singleton.

# File lib/chef-umami/runner.rb, line 49
def chef_config
  Chef::Config
end
chef_zero_server() click to toggle source
# File lib/chef-umami/runner.rb, line 90
def chef_zero_server
  @chef_zero_server ||= Umami::Server.new
end
policy_group() click to toggle source
# File lib/chef-umami/runner.rb, line 61
def policy_group
  chef_config['policy_group']
end
policyfile() click to toggle source
# File lib/chef-umami/runner.rb, line 57
def policyfile
  umami_config[:policyfile]
end
policyfile_lock_file() click to toggle source

Return the computed policyfile lock name.

# File lib/chef-umami/runner.rb, line 66
def policyfile_lock_file
  policyfile.gsub(/\.rb$/, '.lock.json')
end
push() click to toggle source
# File lib/chef-umami/runner.rb, line 80
def push
  # rubocop:disable Layout/AlignHash
  @push ||= Umami::PolicyfileServices::Push.new(policyfile: policyfile,
                                                  ui:           ui,
                                                  policy_group: policy_group,
                                                  config:       chef_config,
                                                  root_dir:     cookbook_dir)
  # rubocop:enable Layout/AlignHash
end
run() click to toggle source
# File lib/chef-umami/runner.rb, line 98
def run
  validate_lock_file!
  chef_client.apply_config!
  chef_zero_server.start
  puts "\nUploading the policy and related cookbooks..."
  push.run
  puts "\nExecuting chef-client compile phase..."
  chef_client.compile
  # Build a hash of all the recipes' resources, keyed by the canonical
  # name of the recipe (i.e. ohai::default).
  recipe_resources = {}
  chef_client.resource_collection.each do |resource|
    canonical_recipe = "#{resource.cookbook_name}::#{resource.recipe_name}"
    unless umami_config[:recipes].nil? || umami_config[:recipes].empty?
      # The user has explicitly requested that one or more recipes have
      # tests written, to the exclusion of others.
      # ONLY include the recipe if it matches the list.
      next unless umami_config[:recipes].include?(canonical_recipe)
    end
    if recipe_resources.key?(canonical_recipe)
      recipe_resources[canonical_recipe] << resource
    else
      recipe_resources[canonical_recipe] = [resource]
    end
  end

  # Remove the temporary directory using a naive guard to ensure we're
  # deleting what we expect.
  re_export_path = Regexp.new('/tmp/umami')
  FileUtils.rm_rf(chef_client.staging_dir) if chef_client.staging_dir.match(re_export_path)

  if umami_config[:unit_tests]
    puts "\nGenerating a set of unit tests..."
    unit_tester = Umami::Test::Unit.new(umami_config[:test_root])
    unit_tester.generate(recipe_resources)
  end

  if umami_config[:integration_tests]
    puts "\nGenerating a set of integration tests..."
    integration_tester = Umami::Test::Integration.new(umami_config[:test_root])
    integration_tester.generate(recipe_resources)
  end
end
ui() click to toggle source
# File lib/chef-umami/runner.rb, line 53
def ui
  @ui ||= ChefDK::UI.new
end
umami_config() click to toggle source

A hash of values describing the Umami config. Comprised of command line options. May (in the future) contain options read from a config file.

# File lib/chef-umami/runner.rb, line 44
def umami_config
  @umami_config ||= parse_options
end
validate_lock_file!() click to toggle source
# File lib/chef-umami/runner.rb, line 70
def validate_lock_file!
  unless policyfile_lock_file.end_with?('lock.json')
    raise InvalidPolicyfileLockFilename, "Policyfile lock files must end in '.lock.json'. I received '#{policyfile_lock_file}'."
  end

  unless File.exist?(policyfile_lock_file)
    raise InvalidPolicyfileLockFilename, "Unable to locate '#{policyfile_lock_file}' You may need to run `chef install` to generate it."
  end
end