test/test_backup.rb in encbs-0.2.1.beta2 vs test/test_backup.rb in encbs-0.2.1

- old
+ new

@@ -1,22 +1,40 @@ -require File.expand_path("../helper.rb", __FILE__) +require File.expand_path('../helper', __FILE__) class TestBackup < Test::Unit::TestCase def setup @backups_path = File.expand_path("../fixtures/backups", __FILE__) + @restore_path = File.expand_path("../fixtures/restore", __FILE__) - FileUtils.rm_r @backups_path, :force => true - FileUtils.mkdir_p @backups_path - @hostname = Socket.gethostname @backup = Backup::Instance.new( @backups_path, false ) end + def teardown + FileUtils.rm_r @backups_path, :force => true + FileUtils.rm_r @restore_path, :force => true + + FileUtils.mkdir_p @backups_path + FileUtils.mkdir_p @restore_path + + File.open(File.expand_path('../fixtures/etc/root/file', __FILE__), 'w') do |f| + f.puts "Root file\n" + end + end + + def create_backup! increment = false + @local_path = File.expand_path('../fixtures/etc', __FILE__) + @local_path_hash = Digest::MD5.hexdigest @local_path + + @timestamp = @backup.create! @local_path, increment, false + @back_path = "#{@backups_path}/#{@hostname}" + end + def test_backup_attributes assert_equal( @backup.root_path, "#{@backups_path}/#{@hostname}" ) @@ -24,38 +42,133 @@ assert_equal @backup.file_item.class, Backup::FileItem::Local assert_not_nil @backup.timestamp end def test_create - local_path = File.expand_path('../fixtures/etc', __FILE__) - local_path_hash = Digest::MD5.hexdigest local_path + create_backup! - timestamp = @backup.create! local_path, false, false - back_path = "#{@backups_path}/#{@hostname}" + assert File.exists?("#{@back_path}") + assert File.exists?("#{@back_path}/meta") + assert File.exists?("#{@back_path}/meta/#{@local_path_hash}") + assert File.exists?("#{@back_path}/meta/#{@local_path_hash}/#{@timestamp}.yml") + assert File.exists?("#{@back_path}/meta/jars") + assert File.exists?("#{@back_path}/meta/jars/#{@local_path_hash}") + assert File.exists?("#{@back_path}/#{@local_path_hash}") - assert File.exists?("#{back_path}") - assert File.exists?("#{back_path}/meta") - assert File.exists?("#{back_path}/meta/#{local_path_hash}") - assert File.exists?("#{back_path}/meta/#{local_path_hash}/#{timestamp}.yml") - assert File.exists?("#{back_path}/meta/jars") - assert File.exists?("#{back_path}/meta/jars/#{local_path_hash}") - assert File.exists?("#{back_path}/#{local_path_hash}") + assert_equal "#{@local_path}/", open( + "#{@back_path}/meta/jars/#{@local_path_hash}" + ).read.chomp - assert_equal "#{local_path}/", open("#{back_path}/meta/jars/#{local_path_hash}").read.chomp - meta_index = YAML::load open("#{back_path}/meta/#{local_path_hash}/#{timestamp}.yml").read - assert meta_index.has_key? local_path + meta_index = YAML::load open( + "#{@back_path}/meta/#{@local_path_hash}/#{@timestamp}.yml" + ).read + assert meta_index.has_key? @local_path + root_file = File.expand_path '../fixtures/etc/root/file', __FILE__ assert_equal open(root_file).read, open( - "#{back_path}/#{local_path_hash}/#{timestamp}/#{Digest::MD5.hexdigest root_file}" + "#{@back_path}/#{@local_path_hash}/#{@timestamp}/#{Digest::MD5.hexdigest root_file}" ).read end - #def test_show_jars - #end + def test_create_with_crypt + @backup.rsa_key( + File.expand_path('../fixtures/rsa_key.public', __FILE__), + 2048 + ) + private_key = Crypto::Key.from_file( + File.expand_path('../fixtures/rsa_key.private', __FILE__), + 2048 + ) - #def test_jar_versions - #end + create_backup! - #def test_restore - #end + root_file = File.expand_path '../fixtures/etc/root/file', __FILE__ + root_file_content = open(root_file).read + + root_file_crypt_content = open( + "#{@back_path}/#{@local_path_hash}/#{@timestamp}/#{Digest::MD5.hexdigest root_file}" + ).read + + assert_not_equal root_file_content, root_file_crypt_content + assert_equal root_file_content, private_key.decrypt_from_stream( + root_file_crypt_content.chomp + ) + end + + def test_show_jars + create_backup! + + jars = @backup.jars + + assert jars.has_key? "#{@local_path}/" + assert_equal jars["#{@local_path}/"], Digest::MD5.hexdigest(@local_path) + end + + def test_jar_versions + create_backup! + + jar_timestamp = @backup.jar_versions @local_path + + assert_equal @timestamp, jar_timestamp.first + end + + def test_restore + create_backup! + restore_path = File.expand_path '../fixtures/restore', __FILE__ + + @backup.restore_jar_to @local_path_hash, @timestamp, restore_path + + assert "#{@restore_path}/#{@local_path}" + assert "#{@restore_path}/#{@local_path}/etc/root/file" + + root_file_content = open( + File.expand_path '../fixtures/etc/root/file', __FILE__ + ).read + assert_equal root_file_content, open( + "#{@restore_path}/#{@local_path}/root/file" + ).read + end + + def test_restore_with_decrypt + @backup.rsa_key( + File.expand_path('../fixtures/rsa_key.public', __FILE__), + 2048 + ) + create_backup! + + restore_path = File.expand_path '../fixtures/restore', __FILE__ + @backup.rsa_key( + File.expand_path('../fixtures/rsa_key.private', __FILE__), + 2048 + ) + @backup.restore_jar_to @local_path_hash, @timestamp, restore_path + + root_file_content = open( + File.expand_path '../fixtures/etc/root/file', __FILE__ + ).read + root_file_decrypt_content = open( + "#{@restore_path}/#{@local_path}/root/file" + ).read + + assert_equal root_file_content, root_file_decrypt_content + end + + def test_increment_backup + create_backup! + + File.open(File.expand_path('../fixtures/etc/root/file', __FILE__), 'w') do |f| + f.puts "Changed file\n" + end + + sleep 1 + create_backup! true + + root_file = File.expand_path '../fixtures/etc/root/file', __FILE__ + root_file_content = open(root_file).read + + assert_equal root_file_content, "Changed file\n" + assert_equal root_file_content, open( + "#{@back_path}/#{@local_path_hash}/#{@timestamp}/#{Digest::MD5.hexdigest root_file}" + ).read + end end