Add some basic tests for the changes lib
This commit is contained in:
parent
c266f35648
commit
2155bbee6c
1 changed files with 66 additions and 0 deletions
66
test/test_changes.py
Normal file
66
test/test_changes.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
import unittest
|
||||
import tarfile
|
||||
|
||||
from StringIO import StringIO
|
||||
from util.streamlayerformat import AUFS_WHITEOUT
|
||||
from util.changes import files_and_dirs_from_tar
|
||||
|
||||
class TestChanges(unittest.TestCase):
|
||||
def create_layer(self, **kwargs):
|
||||
output = StringIO()
|
||||
with tarfile.open(fileobj=output, mode='w:gz') as tar:
|
||||
for current_contents in kwargs:
|
||||
current_filename = kwargs[current_contents]
|
||||
|
||||
if current_contents == '_':
|
||||
# This is a deleted file.
|
||||
if current_filename.endswith('/'):
|
||||
current_filename = current_filename[:-1]
|
||||
|
||||
parts = current_filename.split('/')
|
||||
if len(parts) > 1:
|
||||
current_filename = '/'.join(parts[:-1]) + '/' + AUFS_WHITEOUT + parts[-1]
|
||||
else:
|
||||
current_filename = AUFS_WHITEOUT + parts[-1]
|
||||
|
||||
current_contents = ''
|
||||
|
||||
info = tarfile.TarInfo(name=current_filename)
|
||||
info.size = len(current_contents)
|
||||
tar.addfile(info, fileobj=StringIO(current_contents))
|
||||
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def test_single_layer(self):
|
||||
tar_layer = self.create_layer(
|
||||
foo = 'some_file',
|
||||
bar = 'another_file',
|
||||
meh = 'third_file')
|
||||
|
||||
deleted_prefixes = set()
|
||||
result = list(files_and_dirs_from_tar(StringIO(tar_layer), deleted_prefixes))
|
||||
|
||||
self.assertIn('/some_file', result)
|
||||
self.assertIn('/another_file', result)
|
||||
self.assertIn('/third_file', result)
|
||||
|
||||
self.assertFalse(deleted_prefixes)
|
||||
|
||||
|
||||
def test_delete_file(self):
|
||||
tar_layer = self.create_layer(
|
||||
foo = 'some_file',
|
||||
_ = 'another_file',
|
||||
meh = 'third_file')
|
||||
|
||||
deleted_prefixes = set()
|
||||
result = list(files_and_dirs_from_tar(StringIO(tar_layer), deleted_prefixes))
|
||||
|
||||
self.assertIn('/some_file', result)
|
||||
self.assertIn('/third_file', result)
|
||||
|
||||
self.assertIn('another_file', deleted_prefixes)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in a new issue