def test_tojson_with_profiles_hooks_metadata(
        self, convert_yaml_schemas_to_json, load_schema
    ):
        t = Transformation("test", namespace="pegasus")
        t.add_sites(
            TransformationSite("local", "/pfn", True).add_env(JAVA_HOME="/java/home")
        )
        t.add_requirement("required")

        t.add_env(JAVA_HOME="/java/home")
        t.add_shell_hook(EventType.START, "/bin/echo hi")
        t.add_metadata(key="value")

        result = json.loads(json.dumps(t, cls=_CustomEncoder))
        expected = {
            "name": "test",
            "namespace": "pegasus",
            "requires": ["required"],
            "sites": [
                {
                    "name": "local",
                    "pfn": "/pfn",
                    "type": "stageable",
                    "profiles": {"env": {"JAVA_HOME": "/java/home"}},
                }
            ],
            "metadata": {"key": "value"},
            "profiles": {Namespace.ENV.value: {"JAVA_HOME": "/java/home"}},
            "hooks": {"shell": [{"_on": EventType.START.value, "cmd": "/bin/echo hi"}]},
        }

        transformation_schema = load_schema("tc-5.0.json")["$defs"]["transformation"]
        validate(instance=result, schema=transformation_schema)

        assert result == expected
    def test_add_duplicate_site(self):
        with pytest.raises(DuplicateError) as e:
            t = Transformation("test")
            t.add_sites(TransformationSite("local", "/pfn", True))
            t.add_sites(TransformationSite("isi", "/pfn", True))

            t.add_sites(TransformationSite("local", "/pfn", True))

        assert "local" in str(e)
    def test_tojson_without_profiles_hooks_metadata(
            self, convert_yaml_schemas_to_json, load_schema):
        t = Transformation("test", namespace="pegasus")
        t.add_sites(TransformationSite("local", "/pfn", True))
        t.add_requirement("required")

        result = json.loads(json.dumps(t, cls=_CustomEncoder))
        expected = {
            "name": "test",
            "namespace": "pegasus",
            "requires": ["required"],
            "sites": [{
                "name": "local",
                "pfn": "/pfn",
                "type": "stageable"
            }],
        }

        transformation_schema = load_schema(
            "tc-5.0.json")["$defs"]["transformation"]

        validate(instance=result, schema=transformation_schema)

        assert result == expected
    def test_add_invalid_site(self):
        with pytest.raises(TypeError) as e:
            t = Transformation("test")
            t.add_sites("badsite")

        assert "badsite" in str(e)
 def test_add_site(self):
     t = Transformation("test")
     t.add_sites(TransformationSite("local", "/pfn", True))
     assert "local" in t.sites
Beispiel #6
0
def _to_tc(d: dict) -> TransformationCatalog:
    """Convert dict to TransformationCatalog

    :param d: TransformationCatalog represented as a dict
    :type d: dict
    :raises PegasusError: encountered error parsing
    :return: a TransformationCatalog object based on d
    :rtype: TransformationCatalog
    """

    try:
        tc = TransformationCatalog()

        # add transformations
        for tr in d["transformations"]:
            tr_to_add = Transformation(
                tr["name"],
                tr.get("namespace"),
                tr.get("version"),
                checksum=tr.get("checksum"),
            )

            # add transformation sites
            for s in tr["sites"]:
                site_to_add = TransformationSite(
                    s["name"],
                    s["pfn"],
                    True if s["type"] == "stageable" else False,
                    bypass_staging=s.get("bypass"),
                    arch=getattr(Arch,
                                 s.get("arch").upper())
                    if s.get("arch") else None,
                    os_type=getattr(OS,
                                    s.get("os.type").upper())
                    if s.get("os.type") else None,
                    os_release=s.get("os.release"),
                    os_version=s.get("os.version"),
                    container=s.get("container"),
                )

                # add profiles
                if s.get("profiles"):
                    site_to_add.profiles = defaultdict(dict, s.get("profiles"))

                # add metadata
                if s.get("metadata"):
                    site_to_add.metadata = s.get("metadata")

                # add site to this tr
                tr_to_add.add_sites(site_to_add)

            # add requires
            if tr.get("requires"):
                tr_to_add.requires = set(tr.get("requires"))

            # add profiles
            if tr.get("profiles"):
                tr_to_add.profiles = defaultdict(dict, tr.get("profiles"))

            # add hooks
            if tr.get("hooks"):
                tr_to_add.hooks = defaultdict(list, tr.get("hooks"))

            # add metadata
            if tr.get("metadata"):
                tr_to_add.metadata = tr.get("metadata")

            # add tr to tc
            tc.add_transformations(tr_to_add)

        # add containers
        if "containers" in d:
            for cont in d["containers"]:
                cont_to_add = Container(
                    cont["name"],
                    getattr(Container, cont["type"].upper()),
                    cont["image"],
                    mounts=cont.get("mounts"),
                    image_site=cont.get("image.site"),
                    checksum=cont.get("checksum"),
                    bypass_staging=cont.get("bypass"),
                )

                # add profiles
                if cont.get("profiles"):
                    cont_to_add.profiles = defaultdict(dict,
                                                       cont.get("profiles"))

                # add cont to tc
                tc.add_containers(cont_to_add)

        return tc

    except KeyError:
        raise PegasusError("error parsing {}".format(d))