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
# 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

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



94
95
96
# File 'lib/chef-umami/runner.rb', line 94

def chef_client
  @chef_client ||= Umami::Client.new(policyfile)
end

#chef_configObject

Convenience to return the Chef::Config singleton.



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

def chef_config
  Chef::Config
end

#chef_zero_serverObject



90
91
92
# File 'lib/chef-umami/runner.rb', line 90

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

#policy_groupObject



61
62
63
# File 'lib/chef-umami/runner.rb', line 61

def policy_group
  chef_config['policy_group']
end

#policyfileObject



57
58
59
# File 'lib/chef-umami/runner.rb', line 57

def policyfile
  umami_config[:policyfile]
end

#policyfile_lock_fileObject

Return the computed policyfile lock name.



66
67
68
# File 'lib/chef-umami/runner.rb', line 66

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

#pushObject



80
81
82
83
84
85
86
87
88
# 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

#runObject



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
131
132
133
134
135
136
137
138
139
140
# 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

#uiObject



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

def ui
  @ui ||= ChefDK::UI.new
end

#umami_configObject

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



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

def umami_config
  @umami_config ||= parse_options
end

#validate_lock_file!Object



70
71
72
73
74
75
76
77
78
# 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