require 'spec_helper' require 'flydata/command/helper' module Flydata module Command describe Helper do include STDCapture let(:helper) { described_class.new } PROCESS_REGEX = /^\[ -f .*helper.pid \] \&\& pgrep -P `cat .*helper.pid`$/ STOP_REGEX = /kill `cat .*helper.pid`\n/ RELOAD_REGEX = /kill -HUP `cat .*helper.pid`/ shared_examples "test start command" do context "when the Helper process is not running" do it "starts the Helper process" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("0") expect(Flydata::Helper::Server).to receive(:run) do |opts| if check_overrides expect(opts[:log_level]).to eq(override_log_level) expect(opts[:log]).to eq(override_log_path) expect(opts[:log_rotate_age]).to eq(override_log_rotate) expect(opts[:log_rotate_size]).to eq(override_log_rotate_size) end end subject end end context "when the Helper process is running" do it "does not attempt to start the Helper" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("1") expect(Flydata::Helper::Server).to_not receive(:run) subject end end end describe "#start" do subject { helper.start } context "when the helper is created without any options" do let(:check_overrides) { false } include_examples "test start command" end context "when the helper is created with options" do let(:helper) do options = described_class.slop_start options.parse!(%w(-e debug -l /.flydata/log_path -r 20 -s 1073741824)) described_class.new(options) end let(:check_overrides) { true } let(:override_log_level) { "debug" } let(:override_log_path) { "/.flydata/log_path" } let(:override_log_rotate) { 20 } let(:override_log_rotate_size) { 1073741824 } # 1 GB include_examples "test start command" end end describe "#stop" do subject { helper.stop } context "when the Helper process is running" do it "stops the process" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("1") expect(Open3).to receive(:capture3) do |env, cmd| expect(cmd).to match(STOP_REGEX) end subject end end context "when the Helper process is not running" do it "does not attempt to stop the process" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("0") expect(Open3).to_not receive(:capture3) subject end end end describe "#restart" do subject { helper.restart } context "when the Helper process is running" do it "stops and starts the process" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("1") expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("0") expect(Open3).to receive(:capture3) do |env, cmd| expect(cmd).to match(STOP_REGEX) end expect(Flydata::Helper::Server).to receive(:run) subject end end context "when the Helper process is not running" do it "starts the process" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("0").twice expect(Flydata::Helper::Server).to receive(:run) subject end end end describe '#reload' do subject { helper.reload } context "when the Helper process is running" do it "sends the KILL -HUP to the process to reload config" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("1") expect(Open3).to receive(:capture3) do |env, cmd| expect(cmd).to match(RELOAD_REGEX) end subject end end end describe "#status" do subject { helper.status } context "when the Helper process is running" do it "shows a message to the user that indicates that the process is running" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("1") subject expect(stdout).to match(/Helper is running/) end end context "when the Helper process is not running" do it "shows a message to the user that indicates that the process is not running" do expect(helper).to receive(:'`').with(PROCESS_REGEX).and_return("0") subject expect(stdout).to match(/Helper is not running/) end end end end end end