chef-dk 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +192 -0
  3. data/LICENSE +201 -0
  4. data/README.md +198 -0
  5. data/Rakefile +18 -0
  6. data/bin/chef +25 -0
  7. data/lib/chef-dk.rb +19 -0
  8. data/lib/chef-dk/builtin_commands.rb +31 -0
  9. data/lib/chef-dk/chef_runner.rb +83 -0
  10. data/lib/chef-dk/cli.rb +135 -0
  11. data/lib/chef-dk/command/base.rb +71 -0
  12. data/lib/chef-dk/command/exec.rb +33 -0
  13. data/lib/chef-dk/command/gem.rb +47 -0
  14. data/lib/chef-dk/command/generate.rb +97 -0
  15. data/lib/chef-dk/command/generator_commands.rb +417 -0
  16. data/lib/chef-dk/command/shell_init.rb +80 -0
  17. data/lib/chef-dk/command/verify.rb +226 -0
  18. data/lib/chef-dk/commands_map.rb +115 -0
  19. data/lib/chef-dk/component_test.rb +142 -0
  20. data/lib/chef-dk/cookbook_metadata.rb +36 -0
  21. data/lib/chef-dk/cookbook_omnifetch.rb +29 -0
  22. data/lib/chef-dk/cookbook_profiler/git.rb +95 -0
  23. data/lib/chef-dk/cookbook_profiler/identifiers.rb +79 -0
  24. data/lib/chef-dk/cookbook_profiler/null_scm.rb +32 -0
  25. data/lib/chef-dk/exceptions.rb +46 -0
  26. data/lib/chef-dk/generator.rb +70 -0
  27. data/lib/chef-dk/helpers.rb +95 -0
  28. data/lib/chef-dk/policyfile/chef_server_cookbook_source.rb +46 -0
  29. data/lib/chef-dk/policyfile/community_cookbook_source.rb +84 -0
  30. data/lib/chef-dk/policyfile/cookbook_sources.rb +20 -0
  31. data/lib/chef-dk/policyfile/cookbook_spec.rb +96 -0
  32. data/lib/chef-dk/policyfile/dsl.rb +148 -0
  33. data/lib/chef-dk/policyfile/null_cookbook_source.rb +37 -0
  34. data/lib/chef-dk/policyfile_compiler.rb +217 -0
  35. data/lib/chef-dk/policyfile_lock.rb +243 -0
  36. data/lib/chef-dk/shell_out.rb +36 -0
  37. data/lib/chef-dk/skeletons/code_generator/files/default/Berksfile +3 -0
  38. data/lib/chef-dk/skeletons/code_generator/files/default/chefignore +96 -0
  39. data/lib/chef-dk/skeletons/code_generator/files/default/converge_spec.rb +9 -0
  40. data/lib/chef-dk/skeletons/code_generator/files/default/gitignore +16 -0
  41. data/lib/chef-dk/skeletons/code_generator/files/default/spec_helper.rb +8 -0
  42. data/lib/chef-dk/skeletons/code_generator/metadata.rb +8 -0
  43. data/lib/chef-dk/skeletons/code_generator/recipes/app.rb +65 -0
  44. data/lib/chef-dk/skeletons/code_generator/recipes/attribute.rb +12 -0
  45. data/lib/chef-dk/skeletons/code_generator/recipes/cookbook.rb +50 -0
  46. data/lib/chef-dk/skeletons/code_generator/recipes/cookbook_file.rb +24 -0
  47. data/lib/chef-dk/skeletons/code_generator/recipes/lwrp.rb +23 -0
  48. data/lib/chef-dk/skeletons/code_generator/recipes/recipe.rb +9 -0
  49. data/lib/chef-dk/skeletons/code_generator/recipes/template.rb +32 -0
  50. data/lib/chef-dk/skeletons/code_generator/templates/default/README.md.erb +4 -0
  51. data/lib/chef-dk/skeletons/code_generator/templates/default/attribute.rb.erb +0 -0
  52. data/lib/chef-dk/skeletons/code_generator/templates/default/cookbook_file.erb +0 -0
  53. data/lib/chef-dk/skeletons/code_generator/templates/default/default_recipe.rb.erb +8 -0
  54. data/lib/chef-dk/skeletons/code_generator/templates/default/kitchen.yml.erb +16 -0
  55. data/lib/chef-dk/skeletons/code_generator/templates/default/metadata.rb.erb +8 -0
  56. data/lib/chef-dk/skeletons/code_generator/templates/default/provider.rb.erb +0 -0
  57. data/lib/chef-dk/skeletons/code_generator/templates/default/recipe.rb.erb +0 -0
  58. data/lib/chef-dk/skeletons/code_generator/templates/default/resource.rb.erb +0 -0
  59. data/lib/chef-dk/skeletons/code_generator/templates/default/template.erb +0 -0
  60. data/lib/chef-dk/version.rb +20 -0
  61. data/spec/shared/setup_git_cookbooks.rb +53 -0
  62. data/spec/spec_helper.rb +32 -0
  63. data/spec/test_helpers.rb +59 -0
  64. data/spec/unit/chef_runner_spec.rb +70 -0
  65. data/spec/unit/cli_spec.rb +151 -0
  66. data/spec/unit/command/base_spec.rb +88 -0
  67. data/spec/unit/command/exec_spec.rb +123 -0
  68. data/spec/unit/command/generate_spec.rb +102 -0
  69. data/spec/unit/command/generator_commands_spec.rb +504 -0
  70. data/spec/unit/command/shell_init_spec.rb +109 -0
  71. data/spec/unit/command/verify_spec.rb +311 -0
  72. data/spec/unit/commands_map_spec.rb +57 -0
  73. data/spec/unit/component_test_spec.rb +126 -0
  74. data/spec/unit/cookbook_metadata_spec.rb +62 -0
  75. data/spec/unit/cookbook_profiler/git_spec.rb +127 -0
  76. data/spec/unit/cookbook_profiler/identifiers_spec.rb +79 -0
  77. data/spec/unit/fixtures/chef-runner-cookbooks/test_cookbook/recipes/recipe_one.rb +9 -0
  78. data/spec/unit/fixtures/chef-runner-cookbooks/test_cookbook/recipes/recipe_two.rb +9 -0
  79. data/spec/unit/fixtures/command/cli_test_command.rb +26 -0
  80. data/spec/unit/fixtures/command/explicit_path_example.rb +7 -0
  81. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/.kitchen.yml +16 -0
  82. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/Berksfile +3 -0
  83. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/README.md +4 -0
  84. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/chefignore +96 -0
  85. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/metadata.rb +8 -0
  86. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/recipes/default.rb +8 -0
  87. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/.kitchen.yml +16 -0
  88. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/Berksfile +3 -0
  89. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/README.md +4 -0
  90. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/chefignore +96 -0
  91. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/metadata.rb +8 -0
  92. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/recipes/default.rb +8 -0
  93. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/.kitchen.yml +16 -0
  94. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/Berksfile +3 -0
  95. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/README.md +4 -0
  96. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/chefignore +96 -0
  97. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/metadata.rb +8 -0
  98. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/recipes/default.rb +8 -0
  99. data/spec/unit/fixtures/cookbooks_api/pruned_small_universe.json +1322 -0
  100. data/spec/unit/fixtures/cookbooks_api/small_universe.json +2987 -0
  101. data/spec/unit/fixtures/cookbooks_api/universe.json +1 -0
  102. data/spec/unit/fixtures/cookbooks_api/update_fixtures.rb +36 -0
  103. data/spec/unit/fixtures/dev_cookbooks/README.md +16 -0
  104. data/spec/unit/fixtures/dev_cookbooks/bar-cookbook.gitbundle +0 -0
  105. data/spec/unit/fixtures/eg_omnibus_dir/missing_apps/bin/.keep +0 -0
  106. data/spec/unit/fixtures/eg_omnibus_dir/missing_apps/embedded/.keep +0 -0
  107. data/spec/unit/fixtures/eg_omnibus_dir/missing_apps/embedded/bin/.keep +0 -0
  108. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/bin/.keep +0 -0
  109. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/berkshelf/.keep +0 -0
  110. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/test-kitchen/.keep +0 -0
  111. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/bin/.keep +0 -0
  112. data/spec/unit/fixtures/eg_omnibus_dir/valid/bin/.keep +0 -0
  113. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/integration_test +2 -0
  114. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/verify_me +5 -0
  115. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef-dk/.keep +0 -0
  116. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef/verify_me +3 -0
  117. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/test-kitchen/verify_me +2 -0
  118. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/bin/.keep +0 -0
  119. data/spec/unit/fixtures/example_app/Policyfile.rb +0 -0
  120. data/spec/unit/fixtures/example_cookbook/.gitignore +17 -0
  121. data/spec/unit/fixtures/example_cookbook/.kitchen.yml +16 -0
  122. data/spec/unit/fixtures/example_cookbook/Berksfile +3 -0
  123. data/spec/unit/fixtures/example_cookbook/README.md +4 -0
  124. data/spec/unit/fixtures/example_cookbook/chefignore +96 -0
  125. data/spec/unit/fixtures/example_cookbook/metadata.rb +8 -0
  126. data/spec/unit/fixtures/example_cookbook/recipes/default.rb +8 -0
  127. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/.kitchen.yml +16 -0
  128. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/Berksfile +3 -0
  129. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/README.md +4 -0
  130. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/chefignore +96 -0
  131. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/metadata.rb +8 -0
  132. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/recipes/default.rb +8 -0
  133. data/spec/unit/fixtures/local_path_cookbooks/metadata-missing/README.md +2 -0
  134. data/spec/unit/policyfile/chef_server_cookbook_source_spec.rb +34 -0
  135. data/spec/unit/policyfile/community_cookbook_source_spec.rb +51 -0
  136. data/spec/unit/policyfile/cookbook_spec_spec.rb +200 -0
  137. data/spec/unit/policyfile/null_cookbook_source_spec.rb +35 -0
  138. data/spec/unit/policyfile_builder_spec.rb +489 -0
  139. data/spec/unit/policyfile_demands_spec.rb +484 -0
  140. data/spec/unit/policyfile_evaluation_spec.rb +284 -0
  141. data/spec/unit/shell_out_spec.rb +34 -0
  142. metadata +422 -0
@@ -0,0 +1,12 @@
1
+
2
+ context = ChefDK::Generator.context
3
+ cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
4
+ attribute_dir = File.join(cookbook_dir, "attributes")
5
+ attribute_path = File.join(cookbook_dir, "attributes", "#{context.new_file_basename}.rb")
6
+
7
+ directory attribute_dir
8
+
9
+ template attribute_path do
10
+ source "attribute.rb.erb"
11
+ helpers(ChefDK::Generator::TemplateHelper)
12
+ end
@@ -0,0 +1,50 @@
1
+
2
+ context = ChefDK::Generator.context
3
+ cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
4
+
5
+ # cookbook root dir
6
+ directory cookbook_dir
7
+
8
+ # metadata.rb
9
+ template "#{cookbook_dir}/metadata.rb" do
10
+ helpers(ChefDK::Generator::TemplateHelper)
11
+ end
12
+
13
+ # README
14
+ template "#{cookbook_dir}/README.md" do
15
+ helpers(ChefDK::Generator::TemplateHelper)
16
+ end
17
+
18
+ # chefignore
19
+ cookbook_file "#{cookbook_dir}/chefignore"
20
+
21
+ # Berks
22
+ cookbook_file "#{cookbook_dir}/Berksfile"
23
+
24
+ # TK
25
+ template "#{cookbook_dir}/.kitchen.yml" do
26
+ source 'kitchen.yml.erb'
27
+ helpers(ChefDK::Generator::TemplateHelper)
28
+ end
29
+
30
+ # Recipes
31
+
32
+ directory "#{cookbook_dir}/recipes"
33
+
34
+ template "#{cookbook_dir}/recipes/default.rb" do
35
+ source "default_recipe.rb.erb"
36
+ helpers(ChefDK::Generator::TemplateHelper)
37
+ end
38
+
39
+ # git
40
+ if context.have_git && !context.skip_git_init
41
+
42
+ execute("initialize-git") do
43
+ command("git init .")
44
+ cwd cookbook_dir
45
+ end
46
+
47
+ cookbook_file "#{cookbook_dir}/.gitignore" do
48
+ source "gitignore"
49
+ end
50
+ end
@@ -0,0 +1,24 @@
1
+
2
+ context = ChefDK::Generator.context
3
+ cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
4
+ files_dir = File.join(cookbook_dir, "files", "default")
5
+ cookbook_file_path = File.join(cookbook_dir, "files", "default", "#{context.new_file_basename}")
6
+
7
+ directory files_dir do
8
+ recursive true
9
+ end
10
+
11
+ if source_file = context.content_source
12
+
13
+ file cookbook_file_path do
14
+ content(IO.read(source_file))
15
+ end
16
+
17
+ else
18
+
19
+ template cookbook_file_path do
20
+ source "cookbook_file.erb"
21
+ helpers(ChefDK::Generator::TemplateHelper)
22
+ end
23
+
24
+ end
@@ -0,0 +1,23 @@
1
+
2
+ context = ChefDK::Generator.context
3
+ cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
4
+
5
+ resource_dir = File.join(cookbook_dir, "resources")
6
+ resource_path = File.join(resource_dir, "#{context.new_file_basename}.rb")
7
+
8
+ provider_dir = File.join(cookbook_dir, "providers")
9
+ provider_path = File.join(provider_dir, "#{context.new_file_basename}.rb")
10
+
11
+ directory resource_dir
12
+
13
+ template resource_path do
14
+ source "resource.rb.erb"
15
+ helpers(ChefDK::Generator::TemplateHelper)
16
+ end
17
+
18
+ directory provider_dir
19
+
20
+ template provider_path do
21
+ source "provider.rb.erb"
22
+ helpers(ChefDK::Generator::TemplateHelper)
23
+ end
@@ -0,0 +1,9 @@
1
+
2
+ context = ChefDK::Generator.context
3
+ cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
4
+ recipe_path = File.join(cookbook_dir, "recipes", "#{context.new_file_basename}.rb")
5
+
6
+ template recipe_path do
7
+ source "recipe.rb.erb"
8
+ helpers(ChefDK::Generator::TemplateHelper)
9
+ end
@@ -0,0 +1,32 @@
1
+
2
+ context = ChefDK::Generator.context
3
+ cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
4
+ template_dir = File.join(cookbook_dir, "templates", "default")
5
+ template_filename = context.new_file_basename
6
+
7
+ unless File.extname(template_filename) == ".erb"
8
+ # new_file_basename is a frozen string, so we have to create an entirely
9
+ # new string here instead of using concat.
10
+ template_filename = "#{template_filename}.erb"
11
+ end
12
+
13
+ template_path = File.join(cookbook_dir, "templates", "default", template_filename)
14
+
15
+ directory template_dir do
16
+ recursive true
17
+ end
18
+
19
+ if source_file = context.content_source
20
+
21
+ file template_path do
22
+ content(IO.read(source_file))
23
+ end
24
+
25
+ else
26
+
27
+ template template_path do
28
+ source "template.erb"
29
+ helpers(ChefDK::Generator::TemplateHelper)
30
+ end
31
+
32
+ end
@@ -0,0 +1,4 @@
1
+ # <%= cookbook_name %>
2
+
3
+ TODO: Enter the cookbook description here.
4
+
@@ -0,0 +1,8 @@
1
+ #
2
+ # Cookbook Name:: <%= cookbook_name %>
3
+ # Recipe:: default
4
+ #
5
+ # Copyright (C) <%= Time.new.strftime("%Y") %> <%= author_name %>
6
+ #
7
+ # <%= license_description %>
8
+ #
@@ -0,0 +1,16 @@
1
+ ---
2
+ driver:
3
+ name: vagrant
4
+
5
+ provisioner:
6
+ name: chef_zero
7
+
8
+ platforms:
9
+ - name: ubuntu-12.04
10
+ - name: centos-6.4
11
+
12
+ suites:
13
+ - name: default
14
+ run_list:
15
+ - recipe[<%= cookbook_name %>::default]
16
+ attributes:
@@ -0,0 +1,8 @@
1
+ name '<%= cookbook_name %>'
2
+ maintainer '<%= author_name %>'
3
+ maintainer_email '<%= author_email %>'
4
+ license '<%= license_description %>'
5
+ description 'Installs/Configures <%= cookbook_name %>'
6
+ long_description 'Installs/Configures <%= cookbook_name %>'
7
+ version '0.1.0'
8
+
@@ -0,0 +1,20 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module ChefDK
19
+ VERSION = "0.2.0"
20
+ end
@@ -0,0 +1,53 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'chef-dk/helpers'
19
+
20
+ RSpec.shared_context "setup git cookbooks" do
21
+
22
+ include ChefDK::Helpers
23
+
24
+ let(:cookbook_pristine_path) do
25
+ File.expand_path("spec/unit/fixtures/dev_cookbooks/bar-cookbook.gitbundle", project_root)
26
+ end
27
+
28
+ let(:cookbook_path) { File.join(tempdir, "bar") }
29
+
30
+ let(:current_rev) { 'dfc68070c47cbf4267be14ea87f80680cb5dafb3' }
31
+
32
+ before do
33
+ reset_tempdir
34
+ system_command("git clone #{cookbook_pristine_path} #{cookbook_path}").error!
35
+ system_command("git reset --hard #{current_rev}", cwd: cookbook_path).error!
36
+ system_command("git remote remove origin", cwd: cookbook_path).error!
37
+ end
38
+
39
+ after do
40
+ clear_tempdir
41
+ end
42
+
43
+ end
44
+
45
+ RSpec.shared_context "setup git cookbook remote" do
46
+ let(:remote_url) { "file://#{tempdir}/bar-cookbook.git" }
47
+
48
+ before do
49
+ system_command("git init --bare #{tempdir}/bar-cookbook.git").error!
50
+ system_command("git remote add origin #{remote_url}", cwd: cookbook_path).error!
51
+ system_command("git push -u origin master", cwd: cookbook_path).error!
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'rubygems'
19
+ require 'rspec/mocks'
20
+ require 'pry-debugger'
21
+ require 'test_helpers'
22
+
23
+ RSpec.configure do |c|
24
+ c.include ChefDK
25
+ c.include TestHelpers
26
+
27
+ c.after(:all) { clear_tempdir }
28
+
29
+ c.filter_run :focus => true
30
+ c.run_all_when_everything_filtered = true
31
+ c.treat_symbols_as_metadata_keys_with_true_values = true
32
+ end
@@ -0,0 +1,59 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'fileutils'
19
+ require 'tmpdir'
20
+
21
+ module TestHelpers
22
+
23
+ # A globally accessible place where we can put some state to verify that a
24
+ # test performed a certain operation.
25
+ def self.test_state
26
+ @test_state ||= {}
27
+ end
28
+
29
+ def self.reset!
30
+ @test_state = nil
31
+ end
32
+
33
+ def test_state
34
+ TestHelpers.test_state
35
+ end
36
+
37
+ def fixtures_path
38
+ File.expand_path(File.dirname(__FILE__) + "/unit/fixtures/")
39
+ end
40
+
41
+ def project_root
42
+ File.expand_path("../..", __FILE__)
43
+ end
44
+
45
+ def reset_tempdir
46
+ clear_tempdir
47
+ FileUtils.mkdir_p(tempdir)
48
+ end
49
+
50
+ def clear_tempdir
51
+ FileUtils.rm_rf(tempdir)
52
+ @tmpdir = nil
53
+ end
54
+
55
+ def tempdir
56
+ @tmpdir ||= Dir.mktmpdir("chef-dk")
57
+ File.realpath(@tmpdir)
58
+ end
59
+ end
@@ -0,0 +1,70 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'spec_helper'
19
+ require 'stringio'
20
+ require 'chef-dk/chef_runner'
21
+
22
+ describe ChefDK::ChefRunner do
23
+
24
+ let(:stdout_io) { StringIO.new }
25
+
26
+ let(:stderr_io) { StringIO.new }
27
+
28
+ let(:default_cookbook_path) do
29
+ File.expand_path("chef-runner-cookbooks", fixtures_path)
30
+ end
31
+
32
+ let(:run_list) { [ "recipe[test_cookbook::recipe_one]", "recipe[test_cookbook::recipe_two]" ] }
33
+
34
+ subject(:chef_runner) do
35
+ r = ChefDK::ChefRunner.new(default_cookbook_path, run_list)
36
+ r.stub(:stdout).and_return(stdout_io)
37
+ r.stub(:stderr).and_return(stderr_io)
38
+ r
39
+ end
40
+
41
+ it "sets up Chef::Config" do
42
+ chef_runner.configure
43
+ expect(Chef::Config.solo).to be_true
44
+ expect(Chef::Config.cookbook_path).to eq(default_cookbook_path)
45
+ expect(Chef::Config.color).to be_true
46
+ expect(Chef::Config.diff_disabled).to be_true
47
+ end
48
+
49
+ it "configures a formatter for the chef run" do
50
+ expect(chef_runner.formatter).to be_a(Chef::Formatters::Doc)
51
+ end
52
+
53
+ it "detects the platform with ohai" do
54
+ expect(chef_runner.ohai).to be_a(Ohai::System)
55
+ expect(chef_runner.ohai.data["platform"]).to_not be_nil
56
+ expect(chef_runner.ohai.data["platform_version"]).to_not be_nil
57
+ end
58
+
59
+ it "sets up chef policy" do
60
+ expect(chef_runner.policy.node.run_list).to eq(run_list)
61
+ end
62
+
63
+ it "runs a chef converge" do
64
+ chef_runner.converge
65
+ expect(test_state[:loaded_recipes]).to eq([ "recipe_one", "recipe_two" ])
66
+ expect(test_state[:converged_recipes]).to eq([ "recipe_one", "recipe_two" ])
67
+ end
68
+ end
69
+
70
+