def test_framework_run_operator(): """Check for Operator Framework was successful.""" checker = Framework() with patch.object(Framework, "_check_operator", lambda self, path: True): result = checker.run("somepath") assert result == Framework.Result.operator assert checker.text == "The charm is based on the Operator Framework."
def test_framework_run_unknown(): """No check for any framework was successful.""" checker = Framework() with patch.object(Framework, "_check_operator", lambda self, path: False): with patch.object(Framework, "_check_reactive", lambda self, path: False): result = checker.run("somepath") assert result == Framework.Result.unknown assert checker.text == "The charm is not based on any known Framework."
def test_framework_run_reactive(): """Check for Reactive Framework was successful.""" checker = Framework() with patch.object(Framework, "_check_operator", lambda self, path: False): with patch.object(Framework, "_check_reactive", lambda self, path: True): result = checker.run("somepath") assert result == Framework.Result.reactive assert checker.text == "The charm is based on the Reactive Framework."
def test_framework_operator_venv_directory_missing(tmp_path): """The charm has not a specific 'venv' dir.""" # an entry point that import ops entrypoint = tmp_path / "charm.py" entrypoint.write_text("import ops") # check with patch("charmcraft.linters.check_dispatch_with_python_entrypoint" ) as mock_check: mock_check.return_value = pathlib.Path(entrypoint) result = Framework()._check_operator(tmp_path) assert result is False
def test_framework_reactive_no_wheelhouse(tmp_path, monkeypatch): """The wheelhouse directory does not exist.""" # metadata file with needed name field metadata_file = tmp_path / "metadata.yaml" metadata_file.write_text("name: foobar") # a Python file that imports charms.reactive entrypoint = tmp_path / "reactive" / "foobar.py" entrypoint.parent.mkdir() entrypoint.write_text("import charms.reactive") # check result = Framework()._check_reactive(tmp_path) assert result is False
def test_framework_reactive_no_entrypoint(tmp_path, monkeypatch): """Missing entrypoint file.""" # metadata file with needed name field metadata_file = tmp_path / "metadata.yaml" metadata_file.write_text("name: foobar") # the reactive lib is used reactive_lib = tmp_path / "wheelhouse" / "charms.reactive-1.0.1.zip" reactive_lib.parent.mkdir() reactive_lib.touch() # check result = Framework()._check_reactive(tmp_path) assert result is False
def test_framework_reactive_no_metadata(tmp_path, monkeypatch): """No useful name from metadata.""" # a Python file that imports charms.reactive entrypoint = tmp_path / "reactive" / "foobar.py" entrypoint.parent.mkdir() entrypoint.write_text("import charms.reactive") # the reactive lib is used reactive_lib = tmp_path / "wheelhouse" / "charms.reactive-1.0.1.zip" reactive_lib.parent.mkdir() reactive_lib.touch() # check result = Framework()._check_reactive(tmp_path) assert result is False
def test_framework_operator_corrupted_entrypoint(tmp_path): """Cannot parse the Python file.""" # an entry point that import ops entrypoint = tmp_path / "charm.py" entrypoint.write_text("xx --") # not really Python # an ops directory inside venv opsdir = tmp_path / "venv" / "ops" opsdir.mkdir(parents=True) # check with patch("charmcraft.linters.check_dispatch_with_python_entrypoint" ) as mock_check: mock_check.return_value = pathlib.Path(entrypoint) result = Framework()._check_operator(tmp_path) assert result is False
def test_framework_operator_language_not_python(tmp_path): """The language trait is not set to Python.""" # an entry point that is not really python entrypoint = tmp_path / "charm.py" entrypoint.write_text("no python :)") # an ops directory inside venv opsdir = tmp_path / "venv" / "ops" opsdir.mkdir(parents=True) # check with patch("charmcraft.linters.check_dispatch_with_python_entrypoint" ) as mock_check: mock_check.return_value = None result = Framework()._check_operator(tmp_path) assert result is False
def test_framework_operator_no_ops_imported(tmp_path, monkeypatch, import_line): """Different imports that are NOT importing the Operator Framework.""" # an entry point that import ops entrypoint = tmp_path / "charm.py" entrypoint.write_text(f"{import_line}") # an ops directory inside venv opsdir = tmp_path / "venv" / "ops" opsdir.mkdir(parents=True) # check with patch("charmcraft.linters.check_dispatch_with_python_entrypoint" ) as mock_check: mock_check.return_value = pathlib.Path(entrypoint) result = Framework()._check_operator(tmp_path) assert result is False
def test_framework_operator_venv_ops_directory_is_not_a_dir(tmp_path): """The charm has not a specific 'venv/ops' *dir*.""" # an entry point that import ops entrypoint = tmp_path / "charm.py" entrypoint.write_text("import ops") # an ops *file* inside venv opsfile = tmp_path / "venv" / "ops" opsfile.parent.mkdir() opsfile.touch() # check with patch("charmcraft.linters.check_dispatch_with_python_entrypoint" ) as mock_check: mock_check.return_value = pathlib.Path(entrypoint) result = Framework()._check_operator(tmp_path) assert result is False
def test_framework_operator_used_ok(tmp_path, import_line): """All conditions for 'framework' are in place.""" # an entry point that import ops entrypoint = tmp_path / "charm.py" entrypoint.write_text(f"{import_line}") # an ops directory inside venv opsdir = tmp_path / "venv" / "ops" opsdir.mkdir(parents=True) # check with patch("charmcraft.linters.check_dispatch_with_python_entrypoint" ) as mock_check: mock_check.return_value = pathlib.Path(entrypoint) result = Framework()._check_operator(tmp_path) assert result is True mock_check.assert_called_with(tmp_path)
def test_framework_reactive_no_reactive_lib(tmp_path, monkeypatch): """The wheelhouse directory has no reactive lib.""" # metadata file with needed name field metadata_file = tmp_path / "metadata.yaml" metadata_file.write_text("name: foobar") # a Python file that imports charms.reactive entrypoint = tmp_path / "reactive" / "foobar.py" entrypoint.parent.mkdir() entrypoint.write_text("import charms.reactive") # the reactive lib is used reactive_lib = tmp_path / "wheelhouse" / "charms.noreallyreactive-1.0.1.zip" reactive_lib.parent.mkdir() reactive_lib.touch() # check result = Framework()._check_reactive(tmp_path) assert result is False
def test_framework_reactive_no_reactive_imported(tmp_path, monkeypatch, import_line): """Different imports that are NOT importing the Reactive Framework.""" # metadata file with needed name field metadata_file = tmp_path / "metadata.yaml" metadata_file.write_text("name: foobar") # a Python file that imports charms.reactive entrypoint = tmp_path / "reactive" / "foobar.py" entrypoint.parent.mkdir() entrypoint.write_text(f"{import_line}") # the reactive lib is used reactive_lib = tmp_path / "wheelhouse" / "charms.reactive-1.0.1.zip" reactive_lib.parent.mkdir() reactive_lib.touch() # check result = Framework()._check_reactive(tmp_path) assert result is False
def test_framework_reactive_unaccesible_entrypoint(tmp_path, monkeypatch): """Cannot read the entrypoint file.""" # metadata file with needed name field metadata_file = tmp_path / "metadata.yaml" metadata_file.write_text("name: foobar") # a Python file that imports charms.reactive entrypoint = tmp_path / "reactive" / "foobar.py" entrypoint.parent.mkdir() entrypoint.write_text("import charms.reactive") entrypoint.chmod(0o000) # the reactive lib is used reactive_lib = tmp_path / "wheelhouse" / "charms.reactive-1.0.1.zip" reactive_lib.parent.mkdir() reactive_lib.touch() # check result = Framework()._check_reactive(tmp_path) assert result is False
def test_framework_reactive_used_ok(tmp_path, monkeypatch, import_line): """The reactive framework was used. Parametrized args: - import_line: different ways to express the import """ # metdata file with needed name field metadata_file = tmp_path / "metadata.yaml" metadata_file.write_text("name: foobar") # a Python file that imports charms.reactive entrypoint = tmp_path / "reactive" / "foobar.py" entrypoint.parent.mkdir() entrypoint.write_text(f"{import_line}") # the reactive lib is used reactive_lib = tmp_path / "wheelhouse" / "charms.reactive-1.0.1.zip" reactive_lib.parent.mkdir() reactive_lib.touch() # check result = Framework()._check_reactive(tmp_path) assert result is True