Kouhei Sutou
null+****@clear*****
Mon Jan 2 01:10:00 JST 2017
Kouhei Sutou 2017-01-02 01:10:00 +0900 (Mon, 02 Jan 2017) New Revision: a54dee1fee0673c03a2df6405f7cd355d7153585 https://github.com/pgroonga/pgroonga/commit/a54dee1fee0673c03a2df6405f7cd355d7153585 Message: Add a test for pgroonga-check Added files: test/helpers/sandbox.rb test/run-test.rb test/test-pgroonga-check.rb Modified files: .travis.yml appveyor.yml Modified: .travis.yml (+9 -0) =================================================================== --- .travis.yml 2017-01-01 00:34:43 +0900 (421bd53) +++ .travis.yml 2017-01-02 01:10:00 +0900 (6e75ea4) @@ -25,6 +25,11 @@ install: - curl --silent --location https://github.com/groonga/groonga/raw/master/data/travis/setup.sh | sh - sudo apt-get update -qq - sudo apt-get install -qq -y postgresql-server-dev-${PG_VERSION} + - | + if [ ${PG_VERSION} = "9.6" ]; then + rvm use 2.3.3 --install --binary --fuzzy + gem install test-unit + fi before_script: - make DEBUG=1 - sudo make install @@ -38,5 +43,9 @@ script: fi - sudo -u postgres -H mkdir -p /tmp/space - make installcheck TMP_DIR=/tmp SETUP_TMP_DIR=no + - | + if [ ${PG_VERSION} = "9.6" ]; then + test/run-test.rb + fi after_failure: - cat regression.diffs Modified: appveyor.yml (+1 -0) =================================================================== --- appveyor.yml 2017-01-01 00:34:43 +0900 (9c18f8a) +++ appveyor.yml 2017-01-02 01:10:00 +0900 (360e9c0) @@ -61,3 +61,4 @@ test_script: --temp-instance=..\pgsql\test --schedule=schedule || (type regression.diffs & nonexistent_command) + - ruby test\run-test.rb Added: test/helpers/sandbox.rb (+161 -0) 100644 =================================================================== --- /dev/null +++ test/helpers/sandbox.rb 2017-01-02 01:10:00 +0900 (c49bebb) @@ -0,0 +1,161 @@ +require "fileutils" +require "socket" +require "stringio" + +module Helpers + module Sandbox + class << self + def included(base) + base.module_eval do + setup :setup_tmp_dir + teardown :teardown_tmp_dir + + setup :setup_db + teardown :teardown_db + + setup :setup_postgres + teardown :teardown_postgres + + setup :setup_test_db + teardown :teardown_test_db + end + end + end + + def spawn_process(*args) + env = {} + output_read, output_write = IO.pipe + error_read, error_write = IO.pipe + options = { + :out => output_write, + :err => error_write, + } + pid = spawn(env, *args, options) + output_write.close + error_write.close + [pid, output_read, error_read] + end + + def run_command(*args) + pid, output_read, error_read = spawn_process(*args) + _, status = Process.waitpid2(pid) + output = output_read.read + error = error_read.read + unless status.success? + command_line = args.join(" ") + raise "failed to run: #{command_line}: #{error}" + end + [output, error] + end + + def psql(db, sql) + run_command("psql", + "--host", @host, + "--port", @port.to_s, + "--dbname", db, + "--echo-all", + "--command", sql) + end + + def run_sql(sql) + psql(@test_db_name, sql) + end + + def start_postgres + @postgres_pid, @postgres_output, @postgres_error = + spawn_process("postgres", + "-D", @db_dir) + loop do + begin + TCPSocket.open(@host, @port) do + end + rescue SystemCallError + pid = Process.waitpid(@postgres_pid, Process::WNOHANG) + if pid + message = "failed to start postgres:\n" + message << "output:\n" + message << @postgres_output.read + message << "error:\n" + message << @postgres_error.read + @postgres_pid = nil + raise message + end + sleep(0.1) + else + break + end + end + end + + def stop_postgres + return if @postgres_pid.nil? + Process.kill(:TERM, @postgres_pid) + _, status = Process.waitpid2(@postgres_pid) + unless status.success? + puts("failed to stop postgres:") + puts("output:") + puts(@postgres_output.read) + puts("error:") + puts(@postgres_error.read) + end + end + + def setup_tmp_dir + memory_fs = "/dev/shm" + if File.exist?(memory_fs) + @tmp_dir = File.join(memory_fs, "pgroonga-check") + else + @tmp_dir = File.join(__dir__, "tmp") + end + FileUtils.rm_rf(@tmp_dir) + FileUtils.mkdir_p(@tmp_dir) + end + + def teardown_tmp_dir + FileUtils.rm_rf(@tmp_dir) + end + + def setup_db + @db_dir = @tmp_dir + @socket_dir = File.join(@db_dir, "socket") + @host = "127.0.0.1" + @port = 15432 + run_command("initdb", + "--locale", "C", + "--encoding", "UTF-8", + "-D", @db_dir) + FileUtils.mkdir_p(@socket_dir) + postgresql_conf = File.join(@db_dir, "postgresql.conf") + File.open(postgresql_conf, "a") do |conf| + conf.puts("listen_addresses = '#{@host}'") + conf.puts("port = #{@port}") + conf.puts("unix_socket_directories = '#{@socket_dir}'") + conf.puts("shared_preload_libraries = 'pgroonga-check.so'") + conf.puts("pgroonga.enable_wal = yes") + end + end + + def teardown_db + end + + def setup_postgres + start_postgres + end + + def teardown_postgres + stop_postgres + end + + def setup_test_db + @test_db_name = "test" + psql("postgres", "CREATE DATABASE #{@test_db_name}") + run_sql("CREATE EXTENSION pgroonga") + Dir.glob(File.join(@db_dir, "base", "*", "pgrn")) do |pgrn| + @test_db_dir = File.dirname(pgrn) + end + end + + def teardown_test_db + end + end +end Added: test/run-test.rb (+6 -0) 100755 =================================================================== --- /dev/null +++ test/run-test.rb 2017-01-02 01:10:00 +0900 (f039a79) @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require "test-unit" + +test_dir = __dir__ +exit(Test::Unit::AutoRunner.run(true, test_dir)) Added: test/test-pgroonga-check.rb (+32 -0) 100644 =================================================================== --- /dev/null +++ test/test-pgroonga-check.rb 2017-01-02 01:10:00 +0900 (9bd080c) @@ -0,0 +1,32 @@ +require_relative "helpers/sandbox" + +class PGroongaCheckTestCase < Test::Unit::TestCase + include Helpers::Sandbox + + sub_test_case "broken" do + sub_test_case "DB" do + test "can't open" do + run_sql("CREATE TABLE memos (content text);") + run_sql("CREATE INDEX memos_content ON memos USING pgroonga (content);") + run_sql("INSERT INTO memos VALUES ('PGroonga is good!');") + stop_postgres + File.open(File.join(@test_db_dir, "pgrn"), "w") do |pgrn| + pgrn.puts("Broken") + end + start_postgres + output = <<-OUTPUT +SET enable_seqscan = no; +SELECT * FROM memos WHERE content %% 'PGroonga'; + content +------------------- + PGroonga is good! +(1 row) + + OUTPUT + assert_equal([output, ""], + run_sql("SET enable_seqscan = no;\n" + + "SELECT * FROM memos WHERE content %% 'PGroonga';")) + end + end + end +end -------------- next part -------------- HTML����������������������������...Download