spec/unit/ridley/resources/node_resource_spec.rb in ridley-0.12.4 vs spec/unit/ridley/resources/node_resource_spec.rb in ridley-1.0.0.rc1

- old
+ new

@@ -1,166 +1,106 @@ require 'spec_helper' describe Ridley::NodeResource do let(:host) { "33.33.33.10" } let(:worker) { double('worker', alive?: true, terminate: nil) } + let(:host_commander) { double('host-commander') } let(:options) do { - server_url: "https://api.opscode.com/organizations/vialstudios", - validator_path: "/some/path", - validator_client: "chef-validator", - encrypted_data_bag_secret: "hellokitty", - ssh: { - user: "reset", - password: "lol" - }, - winrm: { - user: "Administrator", - password: "secret" - }, - chef_version: "11.4.0" + server_url: double('server_url'), + validator_path: double('validator_path'), + validator_client: double('validator_client'), + encrypted_data_bag_secret: double('encrypted_data_bag_secret'), + ssh: double('ssh'), + winrm: double('winrm'), + chef_version: double('chef_version') } end - let(:instance) { described_class.new(double, options) } + let(:instance) do + inst = described_class.new(double, options) + inst.stub(connection: chef_zero_connection, host_commander: host_commander) + inst + end describe "#bootstrap" do - let(:hosts) { [ "192.168.1.2" ] } - let(:options) do - { - validator_path: fixtures_path.join("reset.pem").to_s, - encrypted_data_bag_secret: File.read(fixtures_path.join("reset.pem")) - } + it "sends the message #bootstrap to the instance's host_commander" do + host_commander.should_receive(:bootstrap).with(host, options) + instance.bootstrap(host) end - let(:bootstrapper) { double('bootstrapper', run: nil) } - subject { instance } - before { Ridley::Bootstrapper.should_receive(:new).with(hosts, anything).and_return(bootstrapper) } - it "runs the Bootstrapper" do - bootstrapper.should_receive(:run) - - subject.bootstrap("192.168.1.2", options) + it "passes pre-configured options to #bootstrap" do + host_commander.should_receive(:bootstrap).with(host, options) + instance.bootstrap(host) end end describe "#chef_run" do - let(:chef_run) { instance.chef_run(host) } - let(:response) { [:ok, double('response', stdout: 'success_message')] } - subject { chef_run } - - before do - Ridley::HostConnector.stub(:new).and_return(worker) - worker.stub(:chef_client).and_return(response) + it "sends the message #chef_client to the instance's host_commander" do + host_commander.should_receive(:chef_client).with(host, ssh: instance.ssh, winrm: instance.winrm) + instance.chef_run(host) end - - it { should eql(response) } - - context "when it executes unsuccessfully" do - let(:response) { [ :error, double('response', stderr: 'failure_message') ] } - - it { should eql(response) } - end - - it "terminates the worker" do - worker.should_receive(:terminate) - chef_run - end end describe "#put_secret" do - let(:put_secret) { instance.put_secret(host) } - let(:response) { [ :ok, double('response', stdout: 'success_message') ] } - subject { put_secret } + let(:secret) { options[:encrypted_data_bag_secret] } - before do - Ridley::HostConnector.stub(:new).and_return(worker) - worker.stub(:put_secret).and_return(response) + it "sends the message #put_secret to the instance's host_commander" do + host_commander.should_receive(:put_secret).with(host, secret, options.slice(:ssh, :winrm)) + instance.put_secret(host) end - - it { should eql(response) } - - context "when it executes unsuccessfully" do - let(:response) { [ :error, double('response', stderr: 'failure_message') ] } - - it { should eql(response) } - end - - it "terminates the worker" do - worker.should_receive(:terminate) - put_secret - end end describe "#ruby_script" do - let(:ruby_script) { instance.ruby_script(host, command_lines) } - let(:response) { [:ok, double('response', stdout: 'success_message')] } let(:command_lines) { ["puts 'hello'", "puts 'there'"] } - subject { ruby_script } - before do - Ridley::HostConnector.stub(:new).and_return(worker) - worker.stub(:ruby_script).and_return(response) + it "sends the message #ruby_script to the instance's host_commander" do + host_commander.should_receive(:ruby_script).with(host, command_lines, ssh: instance.ssh, winrm: instance.winrm) + instance.ruby_script(host, command_lines) end - - it { should eq(response) } - - context "when it executes unsuccessfully" do - let(:response) { [:error, double('response', stderr: 'failure_message')] } - - it { should eq(response) } - end - - it "terminates the worker" do - worker.should_receive(:terminate) - ruby_script - end end describe "#execute_command" do - let(:execute_command) { instance.execute_command(host, command) } - let(:response) { [:ok, double('response', stdout: 'success_message')] } let(:command) { "echo 'hello world'" } - subject { execute_command } - before do - Ridley::HostConnector.stub(:new).and_return(worker) - worker.stub(:run).and_return(response) + it "sends the message #run to the instance's host_commander" do + host_commander.should_receive(:run).with(host, command, ssh: instance.ssh, winrm: instance.winrm) + instance.execute_command(host, command) end - - it { should eq(response) } - - context "when it executes unsuccessfully" do - let(:response) { [:error, double('response', stderr: 'failure_message')] } - - it { should eq(response) } - end end describe "#merge_data" do - subject { instance } - let(:node) { double('node') } - let(:data) { double('data') } + let(:node_name) { "rspec-test" } + let(:run_list) { [ "recipe[one]", "recipe[two]" ] } + let(:attributes) { { deep: { two: "val" } } } - before { subject.stub(find: nil) } + subject(:result) { instance.merge_data(node_name, run_list: run_list, attributes: attributes) } - context "when a node with the given name exists" do - before { subject.should_receive(:find).and_return(node) } + context "when a node of the given name exists" do + before do + chef_node(node_name, + run_list: [ "recipe[one]", "recipe[three]" ], + normal: { deep: { one: "val" } } + ) + end - it "finds the target node, sends it the merge_data message, and updates it" do - updated = double('updated') - node.should_receive(:merge_data).with(data).and_return(updated) - subject.should_receive(:update).with(updated) + it "returns a Ridley::NodeObject" do + expect(result).to be_a(Ridley::NodeObject) + end - subject.merge_data(node, data) + it "has a union between the run list of the original node and the new run list" do + expect(result.run_list).to eql(["recipe[one]","recipe[three]","recipe[two]"]) end + + it "has a deep merge between the attributes of the original node and the new attributes" do + expect(result.normal.to_hash).to eql(deep: { one: "val", two: "val" }) + end end context "when a node with the given name does not exist" do - before { subject.should_receive(:find).with(node).and_return(nil) } + let(:node_name) { "does_not_exist" } it "raises a ResourceNotFound error" do - expect { - subject.merge_data(node, data) - }.to raise_error(Ridley::Errors::ResourceNotFound) + expect { result }.to raise_error(Ridley::Errors::ResourceNotFound) end end end end