From 07f2e415823ef164b409dad71756ed7d7129241d Mon Sep 17 00:00:00 2001 From: David Dollar Date: Fri, 21 May 2010 15:31:56 -0400 Subject: [PATCH] update tests --- lib/foreman/cli.rb | 2 -- lib/foreman/configuration.rb | 12 +++----- lib/foreman/engine.rb | 24 ++++++++------- spec/foreman/cli_spec.rb | 2 +- spec/foreman/configuration_spec.rb | 47 ++++++++++++++++++++++++++++++ spec/foreman/engine_spec.rb | 30 +++++++++++++++++++ 6 files changed, 96 insertions(+), 21 deletions(-) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 23dc367..8625305 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -32,8 +32,6 @@ class Foreman::CLI < Thor config = Foreman::Configuration.new(app) error "No such process: #{process}." unless config.processes[process] config.scale(process, amount) - rescue Foreman::AppDoesNotExist - error "No such app: #{app}." end private ###################################################################### diff --git a/lib/foreman/configuration.rb b/lib/foreman/configuration.rb index 1a3eb3f..15c57b6 100644 --- a/lib/foreman/configuration.rb +++ b/lib/foreman/configuration.rb @@ -17,9 +17,9 @@ class Foreman::Configuration amount = amount.to_i if (old_amount < amount) - ((old_amount + 1) .. amount).each { |num| run "start #{app}-#{process} NUM=#{num}" } + ((old_amount + 1) .. amount).each { |num| system "start #{app}-#{process} NUM=#{num}" } elsif (amount < old_amount) - ((amount + 1) .. old_amount).each { |num| run "stop #{app}-#{process} NUM=#{num}" } + ((amount + 1) .. old_amount).each { |num| system "stop #{app}-#{process} NUM=#{num}" } end write @@ -27,8 +27,8 @@ class Foreman::Configuration def write write_file "/etc/foreman/#{app}.conf", <<-UPSTART_CONFIG -#{app}_processes="#{processes.keys.join(' ')}" -#{processes.keys.map { |k| "#{app}_#{k}=\"#{processes[k]}\"" }.join("\n")} +#{app}_processes="#{processes.keys.sort.join(' ')}" +#{processes.keys.sort.map { |k| "#{app}_#{k}=\"#{processes[k]}\"" }.join("\n")} UPSTART_CONFIG end @@ -46,10 +46,6 @@ private ###################################################################### rescue Errno::ENOENT end - def run(command) - system command - end - def write_file(filename, contents) File.open(filename, "w") do |file| file.puts contents diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index 19788e5..d55ff93 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -31,12 +31,7 @@ class Foreman::Engine trap("TERM") { kill_and_exit("TERM") } trap("INT") { kill_and_exit("INT") } - while true - pid, status = Process.wait2 - process = running_processes.delete(pid) - info "exited with code #{status}", process - fork process - end + run_loop end private ###################################################################### @@ -76,16 +71,25 @@ private ###################################################################### end end + def proctitle(title) + $0 = title + end + def read_procfile(procfile) File.read(procfile) end + def run_loop + while true + pid, status = Process.wait2 + process = running_processes.delete(pid) + info "exited with code #{status}", process + fork process + end + end + def running_processes @running_processes ||= {} end - def proctitle(title) - $0 = title - end - end diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index 094e36c..8ddffeb 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -62,7 +62,7 @@ describe "Foreman::CLI" do describe "scale" do describe "without an existing configuration" do it "displays an error" do - mock_error(subject, "No such app: testapp.") do + mock_error(subject, "No such process: alpha.") do subject.scale("testapp", "alpha", "2") end end diff --git a/spec/foreman/configuration_spec.rb b/spec/foreman/configuration_spec.rb index 7120dc3..9a8f433 100644 --- a/spec/foreman/configuration_spec.rb +++ b/spec/foreman/configuration_spec.rb @@ -1,2 +1,49 @@ require "spec_helper" require "foreman/configuration" + +describe "Foreman::Configuration" do + subject { Foreman::Configuration.new("testapp") } + + describe "initialize" do + describe "without an existing config" do + it "has no processes" do + subject.processes.length.should == 0 + end + end + + describe "with an existing config" do + it "has processes" do + write_foreman_config("testapp") + subject.processes["alpha"].should == 1 + subject.processes["bravo"].should == 2 + end + end + end + + describe "scale" do + before(:each) { write_foreman_config("testapp") } + + it "can scale up" do + mock(subject).system("start testapp-alpha NUM=2") + mock(subject).system("start testapp-alpha NUM=3") + subject.scale("alpha", 3) + end + + it "can scale down" do + mock(subject).system("stop testapp-bravo NUM=2") + subject.scale("bravo", 1) + end + end + + describe "wite" do + it "can write a configuration file" do + subject.scale("charlie", 3) + subject.scale("delta", 4) + File.read("/etc/foreman/testapp.conf").should == <<-FOREMAN_CONFIG +testapp_processes="charlie delta" +testapp_charlie="3" +testapp_delta="4" + FOREMAN_CONFIG + end + end +end diff --git a/spec/foreman/engine_spec.rb b/spec/foreman/engine_spec.rb index b778680..f2aae70 100644 --- a/spec/foreman/engine_spec.rb +++ b/spec/foreman/engine_spec.rb @@ -1,2 +1,32 @@ require "spec_helper" require "foreman/engine" + +describe "Foreman::Engine" do + subject { Foreman::Engine.new("Procfile") } + + describe "initialize" do + describe "without an existing Procfile" do + it "raises an error" do + lambda { subject }.should raise_error + end + end + + describe "with a Procfile" do + it "reads the processes" do + write_procfile + subject.processes["alpha"].command.should == "./alpha" + subject.processes["bravo"].command.should == "./bravo" + end + end + end + + describe "start" do + it "forks the processes" do + write_procfile + mock(subject).fork(subject.processes["alpha"]) + mock(subject).fork(subject.processes["bravo"]) + mock(subject).run_loop + subject.start + end + end +end