def testArchiveIsValid(self): """Test the DirectoryZipper.ArchiveIsValid method. Run two tests, one that we expect to pass and one that we expect to fail """ test_file_size = 1056730 self.my_mox.StubOutWithMock(os, 'stat') os.stat('/foo/0.zip').AndReturn([test_file_size]) self.my_mox.StubOutWithMock(stat, 'ST_SIZE') stat.ST_SIZE = 0 os.stat('/baz/0.zip').AndReturn([test_file_size]) mox.Replay(os.stat) test_target = divide_and_compress.DirectoryZipper( '/foo/', 'bar', test_file_size - 1, True) self.assertEqual(False, test_target.ArchiveIsValid(), msg=('ERROR: Test failed, ArchiveIsValid should have ' 'returned false, but returned true')) test_target = divide_and_compress.DirectoryZipper( '/baz/', 'bar', test_file_size + 1, True) self.assertEqual(True, test_target.ArchiveIsValid(), msg=('ERROR: Test failed, ArchiveIsValid should have' ' returned true, but returned false'))
def testRemoveLastFile(self): """Test DirectoryZipper.RemoveLastFile method. Construct a ZipInfo mock object with two records, verify that write is only called once on the new ZipFile object. """ source = self.CreateZipSource() dest = self.CreateZipDestination() source_path = ''.join([os.getcwd(), '/0-old.zip']) dest_path = ''.join([os.getcwd(), '/0.zip']) test_target = divide_and_compress.DirectoryZipper( ''.join([os.getcwd(), '/']), 'dummy', 1024 * 1024, True) self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath') test_target.OpenZipFileAtPath(source_path, mode='r').AndReturn(source) test_target.OpenZipFileAtPath(dest_path, compress=zipfile.ZIP_DEFLATED, mode='w').AndReturn(dest) self.my_mox.StubOutWithMock(os, 'rename') os.rename(dest_path, source_path) self.my_mox.StubOutWithMock(os, 'unlink') os.unlink(source_path) self.my_mox.ReplayAll() test_target.RemoveLastFile() self.my_mox.VerifyAll()
def setUp(self): """Setup all the mocks for this test.""" self.my_mox = mox.Mox() self.base_dir = '/dir1' self.output_path = '/out_dir/' self.test_target = divide_and_compress.DirectoryZipper( self.output_path, self.base_dir, 1024 * 1024, True) self.InitArgLists() self.InitOsDotPath() self.InitArchiveIsValid() self.InitWriteIndexRecord() self.InitAddFileToArchive()
def testSingleFileArchive(self): """Test behavior of FixArchive when the archive has a single member. We expect that when this method is called with an archive that has a single member that it will return False and unlink the archive. """ test_target = divide_and_compress.DirectoryZipper( ''.join([os.getcwd(), '/']), 'dummy', 1024 * 1024, True) self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath') test_target.OpenZipFileAtPath(''.join([os.getcwd(), '/0.zip']), mode='r').AndReturn( self.CreateSingleFileMock()) self.my_mox.StubOutWithMock(os, 'unlink') os.unlink(''.join([os.getcwd(), '/0.zip'])) self.my_mox.ReplayAll() self.assertEqual(False, test_target.FixArchive('SIZE')) self.my_mox.VerifyAll()
def testAddFileToArchive(self): """Test the DirectoryZipper.AddFileToArchive method. We are testing a pretty trivial method, we just expect it to look at the file its adding, so that it possible can through out a warning. """ test_target = divide_and_compress.DirectoryZipper( self.output_dir, self.input_dir, 1024 * 1024, True) self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath') archive_mock = self.CreateArchiveMock() test_target.OpenZipFileAtPath( ''.join([self.output_dir, '0.zip']), compress=zipfile.ZIP_DEFLATED).AndReturn(archive_mock) self.StubOutOsModule() self.my_mox.ReplayAll() test_target.AddFileToArchive( ''.join([self.input_dir, self.file_to_add]), zipfile.ZIP_DEFLATED) self.my_mox.VerifyAll()
def testMultiFileArchive(self): """Test behavior of DirectoryZipper.FixArchive with a multi-file archive. We expect that FixArchive will rename the old archive, adding '-old' before '.zip', read all the members except the last one of '-old' into a new archive with the same name as the original, and then unlink the '-old' copy """ test_target = divide_and_compress.DirectoryZipper( ''.join([os.getcwd(), '/']), 'dummy', 1024 * 1024, True) self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath') test_target.OpenZipFileAtPath(''.join([os.getcwd(), '/0.zip']), mode='r').AndReturn( self.CreateMultiFileMock()) self.my_mox.StubOutWithMock(test_target, 'RemoveLastFile') test_target.RemoveLastFile(''.join([os.getcwd(), '/0.zip'])) self.my_mox.StubOutWithMock(os, 'stat') os.stat(''.join([os.getcwd(), '/0.zip'])).AndReturn([49302]) self.my_mox.StubOutWithMock(stat, 'ST_SIZE') stat.ST_SIZE = 0 self.my_mox.ReplayAll() self.assertEqual(True, test_target.FixArchive('SIZE')) self.my_mox.VerifyAll()