def test_can_represent_linux_kernel_object_as_template(self): self.options['install-condition-pattern-type'] = 'linux-kernel' self.template[ 'install-condition-pattern-type'] = 'linux-kernel' # nopep8 self.template['filename'] = __file__ obj = Object(self.options) self.assertEqual(obj.to_template(), self.template)
def test_can_generate_template(self): obj = Object({ 'filename': __file__, 'mode': 'raw', 'target-type': 'device', 'target': '/dev/sda', 'chunk-size': 1, 'count': 2, 'seek': 3, 'skip': 4, 'truncate': True, }) expected = { 'mode': 'raw', 'filename': __file__, 'install-condition': 'always', 'target-type': 'device', 'target': '/dev/sda', 'chunk-size': 1, 'count': 2, 'seek': 3, 'skip': 4, 'truncate': True, } self.assertEqual(obj.to_template(), expected)
def test_can_generate_metadata(self): content = b'spam' fn = self.create_file(content) obj = Object({ 'filename': fn, 'mode': 'raw', 'target-type': 'device', 'target': '/dev/sda', 'chunk-size': 1, 'count': 2, 'seek': 3, 'skip': 4, 'truncate': True, }) expected = { 'mode': 'raw', 'filename': fn, 'target-type': 'device', 'target': '/dev/sda', 'chunk-size': 1, 'count': 2, 'seek': 3, 'skip': 4, 'truncate': True, 'size': len(content), 'sha256sum': hashlib.sha256(content).hexdigest(), } self.assertEqual(obj.to_metadata(), expected)
def test_can_represent_compressed_object_as_metadata(self): self.options['filename'] = os.path.join(self.fixtures_dir, 'base.txt.lzo') obj = Object(self.options) metadata = obj.to_metadata() self.assertEqual(metadata['compressed'], True) self.assertEqual(metadata['required-uncompressed-size'], self.size)
def test_can_represent_compressed_object_of_symlink_as_metadata(self): self.options['filename'] = os.path.join(self.fixtures_dir, 'symbolic.gz') obj = Object(self.options) metadata = obj.to_metadata() self.assertTrue(metadata['compressed']) self.assertEqual(metadata['required-uncompressed-size'], self.size)
def test_can_load_u_boot_version(self): self.options['install-condition-pattern-type'] = 'u-boot' self.options['filename'] = create_u_boot_file() obj = Object(self.options) metadata = obj.to_metadata() self.assertEqual(metadata['install-if-different']['version'], '13.08.1988')
def test_can_get_tar_uncompressed_size(self): self.options['filename'] = os.path.join(self.fixtures_dir, 'archive.tar.gz') obj = Object(self.options) expected = os.path.getsize( os.path.join(self.fixtures_dir, 'archive.tar')) observed = obj.to_metadata().get('required-uncompressed-size') self.assertEqual(observed, expected)
def test_cannot_overwrite_compression_properties_on_metadata(self): self.options['filename'] = os.path.join(self.fixtures_dir, 'base.txt.bz2') obj = Object(self.options) obj._compressed = True # it's a compressed file, but not supported obj.compressor = 'gzip' # and it is a bz2, not a gzip. metadata = obj.to_metadata() # luckily metadata will ignore all this self.assertIsNone(metadata.get('compressed')) self.assertIsNone(metadata.get('required-uncompressed-size'))
def test_can_load_linux_kernel_versions(self): for image, version in self.images: self.options['install-condition-pattern-type'] = 'linux-kernel' self.options['install-condition'] = 'version-diverges' self.options['filename'] = image obj = Object(self.options) metadata = obj.to_metadata() self.assertEqual(metadata['install-if-different']['version'], version)
def test_can_create_manager_from_dump(self, sets): dump = {ObjectsManager.metadata: [[self.options] for _ in range(sets)]} manager = ObjectsManager(dump=dump) self.assertEqual(len(manager), sets) self.assertEqual(len(manager.all()), sets) for objs in manager.objects: self.assertEqual(len(objs), sets) base_object = Object(self.options) for obj in manager.all(): self.assertEqual(base_object.to_metadata(), obj.to_metadata())
def test_can_load_object(self): content = b'spam' sha256sum = hashlib.sha256(content).hexdigest() md5 = hashlib.md5(content).hexdigest() self.options['filename'] = self.create_file(content) obj = Object(self.options) self.assertIsNone(obj.md5) self.assertIsNone(obj['sha256sum']) obj.load() self.assertEqual(obj.md5, md5) self.assertEqual(obj['sha256sum'], sha256sum)
def test_can_represent_u_boot_object_as_metadata(self): fn = create_u_boot_file() self.options['filename'] = fn self.options['install-condition-pattern-type'] = 'u-boot' self.metadata['install-if-different']['pattern'] = 'u-boot' self.metadata['install-if-different']['version'] = '13.08.1988' self.metadata['filename'] = fn self.metadata['size'] = os.path.getsize(fn) with open(fn, 'rb') as fp: sha256sum = hashlib.sha256(fp.read()).hexdigest() self.metadata['sha256sum'] = sha256sum obj = Object(self.options) self.assertEqual(obj.to_metadata(), self.metadata)
def test_can_represent_linux_kernel_object_as_metadata(self): self.metadata['install-if-different']['pattern'] = 'linux-kernel' for image, version in self.images: self.options['filename'] = image self.options['install-condition'] = 'version-diverges' self.options['install-condition-pattern-type'] = 'linux-kernel' self.metadata['install-if-different']['version'] = version self.metadata['filename'] = image self.metadata['size'] = os.path.getsize(image) with open(image, 'rb') as fp: sha256sum = hashlib.sha256(fp.read()).hexdigest() self.metadata['sha256sum'] = sha256sum obj = Object(self.options) self.assertEqual(obj.to_metadata(), self.metadata)
def test_can_represent_as_template(self): obj = Object(self.options) expected = { 'filename': self.fn, 'mode': 'raw', 'chunk-size': 131072, 'count': -1, 'seek': 0, 'skip': 0, 'target-type': 'device', 'target': '/dev/sda', 'truncate': False, 'install-condition': 'content-diverges', } observed = obj.to_template() self.assertEqual(observed, expected)
def test_can_create_full_object(self): obj = Object(self.full_options) self.assertEqual(obj.mode, self.mode) for option in self.mode_class.options: if not option.volatile: self.assertEqual(obj[option.metadata], self.full_options[option.metadata])
def test_can_iter_over_the_object_content(self): self.set_env_var(CHUNK_SIZE_VAR, 1) self.options['filename'] = self.create_file(b'spam') obj = Object(self.options) expected = [b's', b'p', b'a', b'm'] observed = list(obj) self.assertEqual(expected, observed)
def test_manager_as_template(self, sets): manager = ObjectsManager(sets) manager.create(self.options) template = manager.to_template()[manager.metadata] expected = [Object(self.options).to_template()] self.assertEqual(len(template), sets) for index in range(sets): self.assertEqual(template[index], expected)
def test_manager_as_metadata(self, sets): manager = ObjectsManager(sets) manager.create(self.options) metadata = manager.to_metadata()[manager.metadata] expected = [Object(self.options).to_metadata()] self.assertEqual(len(metadata), sets) for index in range(sets): self.assertEqual(metadata[index], expected)
def test_can_represent_as_metadata(self): obj = Object(self.options) expected = { 'filename': self.fn, 'mode': 'raw', 'chunk-size': 131072, 'count': -1, 'seek': 0, 'skip': 0, 'target-type': 'device', 'target': '/dev/sda', 'truncate': False, 'size': 4, 'sha256sum': hashlib.sha256(b'spam').hexdigest(), } observed = obj.to_metadata() self.assertEqual(observed, expected)
def test_can_create_object(self): obj = Object(self.options) self.assertEqual(obj['install-condition'], 'version-diverges') self.assertEqual(obj['install-condition-pattern-type'], 'regexp') self.assertEqual(obj['install-condition-pattern'], '\d+\.\d+') self.assertEqual(obj['install-condition-seek'], 3) self.assertEqual(obj['install-condition-buffer-size'], 5) self.assertIsNone(obj['install-condition-version'])
def test_object_without_install_condition_support(self): obj = Object({ 'filename': 'file.txt', 'mode': 'ubifs', 'target-type': 'ubivolume', 'target': 'system0', }) expected = self.get_fixture('install_condition_ubifs.txt') self.assertEqual(str(obj), expected)
def test_object_with_install_condition_content_diverges(self): expected = self.get_fixture('install_condition_content.txt') obj = Object({ 'filename': 'file.txt', 'mode': 'raw', 'target-type': 'device', 'target': '/', 'install-condition': 'content-diverges', }) self.assertEqual(str(obj), expected)
def test_updating_filename_cleans_compression(self): uncompressed_fn = os.path.join(self.fixtures_dir, 'base.txt') compressed_fn = os.path.join(self.fixtures_dir, 'base.txt.gz') self.options['filename'] = uncompressed_fn obj = Object(self.options) metadata = obj.to_metadata() self.assertIsNone(metadata.get('compressed')) obj.update('filename', compressed_fn) metadata = obj.to_metadata() self.assertEqual(metadata['compressed'], True) obj.update('filename', uncompressed_fn) metadata = obj.to_metadata() self.assertIsNone(metadata.get('compressed'))
def test_object_with_install_condition_known_version_diverges(self): expected = self.get_fixture('install_condition_known_version.txt') obj = Object({ 'filename': 'file.txt', 'mode': 'raw', 'target-type': 'device', 'target': '/', 'install-condition': 'version-diverges', 'install-condition-pattern-type': 'linux-kernel', }) self.assertEqual(str(obj), expected)
def test_can_generate_upload_body(self): obj = Object({ 'filename': __file__, 'mode': 'raw', 'target-type': 'device', 'target': '/dev/sda', }) obj.load() with open(__file__) as fp: data = fp.read().encode() sha = hashlib.sha256(data).hexdigest() md5 = hashlib.md5(data).hexdigest() expected = { 'filename': __file__, 'size': os.path.getsize(__file__), 'sha256sum': sha, 'md5': md5, 'chunks': 1, } self.assertEqual(obj.to_upload(), expected)
def test_object_with_install_condition_regexp_version_diverges(self): expected = self.get_fixture('install_condition_version_regexp.txt') obj = Object({ 'filename': 'file.txt', 'mode': 'raw', 'target-type': 'device', 'target': '/', 'install-condition': 'version-diverges', 'install-condition-pattern-type': 'regexp', 'install-condition-pattern': '.+', 'install-condition-seek': 0, 'install-condition-buffer-size': 100, }) self.assertEqual(str(obj), expected)
def test_can_create_default_object(self): obj = Object(self.default_options) self.assertEqual(obj.mode, self.mode) install_condition_options = [ 'install-condition-pattern-type', 'install-condition-pattern', 'install-condition-seek', 'install-condition-buffer-size' ] for option in self.mode_class.options: if option.metadata in install_condition_options: # due to its complexity, install condition will have # it own test suite. continue self.assertEqual( obj[option.metadata], self.default_options.get(option.metadata, option.default))
def test_can_represent_as_metadata(self): obj = Object(self.options) self.assertEqual(obj.to_metadata(), self.metadata)
def test_can_represent_as_template(self): obj = Object(self.options) self.assertEqual(obj.to_template(), self.template)
def test_can_load_custom_object_version(self): obj = Object(self.options) metadata = obj.to_metadata() self.assertEqual(metadata['install-if-different']['version'], '1.0')