def test_unmarshal_package_repositories_invalid_data():
    with pytest.raises(errors.PackageRepositoryValidationError) as exc_info:
        PackageRepository.unmarshal_package_repositories("not-a-list")

    assert (exc_info.value.brief ==
            "Invalid package-repositories list object 'not-a-list'.")
    assert exc_info.value.details == "Package repositories must be a list of objects."
    assert (
        exc_info.value.resolution ==
        "Verify 'package-repositories' configuration and ensure that the correct syntax is used."
    )
示例#2
0
    def test_marshal_ppa(self):
        test_dict = {"type": "apt", "ppa": "test/foo"}
        test_list = [test_dict]

        unmarshalled_list = [
            repo.marshal() for repo in
            PackageRepository.unmarshal_package_repositories(test_list)
        ]

        self.assertThat(unmarshalled_list, Equals(test_list))
def test_unmarshal_package_repositories_list_ppa():
    test_dict = {"type": "apt", "ppa": "test/foo"}
    test_list = [test_dict]

    unmarshalled_list = [
        repo.marshal()
        for repo in PackageRepository.unmarshal_package_repositories(test_list)
    ]

    assert unmarshalled_list == test_list
def test_unmarshal_package_repositories_list_apt():
    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",
    }

    test_list = [test_dict]

    unmarshalled_list = [
        repo.marshal()
        for repo in PackageRepository.unmarshal_package_repositories(test_list)
    ]

    assert unmarshalled_list == test_list
示例#5
0
    def test_marshal_deb(self):
        test_dict = {
            "architectures": ["amd64", "i386"],
            "components": ["main", "multiverse"],
            "deb-types": ["deb", "deb-src"],
            "key-id": "test-key-id",
            "key-server": "keyserver.ubuntu.com",
            "name": "test-name",
            "suites": ["xenial", "xenial-updates"],
            "type": "apt",
            "url": "http://archive.ubuntu.com/ubuntu",
        }

        test_list = [test_dict]

        unmarshalled_list = [
            repo.marshal() for repo in
            PackageRepository.unmarshal_package_repositories(test_list)
        ]

        self.assertThat(unmarshalled_list, Equals(test_list))
示例#6
0
    def from_dict(cls, snap_dict: Dict[str, Any]) -> "Snap":
        snap_dict = deepcopy(snap_dict)

        # Using pop() so we can catch if we *miss* fields
        # with whatever remains in the dictionary.
        adopt_info = snap_dict.pop("adopt-info", None)
        architectures = snap_dict.pop("architectures", None)

        # Process apps into Applications.
        apps: Dict[str, Application] = dict()
        apps_dict = snap_dict.pop("apps", None)
        if apps_dict:
            for app_name, app_dict in apps_dict.items():
                app = Application.from_dict(app_dict=app_dict,
                                            app_name=app_name)
                apps[app_name] = app

        # Treat `assumes` as a set, not as a list.
        assumes = set(snap_dict.pop("assumes", set()))

        base = snap_dict.pop("base", None)
        build_base = snap_dict.pop("build-base", None)
        confinement = snap_dict.pop("confinement", None)
        description = snap_dict.pop("description", None)
        environment = snap_dict.pop("environment", None)
        epoch = snap_dict.pop("epoch", None)
        grade = snap_dict.pop("grade", None)

        # Process hooks into Hooks.
        hooks: Dict[str, Hook] = dict()
        hooks_dict = snap_dict.pop("hooks", None)
        if hooks_dict:
            for hook_name, hook_dict in hooks_dict.items():
                # This can happen, but should be moved into Hook.from_object().
                if hook_dict is None:
                    continue

                hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
                hooks[hook_name] = hook

        layout = snap_dict.pop("layout", None)
        license = snap_dict.pop("license", None)
        name = snap_dict.pop("name", None)

        raw_repositories = snap_dict.pop("package-repositories", None)
        if raw_repositories is None:
            package_repositories = None
        else:
            logger.warning("*EXPERIMENTAL* package-repositories in use")
            package_repositories = PackageRepository.unmarshal_package_repositories(
                raw_repositories)

        passthrough = snap_dict.pop("passthrough", None)

        # Process plugs into Plugs.
        plugs: Dict[str, Plug] = dict()
        plugs_dict = snap_dict.pop("plugs", None)
        if plugs_dict:
            for plug_name, plug_object in plugs_dict.items():
                plug = Plug.from_object(plug_object=plug_object,
                                        plug_name=plug_name)
                plugs[plug_name] = plug

        # Process slots into Slots.
        slots: Dict[str, Slot] = dict()
        slots_dict = snap_dict.pop("slots", None)
        if slots_dict:
            for slot_name, slot_object in slots_dict.items():
                slot = Slot.from_object(slot_object=slot_object,
                                        slot_name=slot_name)
                slots[slot_name] = slot

        summary = snap_dict.pop("summary", None)

        # Process sytemusers into SystemUsers.
        system_usernames: Dict[str, SystemUser] = dict()
        system_usernames_dict = snap_dict.pop("system-usernames", None)
        if system_usernames_dict:
            for user_name, user_object in system_usernames_dict.items():
                system_username = SystemUser.from_object(
                    user_object=user_object, user_name=user_name)
                system_usernames[user_name] = system_username

        title = snap_dict.pop("title", None)
        type = snap_dict.pop("type", None)
        version = snap_dict.pop("version", None)

        # Report unhandled keys.
        for key, value in snap_dict.items():
            logger.debug(f"ignoring or passing through unknown {key}={value}")

        return Snap(
            adopt_info=adopt_info,
            architectures=architectures,
            apps=apps,
            assumes=assumes,
            base=base,
            build_base=build_base,
            confinement=confinement,
            description=description,
            environment=environment,
            epoch=epoch,
            grade=grade,
            hooks=hooks,
            layout=layout,
            license=license,
            name=name,
            passthrough=passthrough,
            package_repositories=package_repositories,
            plugs=plugs,
            slots=slots,
            summary=summary,
            system_usernames=system_usernames,
            title=title,
            type=type,
            version=version,
        )
def test_unmarshal_package_repositories_list_empty():
    assert PackageRepository.unmarshal_package_repositories(list()) == list()
def test_unmarshal_package_repositories_list_none():
    assert PackageRepository.unmarshal_package_repositories(None) == list()
示例#9
0
 def test_marshal_empty(self):
     self.assertThat(
         PackageRepository.unmarshal_package_repositories(list()),
         Equals(list()))
示例#10
0
文件: snap.py 项目: nessita/snapcraft
    def from_dict(cls, snap_dict: Dict[str, Any]) -> "Snap":  # noqa: C901
        snap_dict = deepcopy(snap_dict)

        # Using pop() so we can catch if we *miss* fields
        # with whatever remains in the dictionary.
        adopt_info = snap_dict.pop("adopt-info", None)
        architectures = snap_dict.pop("architectures", None)

        # Process apps into Applications.
        apps: Dict[str, Application] = dict()
        apps_dict = snap_dict.pop("apps", None)
        if apps_dict:
            for app_name, app_dict in apps_dict.items():
                app = Application.from_dict(app_dict=app_dict,
                                            app_name=app_name)
                apps[app_name] = app

        # Treat `assumes` as a set, not as a list.
        assumes = set(snap_dict.pop("assumes", set()))

        base = snap_dict.pop("base", None)
        build_base = snap_dict.pop("build-base", None)
        compression = snap_dict.pop("compression", None)
        confinement = snap_dict.pop("confinement", None)
        description = snap_dict.pop("description", None)
        environment = snap_dict.pop("environment", None)
        epoch = snap_dict.pop("epoch", None)
        grade = snap_dict.pop("grade", None)

        # Process hooks into Hooks.
        hooks: Dict[str, Hook] = dict()
        hooks_dict = snap_dict.pop("hooks", None)
        if hooks_dict:
            for hook_name, hook_dict in hooks_dict.items():
                # This can happen, but should be moved into Hook.from_object().
                if hook_dict is None:
                    continue

                hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
                hooks[hook_name] = hook

        layout = snap_dict.pop("layout", None)
        license = snap_dict.pop("license", None)

        # snap.yaml will have links, snapcraft.yaml the top level entries.
        # Some considerations:
        # - links should not be allowed in snapcraft.yaml by the schema.
        # - the top level entries would never exist in snap.yaml if generated with
        #   Snapcraft (contact was never allowed).
        #
        # snap.yaml can have a contact entry (this was never supported by Snapcraft)
        # at the top level which is equivalent to .links.contact[0] so even if
        # Snapcraft does not perform a snap.yaml to snap.yaml transformation
        # today it would lead to a compatible result once snapd supports this.
        links = snap_dict.pop("links", None)
        if links is None:
            links = dict()
            for link_name in (
                    "contact",
                    "donation",
                    "issues",
                    "source-code",
                    "website",
            ):
                link_value = snap_dict.pop(link_name, None)
                if isinstance(link_value, str):
                    links[link_name] = [link_value]
                elif isinstance(link_value, list):
                    links[link_name] = link_value

        name = snap_dict.pop("name", None)

        raw_repositories = snap_dict.pop("package-repositories", None)
        if raw_repositories is None:
            package_repositories = None
        else:
            package_repositories = PackageRepository.unmarshal_package_repositories(
                raw_repositories)

        passthrough = snap_dict.pop("passthrough", None)

        # Process plugs into Plugs.
        plugs: Dict[str, Plug] = dict()
        plugs_dict = snap_dict.pop("plugs", None)
        if plugs_dict:
            for plug_name, plug_object in plugs_dict.items():
                plug = Plug.from_object(plug_object=plug_object,
                                        plug_name=plug_name)
                plugs[plug_name] = plug

        # Process slots into Slots.
        slots: Dict[str, Slot] = dict()
        slots_dict = snap_dict.pop("slots", None)
        if slots_dict:
            for slot_name, slot_object in slots_dict.items():
                slot = Slot.from_object(slot_object=slot_object,
                                        slot_name=slot_name)
                slots[slot_name] = slot

        summary = snap_dict.pop("summary", None)

        # Process sytemusers into SystemUsers.
        system_usernames: Dict[str, SystemUser] = dict()
        system_usernames_dict = snap_dict.pop("system-usernames", None)
        if system_usernames_dict:
            for user_name, user_object in system_usernames_dict.items():
                system_username = SystemUser.from_object(
                    user_object=user_object, user_name=user_name)
                system_usernames[user_name] = system_username

        title = snap_dict.pop("title", None)
        type = snap_dict.pop("type", None)
        version = snap_dict.pop("version", None)

        # Report unhandled keys.
        for key, value in snap_dict.items():
            logger.debug(f"ignoring or passing through unknown {key}={value}")

        return Snap(
            adopt_info=adopt_info,
            architectures=architectures,
            apps=apps,
            assumes=assumes,
            base=base,
            build_base=build_base,
            compression=compression,
            confinement=confinement,
            description=description,
            environment=environment,
            epoch=epoch,
            grade=grade,
            hooks=hooks,
            layout=layout,
            license=license,
            links=links,
            name=name,
            passthrough=passthrough,
            package_repositories=package_repositories,
            plugs=plugs,
            slots=slots,
            summary=summary,
            system_usernames=system_usernames,
            title=title,
            type=type,
            version=version,
        )