Class: Fhcap::CLI::Kitchen

Inherits:
ThorBase show all
Defined in:
lib/fhcap/kitchen.rb

Constant Summary collapse

TEST_SUITE_NAME =
'default'
DEFAULT_INSTANCE =
'default'

Instance Attribute Summary

Attributes inherited from ThorBase

#config, #thor

Instance Method Summary collapse

Methods inherited from ThorBase

add_shared_option, chef_server_names, cluster_template_names, config_key_collection, exit_on_failure?, #initialize, provider_names, provider_names_for, repo_names, run_cmd, run_knife_cmd, shared_options

Methods included from ReposHelper

#find_cluster, #find_cluster_pwds, #find_cluster_pwds_with_repo, #find_cluster_with_repo, #find_data_bag, #find_data_bag_item, #find_environment, #find_repo_item, #find_role, #get_cookbook_deps, #get_cookbook_meta, #get_cookbook_versions, #get_cookbooks, #get_entry_dependencies, #get_modified_cookbooks, #is_dirty?, #modified?, #repo_cfg, #repo_clusters_dir, #repo_cookbook_paths, #repo_dir, #repo_names, #repo_paths, #repos_config, #repos_dir, #run_inside_repo_dir

Methods included from ProvidersHelper

#provider_availability_zones, #provider_config, #provider_credentials, #provider_for, #provider_names, #provider_names_for, #provider_regions, #provider_type, #providers_config

Methods included from Thor::Actions

#run

Constructor Details

This class inherits a constructor from Fhcap::ThorBase

Instance Method Details

#cleanObject



167
168
169
170
171
# File 'lib/fhcap/kitchen.rb', line 167

def clean
  say("Clean Test Suite", :yellow)
  Fhcap::CLI::Kitchen.new.invoke(:destroy, ['all'], {}) if is_kitchen_generated?
  remove_file test_suite_dir
end

#generateObject



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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fhcap/kitchen.rb', line 31

def generate
  require 'chef'
  cookbooks = get_cookbooks(options, [], repo_cookbook_paths('-cookbooks'))
  roles = options[:roles] || []
  driver = options[:driver]
  provisioner = options[:provisioner]
  say("Kitchen Generate: Cookbooks = #{cookbooks} : Roles = #{roles}: Driver = #{driver} : Provisioner = #{provisioner}", :yellow)

  Fhcap::CLI::Kitchen.new.invoke(:clean, [], {}) if is_kitchen_generated? && options[:clean]
  empty_directory test_suite_dir

  kitchen_config = YAML.load_file(File.join(Fhcap.source_root, "templates", "kitchen", "kitchen.#{driver}.yml.erb"))
  kitchen_config['suites'] = []
  kitchen_config['provisioner']['name'] = provisioner
  kitchen_config['provisioner']['data_bags_path'] = File.join(repo_dir(:fhcap), 'data_bags')
  kitchen_config['provisioner']['environments_path'] = File.join(repo_dir(:fhcap), 'environments')
  kitchen_config['provisioner']['roles_path'] = File.join(repo_dir(:fhcap), 'roles')

  cookbook_dependencies = {}

  cookbooks.each do |cookbook|
    meta = cookbook_loader.cookbooks_by_name[cookbook]
    if meta
      cookbook_kitchen_yaml = File.join(meta.root_dir, ".kitchen.yml")
      if File.exists? cookbook_kitchen_yaml
        cookbook_kitchen_config = YAML.load_file(cookbook_kitchen_yaml)

        if cookbook_kitchen_config['platforms']
          cookbook_kitchen_config['platforms'].map! do |platform|
            default_values = kitchen_config['platforms'].detect {|f| f["name"] == platform['name']} || {}
            default_values.merge(platform)
          end
          kitchen_config['platforms'] = cookbook_kitchen_config['platforms']
        end

        cookbook_kitchen_config['suites'].each do |suite|
          suite['name'] = "#{suite['name']}_#{cookbook}"
          suite['run_list'].unshift 'recipe[feedhenry_utils::kitchen_docker]' if driver == 'docker'
          suite['run_list'].unshift 'recipe[apt::default]'
          suite['run_list'].unshift 'recipe[chef-solo-search]' if provisioner == 'chef_solo'
          suite['run_list'].unshift 'role[test-kitchen_server]'
          suite['run_list'].uniq!

          suite_cookbooks = [cookbook] | suite['run_list'].collect do |item|
            recipe_match = /recipe\[(.+?(?=::)|.+?(?=\]))/.match item
            recipe_match ? recipe_match[1] : nil
          end.compact.uniq

          kitchen_config['suites'] << suite

          suite_cookbooks.each do |c|
            get_cookbook_deps(c, cookbook_dependencies, {cwd: test_suite_dir})
          end

        end
      else
        say_status("ERROR", "No test suites found for #{cookbook}", :red)
      end

    else
      say_status("ERROR", "No cookbook found for #{cookbook}", :red)
    end
  end

  roles.each do |r|
    role_file = find_role(r)
    if File.exists? role_file
      ::Chef::Config[:role_path] = File.join(repo_dir(:fhcap), 'roles')
      role = ::Chef::Role.from_disk r
      run_list = role.run_list.expand('development', 'disk')

      role_cookbooks = run_list.recipes.collect do |recipe|
        recipe.split('::').first
      end.compact.uniq

      suite_attributes = {}
      suite_run_list = []
      suite_run_list << 'recipe[feedhenry_utils::kitchen_docker]' if driver == 'docker'
      suite_run_list << 'recipe[apt::default]'
      suite_run_list << 'recipe[chef-solo-search]' if provisioner == 'chef_solo'
      suite_run_list << 'role[test-kitchen_server]'
      suite_run_list << "role[#{r}]"

      role_cookbooks.each do |c|
        get_cookbook_deps(c, cookbook_dependencies, {cwd: test_suite_dir})
        meta = cookbook_loader.cookbooks_by_name[c]
        if meta
          cookbook_kitchen_yaml = File.join(meta.root_dir, ".kitchen.yml")
          if File.exists? cookbook_kitchen_yaml
            cookbook_kitchen_config = YAML.load_file(cookbook_kitchen_yaml)
            default_values = cookbook_kitchen_config['suites'].detect {|f| f["name"] == 'default'} || {}
            default_attributes = default_values['attributes'] || {}
            suite_attributes.deep_merge!(default_attributes)
          end
        end
      end

      # The env overrides in some of the wrapper cookbooks cause issues when using roles, for now we will just remove them entirely.
      if suite_attributes['feedhenry_common']
        suite_attributes['feedhenry_common'].delete('env')
      end

      suite = {}
      suite['name'] = "default_#{r}"
      suite['run_list'] = suite_run_list
      suite['attributes'] = suite_attributes
      kitchen_config['suites'] << suite

      link_cookbook_tests(role_cookbooks, options, r, 'default')

    else
      say "Unknown Role #{role_file}"
    end
  end

  # 1. Create kitchen.yml
  template(File.join("templates", "kitchen", "kitchen.generate.yml.erb"), test_suite_kitchen_yml, :kitchen_yaml => kitchen_config.to_yaml, :force => true)

  #2. Create Cheffile
  template(File.join("templates", "kitchen", "Cheffile.erb"), File.join(test_suite_dir, "Cheffile"), :cookbooks => cookbook_dependencies.to_h, :name => "Combined Cheffile for #{cookbooks}", :force => true)

  #3. Configure tests
  link_cookbook_tests(cookbooks, options)

  #4. Add custom driver config
  if driver == 'docker'
    template(File.join("templates", "kitchen", "Dockerfile-rhel"), File.join(test_suite_dir, "Dockerfile-rhel"), {}, :force => true)
    template(File.join("templates", "kitchen", "docker_id_rsa"), File.join(test_suite_dir, "docker_id_rsa"), {}, :force => true)
    template(File.join("templates", "kitchen", "docker_id_rsa.pub"), File.join(test_suite_dir, "docker_id_rsa.pub"), {}, :force => true)
  elsif driver == 'vagrant'
    template(File.join("templates", "kitchen", "Vagrantfile-osx.rb"), File.join(test_suite_dir, "Vagrantfile-osx.rb"), {}, :force => true)
  end
end