#!/usr/bin/env ruby1.9 require 'P4' require 'FileUtils' require_relative 'MirrorPending' module MirrorTest @@STARTDIR = Dir.getwd @@TESTROOTDIR = File.join(@@STARTDIR, "mirrortest") @@P4D = "p4d" # Assume it's in the PATH attr_accessor :p4 def setup create_test_tree end def teardown if preserve? abort("Abandoning tests to preserve ") else cleanup_test_tree end end def preserve? return false end # # Set up the test tree - cleaning out any old data # def create_test_tree() if( File.directory?( @@TESTROOTDIR ) ) puts( "Removing old test tree!" ) cleanup_test_tree() end Dir.mkdir( @@TESTROOTDIR ) Dir.mkdir( server_root ) Dir.mkdir( source_root ) Dir.mkdir( target_root ) init_client create_p4config_file true end def init_client() # Create a P4 object for all tests. Makes life simple. @p4 = P4.new @p4.port = "rsh:#{@@P4D} -r #{server_root} -L log -vserver=3 -i -vdm.integ.engine=3" @p4.connect @source = "source" @target = "target" s = @p4.fetch_client(@source) s._root = source_root @p4.save_client(s) t = @p4.fetch_client(@target) t._root = target_root @p4.save_client(t) @mirror = MirrorPending.new(@p4, @source, @target) true end def use_source @p4.client = @source return source_root end def use_target @p4.client = @target return target_root end def create_mirror MirrorPending.new(@p4, @source, @target) end def get_fstat(file) @p4.run_fstat("-Oar", "-Ro", file) end def compare_fstats(state1, state2) def diff(s1, s2) (s1.keys + s2.keys).uniq.inject({}) do |memo, key| unless s1[key] == s2[key] memo[key] = [s1[key], s2[key]] end memo end end if state1.length == state2.length pass = true state1.zip(state2).each do |s1, s2| memo = diff(s1,s2) memo.each do |key,value| if key.start_with?("other") true elsif key == "clientFile" true else puts "Found difference in fstats : #{key} : #{value}" pass = false end end end puts "state1 = #{state1.inspect}\nstate2 = #{state2.inspect}" unless pass pass else false end end def mirror_and_compare sourceFstats = get_fstat("#{source_root}/...") @mirror.mirror() targetFstats = get_fstat("#{target_root}/...") assert(compare_fstats(sourceFstats, targetFstats), "source and target are not identical") end # # Cleanup the test tree # def cleanup_test_tree() # Go back to where we started, or we can't remove the tree. Dir.chdir( @@STARTDIR ) FileUtils.rm_rf( @@TESTROOTDIR ) true end # # Return the path of the server root # def server_root File.join(@@TESTROOTDIR, "root") end # # Return the source root # def source_root File.join(@@TESTROOTDIR, "source_ws") end # # Return the target root # def target_root File.join(@@TESTROOTDIR, "target_ws") end private def create_p4config_file return unless ( ENV.has_key?( 'P4CONFIG' ) ) File.open( File.join(source_root, ENV[ 'P4CONFIG' ]), "w" ) do |f| f.puts( "P4PORT=#{@p4.port}" ) f.puts( "P4CLIENT=#{@source}" ) end File.open( File.join(target_root, ENV[ 'P4CONFIG' ]), "w" ) do |f| f.puts( "P4PORT=#{@p4.port}" ) f.puts( "P4CLIENT=#{@target}" ) end end end files = Dir.entries(File.join(File.dirname(__FILE__), "tests")).select do |file| file =~ /_test.rb$/ end files.each { |file| require_relative 'tests/' + file }