from gapic.samplegen_utils import (types, yaml)

BASE_PATH_KEY = "base_path"
DEFAULT_SAMPLE_DIR = "samples"

# The default environment for executing python samples.
# Custom environments must adhere to the following pattern:
# they must be a yaml.Map with a defined anchor_name field,
# and 'environment', 'base_path', and 'invocation' keys must be present.
# The 'invocation' key must map to an interpolable commandline
# that will invoke the given sample.
PYTHON3_ENVIRONMENT = yaml.Map(
    name="python",
    anchor_name="python",
    elements=[
        yaml.KeyVal("environment", "python"),
        yaml.KeyVal("bin", "python3"),
        yaml.KeyVal(BASE_PATH_KEY, DEFAULT_SAMPLE_DIR),
        yaml.KeyVal("invocation", "'{bin} {path} @args'"),
    ],
)


def generate(fpaths_and_samples,
             api_schema,
             *,
             environment: yaml.Map = PYTHON3_ENVIRONMENT,
             manifest_time: int = None) -> Tuple[str, yaml.Doc]:
    """Generate a samplegen manifest for use by sampletest

    Args:
        fpaths_and_samples (Iterable[Tuple[str, Mapping[str, Any]]]):
Ejemplo n.º 2
0
def test_generate_manifest():
    fpath_to_dummy_sample = {
        "samples/squid_fpath.py": {"id": "squid_sample"},
        "samples/clam_fpath.py": {"id": "clam_sample",
                                  "region_tag": "giant_clam_sample"},
    }

    fname, info = manifest.generate(
        fpath_to_dummy_sample.items(),
        DummyApiSchema(naming=DummyNaming(name="Mollusc", version="v1")),
        # Empirically derived number such that the
        # corresponding time_struct tests the zero
        # padding in the returned filename.
        manifest_time=4486525628
    )

    assert fname == "Mollusc.v1.python.21120304.090708.manifest.yaml"

    doc = gapic_yaml.Doc([
        gapic_yaml.KeyVal("type", "manifest/samples"),
        gapic_yaml.KeyVal("schema_version", "3"),
        gapic_yaml.Map(name="python",
                       anchor_name="python",
                       elements=[
                           gapic_yaml.KeyVal(
                               "environment", "python"),
                           gapic_yaml.KeyVal(
                               "bin", "python3"),
                           gapic_yaml.KeyVal(
                               "base_path", "samples"),
                           gapic_yaml.KeyVal(
                               "invocation", "'{bin} {path} @args'"),
                       ]),
        gapic_yaml.Collection(name="samples",
                              elements=[
                                  [
                                      gapic_yaml.Alias(
                                          "python"),
                                      gapic_yaml.KeyVal(
                                          "sample", "squid_sample"),
                                      gapic_yaml.KeyVal(
                                          "path", "'{base_path}/squid_fpath.py'"),
                                      gapic_yaml.Null,
                                  ],
                                  [
                                      gapic_yaml.Alias("python"),
                                      gapic_yaml.KeyVal(
                                          "sample", "clam_sample"),
                                      gapic_yaml.KeyVal(
                                          "path", "'{base_path}/clam_fpath.py'"),
                                      gapic_yaml.KeyVal(
                                          "region_tag", "giant_clam_sample")
                                  ],
                              ])
    ])

    assert info == doc

    expected_rendering = dedent(
        """\
        ---
        type: manifest/samples
        schema_version: 3
        python: &python
          environment: python
          bin: python3
          base_path: samples
          invocation: '{bin} {path} @args'
        samples:
        - <<: *python
          sample: squid_sample
          path: '{base_path}/squid_fpath.py'
        - <<: *python
          sample: clam_sample
          path: '{base_path}/clam_fpath.py'
          region_tag: giant_clam_sample
        """)

    rendered_yaml = doc.render()
    assert rendered_yaml == expected_rendering

    expected_parsed_manifest = {
        "type": "manifest/samples",
        "schema_version": 3,
        "python": {
            "environment": "python",
            "bin": "python3",
            "base_path": "samples",
            "invocation": "{bin} {path} @args",
        },
        "samples": [
            {
                "environment": "python",
                "bin": "python3",
                "base_path": "samples",
                "invocation": "{bin} {path} @args",
                "sample": "squid_sample",
                "path": "{base_path}/squid_fpath.py",
            },
            {
                "environment": "python",
                "bin": "python3",
                "base_path": "samples",
                "invocation": "{bin} {path} @args",
                "sample": "clam_sample",
                "path": "{base_path}/clam_fpath.py",
                "region_tag": "giant_clam_sample",
            },
        ],
    }

    parsed_manifest = yaml.safe_load(rendered_yaml)
    assert parsed_manifest == expected_parsed_manifest