Skip to content

Commit

Permalink
Fix small specs problems
Browse files Browse the repository at this point in the history
  • Loading branch information
simonswine committed Dec 1, 2014
1 parent 8dbddb1 commit 78e388a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 57 deletions.
16 changes: 5 additions & 11 deletions lib/php_fpm_docker/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
require 'php_fpm_docker/logging'

module PhpFpmDocker

# Application that is used as init script
class Application # rubocop:disable ClassLength

@@log_path = nil
@log_path = nil

def self.log_path
@@log_path ||= log_dir_path.join('wrapper.log')
@log_path ||= log_dir_path.join('wrapper.log')
end

def self.log_path=(path)
# TODO: Check if input is pathname
@@log_path = path
path = Pathname.new path unless path.is_a? Pathname
@log_path = path
end

def self.log_dir_path
Expand All @@ -32,11 +30,6 @@ def initialize
@longname = 'PHP FPM Docker Wrapper'
end


def log_path
Application.log_dir_path.join('wrapper.log')
end

def start
print "Starting #{full_name}: "
$stdout.flush
Expand Down Expand Up @@ -180,6 +173,7 @@ def allowed_methods
[
:install,
:run,
:logger,
:bind_mounts,
:config_dir_path,
:web_path,
Expand Down
9 changes: 7 additions & 2 deletions lib/php_fpm_docker/docker_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ module PhpFpmDocker
class DockerContainer
include Logging

def self.cmd(image, *args)
def self.cmd(*args)
c = DockerContainer.create(*args)
c.output
end

def self.create(image, *args)
c = DockerContainer.new(image)
c.create(*args)
c.output
c
end

def initialize(image)
Expand Down
4 changes: 4 additions & 0 deletions lib/php_fpm_docker/docker_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def cmd(*args)
DockerContainer.cmd(self, *args)
end

def create(*args)
DockerContainer.create(self, *args)
end

def to_s
"#<DockerImage:#{name}>"
end
Expand Down
10 changes: 6 additions & 4 deletions lib/php_fpm_docker/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def reload_pools(pools = nil)
@pools_old = @pools
if pools.nil?
@pools = pools_config
puts @pools.inspect
else
@pools = pools
end
Expand Down Expand Up @@ -124,7 +123,6 @@ def pools_action(pools, pools_hashes, action)
logger.warn(pool[:object].to_s) do
"Failed to #{action}: #{e.message}"
end
raise e
end
end
else
Expand All @@ -147,10 +145,14 @@ def to_s

# Get neccessary bind mounts
def bind_mounts
mine = config['global']['bind_mounts']
begin
mine = config['global']['bind_mounts']
mine = mine.split(',')
rescue NoMethodError
mine = nil
end
parent = @app.bind_mounts
return parent if mine.nil?
mine = mine.split(',')
mine.map!(&:strip)
(mine + parent).uniq
end
Expand Down
7 changes: 4 additions & 3 deletions lib/php_fpm_docker/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module PhpFpmDocker
module Logging
def logger
return @logger if @logger
if Application.log_path.is_a? Pathname
dir = Application.log_path.parent
path = ::PhpFpmDocker::Application.log_path
if path.is_a? Pathname
dir = path.parent
FileUtils.mkdir_p dir unless dir.directory?
end
@logger = ::Logger.new Application.log_path
@logger = ::Logger.new path
@logger.formatter = proc { |severity, datetime, _progname, msg|
sprintf("%s [%-5s] [%-40.40s] %s\n", datetime, severity, to_s, msg)
}
Expand Down
91 changes: 61 additions & 30 deletions spec/unit/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,68 @@

describe PhpFpmDocker::Application do
before(:example) {
@dbl_logger_instance = double
@dbl_logger = class_double('Logger').as_stubbed_const()
allow(@dbl_logger).to receive(:new).and_return(@dbl_logger_instance)

@dbl_fileutils = class_double('FileUtils').as_stubbed_const()
allow(@dbl_fileutils).to receive(:mkdir_p)
}

let (:a_i){
described_class.new
}
let (:a_c){
described_class
i=described_class.new
mock_logger(i)
i
}
let (:pid_default) {
1234
}

describe '.log_path' do
let(:method) do
described_class.log_path
end
it 'should show class inst variable' do
described_class.instance_variable_set(:@log_path, Pathname.new('/tmp/test/test.log'))
expect(method).to eq(Pathname.new '/tmp/test/test.log')
end
it 'should append wrapper.log to default dir' do
described_class.instance_variable_set(:@log_path, nil)
expect(described_class).to receive(:log_dir_path).and_return(Pathname.new '/tmp/test')
expect(method).to eq(Pathname.new '/tmp/test/wrapper.log')
end
end

describe '.log_path=' do
let(:method) do
described_class.log_path = @args.dup
end
it 'should take a String arg' do
@args = '/tmp/test/test.log'
method
log_path = described_class.instance_variable_get(:@log_path)
expect(log_path.to_s).to eq(@args)
expect(log_path).to be_a(Pathname)
end
it 'should take a Pathname arg' do
@args = Pathname.new '/tmp/test/test.log'
method
log_path = described_class.instance_variable_get(:@log_path)
expect(log_path).to eq(@args)
end
end

describe '.log_dir_path' do
let(:method) do
described_class.log_dir_path
end
it 'should return a pathname' do
expect(method).to be_a(Pathname)
end
end

describe '#initialize' do
it "not raise error" do
expect{a_i}.not_to raise_error
end
end
describe '#help' do
let (:method) {
a_i.instance_eval{ help }
}
it "return help" do
expect(a_i).to receive(:allowed_methods).and_return([:valid])
expect{method}.to output(/valid/).to_stderr
Expand All @@ -51,7 +86,7 @@
expect{method}.not_to raise_error
end
it "incorrect arguments" do
allow(@dbl_logger_instance).to receive(:warn).with('php_name')
allow(dbl_logger).to receive(:warn).with('php_name')
expect(a_i).to receive(:parse_arguments).with(@argv).and_raise(RuntimeError, 'wrong')
expect(a_i).to receive(:help)
expect(a_i).to receive(:exit).with(3)
Expand All @@ -77,7 +112,7 @@
it "args=['name','valid']" do
valid_args
@args = ['name','valid']
expect(@dbl_logger_instance).to receive(:info)
expect(dbl_logger).to receive(:info)
expect(method).to eq(:valid)
expect(a_i.instance_variable_get(:@php_name)).to eq('name')
end
Expand All @@ -100,26 +135,22 @@
end
end
describe '#pid' do
let (:method) {
a_i.instance_eval{ pid }
}
let (:file) {
Tempfile.new('foo')
}
before (:example) do
@file = Tempfile.new('pid_file')
allow(a_i).to receive(:pid_path).and_return(Pathname.new(@file.path))
end
it 'return pid from file if exists and int' do
file.write(pid_default.to_s)
file.flush
allow(a_i).to receive(:pid_file).and_return(file.path)
@file.write(pid_default.to_s)
@file.flush
expect(method).to eq(pid_default)
end
it 'return nil without pid file' do
allow(a_i).to receive(:pid_file).and_return('/tmp/not/existing')
@file.delete
expect(method).to eq(nil)
end
it 'return nil pid from file if exists and no int' do
file.write("fuckyeah!")
file.flush
allow(a_i).to receive(:pid_file).and_return(file.path)
@file.write("fuckyeah!")
@file.flush
expect(method).to eq(nil)
end
end
Expand All @@ -134,7 +165,7 @@
a_i.instance_exec(nil) {|x| self.pid=456 }
}
after(:example) do
allow(a_i).to receive(:pid_file).and_return(@file.path)
allow(a_i).to receive(:pid_path).and_return(Pathname.new(@file.path))
method
expect(open(@file.path).read.strip.to_i).to eq(456)
end
Expand All @@ -149,15 +180,15 @@
a_i.instance_exec(nil) {|x| self.pid=x }
}
it 'pid file will removed if exists' do
allow(a_i).to receive(:pid_file).and_return(@file.path)
allow(a_i).to receive(:pid_path).and_return(@file.path)
method
expect(File.exist?(@file.path)).to eq(false)
end
it 'pid file not existing' do
path = @file.path
@file.delete
expect(@dbl_logger_instance).to receive(:debug).with(/No pid file found/)
allow(a_i).to receive(:pid_file).and_return(path)
expect(dbl_logger).to receive(:debug).with(/No pid file found/)
allow(a_i).to receive(:pid_path).and_return(path)
method
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/unit/config_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
}
let (:a_i_only) do
@path ||= Pathname.new '/tmp/etc/config.ini'
described_class.new(@path, @filter)
i=described_class.new(@path, @filter)
mock_logger i
i
end
let (:a_i) do
a_i_only
Expand Down
20 changes: 15 additions & 5 deletions spec/unit/launcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

describe PhpFpmDocker::Launcher do
before(:example) {
@dbl_pool = class_double('PhpFpmDocker::Pool').as_stubbed_const()
@dbl_c_pool = class_double('PhpFpmDocker::Pool').as_stubbed_const()

# Mock Application
@dbl_c_application = class_double('PhpFpmDocker::Application').as_stubbed_const()
allow(@dbl_c_application).to receive(:log_dir_path).and_return(Pathname.new '/tmp/test123/')
allow(@dbl_c_application).to receive(:log_path=)

# Fileutils
@orig_fileutils = FileUtils
Expand Down Expand Up @@ -182,7 +187,7 @@
compare
}
before(:example) {
allow(@dbl_pool).to receive(:new).and_return(:object)
allow(@dbl_c_pool).to receive(:new).and_return(:object)
}
it 'should not fail with nil value' do
@pools = nil
Expand All @@ -200,7 +205,7 @@
:hash2 => {:name => :name2, :config => :config2},
:hash3 => {:object => :object3}
}
expect(@dbl_pool).to receive(:new) do |args|
expect(@dbl_c_pool).to receive(:new) do |args|
expect(args).to have_key(:launcher)
expect(args).to include(@pools[:hash2])
end.and_return(:object2)
Expand All @@ -215,7 +220,7 @@
:hash1 => {:object => :object1},
:hash2 => {:object => :object2},
}
expect(@dbl_pool).not_to receive(:new)
expect(@dbl_c_pool).not_to receive(:new)
set_method_compare
end
end
Expand Down Expand Up @@ -365,7 +370,7 @@
@bind_mounts_mine = ['mine1', ' mine2 ', 'dup1 ', 'dup2' ]
}
let(:set_mine){
allow(a_i).to receive(:config).and_return({:main => {'bind_mounts' => @bind_mounts_mine.join(',')}})
allow(a_i).to receive(:config).and_return({'global' => {'bind_mounts' => @bind_mounts_mine.join(',')}})
}
let(:set_apps){
expect(@dbl_app).to receive(:bind_mounts).and_return(@bind_mounts_apps.dup)
Expand All @@ -389,6 +394,11 @@
result = method
expect(result).to contain_exactly(*result.uniq)
end
it 'does not fail if not configured' do
allow(a_i).to receive(:config).and_return({})
set_apps
expect(method).to contain_exactly(*@bind_mounts_apps)
end
end
describe 'join functions' do
before(:example){
Expand Down
4 changes: 3 additions & 1 deletion spec/unit/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class TestClassB
end
before(:example) do
@stringio = StringIO.new
stub_const("PhpFpmDocker::LOG_FILE", @stringio)
@dbl_app = double('PhpFpmDocker::Application')
allow(@dbl_app).to receive(:log_path).and_return(@stringio)
stub_const("PhpFpmDocker::Application", @dbl_app)
end
after(:example) do
expect(@stringio.string).to match(/message1/)
Expand Down

0 comments on commit 78e388a

Please sign in to comment.