require 'spec_helper' require 'flydata/helper/action/check_remote_actions' require 'flydata/api_client' require_relative '../helper_shared_context' module Flydata module Helper module Action describe CheckRemoteActions do include_context 'helper context' let(:agent) { double("agent") } let(:api_client) do a = double("api_client") allow(a).to receive(:agent).and_return(agent) a end let(:check_remote_action) do allow(ApiClient).to receive(:instance).and_return(api_client) described_class.new(config) end let(:empty_actions) do { 'actions' => [] } end let(:send_log_action) do { 'actions' => [ { 'name' => 'send_logs', 'id' => '101' } ] } end let(:file_out) { double("file_out") } describe '#execute' do context 'when the server returns empty result' do it 'does not request any action and does not update the pos file' do expect(FileTest).to receive(:exist?). with(helper_conf.helper_action_position_path). and_return(false) #pos file does not exist expect(agent).to receive(:actions).and_return(empty_actions) expect(check_remote_action.execute).to be_falsey end end context 'when the server returns actions' do it 'requests the actions returned and updates the pos file' do expect(FileTest).to receive(:exist?).and_return(true).twice expect(File).to receive(:readlines).and_return(["100"]) expect(agent).to receive(:actions).with(100).and_return(send_log_action) expect(File).to receive(:delete).with(helper_conf.helper_action_position_path) expect(File).to receive(:open). with(helper_conf.helper_action_position_path, 'w'). and_yield file_out expect(file_out).to receive(:write).with("101") expect{ |b| check_remote_action.execute(&b) }.to yield_with_args(:send_logs, { id: 101, config: nil }) end end context 'when the server returns 401 auth error' do it 'terminates helper process' do expect(FileTest).to receive(:exist?). with(helper_conf.helper_action_position_path). and_return(false) #pos file does not exist expect(agent).to receive(:actions).and_raise(RestClient::Unauthorized) action_names = [] result = check_remote_action.execute do |action_name, config| action_names << action_name end expect(action_names).to eq([:stop_agent, :stop_helper]) expect(result).to be_falsey end end end end end end end