update tests

This commit is contained in:
David Dollar
2010-05-21 15:31:56 -04:00
parent 00eaa2d239
commit 07f2e41582
6 changed files with 96 additions and 21 deletions
-2
View File
@@ -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 ######################################################################
+4 -8
View File
@@ -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
+14 -10
View File
@@ -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
+1 -1
View File
@@ -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
+47
View File
@@ -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
+30
View File
@@ -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