def test_apt_unmarshal_invalid_data(): test_dict = "not-a-dict" with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt.unmarshal(test_dict) # type: ignore err = raised.value assert str( err) == "Invalid package repository for 'not-a-dict': invalid object." assert err.details == "Package repository must be a valid dictionary object." assert err.resolution == ( "Verify repository configuration and ensure that the correct syntax is used." )
def test_install_package_repository_key_apt_from_keyserver(apt_gpg, mocker): mock_install_key_from_keyserver = mocker.patch( "snapcraft.repo.apt_key_manager.AptKeyManager.install_key_from_keyserver" ) mocker.patch( "snapcraft.repo.apt_key_manager.AptKeyManager.is_key_installed", return_value=False, ) key_id = "8" * 40 package_repo = PackageRepositoryApt( components=["main", "multiverse"], key_id=key_id, key_server="key.server", suites=["xenial"], url="http://archive.ubuntu.com/ubuntu", ) updated = apt_gpg.install_package_repository_key(package_repo=package_repo) assert updated is True assert mock_install_key_from_keyserver.mock_calls == [ call(key_id=key_id, key_server="key.server") ]
def test_apt_name(): repo = PackageRepositoryApt( architectures=["amd64", "i386"], components=["main", "multiverse"], formats=["deb", "deb-src"], key_id="A" * 40, key_server="keyserver.ubuntu.com", suites=["xenial", "xenial-updates"], url="http://archive.ubuntu.com/ubuntu", ) assert repo.name == "http_archive_ubuntu_com_ubuntu"
def test_apt_invalid_url(): with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt( key_id="A" * 40, url="", ) err = raised.value assert str(err) == "Invalid package repository for '': invalid URL." assert err.details == "URLs must be non-empty strings." assert err.resolution == ( "Verify the repository configuration and ensure that 'url' " "is correctly specified.")
def test_apt_marshal(): repo = PackageRepositoryApt( architectures=["amd64", "i386"], components=["main", "multiverse"], formats=["deb", "deb-src"], key_id="A" * 40, key_server="xkeyserver.ubuntu.com", name="test-name", suites=["xenial", "xenial-updates"], url="http://archive.ubuntu.com/ubuntu", ) assert repo.marshal() == { "architectures": ["amd64", "i386"], "components": ["main", "multiverse"], "formats": ["deb", "deb-src"], "key-id": "A" * 40, "key-server": "xkeyserver.ubuntu.com", "name": "test-name", "suites": ["xenial", "xenial-updates"], "type": "apt", "url": "http://archive.ubuntu.com/ubuntu", }
def test_apt_unmarshal_invalid_type(): test_dict = { "architectures": ["amd64", "i386"], "components": ["main", "multiverse"], "formats": ["deb", "deb-src"], "key-id": "A" * 40, "key-server": "keyserver.ubuntu.com", "name": "test-name", "suites": ["xenial", "xenial-updates"], "type": "aptx", "url": "http://archive.ubuntu.com/ubuntu", } with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt.unmarshal(test_dict) err = raised.value assert str(err) == ( "Invalid package repository for 'http://archive.ubuntu.com/ubuntu': " "unsupported type 'aptx'.") assert err.details == "The only currently supported type is 'apt'." assert err.resolution == ( "Verify repository configuration and ensure that 'type' is correctly specified." )
def test_apt_unmarshal_invalid_extra_keys(): test_dict = { "architectures": ["amd64", "i386"], "components": ["main", "multiverse"], "formats": ["deb", "deb-src"], "key-id": "A" * 40, "key-server": "keyserver.ubuntu.com", "name": "test-name", "suites": ["xenial", "xenial-updates"], "type": "apt", "url": "http://archive.ubuntu.com/ubuntu", "foo": "bar", "foo2": "bar", } with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt.unmarshal(test_dict) err = raised.value assert str(err) == ( "Invalid package repository for 'http://archive.ubuntu.com/ubuntu': " "unsupported properties 'foo', 'foo2'.") assert err.details is None assert err.resolution == "Verify repository configuration and ensure it is correct."
def test_apt_invalid_path(): with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt( key_id="A" * 40, path="", url="http://archive.ubuntu.com/ubuntu", ) err = raised.value assert str(err) == ( "Invalid package repository for 'http://archive.ubuntu.com/ubuntu': " "invalid path ''.") assert err.details == "Paths must be non-empty strings." assert err.resolution == ( "Verify the repository configuration and ensure that 'path' " "is a non-empty string such as '/'.")
def test_apt_invalid_suites_as_path(): with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt( key_id="A" * 40, suites=["my-suite/"], url="http://archive.ubuntu.com/ubuntu", ) err = raised.value assert str(err) == ( "Invalid package repository for 'http://archive.ubuntu.com/ubuntu': " "invalid suite 'my-suite/'.") assert err.details == "Suites must not end with a '/'." assert err.resolution == ( "Verify the repository configuration and remove the trailing '/' " "from suites or use the 'path' property to define a path.")
def test_apt_invalid_missing_suites(): with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt( key_id="A" * 40, components=["main"], url="http://archive.ubuntu.com/ubuntu", ) err = raised.value assert str(err) == ( "Invalid package repository for 'http://archive.ubuntu.com/ubuntu': " "no suites specified.") assert err.details == "Suites are required when using components." assert err.resolution == ( "Verify the repository configuration and ensure that 'suites' " "is correctly specified.")
def test_apt_invalid_path_with_suites(): with pytest.raises(errors.PackageRepositoryValidationError) as raised: PackageRepositoryApt( key_id="A" * 40, path="/", suites=["xenial", "xenial-updates"], url="http://archive.ubuntu.com/ubuntu", ) err = raised.value assert str(err) == ( "Invalid package repository for 'http://archive.ubuntu.com/ubuntu': " "suites ['xenial', 'xenial-updates'] cannot be combined with path '/'." ) assert err.details == "Path and suites are incomptiable options." assert err.resolution == ( "Verify the repository configuration and remove 'path' or 'suites'.")
def test_install_package_repository_key_already_installed( is_installed, apt_gpg, mocker): mocker.patch( "snapcraft.repo.apt_key_manager.AptKeyManager.is_key_installed", return_value=is_installed, ) package_repo = PackageRepositoryApt( components=["main", "multiverse"], key_id="8" * 40, key_server="xkeyserver.com", suites=["xenial"], url="http://archive.ubuntu.com/ubuntu", ) updated = apt_gpg.install_package_repository_key(package_repo=package_repo) assert updated is not is_installed
def test_install_package_repository_key_from_asset(apt_gpg, key_assets, mocker): mocker.patch( "snapcraft.repo.apt_key_manager.AptKeyManager.is_key_installed", return_value=False, ) mock_install_key = mocker.patch( "snapcraft.repo.apt_key_manager.AptKeyManager.install_key") key_id = "123456789012345678901234567890123456AABB" expected_key_path = key_assets / "3456AABB.asc" expected_key_path.write_text("key-data") package_repo = PackageRepositoryApt( components=["main", "multiverse"], key_id=key_id, suites=["xenial"], url="http://archive.ubuntu.com/ubuntu", ) updated = apt_gpg.install_package_repository_key(package_repo=package_repo) assert updated is True assert mock_install_key.mock_calls == [call(key="key-data")]
def test_apt_valid_architectures(arch): package_repo = PackageRepositoryApt(key_id="A" * 40, url="http://test", architectures=[arch]) assert package_repo.architectures == [arch]
sources_list_d = tmp_path / "sources.list.d" sources_list_d.mkdir(parents=True) yield apt_sources_manager.AptSourcesManager( sources_list_d=sources_list_d, ) @pytest.mark.parametrize( "package_repo,name,content", [ ( PackageRepositoryApt( architectures=["amd64", "arm64"], components=["test-component"], formats=["deb", "deb-src"], key_id="A" * 40, suites=["test-suite1", "test-suite2"], url="http://test.url/ubuntu", ), "snapcraft-http_test_url_ubuntu.sources", dedent( """\ Types: deb deb-src URIs: http://test.url/ubuntu Suites: test-suite1 test-suite2 Components: test-component Architectures: amd64 arm64 """ ).encode(), ), (