Skip to content

Commit

Permalink
Extract engine manager from the test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jan 16, 2014
1 parent c46af79 commit 4568628
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 115 deletions.
155 changes: 155 additions & 0 deletions lib/drntest/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Copyright (C) 2014 Droonga Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require "json"
require "yajl"
require "pathname"
require "fileutils"

module Drntest
class Engine
attr_reader :config_dir, :fluentd, :fluentd_options

def initialize(params)
@config_dir = Pathname(params[:config_dir])
@default_port => params[:default_port]
@default_host => params[:default_host]
@default_tag => params[:default_tag]
@fluentd = params[:fluentd]
@fluentd_options = params[:fluentd_options]
end

def start
prepare
setup
end

def stop
teardown
end

def config_file
config_dir + "fluentd.conf"
end

def catalog_file
config_dir + "catalog.json"
end

def port
@port || @default_port
end

def host
@host || @default_host
end

def tag
@tag || @default_tag
end

private
def prepare
if catalog_file.exist?
catalog_json = JSON.parse(catalog_file.read, :symbolize_names => true)
zone = catalog_json[:zones].first
/\A([^:]+):(\d+)\/(.+)\z/ =~ zone
@host = "localhost" # $1
@port = $2.to_i
@tag = $3
end
end

def setup
return unless temporary?

setup_temporary_dir

temporary_config = temporary_dir + "fluentd.conf"
FileUtils.cp(config_file, temporary_config)
temporary_catalog = temporary_dir + "catalog.json"
FileUtils.cp(catalog_file, temporary_catalog)

command = [
@fluentd,
"--config", temporary_config.to_s,
*@fluentd_options,
]
env = {
"DROONGA_CATALOG" => temporary_catalog.to_s,
}
options = {
:chdir => temporary_dir.to_s,
STDERR => STDOUT,
}
arguments = [env, *command]
arguments << options
@pid = Process.spawn(*arguments)

wait_until_ready
end

def teardown
return unless temporary?

Process.kill(:TERM, @pid)
Process.wait(@pid)

teardown_temporary_dir
end

def setup_temporary_dir
tmpfs = Pathname("/run/shm")
if tmpfs.directory? and tmpfs.writable?
FileUtils.rm_rf(temporary_base_dir)
FileUtils.ln_s(tmpfs.to_s, temporary_base_dir.to_s)
end
FileUtils.rm_rf(temporary_dir)
FileUtils.mkdir_p(temporary_dir)
end

def teardown_temporary_dir
FileUtils.rm_rf(temporary_dir.to_s)
end

def temporary_base_dir
@base_path + "tmp"
end

def temporary_dir
temporary_base_dir + "drntest"
end

def temporary?
@fluentd && config_file.exist?
end

def ready?
begin
socket = TCPSocket.new(@host, @port)
socket.close
true
rescue Errno::ECONNREFUSED
false
end
end

def wait_until_ready
until ready?
sleep 1
end
end
end
end
125 changes: 10 additions & 115 deletions lib/drntest/test-runner.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2013 Droonga Project
# Copyright (C) 2013-2014 Droonga Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -23,6 +23,7 @@
require "drntest/test-results"
require "drntest/test-executor"
require "drntest/json-loader"
require "drntest/engine"

module Drntest
class TestRunner
Expand All @@ -32,16 +33,21 @@ def initialize(owner, target)
@owner = owner
@base_path = Pathname(owner.base_path)
@target_path = Pathname(target)
@engine = Engine.new(:config_dir => config_dir,
:default_port => @owner.port,
:default_host => @owner.host,
:default_tag => @owner.tag,
:fluentd => @owner.fluentd,
:fluentd_options => @owner.fluentd_options)
end

def run
print "#{@target_path}: "
prepare
setup
@engine.start
begin
results = process_requests
ensure
teardown
@engine.stop
end
results
end
Expand All @@ -50,102 +56,7 @@ def config_dir
(@base_path + Path::CONFIG) + @owner.config
end

def config_file
config_dir + "fluentd.conf"
end

def catalog_file
config_dir + "catalog.json"
end

def port
@port || @owner.port
end

def host
@host || @owner.host
end

def tag
@tag || @owner.tag
end

private
def prepare
if catalog_file.exist?
catalog_json = JSON.parse(catalog_file.read, :symbolize_names => true)
zone = catalog_json[:zones].first
/\A([^:]+):(\d+)\/(.+)\z/ =~ zone
@host = "localhost" # $1
@port = $2.to_i
@tag = $3
end
end

def setup
return unless temporary_engine?

setup_temporary_dir

temporary_config = temporary_dir + "fluentd.conf"
FileUtils.cp(config_file, temporary_config)
temporary_catalog = temporary_dir + "catalog.json"
FileUtils.cp(catalog_file, temporary_catalog)

engine_command = [
@owner.fluentd,
"--config", temporary_config.to_s,
*@owner.fluentd_options,
]
engine_env = {
"DROONGA_CATALOG" => temporary_catalog.to_s,
}
engine_options = {
:chdir => temporary_dir.to_s,
STDERR => STDOUT,
}
arguments = [engine_env, *engine_command]
arguments << engine_options
@engine_pid = Process.spawn(*arguments)

wait_until_engine_ready
end

def teardown
return unless temporary_engine?

Process.kill(:TERM, @engine_pid)
Process.wait(@engine_pid)

teardown_temporary_dir
end

def setup_temporary_dir
tmpfs = Pathname("/run/shm")
if tmpfs.directory? and tmpfs.writable?
FileUtils.rm_rf(temporary_base_dir)
FileUtils.ln_s(tmpfs.to_s, temporary_base_dir.to_s)
end
FileUtils.rm_rf(temporary_dir)
FileUtils.mkdir_p(temporary_dir)
end

def teardown_temporary_dir
FileUtils.rm_rf(temporary_dir.to_s)
end

def temporary_base_dir
@base_path + "tmp"
end

def temporary_dir
temporary_base_dir + "drntest"
end

def temporary_engine?
@owner.fluentd && config_file.exist?
end

def process_requests
results = TestResults.new(@target_path)

Expand Down Expand Up @@ -259,21 +170,5 @@ def create_temporary_file(key, content)
file.close
yield(file)
end

def engine_ready?
begin
socket = TCPSocket.new(@host, @port)
socket.close
true
rescue Errno::ECONNREFUSED
false
end
end

def wait_until_engine_ready
until engine_ready?
sleep 1
end
end
end
end

0 comments on commit 4568628

Please sign in to comment.