diff options
author | Tim Smith <tsmith@chef.io> | 2020-09-02 09:28:52 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-02 09:28:52 -0700 |
commit | fb4197f19f60740780c35f4ff37adbd493fdb2dc (patch) | |
tree | 903e446a1d2a779b32c00a2c7eb5975f0a955cbc | |
parent | c0468588af33fa5ac6e07e7fc8e51fb6f44a8ce1 (diff) | |
parent | 76d67e26bfb4366d9dccab1d6c96e6f0685a3d82 (diff) | |
download | chef-fb4197f19f60740780c35f4ff37adbd493fdb2dc.tar.gz |
Merge pull request #10372 from chef/shell-test-cleanup
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | spec/unit/shell/shell_ext_spec.rb | 49 | ||||
-rw-r--r-- | spec/unit/shell/shell_session_spec.rb | 99 | ||||
-rw-r--r-- | spec/unit/shell_spec.rb | 35 |
3 files changed, 97 insertions, 86 deletions
diff --git a/spec/unit/shell/shell_ext_spec.rb b/spec/unit/shell/shell_ext_spec.rb index aca88df500..b2f32efb6a 100644 --- a/spec/unit/shell/shell_ext_spec.rb +++ b/spec/unit/shell/shell_ext_spec.rb @@ -19,14 +19,57 @@ require "spec_helper" describe Shell::Extensions do + let(:test_shell_session) do + Class.new(Shell::ShellSession) do + def rebuild_node + nil + end + + def rebuild_collection + nil + end + + def loading + nil + end + + def loading_complete + nil + end + end + end + + let(:test_job_manager) do + Class.new do + attr_accessor :jobs + end + end + + let(:object_test_harness) do + Proc.new do + extend Shell::Extensions::ObjectCoreExtensions + + def conf=(new_conf) + @conf = new_conf + end + + def conf + @conf + end + + desc "rspecin'" + def rspec_method; end + end + end + describe "extending object for top level methods" do before do - @shell_client = TestableShellSession.instance + @shell_client = test_shell_session.instance allow(Shell).to receive(:session).and_return(@shell_client) - @job_manager = TestJobManager.new + @job_manager = test_job_manager.new @root_context = Object.new - @root_context.instance_eval(&ObjectTestHarness) + @root_context.instance_eval(&object_test_harness) Shell::Extensions.extend_context_object(@root_context) @root_context.conf = double("irbconf") end diff --git a/spec/unit/shell/shell_session_spec.rb b/spec/unit/shell/shell_session_spec.rb index c98989491d..b71cc6fb33 100644 --- a/spec/unit/shell/shell_session_spec.rb +++ b/spec/unit/shell/shell_session_spec.rb @@ -18,32 +18,10 @@ require "spec_helper" require "ostruct" -class TestableShellSession < Shell::ShellSession - - def rebuild_node - nil - end - - def rebuild_collection - nil - end - - def loading - nil - end - - def loading_complete - nil - end - -end - describe Shell::ShellSession do - it "is a singleton object" do expect(Shell::ShellSession).to include(Singleton) end - end describe Shell::ClientSession do @@ -120,20 +98,10 @@ end describe Shell::StandAloneSession do let(:json_attribs) { { "a" => "b" } } - let(:chef_rest) { double("Chef::ServerAPI") } let(:node) { Chef::Node.new } - let(:events) { Chef::EventDispatch::Dispatcher.new } let(:session) { Shell::StandAloneSession.instance } - let(:client) do - double("Chef::Client.new", - run_ohai: true, - load_node: true, - build_node: true, - register: true, - sync_cookbooks: {}) - end let(:recipe) { Chef::Recipe.new(nil, nil, run_context) } - let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:run_context) { Chef::RunContext.new(node, {}, Chef::EventDispatch::Dispatcher.new) } before do Chef::Config[:shell_config] = { override_runlist: [Chef::RunList::RunListItem.new("shell::override")] } @@ -172,6 +140,12 @@ describe Shell::StandAloneSession do end it "passes the shell CLI args to the client" do + client = double("Chef::Client.new", + run_ohai: true, + load_node: true, + build_node: true, + register: true, + sync_cookbooks: {}) expect(Chef::Client).to receive(:new).with(json_attribs, Chef::Config[:shell_config]).and_return(client) session.send(:rebuild_node) end @@ -180,68 +154,65 @@ end describe Shell::SoloLegacySession do let(:json_attribs) { { "a" => "b" } } + let(:node) { Chef::Node.new } + let(:session) { Shell::SoloLegacySession.instance } + let(:recipe) { Chef::Recipe.new(nil, nil, run_context) } + let(:run_context) { Chef::RunContext.new(node, {}, Chef::EventDispatch::Dispatcher.new) } before do Chef::Config[:shell_config] = { override_runlist: [Chef::RunList::RunListItem.new("shell::override")] } - Chef::Config[:shell_solo] = true - @session = Shell::SoloLegacySession.instance - @node = Chef::Node.new - @events = Chef::EventDispatch::Dispatcher.new - @run_context = @session.run_context = Chef::RunContext.new(@node, {}, @events) - @session.node = @node - @recipe = @session.recipe = Chef::Recipe.new(nil, nil, @run_context) - Shell::Extensions.extend_context_recipe(@recipe) - end - - after do - Chef::Config[:shell_solo] = nil + Chef::Config[:solo_legacy_shell] = true + session.node = node + session.json_configuration = json_attribs + session.run_context = run_context + session.recipe = recipe + Shell::Extensions.extend_context_recipe(recipe) end it "returns a collection based on it's compilation object and the extra recipe provided by chef-shell" do - allow(@session).to receive(:node_built?).and_return(true) + allow(session).to receive(:node_built?).and_return(true) kitteh = Chef::Resource::Cat.new("keyboard") - @recipe.run_context.resource_collection << kitteh - expect(@session.resource_collection.include?(kitteh)).to be true + recipe.run_context.resource_collection << kitteh + expect(session.resource_collection.include?(kitteh)).to be true end it "returns definitions from its compilation object" do - expect(@session.definitions).to eq(@run_context.definitions) + expect(session.definitions).to eq(run_context.definitions) end it "keeps json attribs and passes them to the node for consumption" do - @session.node_attributes = { "besnard_lakes" => "are_the_dark_horse" } - expect(@session.node["besnard_lakes"]).to eq("are_the_dark_horse") + session.node_attributes = { "besnard_lakes" => "are_the_dark_horse" } + expect(session.node["besnard_lakes"]).to eq("are_the_dark_horse") # pending "1) keep attribs in an ivar 2) pass them to the node 3) feed them to the node on reset" end it "generates its resource collection from the compiled cookbooks and the ad hoc recipe" do - allow(@session).to receive(:node_built?).and_return(true) + allow(session).to receive(:node_built?).and_return(true) kitteh_cat = Chef::Resource::Cat.new("kitteh") - @run_context.resource_collection << kitteh_cat + run_context.resource_collection << kitteh_cat keyboard_cat = Chef::Resource::Cat.new("keyboard_cat") - @recipe.run_context.resource_collection << keyboard_cat - # @session.rebuild_collection - expect(@session.resource_collection.include?(kitteh_cat)).to be true - expect(@session.resource_collection.include?(keyboard_cat)).to be true + recipe.run_context.resource_collection << keyboard_cat + # session.rebuild_collection + expect(session.resource_collection.include?(kitteh_cat)).to be true + expect(session.resource_collection.include?(keyboard_cat)).to be true end it "runs chef with a resource collection from the compiled cookbooks" do - allow(@session).to receive(:node_built?).and_return(true) + allow(session).to receive(:node_built?).and_return(true) chef_runner = double("Chef::Runner.new", converge: :converged) - expect(Chef::Runner).to receive(:new).with(an_instance_of(Chef::RunContext)).and_return(chef_runner) + expect(Chef::Runner).to receive(:new).with(session.recipe.run_context).and_return(chef_runner) - expect(@recipe.run_chef).to eq(:converged) + expect(recipe.run_chef).to eq(:converged) end it "passes the shell CLI args to the client" do - @client = double("Chef::Client.new", + client = double("Chef::Client.new", run_ohai: true, load_node: true, build_node: true, register: true, sync_cookbooks: {}) - expect(Chef::Client).to receive(:new).with(json_attribs, Chef::Config[:shell_config]).and_return(@client) - @session.json_configuration = json_attribs - @session.send(:rebuild_node) + expect(Chef::Client).to receive(:new).with(json_attribs, Chef::Config[:shell_config]).and_return(client) + session.send(:rebuild_node) end end diff --git a/spec/unit/shell_spec.rb b/spec/unit/shell_spec.rb index 6dba848084..18b622698e 100644 --- a/spec/unit/shell_spec.rb +++ b/spec/unit/shell_spec.rb @@ -18,26 +18,23 @@ require "spec_helper" require "ostruct" -ObjectTestHarness = Proc.new do - extend Shell::Extensions::ObjectCoreExtensions - - def conf=(new_conf) - @conf = new_conf - end - - def conf - @conf - end +describe Shell do + let(:object_test_harness) do + Proc.new do + extend Shell::Extensions::ObjectCoreExtensions - desc "rspecin'" - def rspec_method; end -end + def conf=(new_conf) + @conf = new_conf + end -class TestJobManager - attr_accessor :jobs -end + def conf + @conf + end -describe Shell do + desc "rspecin'" + def rspec_method; end + end + end before do Shell.irb_conf = {} @@ -66,7 +63,7 @@ describe Shell do conf = OpenStruct.new conf.main = Object.new - conf.main.instance_eval(&ObjectTestHarness) + conf.main.instance_eval(&object_test_harness) Shell.irb_conf[:IRB_RC].call(conf) expect(conf.prompt_c).to eq("chef > ") expect(conf.return_format).to eq(" => %s \n") @@ -107,7 +104,7 @@ describe Shell do before do @chef_object = Object.new - @chef_object.instance_eval(&ObjectTestHarness) + @chef_object.instance_eval(&object_test_harness) end it "creates help text for methods with descriptions" do |