Merge pull request #134 from fluent/check_with_dryrun

Check with dryrun
This commit is contained in:
uu59 2015-01-14 15:03:32 +09:00
commit 46a4ef60c2
5 changed files with 68 additions and 7 deletions

View File

@ -42,6 +42,12 @@ class Fluentd
actual_reload actual_reload
end end
def dryrun
Bundler.with_clean_env do
system("fluentd -q --dry-run #{options_to_argv}")
end
end
def version def version
Bundler.with_clean_env do Bundler.with_clean_env do
`fluentd --version`.strip `fluentd --version`.strip
@ -60,9 +66,7 @@ class Fluentd
end end
def validate_fluentd_options def validate_fluentd_options
Bundler.with_clean_env do dryrun
system("fluentd --dry-run #{options_to_argv}")
end
end end
def actual_start def actual_start

View File

@ -133,11 +133,12 @@ class Fluentd
end end
def detached_command(cmd) def detached_command(cmd)
Bundler.with_clean_env do thread = Bundler.with_clean_env do
pid = spawn(cmd) pid = spawn(cmd)
Process.detach(pid) Process.detach(pid)
end end
sleep 1 # NOTE/FIXME: too early return will be caused incorrect status report, "sleep 1" is a adhoc hack thread.join
thread.value.exitstatus.zero?
end end
end end
end end

View File

@ -2,7 +2,6 @@ class Fluentd
class Agent class Agent
class TdAgent class TdAgent
module Macosx module Macosx
def start def start
backup_running_config do backup_running_config do
detached_command("launchctl load #{plist}") && pid_from_launchctl detached_command("launchctl load #{plist}") && pid_from_launchctl
@ -14,7 +13,11 @@ class Fluentd
end end
def restart def restart
stop && start dryrun && stop && start
end
def dryrun
detached_command("/usr/sbin/td-agent --dry-run -q --use-v1-config -c #{config_file}")
end end
private private

View File

@ -17,6 +17,10 @@ class Fluentd
# https://github.com/treasure-data/td-agent/blob/master/debian/td-agent.init#L156 # https://github.com/treasure-data/td-agent/blob/master/debian/td-agent.init#L156
detached_command('/etc/init.d/td-agent restart') detached_command('/etc/init.d/td-agent restart')
end end
def dryrun
detached_command('/etc/init.d/td-agent configtest')
end
end end
end end
end end

View File

@ -22,6 +22,7 @@ describe Fluentd::Agent do
end end
describe "#start" do describe "#start" do
before { instance.config_write "" } # ensure valid config
before { instance.stub(:running?).and_return(running) } before { instance.stub(:running?).and_return(running) }
context "running" do context "running" do
@ -95,6 +96,29 @@ describe Fluentd::Agent do
describe "#restart" do describe "#restart" do
it_should_behave_like "Restart strategy" it_should_behave_like "Restart strategy"
end end
describe "#dryrun" do
subject { instance.dryrun }
describe "valid/invalid" do
before { instance.stub(:system).and_return(ret) }
context "valid config" do
let(:ret) { true }
it { should be_truthy }
end
context "invalid config" do
let(:ret) { false }
it { should be_falsy }
end
end
it "invoke #system" do
instance.should_receive(:system).with(/--dry-run/)
subject
end
end
end end
describe "TdAgent" do describe "TdAgent" do
@ -106,6 +130,7 @@ describe Fluentd::Agent do
before do before do
instance.stub(:detached_command).and_return(true) instance.stub(:detached_command).and_return(true)
instance.stub(:pid_from_launchctl).and_return(true) instance.stub(:pid_from_launchctl).and_return(true)
instance.config_write "" # ensure valid config
end end
after do after do
@ -128,6 +153,30 @@ describe Fluentd::Agent do
expect(File.read(backup_file)).to eq File.read(instance.config_file) expect(File.read(backup_file)).to eq File.read(instance.config_file)
end end
end end
describe "#dryrun" do
subject { instance.dryrun }
describe "valid/invalid" do
before { instance.stub(:detached_command).and_return(ret) }
context "valid config" do
let(:ret) { true }
it { should be_truthy }
end
context "invalid config" do
let(:ret) { false }
it { should be_falsy }
end
end
it "invoke #system" do
# --dry-run check on Mac, configtest for Unix
instance.should_receive(:detached_command).with(/(--dry-run|configtest)/)
subject
end
end
end end
end end