Class: Umami::Runner

Inherits:
Object
  • Object
show all
Includes:
Logger, Options
Defined in:
lib/chef-umami/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options

#parse_options

Methods included from Logger

#log

Constructor Details

#initializeRunner

Returns a new instance of Runner.



32
33
34
35
36
37
38
39
40
41
# File 'lib/chef-umami/runner.rb', line 32

def initialize
  @config = config
  @cookbook_dir = Dir.pwd
  @exporter = exporter
  @chef_zero_server = chef_zero_server
  # If we load the uploader or client now, they won't see the updated
  # Chef config!
  @uploader = nil
  @chef_client = nil
end

Instance Attribute Details

#cookbook_dirObject (readonly)

Returns the value of attribute cookbook_dir.



31
32
33
# File 'lib/chef-umami/runner.rb', line 31

def cookbook_dir
  @cookbook_dir
end

Instance Method Details

#chef_clientObject



80
81
82
# File 'lib/chef-umami/runner.rb', line 80

def chef_client
  @chef_client ||= Umami::Client.new
end

#chef_zero_serverObject



76
77
78
# File 'lib/chef-umami/runner.rb', line 76

def chef_zero_server
  @chef_zero_server ||= Umami::Server.new
end

#configObject

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



45
46
47
# File 'lib/chef-umami/runner.rb', line 45

def config
  @config ||= parse_options
end

#exporterObject



68
69
70
# File 'lib/chef-umami/runner.rb', line 68

def exporter
  @exporter ||= Umami::Policyfile::Exporter.new(policyfile_lock_file, cookbook_dir, policyfile)
end

#policyfileObject



49
50
51
# File 'lib/chef-umami/runner.rb', line 49

def policyfile
  config[:policyfile]
end

#policyfile_lock_fileObject

Return the computed policyfile lock name.



54
55
56
# File 'lib/chef-umami/runner.rb', line 54

def policyfile_lock_file
  policyfile.gsub(/\.rb$/, '.lock.json')
end

#runObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chef-umami/runner.rb', line 84

def run
  validate_lock_file!
  puts "\nExporting the policy, related cookbooks, and a valid client configuration..."
  exporter.export
  Chef::Config.from_file(exporter.chef_config_file)
  chef_zero_server.start
  puts "\nUploading the policy and related cookbooks..."
  uploader.upload
  puts "\nExecuting chef-client compile phase..."
  # Define Chef::Config['config_file'] lest Ohai complain.
  Chef::Config['config_file'] = exporter.chef_config_file
  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 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 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(exporter.export_root) if exporter.export_root.match(re_export_path)

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

  if config[:integration_tests]
    puts "\nGenerating a set of integration tests..."
    integration_tester = Umami::Test::Integration.new(config[:test_root])
    integration_tester.generate(recipe_resources)
  end
end

#uploaderObject



72
73
74
# File 'lib/chef-umami/runner.rb', line 72

def uploader
  @uploader ||= Umami::Policyfile::Uploader.new(policyfile_lock_file)
end

#validate_lock_file!Object



58
59
60
61
62
63
64
65
66
# File 'lib/chef-umami/runner.rb', line 58

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