Esempio n. 1
0
def test_build_basic_complete_structure(tmp_path, monkeypatch, config):
    """Integration test: a simple structure with custom lib and normal src dir."""
    build_dir = tmp_path / BUILD_DIRNAME
    build_dir.mkdir()

    # the metadata (save it and restore to later check)
    metadata_data = {"name": "name-from-metadata"}
    metadata_file = tmp_path / "metadata.yaml"
    metadata_raw = yaml.dump(metadata_data).encode("ascii")
    with metadata_file.open("wb") as fh:
        fh.write(metadata_raw)

    # a lib dir
    lib_dir = tmp_path / "lib"
    lib_dir.mkdir()
    ops_lib_dir = lib_dir / "ops"
    ops_lib_dir.mkdir()
    ops_stuff = ops_lib_dir / "stuff.txt"
    with ops_stuff.open("wb") as fh:
        fh.write(b"ops stuff")

    # simple source code
    src_dir = tmp_path / "src"
    src_dir.mkdir()
    charm_script = src_dir / "charm.py"
    with charm_script.open("wb") as fh:
        fh.write(b"all the magic")

    monkeypatch.chdir(tmp_path)  # so the zip file is left in the temp dir
    builder = Builder(
        {
            "from": tmp_path,
            "entrypoint": charm_script,
            "requirement": [],
        },
        config,
    )
    zipname = builder.run()

    # check all is properly inside the zip
    # contents!), and all relative to build dir
    zf = zipfile.ZipFile(zipname)
    assert zf.read("metadata.yaml") == metadata_raw
    assert zf.read("src/charm.py") == b"all the magic"
    dispatch = DISPATCH_CONTENT.format(
        entrypoint_relative_path="src/charm.py").encode("ascii")
    assert zf.read("dispatch") == dispatch
    assert zf.read("hooks/install") == dispatch
    assert zf.read("hooks/start") == dispatch
    assert zf.read("hooks/upgrade-charm") == dispatch
    assert zf.read("lib/ops/stuff.txt") == b"ops stuff"

    # check the manifest is present and with particular values that depend on given info
    manifest = yaml.safe_load(zf.read("manifest.yaml"))
    assert (manifest["charmcraft-started-at"] ==
            config.project.started_at.isoformat() + "Z")
Esempio n. 2
0
def test_build_basic_complete_structure(tmp_path, monkeypatch, config):
    """Integration test: a simple structure with custom lib and normal src dir."""
    build_dir = tmp_path / BUILD_DIRNAME
    build_dir.mkdir()

    # the metadata (save it and restore to later check)
    metadata_data = {'name': 'name-from-metadata'}
    metadata_file = tmp_path / 'metadata.yaml'
    metadata_raw = yaml.dump(metadata_data).encode('ascii')
    with metadata_file.open('wb') as fh:
        fh.write(metadata_raw)

    # a lib dir
    lib_dir = tmp_path / 'lib'
    lib_dir.mkdir()
    ops_lib_dir = lib_dir / 'ops'
    ops_lib_dir.mkdir()
    ops_stuff = ops_lib_dir / 'stuff.txt'
    with ops_stuff.open('wb') as fh:
        fh.write(b'ops stuff')

    # simple source code
    src_dir = tmp_path / 'src'
    src_dir.mkdir()
    charm_script = src_dir / 'charm.py'
    with charm_script.open('wb') as fh:
        fh.write(b'all the magic')

    monkeypatch.chdir(tmp_path)  # so the zip file is left in the temp dir
    builder = Builder(
        {
            'from': tmp_path,
            'entrypoint': charm_script,
            'requirement': [],
        }, config)
    zipname = builder.run()

    # check all is properly inside the zip
    # contents!), and all relative to build dir
    zf = zipfile.ZipFile(zipname)
    assert zf.read('metadata.yaml') == metadata_raw
    assert zf.read('src/charm.py') == b"all the magic"
    dispatch = DISPATCH_CONTENT.format(
        entrypoint_relative_path='src/charm.py').encode('ascii')
    assert zf.read('dispatch') == dispatch
    assert zf.read('hooks/install') == dispatch
    assert zf.read('hooks/start') == dispatch
    assert zf.read('hooks/upgrade-charm') == dispatch
    assert zf.read('lib/ops/stuff.txt') == b"ops stuff"

    # check the manifest is present and with particular values that depend on given info
    manifest = yaml.safe_load(zf.read('manifest.yaml'))
    assert manifest[
        'charmcraft-started-at'] == config.project.started_at.isoformat() + "Z"
Esempio n. 3
0
def test_build_basic_complete_structure(tmp_path, monkeypatch):
    """Integration test: a simple structure with custom lib and normal src dir."""
    build_dir = tmp_path / BUILD_DIRNAME
    build_dir.mkdir()

    # the metadata (save it and restore to later check)
    metadata_data = {'name': 'name-from-metadata'}
    metadata_file = tmp_path / 'metadata.yaml'
    metadata_raw = yaml.dump(metadata_data).encode('ascii')
    with metadata_file.open('wb') as fh:
        fh.write(metadata_raw)

    # a lib dir
    lib_dir = tmp_path / 'lib'
    lib_dir.mkdir()
    ops_lib_dir = lib_dir / 'ops'
    ops_lib_dir.mkdir()
    ops_stuff = ops_lib_dir / 'stuff.txt'
    with ops_stuff.open('wb') as fh:
        fh.write(b'ops stuff')

    # simple source code
    src_dir = tmp_path / 'src'
    src_dir.mkdir()
    charm_script = src_dir / 'charm.py'
    with charm_script.open('wb') as fh:
        fh.write(b'all the magic')

    monkeypatch.chdir(tmp_path)  # so the zip file is left in the temp dir
    builder = Builder({
        'from': pathlib.Path(
            str(tmp_path)),  # bad support for tmp_path's pathlib2 in Py3.5
        'entrypoint': pathlib.Path(str(charm_script)),
        'requirement': [],
    })
    zipname = builder.run()

    # check all is properly inside the zip
    # contents!), and all relative to build dir
    zf = zipfile.ZipFile(zipname)
    assert zf.read('metadata.yaml') == metadata_raw
    assert zf.read('src/charm.py') == b"all the magic"
    dispatch = DISPATCH_CONTENT.format(
        entrypoint_relative_path='src/charm.py').encode('ascii')
    assert zf.read('dispatch') == dispatch
    assert zf.read('hooks/install') == dispatch
    assert zf.read('hooks/start') == dispatch
    assert zf.read('hooks/upgrade-charm') == dispatch
    assert zf.read('lib/ops/stuff.txt') == b"ops stuff"