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")
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"
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"
def test_build_dispatcher_modern_dispatch_created(tmp_path): """The dispatcher script is properly built.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / 'somestuff.py' builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) builder.handle_dispatcher(linked_entrypoint) included_dispatcher = build_dir / DISPATCH_FILENAME with included_dispatcher.open('rt', encoding='utf8') as fh: dispatcher_code = fh.read() assert dispatcher_code == DISPATCH_CONTENT.format(entrypoint_relative_path='somestuff.py')
def test_build_dispatcher_modern_dispatch_created(tmp_path): """The dispatcher script is properly built.""" metadata = tmp_path / CHARM_METADATA metadata.write_text("name: crazycharm") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / "somestuff.py" builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), ) builder.handle_dispatcher(linked_entrypoint) included_dispatcher = build_dir / DISPATCH_FILENAME with included_dispatcher.open("rt", encoding="utf8") as fh: dispatcher_code = fh.read() assert dispatcher_code == DISPATCH_CONTENT.format(entrypoint_relative_path="somestuff.py")
def test_build_dispatcher_modern_dispatch_created(tmp_path, config): """The dispatcher script is properly built.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / "somestuff.py" builder = Builder( { "from": tmp_path, "entrypoint": "whatever", "requirement": [], }, config, ) builder.handle_dispatcher(linked_entrypoint) included_dispatcher = build_dir / DISPATCH_FILENAME with included_dispatcher.open("rt", encoding="utf8") as fh: dispatcher_code = fh.read() assert dispatcher_code == DISPATCH_CONTENT.format( entrypoint_relative_path="somestuff.py")