diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2019-03-15 10:02:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-15 10:02:03 -0700 |
commit | e56e0f83c5a62d906cbcc5d52f9b0a19301f8509 (patch) | |
tree | e5d74cb8010802b9a5d7ac8472121170b49e8a58 | |
parent | e26d943ab7958a88161f3c5959fad29aa8ed8b36 (diff) | |
parent | 8d9d18672d9f124d5f702474680ee2c25f43ee35 (diff) | |
download | chef-e56e0f83c5a62d906cbcc5d52f9b0a19301f8509.tar.gz |
Merge pull request #8298 from ABewsher/abewsher/fix-delete-entire-chef-repo
Allow the use of `--delete-entire-chef-repo`
-rw-r--r-- | lib/chef/application/client.rb | 2 | ||||
-rw-r--r-- | spec/unit/application/client_spec.rb | 107 |
2 files changed, 107 insertions, 2 deletions
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb index cdeb376f96..789b938213 100644 --- a/lib/chef/application/client.rb +++ b/lib/chef/application/client.rb @@ -332,7 +332,7 @@ class Chef::Application::Client < Chef::Application else if Chef::Config[:delete_entire_chef_repo] Chef::Log.trace "Cleanup path #{Chef::Config.chef_repo_path} before extract recipes into it" - FileUtils.rm_rf(recipes_path, secure: true) + FileUtils.rm_rf(Chef::Config.chef_repo_path, secure: true) end Chef::Log.trace "Creating path #{Chef::Config.chef_repo_path} to extract recipes into" FileUtils.mkdir_p(Chef::Config.chef_repo_path) diff --git a/spec/unit/application/client_spec.rb b/spec/unit/application/client_spec.rb index f58d1ed079..f971848c5b 100644 --- a/spec/unit/application/client_spec.rb +++ b/spec/unit/application/client_spec.rb @@ -91,7 +91,7 @@ describe Chef::Application::Client, "reconfigure" do # protect the unit tests against accidental --delete-entire-chef-repo from firing # for real during tests. DO NOT delete this line. - expect(FileUtils).not_to receive(:rm_rf) + allow(FileUtils).to receive(:rm_rf) end after do @@ -207,6 +207,111 @@ describe Chef::Application::Client, "reconfigure" do end end end + + describe "--recipe-url and --local-mode" do + let(:archive) { double } + let(:config_exists) { false } + + before do + allow(Chef::Config).to receive(:chef_repo_path).and_return("the_path_to_the_repo") + allow(FileUtils).to receive(:rm_rf) + allow(FileUtils).to receive(:mkdir_p) + allow(app).to receive(:fetch_recipe_tarball) + allow(Mixlib::Archive).to receive(:new).and_return(archive) + allow(archive).to receive(:extract) + allow(Chef::Config).to receive(:from_string) + allow(IO).to receive(:read).with(File.join("the_path_to_the_repo", ".chef/config.rb")).and_return("new_config") + allow(File).to receive(:file?).with(File.join("the_path_to_the_repo", ".chef/config.rb")).and_return(config_exists) + end + + context "local mode not set" do + it "fails with a message stating local mode required" do + expect(Chef::Application).to receive(:fatal!).with("chef-client recipe-url can be used only in local-mode").and_raise("error occured") + ARGV.replace(["--recipe-url=test_url"]) + expect { app.reconfigure }.to raise_error "error occured" + end + end + + context "local mode set" do + before do + ARGV.replace(["--local-mode", "--recipe-url=test_url"]) + end + + context "--delete-entire-chef-repo" do + before do + ARGV.replace(["--local-mode", "--recipe-url=test_url", "--delete-entire-chef-repo"]) + end + + it "deletes the repo" do + expect(FileUtils).to receive(:rm_rf) + .with("the_path_to_the_repo", secure: true) + + app.reconfigure + end + end + + it "does not delete the repo" do + expect(FileUtils).not_to receive(:rm_rf) + + app.reconfigure + end + + it "sets { recipe_url: 'test_url' }" do + app.reconfigure + + expect(Chef::Config.configuration).to include recipe_url: "test_url" + end + + it "makes the repo path" do + expect(FileUtils).to receive(:mkdir_p) + .with("the_path_to_the_repo") + + app.reconfigure + end + + it "fetches the tarball" do + expect(app).to receive(:fetch_recipe_tarball) + .with("test_url", File.join("the_path_to_the_repo", "recipes.tgz")) + + app.reconfigure + end + + it "extracts the archive" do + expect(Mixlib::Archive).to receive(:new) + .with(File.join("the_path_to_the_repo", "recipes.tgz")) + .and_return(archive) + + expect(archive).to receive(:extract) + .with("the_path_to_the_repo", perms: false, ignore: /^\.$/) + + app.reconfigure + end + + context "when there is new config" do + let(:config_exists) { true } + + it "updates the config from the extracted config" do + expect(Chef::Config).to receive(:from_string) + .with( + "new_config", + File.join("the_path_to_the_repo", ".chef/config.rb") + ) + + app.reconfigure + end + end + + context "when there is no new config" do + let(:config_exists) { false } + + it "does not updates the config" do + expect(Chef::Config).not_to receive(:from_string) + + app.reconfigure + end + end + end + end end describe "when configured to not fork the client process" do |