update tests
This commit is contained in:
1 parent
00eaa2d239
commit
07f2e41582
6 files changed
+96
-21
No files matched your search
@@ -32,8 +32,6 @@ class Foreman::CLI < Thor
|
|||||||
config = Foreman::Configuration.new(app)
|
config = Foreman::Configuration.new(app)
|
||||||
error "No such process: #{process}." unless config.processes[process]
|
error "No such process: #{process}." unless config.processes[process]
|
||||||
config.scale(process, amount)
|
config.scale(process, amount)
|
||||||
rescue Foreman::AppDoesNotExist
|
|
||||||
error "No such app: #{app}."
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private ######################################################################
|
private ######################################################################
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ class Foreman::Configuration
|
|||||||
amount = amount.to_i
|
amount = amount.to_i
|
||||||
|
|
||||||
if (old_amount < amount)
|
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)
|
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
|
end
|
||||||
|
|
||||||
write
|
write
|
||||||
@@ -27,8 +27,8 @@ class Foreman::Configuration
|
|||||||
|
|
||||||
def write
|
def write
|
||||||
write_file "/etc/foreman/#{app}.conf", <<-UPSTART_CONFIG
|
write_file "/etc/foreman/#{app}.conf", <<-UPSTART_CONFIG
|
||||||
#{app}_processes="#{processes.keys.join(' ')}"
|
#{app}_processes="#{processes.keys.sort.join(' ')}"
|
||||||
#{processes.keys.map { |k| "#{app}_#{k}=\"#{processes[k]}\"" }.join("\n")}
|
#{processes.keys.sort.map { |k| "#{app}_#{k}=\"#{processes[k]}\"" }.join("\n")}
|
||||||
UPSTART_CONFIG
|
UPSTART_CONFIG
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -46,10 +46,6 @@ private ######################################################################
|
|||||||
rescue Errno::ENOENT
|
rescue Errno::ENOENT
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(command)
|
|
||||||
system command
|
|
||||||
end
|
|
||||||
|
|
||||||
def write_file(filename, contents)
|
def write_file(filename, contents)
|
||||||
File.open(filename, "w") do |file|
|
File.open(filename, "w") do |file|
|
||||||
file.puts contents
|
file.puts contents
|
||||||
|
|||||||
+14
-10
@@ -31,12 +31,7 @@ class Foreman::Engine
|
|||||||
trap("TERM") { kill_and_exit("TERM") }
|
trap("TERM") { kill_and_exit("TERM") }
|
||||||
trap("INT") { kill_and_exit("INT") }
|
trap("INT") { kill_and_exit("INT") }
|
||||||
|
|
||||||
while true
|
run_loop
|
||||||
pid, status = Process.wait2
|
|
||||||
process = running_processes.delete(pid)
|
|
||||||
info "exited with code #{status}", process
|
|
||||||
fork process
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private ######################################################################
|
private ######################################################################
|
||||||
@@ -76,16 +71,25 @@ private ######################################################################
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def proctitle(title)
|
||||||
|
$0 = title
|
||||||
|
end
|
||||||
|
|
||||||
def read_procfile(procfile)
|
def read_procfile(procfile)
|
||||||
File.read(procfile)
|
File.read(procfile)
|
||||||
end
|
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
|
def running_processes
|
||||||
@running_processes ||= {}
|
@running_processes ||= {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def proctitle(title)
|
|
||||||
$0 = title
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -62,7 +62,7 @@ describe "Foreman::CLI" do
|
|||||||
describe "scale" do
|
describe "scale" do
|
||||||
describe "without an existing configuration" do
|
describe "without an existing configuration" do
|
||||||
it "displays an error" 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")
|
subject.scale("testapp", "alpha", "2")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,2 +1,49 @@
|
|||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
require "foreman/configuration"
|
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
|
||||||
@@ -1,2 +1,32 @@
|
|||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
require "foreman/engine"
|
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
|
||||||
Reference in new issue
Block a user