def test_stub_behaviour(mocker, cfgdir): """Assert calling run() for "cloudselect.report.stub" plugin.""" cloud = CloudSelect(cfgdir) service_provider = cloud.plugin("cloudselect.report.stub", ReportServiceProvider) stub = service_provider() mocker.patch.object(Stub, "run") stub.run([]) Stub.run.assert_called_once_with([])
def test_stub_behaviour(mocker, tmpdir): """Assert calling run() for "cloudselect.group.stub" plugin.""" cloud = CloudSelect(tmpdir) service_provider = cloud.plugin("cloudselect.group.stub", GroupServiceProvider) stub = service_provider() mocker.patch.object(Stub, "run") stub.run("aws", METADATA) Stub.run.assert_called_once_with("aws", METADATA)
def test_completer(capsys): """Testing that Selector completer process .cloud.json .cloud.yml and .cloud.yaml profiles.""" cloud = CloudSelect(os.path.join(os.path.dirname(__file__), "fixture")) args = cloud.parse_args([]) configuration = cloud.configuration_read() selector = cloud.fabric(configuration, args) selector.complete("", 0) captured = capsys.readouterr() assert captured.out == "a\nb\nc\nempty\nk8s\nsingle\n" assert captured.err == ""
def test_stub_behaviour(mocker, cfgdir): """Assert calling run() for "cloudselect.pathfinder.stub" plugin.""" cloud = CloudSelect(cfgdir) service_provider = cloud.plugin( "cloudselect.pathfinder.stub", PathFinderServiceProvider, ) stub = service_provider() mocker.patch.object(Stub, "run") stub.run(INSTANCE, [INSTANCE]) Stub.run.assert_called_once_with(INSTANCE, [INSTANCE])
def test_select_edit_yml_profile(mocker): """Testing that Selector invoking edit for YML profile properly.""" cloud = CloudSelect(os.path.join(os.path.dirname(__file__), "fixture")) args = cloud.parse_args(["-e", "c"]) configuration = cloud.configuration_read() selector = cloud.fabric(configuration, args) mocker.patch.object(Selector, "edit") selector.select() Selector.edit.assert_called_once_with( # pylint: disable=no-member os.path.join(os.path.dirname(__file__), "fixture", "c.cloud.yml"), )
def test_select_edit_non_existent_profile(mocker, capsys): """Testing that Selector shows error on if profile doesn't exist.""" cloud = CloudSelect(os.path.join(os.path.dirname(__file__), "fixture")) args = cloud.parse_args(["-v", "-e", "non-existent"]) configuration = cloud.configuration_read() selector = cloud.fabric(configuration, args) mocker.patch.object(Selector, "edit") selector.logger.disabled = False selector.select() Selector.edit.assert_not_called() # pylint: disable=no-member captured = capsys.readouterr() assert captured.out == "" assert captured.err.endswith("Profile 'non-existent' does not exist\n")
def test_profile_list(capsys): """Testing that Selector profile_list process .cloud.json .cloud.yml and .cloud.yaml profiles.""" cloud = CloudSelect(os.path.join(os.path.dirname(__file__), "fixture")) args = cloud.parse_args([]) configuration = cloud.configuration_read() selector = cloud.fabric(configuration, args) selector.profile_list() captured = capsys.readouterr() assert ( captured.out == "CloudSelect profiles:\n- a\n- b\n- c\n- empty\n- k8s\n- single\n" ) assert captured.err == ""
def test_kubernetes_discovery(cfgdir, mocker): """ Testing Kubernetes initializaion. Is fabric creating Stub by default? Does Kubernetes return []? """ k8s = os.path.join( os.path.dirname(__file__), "..", "fixture", "k8s.cloud.yaml", ) k8s_data = os.path.join( os.path.dirname(__file__), "..", "fixture", "k8s_discovery_data.yaml", ) k8s_pods = [] with open(k8s_data, "r") as data: k8s_pods = yaml.safe_load(data) cloud = CloudSelect(cfgdir) profile = cloud.configuration_read() profile = cloud.merge(profile, cloud.configuration_read(k8s)) args = cloud.parse_args([]) cloud.fabric(profile, args) assert Container.discovery().__class__.__name__ == "Kubernetes" mocker.patch.object(Kubernetes, "get_pods", return_value=k8s_pods) representation, instances = Container.discovery().run() assert len(instances) == 2 assert representation == [36, 37, 21, 7, 7, 11]
def test_options(cfgdir): """ Testing cloud.options(...) behaviour. There should be {} if there is no any options. There should be dictionary if there is required option. """ cloud = CloudSelect(cfgdir) configuration = cloud.configuration_read() args = cloud.parse_args([]) cloud.fabric(configuration, args) assert cloud.options("test") == {} assert cloud.options("plugin") == configuration["plugin"] assert cloud.options("log") == configuration["log"]
def test_options_errors(caplog, cfgdir): """Test error messages when options block is incorrect.""" caplog.set_level(logging.INFO) configuration = os.path.join( os.path.dirname(__file__), "..", "fixture", "metadata.json", ) logging.Logger.manager.loggerDict.clear() cloud = CloudSelect(cfgdir) configuration = cloud.configuration_read() args = cloud.parse_args([]) configuration["group"] = {"type": "simple"} cloud.fabric(configuration, args) assert isinstance(Container.group(), Simple) caplog.clear() Container.options("option") assert len(caplog.records) == 1 assert (caplog.records[0].getMessage() == "option: 'options' block not found in {'type': 'simple'}") caplog.clear() configuration["group"]["options"] = 123 Container.options("option") assert len(caplog.records) == 1 assert (caplog.records[0].msg == "%s: 'options' block should be list of dictionaries in %s")
def test_select_single(cfgdir): """Testing that Selector automaticaly select the only one available instance.""" profile = os.path.join(os.path.dirname(__file__), "fixture", "single.cloud.json") cloud = CloudSelect(cfgdir) args = cloud.parse_args([profile]) configuration = cloud.configuration_read() configuration = cloud.merge(configuration, cloud.configuration_read(args.profile)) assert args.profile == profile selector = cloud.fabric(configuration, args) assert isinstance(Container.discovery(), Local) report = selector.select() assert len(report["instances"]) == 1 assert report["instances"][0]["host"] == "my.cloud.instance"
def test_select_empty(cfgdir): """Testing that Selector exits if there is no any instances.""" profile = os.path.join(os.path.dirname(__file__), "fixture", "empty.cloud.json") cloud = CloudSelect(cfgdir) args = cloud.parse_args([profile]) configuration = cloud.configuration_read() configuration = cloud.merge(configuration, cloud.configuration_read(args.profile)) assert args.profile == profile selector = cloud.fabric(configuration, args) assert isinstance(Container.discovery(), Local) with pytest.raises(SystemExit) as pytest_wrapped_e: selector.select() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == "Error: No instances found"
def test_bastion_initialization(cfgdir): """Assert plugin initialization.""" cloud = CloudSelect(cfgdir) configuration = cloud.configuration_read() args = cloud.parse_args([]) configuration["pathfinder"] = {"type": "bastion"} cloud.fabric(configuration, args) assert isinstance(Container.pathfinder(), Bastion) result = Container.pathfinder().run(INSTANCE, [INSTANCE]) assert result.jumphost is None
def test_stub_pathfinder(cfgdir): """ Testing Stub initializaion. Is fabric creating Stub by default? Does Stub return {}? Is Stub singleton? """ cloud = CloudSelect(cfgdir) # Read shared part profile = cloud.configuration_read() args = cloud.parse_args([]) cloud.fabric(profile, args) assert Container.pathfinder().__class__.__name__ == "Stub" assert Container.pathfinder().run(INSTANCE, [INSTANCE]) == INSTANCE assert Container.pathfinder() == Container.pathfinder()
def test_stub_discovery(cfgdir): """ Testing Stub initializaion. Is fabric creating Stub by default? Does Stub return []? Is Stub singleton? """ cloud = CloudSelect(cfgdir) # Read shared part profile = cloud.configuration_read() args = cloud.parse_args([]) cloud.fabric(profile, args) assert Container.discovery().__class__.__name__ == "Stub" assert Container.discovery().run() == [] assert Container.discovery() == Container.discovery()
def test_stub_group(tmpdir): """ Testing Stub initializaion. Is fabric creating Stub by default? Does Stub return {}? Is Stub singleton? """ cloud = CloudSelect(tmpdir) # Read shared part profile = cloud.configuration_read() args = cloud.parse_args([]) cloud.fabric(profile, args) assert Container.group().__class__.__name__ == "Stub" assert Container.group().run("aws", METADATA) is None assert Container.group() == Container.group()
def test_options(cfgdir): """ Test options behaviour of simple plugin. It should returns {} if there is no options. It should returns shared dictionary if there is no any matched filters. It should returns clarified dictionary if there is matched filter. """ cloud = CloudSelect(cfgdir) configuration = cloud.configuration_read() args = cloud.parse_args([]) configuration["group"] = {"type": "simple"} cloud.fabric(configuration, args) assert isinstance(Container.group(), Simple) assert Container.options("test") == {} assert Container.options("plugin") == configuration["plugin"] assert Container.options("log") == configuration["log"] assert Container.options("option") == {} options_a = {"ssh": "-t", "ssh_command": "sudo -i"} options_b = {"ssh": "-t", "ssh_command": "su"} options_c = {"ssh": "-t", "ssh_command": None} configuration["option"] = options_a assert Container.options("option") == options_a configuration["group"]["options"] = [{"match": "x:y", "option": options_a}] assert Container.options("option") == options_a assert Container.options("option", {"a": {"b": "c123"}}) == options_a configuration["group"]["options"].append({ "match": "a.b:c123", "option": options_b }) assert Container.options("option") == options_a assert Container.options("option", {"a": {"b": "c123"}}) == options_b configuration["group"]["options"].append( { "match": "a.b:c(.*3|111)", "option": options_c }, ) assert Container.options("option") == options_a assert Container.options("option", {"a": {"b": "c1nnnn3"}}) == options_c assert Container.options("option", {"a": {"b": "c111111"}}) == options_c assert Container.options("option", {"a": {"b": "c112111"}}) == options_a
def test_bastion_behaviour(cfgdir): """Assert bastion returning correct result.""" cloud = CloudSelect(cfgdir) configuration = cloud.configuration_read() args = cloud.parse_args([]) configuration["pathfinder"] = { "type": "bastion", "host": "my-bastion-hostname" } cloud.fabric(configuration, args) assert isinstance(Container.pathfinder(), Bastion) result = Container.pathfinder().run(INSTANCE, [INSTANCE]) assert isinstance(result.jumphost, CloudInstance) assert result.jumphost.host == "my-bastion-hostname"
def test_options_regex(cfgdir): """Test regex against metadata mock.""" metadata = os.path.join(os.path.dirname(__file__), "..", "fixture", "metadata.json") cloud = CloudSelect(cfgdir) configuration = cloud.configuration_read() args = cloud.parse_args([]) configuration["group"] = { "type": "simple", "options": [{ "match": "Tags.Name:my-app.abc", "option": {} }], } configuration["option"] = {"ssh": "-t"} cloud.fabric(configuration, args) assert isinstance(Container.group(), Simple) with open(metadata) as json_file: data = json.load(json_file) assert Container.options("option") == {"ssh": "-t"} assert Container.options("option", data) == {}