def test_metadata_not_shared(self): # Make sure defaults are not shared between objects rel = deb822.Release(dict(Architectures="amd64 i386 aarch64")) md1 = AptRepoMeta(rel) self.assertEqual(['amd64', 'i386', 'aarch64'], md1.architectures) md1.architectures = ['amd64'] self.assertEqual(['amd64'], md1.architectures) md2 = AptRepoMeta(rel) self.assertEqual(['amd64', 'i386', 'aarch64'], md2.architectures)
def test_AptRepo_repo_name(self): meta = AptRepoMeta(codename='aaa') repo = AptRepo(self.new_repo_dir, metadata=meta) self.assertEqual('aaa', repo.repo_name) repo = AptRepo(self.new_repo_dir, metadata=meta, repo_name='bbb') self.assertEqual('bbb', repo.repo_name)
def test_apt_repo_bad_signing_options(self): meta = AptRepoMeta(codename=self.name) with self.assertRaises(ValueError) as ctx: AptRepo(self.new_repo_dir, metadata=meta, gpg_sign_options="really?") self.assertTrue( str(ctx.exception).startswith('gpg_sign_options: unexpected type'))
def test_create_download_request_from_updates_repo(self): repo = AptRepoMeta(release=open( os.path.join(self.current_repo_dir, 'dists', 'updates', 'stable', 'Release'), "rb"), upstream_url='file://%s' % self.current_repo_dir, codename='updates/stable') self.assertNotEqual( {}, repo.component_arch_binary_package_files_from_release()) self.assertNotEqual([], repo.create_Packages_download_requests("/tmp"))
def test_AptRepo_create(self): test_dir = os.path.join(self.test_dir, self.repo_name, '_2001') packagefile_paths = [ u'main/binary-amd64/Packages', u'main/binary-amd64/Packages.gz', u'main/binary-amd64/Packages.bz2' ] files = [] for root, _, fl in os.walk(self.pool_dir): for f in fl: if f.endswith('.deb'): files.append(os.path.join(root, f)) arch = 'amd64' codename = 'stable' component = 'main' repometa = AptRepoMeta(codename=codename, components=[component], architectures=[arch]) repo = AptRepo(test_dir, repo_name=self.repo_name, metadata=repometa, gpg_sign_options=None) # Looking for bad behavior in defaults repo.create(files, component=component, architecture=arch, with_symlinks=True) release_file = repo.metadata.release_path(repo.base_path) with open(release_file, 'r') as fh: release_data = fh.read() release_822 = deb822.Release(release_data) # Test defaults for Origin, Label, Description expected_default_origin = codename.capitalize() self.assertEqual(release_822.get('Origin'), expected_default_origin) self.assertEqual(release_822.get('Label'), expected_default_origin) self.assertEqual(release_822.get('Description'), expected_default_origin) # Test default for Suite self.assertEqual(release_822.get('Suite'), codename) # Test handling of Architectures self.assertEqual(release_822.get('Architectures'), arch) self.assertEqual(repo.metadata.architectures, [arch]) packagefile_paths_256 = [x['name'] for x in release_822['SHA256']] packagefile_paths_1 = [x['name'] for x in release_822['SHA1']] self.assertEqual(packagefile_paths_256, packagefile_paths) self.assertEqual(packagefile_paths_1, packagefile_paths)
def test_metadata_get_component_arch_binary(self): repo_meta = AptRepoMeta(**self.defaults) with self.assertRaises(ValueError) as ctx: repo_meta.get_component_arch_binary('BOGUS', 'amd64') self.assertEqual( "Component BOGUS not supported (expected: main, updates)", str(ctx.exception)) with self.assertRaises(ValueError) as ctx: repo_meta.get_component_arch_binary('main', 'BOGUS') self.assertEqual( "Architecture BOGUS not defined (expected: amd64, i386, aarch64)", str(ctx.exception))
def test__create_Packages_download_requests__md5(self): # PR #10: fix md5 in release-files # Tests that, in the absence of SHA1 and SHA256, MD5 will be used repo = AptRepoMeta(release=open( os.path.join(self.current_repo_dir, 'dists', 'stable', 'Release'), "rb"), upstream_url='file://%s' % self.current_repo_dir, codename='stable') del repo.release['SHA1'] del repo.release['SHA256'] self.assertEqual( [('main', 'amd64'), ('main', 'i386')], sorted(repo.component_arch_binary_package_files_from_release(). keys()))
def test_AptRepo_sign_error(self, _Popen, _NamedTemporaryFile): sign_cmd = self.mkfile("signme", contents="#!/bin/bash -e") os.chmod(sign_cmd, 0o755) so = SignOptions(cmd=sign_cmd) _Popen.return_value.wait.return_value = 2 meta = AptRepoMeta(codename=self.name) repo = AptRepo(self.new_repo_dir, metadata=meta, gpg_sign_options=so) with self.assertRaises(SignerError) as ctx: repo.sign("MyRelease") self.assertEqual(_NamedTemporaryFile.return_value, ctx.exception.stdout) self.assertEqual(_NamedTemporaryFile.return_value, ctx.exception.stderr)
def test_sign(self, _debfile): file_names = ['foo_0.0.1-1_amd64.deb'] files = [self.mkfile(x, contents=x) for x in file_names] D = deb822.Deb822 # We need to reorder the files alphabetically, so the way os.walk # finds them is the same as the mock we're setting up Files = [(D( dict(Package="foo", Architecture="amd64", Version="0.0.1", Release="1")), files[0])] pkgs = [x[0] for x in Files] debcontrol = _debfile.DebFile.return_value.control.debcontrol debcontrol.return_value.copy.side_effect = pkgs repo_name = "my-test-repo" defaults = dict(components=['main'], architectures=['amd64']) gpg_sign_options = SignOptions(cmd=self.gpg_signer, key_id=self.gpg_key_id) meta = AptRepoMeta(codename=repo_name, **defaults) repo = AptRepo(self.new_repo_dir, meta, gpg_sign_options=gpg_sign_options) repo.create([x[1] for x in Files], with_symlinks=True) relfile = os.path.join(self.new_repo_dir, 'dists', repo_name, 'Release') self.assertTrue(os.path.exists(relfile)) relfile_signed = relfile + '.gpg' self.assertTrue(os.path.exists(relfile_signed)) # Make sure the file verifies _, stderr = self._run_gpg("--verify", relfile_signed, relfile) # Make sure the file was signed with the proper key firstline = open(stderr, "rb").readline().strip() assert firstline.endswith(self.gpg_key_id.encode('utf-8')) # Check after the fact that gpg_sign_options was updated with a # repository_name and dist # We will rely on the functional test that the environment was # properly set up self.assertEqual(repo_name, gpg_sign_options.repository_name)
def test_AptRepo_sign(self, _Popen, _NamedTemporaryFile): sign_cmd = self.mkfile("signme", contents="#!/bin/bash -e") os.chmod(sign_cmd, 0o755) so = SignOptions(cmd=sign_cmd) _Popen.return_value.wait.return_value = 0 meta = AptRepoMeta(codename=self.name) repo = AptRepo(self.new_repo_dir, meta, gpg_sign_options=so) repo.sign("MyRelease") _Popen.assert_called_once_with( [sign_cmd, "MyRelease"], env=dict( GPG_CMD=sign_cmd, GPG_REPOSITORY_NAME="unit_test_repo_foo", ), stdout=_NamedTemporaryFile.return_value, stderr=_NamedTemporaryFile.return_value, )
def test_metadata_packages_from_release(self, _strftime): _strftime.return_value = "ABCDE" repo_meta = AptRepoMeta(**self.defaults) repo_meta.release.update(self.checksums) # SHA1 only for main/i386 sha1sums = [ dict(sha1="aaa", size="123", name="main/binary-i386/Packages.gz"), dict(sha1="bbb", size="456", name="main/binary-i386/Packages"), ] repo_meta.release.update(SHA1=sha1sums) ret = repo_meta.component_arch_binary_package_files_from_release() sha256sums = self.checksums['SHA256'] expected = { ('main', 'amd64'): sha256sums, ('main', 'i386'): sha1sums, } self.assertEqual(expected, ret)
def test_metadata(self, _strftime): _strftime.return_value = "ABCDE" repo_meta = AptRepoMeta(**self.defaults) release_path = os.path.join(self.test_dir, 'dists', 'stable', 'Release') self.assertEqual(release_path, repo_meta.release_path(self.test_dir)) repo_meta.write_release(self.test_dir) expected = dict(self.repo_release_data, Date="ABCDE") self.assertEqual(expected, deb822.Release(open(release_path, "rb").read())) comp_binary = repo_meta.get_component_arch_binary('main', 'amd64') release_path = os.path.join(self.test_dir, 'dists', 'stable', 'main', 'binary-amd64', 'Release') self.assertEqual(release_path, comp_binary.release_path(self.test_dir)) comp_binary.write_release(self.test_dir) expected = self.release_data self.assertEqual(expected, deb822.Release(open(release_path, "rb").read()))
def test_AptRepo_create(self, _debfile): with_symlinks = True D = deb822.Deb822 # We need to reorder the files alphabetically, so the way os.walk # finds them is the same as the mock we're setting up Files = [ (D(dict(Package="bar", Architecture="amd64", Version="0.1.1-1")), self.files[3]), (D(dict(Package="bar", Architecture="i386", Version="0.1.1-1")), self.files[4]), (D(dict(Package="foo", Architecture="amd64", Version="0.0.1-1")), self.files[0]), (D(dict(Package="foo", Architecture="i386", Version="0.0.1-1")), self.files[1]), ] # Split files by arch separated = dict() for f in Files: arch = f[0]['Architecture'] separated.setdefault(arch, []).append(f) src_files = [] for arch, files in sorted(separated.items()): src_files.extend(files) pkgs = [x[0] for x in src_files] debcontrol = _debfile.DebFile.return_value.control.debcontrol debcontrol.return_value.copy.side_effect = pkgs blacklist = ['origin', 'label'] defaults = dict( (k, v) for k, v in self.defaults.items() if k not in blacklist) defaults['components'] = ['main'] defaults['architectures'] = ['amd64', 'i386'] meta = AptRepoMeta(**defaults) repo = AptRepo(self.new_repo_dir, meta) self.assertEqual(defaults['codename'], repo.repo_name) for arch, files in sorted(separated.items()): fpaths = [x[1] for x in files] repo.add_packages(fpaths, component='main', architecture=arch, with_symlinks=with_symlinks) repo.create(with_symlinks=with_symlinks) self.assertEqual([base.mock.call(filename=x[1]) for x in src_files], _debfile.DebFile.call_args_list) pool_files = [ os.path.join(self.new_repo_dir, "pool", "main", os.path.basename(x[1])) for x in src_files ] for src, dst in zip(src_files, pool_files): self.assertTrue(os.path.islink(dst)) self.assertEqual(src[1], os.readlink(dst)) # Make sure Size and Filename are part of the debcontrol # The contents of the files are the filenames, so it is easy to check # the file size self.assertEqual([(os.path.join("pool", "main", os.path.basename(x)), str(len(os.path.basename(x)))) for x in sorted(y[1] for y in Files)], [(x[0]['Filename'], x[0]['Size']) for x in Files]) # Make sure we have some Packages files dist_dir = os.path.join(self.new_repo_dir, "dists", "stable", "main") self.assertFalse( os.path.exists(os.path.join(dist_dir, 'binary-aarch64'))) for exp, arch, idx in [(525, 'amd64', [0, 2]), (521, 'i386', [1, 3])]: path = os.path.join(dist_dir, 'binary-%s' % arch, 'Packages') self.assertEqual(exp, os.stat(path).st_size) pkgs = [x for x in D.iter_paragraphs(open(path, "rb"))] exp_filenames = [ os.path.join("pool", "main", os.path.basename(Files[i][1])) for i in idx ] self.assertEqual(exp_filenames, [x['Filename'] for x in pkgs])
def test_set_date(self, _gmtime): _gmtime.return_value = orig_gmtime(1234567890.123) repo_meta = AptRepoMeta(**self.defaults) self.assertEqual("Fri, 13 Feb 2009 23:31:30 +0000", repo_meta.release['Date'])