Addresses duplicate code in agents_controller

- Adds agents_controller_spec
This commit is contained in:
Ryan T. Hosford 2015-01-13 00:28:02 -06:00
parent 844148db4e
commit 6572b990bb
2 changed files with 58 additions and 15 deletions

View File

@ -2,29 +2,17 @@ class Fluentd::AgentsController < ApplicationController
before_action :find_fluentd
def start
if @fluentd.agent.start
flash[:success] = t("messages.fluentd_start_stop_delay_notice", action: t('fluentd.common.start'))
else
flash[:error] = t("messages.fluentd_start_failed", brand: fluentd_ui_title) + @fluentd.agent.log_tail(1).first
end
run_action(__method__) { @fluentd.agent.log_tail(1).first }
redirect_to daemon_path(@fluentd), status: 303 # 303 is change HTTP Verb GET
end
def stop
if @fluentd.agent.stop
flash[:success] = t("messages.fluentd_start_stop_delay_notice", action: t('fluentd.common.stop'))
else
flash[:error] = t("messages.fluentd_stop_failed", brand: fluentd_ui_title)
end
run_action(__method__)
redirect_to daemon_path(@fluentd), status: 303 # 303 is change HTTP Verb GET
end
def restart
if @fluentd.agent.restart
flash[:success] = t("messages.fluentd_start_stop_delay_notice", action: t('fluentd.common.restart'))
else
flash[:error] = t("messages.fluentd_restart_failed", brand: fluentd_ui_title) + @fluentd.agent.log_tail(1).first
end
run_action(__method__) { @fluentd.agent.log_tail(1).first }
redirect_to daemon_path(@fluentd), status: 303 # 303 is change HTTP Verb GET
end
@ -32,4 +20,14 @@ class Fluentd::AgentsController < ApplicationController
@logs = @fluentd.agent.log_tail(params[:limit]).reverse if @fluentd
render json: @logs
end
private
def run_action(action)
if @fluentd.agent.public_send(action)
flash[:success] = t("messages.fluentd_start_stop_delay_notice", action: t("fluentd.common.#{action}"))
else
flash[:error] = t("messages.fluentd_#{action}_failed", brand: fluentd_ui_title)
flash[:error] += yield if block_given?
end
end
end

View File

@ -0,0 +1,45 @@
require 'spec_helper'
describe Fluentd::AgentsController do
before do
allow(controller).to receive(:current_user).and_return true
allow(controller).to receive(:find_fluentd).and_return(nil)
controller.instance_variable_set(:@fluentd, double(agent: @agent = double(:agent)))
end
describe "when the action succeeds" do
it "stops" do
expect(@agent).to receive(:stop).and_return true
put :stop
end
it "starts" do
expect(@agent).to receive(:start).and_return true
put :start
end
it "restarts" do
expect(@agent).to receive(:restart).and_return true
put :restart
end
end
describe "when the action does not succeed" do
it "stops" do
expect(@agent).to receive(:stop).and_return false
put :stop
end
it "starts" do
expect(@agent).to receive(:start).and_return false
expect(@agent).to receive(:log_tail).with(1).and_return ["some message"]
put :start
end
it "restarts" do
expect(@agent).to receive(:restart).and_return false
expect(@agent).to receive(:log_tail).with(1).and_return ["some message"]
put :restart
end
end
end