From 6572b990bbb329ef2a21fe1c5482bd65cc4f7ff6 Mon Sep 17 00:00:00 2001 From: "Ryan T. Hosford" Date: Tue, 13 Jan 2015 00:28:02 -0600 Subject: [PATCH] Addresses duplicate code in agents_controller - Adds agents_controller_spec --- app/controllers/fluentd/agents_controller.rb | 28 ++++++------ .../fluentd/agents_controller_spec.rb | 45 +++++++++++++++++++ 2 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 spec/controllers/fluentd/agents_controller_spec.rb diff --git a/app/controllers/fluentd/agents_controller.rb b/app/controllers/fluentd/agents_controller.rb index bf575f3..bdf3ebb 100644 --- a/app/controllers/fluentd/agents_controller.rb +++ b/app/controllers/fluentd/agents_controller.rb @@ -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 diff --git a/spec/controllers/fluentd/agents_controller_spec.rb b/spec/controllers/fluentd/agents_controller_spec.rb new file mode 100644 index 0000000..f4ac667 --- /dev/null +++ b/spec/controllers/fluentd/agents_controller_spec.rb @@ -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