require 'timecop' require 'fileutils' module Fluent FLYDATA_HOME = Flydata::FLYDATA_HOME TABLE_POSITIONS_DIR = Flydata::FLYDATA_TABLE_POSITIONS_DIR TEST_TAG = "test_tag" TEST_DB = "test_db" TEST_TABLE = "test_table" TEST_SEQUENCE_FILE = File.join(FLYDATA_HOME, "positions/#{TEST_TABLE}.pos") TEST_SEQUENCE_NUM = 1 TEST_TABLE_BINLOG_POS = File.join(FLYDATA_HOME, "positions/#{TEST_TABLE}.binlog.pos") TEST_TABLES = "#{TEST_TABLE},test_table_1,test_table_2,test_table_3" TEST_POSITION_FILE = "test_position.binlog.pos" TEST_SENT_POSITION_FILE = "test_position.binlog.sent.pos" TEST_REVISION_FILE = File.join(FLYDATA_HOME, "positions/#{TEST_TABLE}.rev") TEST_TIMESTAMP = 1389214083 TEST_TABLE_APPEND_ONLY = "test_table_4" TEST_SEQUENCE_FILE_1 = File.join(FLYDATA_HOME, "positions/#{TEST_TABLE_APPEND_ONLY}.pos") shared_context 'flydata sync context' do def create_file(file_path, content) File.open(file_path, 'w') {|f| f.write(content)} end def delete_file(file_path) FileUtils.rm(file_path) if File.exists?(file_path) end def create_binlog_pos_file(content = '') create_file(TEST_POSITION_FILE, content) end def create_table_pos_file(tables, content = '') create_table_file(tables, 'pos', content) end def create_table_rev_file(tables, content = '') create_table_file(tables, 'rev', content) end def create_table_file(tables, ext, content = '') tables = Array(tables) tables.each do |table| create_file(File.join(FLYDATA_HOME, 'positions', "#{table}.#{ext}"), content) end end def config_value(variable_name, a_plugin = plugin) a_plugin.instance_variable_get(variable_name.to_s) end def replace_conf(conf_str, conf_name, new_value) if conf_str.include?(conf_name) conf_str.gsub(/#{conf_name} .*$/, "#{conf_name} #{new_value}") else conf_str += "\n #{conf_name} #{new_value}\n" end end def setup_initial_flydata_dirs %w(positions dump conf).each{|f| FileUtils.mkdir_p(File.join(FLYDATA_HOME, f))} end def cleanup_flydata_dirs %w(positions dump conf).each{|f| FileUtils.rm_rf(File.join(FLYDATA_HOME, f))} end before do cleanup_flydata_dirs setup_initial_flydata_dirs @current_dir_path = Dir::pwd Dir.chdir(FLYDATA_HOME) create_file(TEST_POSITION_FILE, '') end after do delete_file(TEST_POSITION_FILE) Dir.chdir(@current_dir_path) cleanup_flydata_dirs end end end