def test_slot_name(self): slot_name = "slot-test" slot = Slot(slot_name=slot_name) slot_from_dict = Slot.from_dict(slot_dict={"interface": "somevalue"}, slot_name=slot_name) self.assertEqual(slot_name, slot.slot_name) self.assertEqual(slot_name, slot_from_dict.slot_name)
def test_invalid_from_dict_raises_exception(self): slot_dict = OrderedDict({}) slot_name = "slot-test" slot = Slot.from_dict(slot_dict=slot_dict, slot_name=slot_name) self.assertRaises(errors.SlotValidationError, slot.validate)
def test_from_valid_dict(self): slot_dict = OrderedDict({"interface": "somevalue", "someprop": "somevalue"}) slot_name = "slot-test" slot = Slot.from_dict(slot_dict=slot_dict, slot_name=slot_name) slot.validate()
def test_from_empty_dict(self): slot_dict = OrderedDict({}) slot_name = "slot-test" slot = Slot.from_dict(slot_dict=slot_dict, slot_name=slot_name) slot.validate()
def test_from_object_string(self): slot = Slot.from_object(slot_name="slot-name", slot_object="some-interface") slot.validate() self.assertThat(slot._slot_dict["interface"], Equals("some-interface")) self.assertThat(slot.use_string_representation, Equals(True))
def test_empty_from_dict(self): slot_dict = OrderedDict({"interface": "content"}) slot_name = "slot-name" slot = Slot.from_dict(slot_dict=slot_dict, slot_name=slot_name) self.assertIsInstance(slot, ContentSlot) self.assertEqual(slot_dict, slot.to_dict()) self.assertEqual(slot_name, slot.slot_name) self.assertRaises(errors.SlotValidationError, slot.validate) self.assertEqual(set(), slot.get_content_dirs(installed_path=""))
def test_from_object_dict(self): slot_dict = OrderedDict( {"interface": "some-interface", "someprop": "somevalue"} ) slot_name = "slot-test" slot = Slot.from_object(slot_object=slot_dict, slot_name=slot_name) slot.validate() self.assertThat(slot._slot_dict["interface"], Equals("some-interface")) self.assertThat(slot._slot_dict["someprop"], Equals("somevalue"))
def from_dict(cls, snap_dict: Dict[str, Any]) -> "Snap": snap_dict = deepcopy(snap_dict) snap = Snap() if "passthrough" in snap_dict: snap.passthrough = snap_dict.pop("passthrough") for key in snap_dict: if key == "plugs": for plug_name, plug_dict in snap_dict[key].items(): plug = Plug.from_dict(plug_dict=plug_dict, plug_name=plug_name) snap.plugs[plug_name] = plug elif key == "slots": for slot_name, slot_dict in snap_dict[key].items(): slot = Slot.from_dict(slot_dict=slot_dict, slot_name=slot_name) snap.slots[slot_name] = slot elif key == "apps": for app_name, app_dict in snap_dict[key].items(): app = Application.from_dict(app_dict=app_dict, app_name=app_name) snap.apps[app_name] = app elif key == "hooks": for hook_name, hook_dict in snap_dict[key].items(): hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name) snap.hooks[hook_name] = hook elif key in _MANDATORY_PACKAGE_KEYS: snap.__dict__[key] = snap_dict[key] elif key in _OPTIONAL_PACKAGE_KEYS: snap.__dict__[key] = snap_dict[key] else: logger.debug( "ignoring or passing through unknown key: {}".format(key)) continue if "adopt-info" in snap_dict: snap.adopt_info = snap_dict["adopt-info"] return snap
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 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, )
def test_invalid_raises_exception(self): slot_name = "slot-test" slot = Slot(slot_name=slot_name) self.assertRaises(errors.SlotValidationError, slot.validate)
def test_from_object_none(self): slot = Slot.from_object(slot_name="slot-name", slot_object=None) slot.validate() self.assertThat(slot._slot_dict, Equals(dict()))