YUKI Hiroshi
null+****@clear*****
Thu Jan 16 15:48:49 JST 2014
YUKI Hiroshi 2014-01-16 15:48:49 +0900 (Thu, 16 Jan 2014) New Revision: 4568628159694393a8809829454390a9ae4a34ce https://github.com/droonga/drntest/commit/4568628159694393a8809829454390a9ae4a34ce Message: Extract engine manager from the test runner Added files: lib/drntest/engine.rb Modified files: lib/drntest/test-runner.rb Added: lib/drntest/engine.rb (+155 -0) 100644 =================================================================== --- /dev/null +++ lib/drntest/engine.rb 2014-01-16 15:48:49 +0900 (15ceb93) @@ -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 Modified: lib/drntest/test-runner.rb (+10 -115) =================================================================== --- lib/drntest/test-runner.rb 2014-01-16 15:48:00 +0900 (497eecc) +++ lib/drntest/test-runner.rb 2014-01-16 15:48:49 +0900 (1c95767) @@ -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 @@ -23,6 +23,7 @@ require "drntest/path" require "drntest/test-results" require "drntest/test-executor" require "drntest/json-loader" +require "drntest/engine" module Drntest class TestRunner @@ -32,16 +33,21 @@ module Drntest @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 @@ -50,102 +56,7 @@ module Drntest (@base_path + Path::CONFIG) +****@owner***** 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) @@ -259,21 +170,5 @@ module Drntest 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 -------------- next part -------------- HTML����������������������������... 下載