def mock_get_os_platform(): os_platform = OSPlatform(system="host-OS", release="host-CHANNEL", machine="host-ARCH") with patch("charmcraft.bases.get_os_platform", return_value=os_platform) as mock_platform: yield mock_platform
def test_manifest_architecture_translated(tmp_path, monkeypatch): """All known architectures must be translated.""" monkeypatch.setitem(ARCH_TRANSLATIONS, 'weird_arch', 'nice_arch') os_platform = OSPlatform(system='Ubuntu', release='40.10', machine='weird_arch') with patch('charmcraft.utils.get_os_platform', return_value=os_platform): result_filepath = create_manifest(tmp_path, datetime.datetime.now()) saved = yaml.safe_load(result_filepath.read_text()) assert saved['bases'][0]['architectures'] == ['nice_arch']
def test_manifest_architecture_translated(tmp_path, monkeypatch): """All known architectures must be translated.""" monkeypatch.setitem(ARCH_TRANSLATIONS, "weird_arch", "nice_arch") os_platform = OSPlatform(system="Ubuntu", release="40.10", machine="weird_arch") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, datetime.datetime.now()) saved = yaml.safe_load(result_filepath.read_text()) assert saved["bases"][0]["architectures"] == ["nice_arch"]
def test_useragent_linux(monkeypatch): """Construct a user-agent as a patched Linux machine""" monkeypatch.setenv("TRAVIS_TESTING", "1") os_platform = OSPlatform(system="Arch Linux", release="5.10.10-arch1-1", machine="x86_64") with patch('charmcraft.commands.store.client.__version__', '1.2.3'), \ patch('charmcraft.utils.get_os_platform', return_value=os_platform), \ patch('platform.system', return_value='Linux'), \ patch('platform.machine', return_value='x86_64'), \ patch('platform.python_version', return_value='3.9.1'): ua = build_user_agent() assert ua == "charmcraft/1.2.3 (testing) Arch Linux/5.10.10-arch1-1 (x86_64) python/3.9.1"
def test_manifest_fields_from_strict_snap(tmp_path, monkeypatch): """Fields found in a strict snap must be translated.""" os_platform = OSPlatform(system="ubuntu-core", release="20", machine="amd64") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, datetime.datetime.now()) saved = yaml.safe_load(result_filepath.read_text()) base = saved["bases"][0] assert base["name"] == "ubuntu" assert base["channel"] == "20.04"
def test_manifest_image_info_ok(tmp_path, monkeypatch): """Include the image info in the manifest.""" test_image_content = {"some info": ["whatever", 123]} monkeypatch.setenv(IMAGE_INFO_ENV_VAR, json.dumps(test_image_content)) tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system="SuperUbuntu", release="40.10", machine="SomeRISC") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp, None, []) saved = yaml.safe_load(result_filepath.read_text()) assert saved["image-info"] == test_image_content
def test_manifest_no_bases(tmp_path): """Manifest without bases (used for bundles).""" tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system="SuperUbuntu", release="40.10", machine="SomeRISC") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp, None, []) saved = yaml.safe_load(result_filepath.read_text()) assert result_filepath == tmp_path / "manifest.yaml" assert saved == { "charmcraft-started-at": "2020-02-01T15:40:33Z", "charmcraft-version": __version__, "analysis": {"attributes": []}, }
def test_manifest_checkers_multiple(tmp_path): """Multiple checkers, attributes and a linter.""" linting_results = [ linters.CheckResult( name="attrib-name-1", check_type=linters.CheckType.attribute, url="url", text="text", result="result-1", ), linters.CheckResult( name="attrib-name-2", check_type=linters.CheckType.attribute, url="url", text="text", result="result-2", ), linters.CheckResult( name="warning-name", check_type=linters.CheckType.lint, url="url", text="text", result="result", ), ] tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system="SuperUbuntu", release="40.10", machine="SomeRISC") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp, None, linting_results) assert result_filepath == tmp_path / "manifest.yaml" saved = yaml.safe_load(result_filepath.read_text()) expected = [ { "name": "attrib-name-1", "result": "result-1", }, { "name": "attrib-name-2", "result": "result-2", }, ] assert saved["analysis"]["attributes"] == expected
def test_manifest_simple_ok(tmp_path): """Simple construct.""" tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system='SuperUbuntu', release='40.10', machine='SomeRISC') with patch('charmcraft.utils.get_os_platform', return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp) assert result_filepath == tmp_path / 'manifest.yaml' saved = yaml.safe_load(result_filepath.read_text()) expected = { 'charmcraft-started-at': '2020-02-01T15:40:33Z', 'charmcraft-version': __version__, 'bases': [{ 'name': 'SuperUbuntu', 'channel': '40.10', 'architectures': ['SomeRISC'], }], } assert saved == expected
def test_manifest_simple_ok(tmp_path): """Simple construct.""" tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system="SuperUbuntu", release="40.10", machine="SomeRISC") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp) assert result_filepath == tmp_path / "manifest.yaml" saved = yaml.safe_load(result_filepath.read_text()) expected = { "charmcraft-started-at": "2020-02-01T15:40:33Z", "charmcraft-version": __version__, "bases": [{ "name": "superubuntu", "channel": "40.10", "architectures": ["SomeRISC"], }], } assert saved == expected
def test_manifest_simple_ok(tmp_path): """Simple construct.""" bases_config = config.BasesConfiguration( **{ "build-on": [ config.Base( name="test-name", channel="test-channel", ), ], "run-on": [ config.Base( name="test-name", channel="test-channel", architectures=["arch1"], ), config.Base( name="test-name2", channel="test-channel2", architectures=["arch1", "arch2"], ), ], } ) linting_results = [ linters.CheckResult( name="check-name", check_type=linters.CheckType.attribute, url="url", text="text", result="check-result", ), ] tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system="SuperUbuntu", release="40.10", machine="SomeRISC") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp, bases_config, linting_results) assert result_filepath == tmp_path / "manifest.yaml" saved = yaml.safe_load(result_filepath.read_text()) expected = { "charmcraft-started-at": "2020-02-01T15:40:33Z", "charmcraft-version": __version__, "bases": [ { "name": "test-name", "channel": "test-channel", "architectures": ["arch1"], }, { "name": "test-name2", "channel": "test-channel2", "architectures": ["arch1", "arch2"], }, ], "analysis": { "attributes": [ { "name": "check-name", "result": "check-result", }, ], }, } assert saved == expected