Skip to content

Commit

Permalink
Improve testing behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ezh committed Feb 18, 2020
1 parent 708c544 commit e81c777
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 72 deletions.
24 changes: 24 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""PyTest fixtures."""

import logging
import os
from shutil import copyfile

import pytest


@pytest.fixture(autouse=True)
def run_around_tests():
"""Clear logging cache before each test."""
logging.Logger.manager.loggerDict.clear()
yield


@pytest.fixture(scope="session")
def cfgdir(tmpdir_factory):
"""Prepare configuration directory for cloudselect."""
tmp = tmpdir_factory.mktemp("cloudselect")
src = os.path.join(os.path.dirname(__file__), "fixture", "cloud.json")
dst = os.path.join(str(tmp), "cloud.json")
copyfile(src, dst)
return tmp
8 changes: 4 additions & 4 deletions test/discovery/test_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from cloudselect.discovery.stub import Stub


def test_stub_discovery(tmpdir):
def test_stub_discovery(cfgdir):
"""
Testing Stub initializaion.
Is fabric creating Stub by default?
Does Stub return []?
Is Stub singleton?
"""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
# Read shared part
profile = cloud.configuration_read()
args = cloud.parse_args([])
Expand All @@ -30,9 +30,9 @@ def test_stub_discovery(tmpdir):
assert Container.discovery() == Container.discovery()


def test_stub_behaviour(mocker, tmpdir):
def test_stub_behaviour(mocker, cfgdir):
"""Assert calling run() for "cloudselect.discovery.stub" plugin."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
service_provider = cloud.plugin(
"cloudselect.discovery.stub", DiscoveryServiceProvider,
)
Expand Down
26 changes: 11 additions & 15 deletions test/group/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,20 @@
import logging
import os

import pytest

from cloudselect import Container
from cloudselect.cloudselect import CloudSelect
from cloudselect.group.simple import Simple


@pytest.fixture(autouse=True)
def run_around_tests():
"""Clear logging cache before each test."""
logging.Logger.manager.loggerDict.clear()
yield


def test_options(tmpdir):
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(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()
args = cloud.parse_args([])
configuration["group"] = {"type": "simple"}
Expand Down Expand Up @@ -68,10 +59,15 @@ def test_options(tmpdir):
assert Container.options("option", {"a": {"b": "c112111"}}) == options_a


def test_options_errors(caplog, tmpdir):
def test_options_errors(caplog, cfgdir):
"""Test error messages when options block is incorrect."""
caplog.set_level(logging.INFO)
cloud = CloudSelect(tmpdir)
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"}
Expand All @@ -95,11 +91,11 @@ def test_options_errors(caplog, tmpdir):
)


def test_options_regex(tmpdir):
def test_options_regex(cfgdir):
"""Test regex against metadata mock."""
metadata = os.path.join(os.path.dirname(__file__), "..", "fixture", "metadata.json")

cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()
args = cloud.parse_args([])
configuration["group"] = {
Expand Down
8 changes: 4 additions & 4 deletions test/pathfinder/test_bastion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
INSTANCE = Instance(1, "127.0.0.1", "key", "user", 22, None, {}, [])


def test_bastion_initialization(tmpdir):
def test_bastion_initialization(cfgdir):
"""Assert plugin initialization."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()
args = cloud.parse_args([])
configuration["pathfinder"] = {"type": "bastion"}
Expand All @@ -27,9 +27,9 @@ def test_bastion_initialization(tmpdir):
assert result.jumphost is None


def test_bastion_behaviour(tmpdir):
def test_bastion_behaviour(cfgdir):
"""Assert bastion returning correct result."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()
args = cloud.parse_args([])
configuration["pathfinder"] = {"type": "bastion", "host": "my-bastion-hostname"}
Expand Down
8 changes: 4 additions & 4 deletions test/pathfinder/test_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
INSTANCE = Instance(1, "127.0.0.1", "key", "user", 22, [], {}, [])


def test_stub_pathfinder(tmpdir):
def test_stub_pathfinder(cfgdir):
"""
Testing Stub initializaion.
Is fabric creating Stub by default?
Does Stub return {}?
Is Stub singleton?
"""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
# Read shared part
profile = cloud.configuration_read()
args = cloud.parse_args([])
Expand All @@ -33,9 +33,9 @@ def test_stub_pathfinder(tmpdir):
assert Container.pathfinder() == Container.pathfinder()


def test_stub_behaviour(mocker, tmpdir):
def test_stub_behaviour(mocker, cfgdir):
"""Assert calling run() for "cloudselect.pathfinder.stub" plugin."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
service_provider = cloud.plugin(
"cloudselect.pathfinder.stub", PathFinderServiceProvider,
)
Expand Down
8 changes: 4 additions & 4 deletions test/report/test_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from cloudselect.report.stub import Stub


def test_stub_report(tmpdir):
def test_stub_report(cfgdir):
"""
Testing Stub initializaion.
Is fabric creating Stub by default?
Does Stub return []?
Is Stub singleton?
"""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
# Read shared part
profile = cloud.configuration_read()
args = cloud.parse_args([])
Expand All @@ -30,9 +30,9 @@ def test_stub_report(tmpdir):
assert Container.report() == Container.report()


def test_stub_behaviour(mocker, tmpdir):
def test_stub_behaviour(mocker, cfgdir):
"""Assert calling run() for "cloudselect.report.stub" plugin."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
service_provider = cloud.plugin("cloudselect.report.stub", ReportServiceProvider)
stub = service_provider()
mocker.patch.object(Stub, "run")
Expand Down
56 changes: 21 additions & 35 deletions test/test_cloudselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# This file may not be copied, modified, or distributed
# except according to those terms.
"""This module is used for testing CloudSelect class."""
import logging
import os

import dependency_injector.providers as providers
Expand Down Expand Up @@ -37,21 +36,21 @@ def test_cli_incorrect_configuration(script_runner):
assert ret.stderr == 'Error: Profile "something_that_does_not_exist" not found\n'


def test_cli_version(tmpdir, script_runner):
def test_cli_version(cfgdir, script_runner):
"""Testing that cloudselect has expected version."""
ret = script_runner.run("cloudselect", "--version")
assert ret.success
assert ret.stdout == "cloudselect version {}\n".format(cloudselect.__version__)
assert ret.stderr == ""

with pytest.raises(SystemExit):
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
cloud.parse_args(["--version"])


def test_cli_verbose(tmpdir):
def test_cli_verbose(cfgdir):
"""Testing cloudselect verbose behavior."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
if os.environ.get("CLOUDSELECT_VERBOSE"):
del os.environ["CLOUDSELECT_VERBOSE"]
args = cloud.parse_args("-v".split(" "))
Expand All @@ -68,9 +67,9 @@ def test_cli_verbose(tmpdir):
assert args.verbose == 2


def test_cli_query(tmpdir):
def test_cli_query(cfgdir):
"""Testing query option."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
args = cloud.parse_args([])
assert args.query == ""
args = cloud.parse_args("-q test".split(" "))
Expand All @@ -87,23 +86,10 @@ def test_cli_profile():
assert args.profile == "test"


def test_cli_configuration_read(tmpdir):
def test_cli_configuration_read(cfgdir):
"""Testing initial configuration."""
initial_config = {
"log": {
"version": 1,
"formatters": {
"f": {"format": "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"},
},
"handlers": {
"h": {
"class": "logging.StreamHandler",
"formatter": "f",
"level": logging.DEBUG,
},
},
"root": {"handlers": ["h"], "level": logging.ERROR},
},
"log": {"version": 1},
"plugin": {
"discovery": {
"aws": "cloudselect.discovery.aws",
Expand All @@ -118,26 +104,26 @@ def test_cli_configuration_read(tmpdir):
},
},
}
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()
assert configuration == initial_config


def test_resolve(tmpdir):
def test_resolve(cfgdir):
"""Testing resolve behavior."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
discovery = cloud.resolve("cloudselect.discovery.aws", DiscoveryService)
discovery_instance = discovery()
assert discovery_instance.__class__.__name__ == "AWS"


def test_factory_load_plugin(tmpdir):
def test_factory_load_plugin(cfgdir):
"""Testing factory_load_plugin behavior."""
# fmt off
class BrokenServiceProvider(providers.Singleton):
"""Broken provider."""

cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = {}
discovery = cloud.fabric_load_plugin(
configuration, "discovery", DiscoveryServiceProvider, DiscoveryStub,
Expand Down Expand Up @@ -168,9 +154,9 @@ class BrokenServiceProvider(providers.Singleton):
assert isinstance(discovery(), AWS)


def test_factory_load_discovery(tmpdir):
def test_factory_load_discovery(cfgdir):
"""Testing that factory is able to load AWS discovery plugin."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()

configuration["discovery"] = {"type": "aws"}
Expand All @@ -186,9 +172,9 @@ def test_factory_load_discovery(tmpdir):
assert isinstance(plugin(), Local)


def test_factory_load_group(tmpdir):
def test_factory_load_group(cfgdir):
"""Testing that factory is able to load simple group plugin."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()

configuration["group"] = {"type": "simple"}
Expand All @@ -198,9 +184,9 @@ def test_factory_load_group(tmpdir):
assert isinstance(plugin(), Simple)


def test_factory_load_pathfinder(tmpdir):
def test_factory_load_pathfinder(cfgdir):
"""Testing that factory is able to load bastion pathfinder plugin."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()

configuration["pathfinder"] = {"type": "bastion"}
Expand All @@ -210,9 +196,9 @@ def test_factory_load_pathfinder(tmpdir):
assert isinstance(plugin(), Bastion)


def test_factory_load_report(tmpdir):
def test_factory_load_report(cfgdir):
"""Testing that factory is able to load json report plugin."""
cloud = CloudSelect(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()

configuration["report"] = {"type": "json"}
Expand Down
4 changes: 2 additions & 2 deletions test/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from cloudselect.cloudselect import CloudSelect


def test_options(tmpdir):
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(tmpdir)
cloud = CloudSelect(cfgdir)
configuration = cloud.configuration_read()
args = cloud.parse_args([])
cloud.fabric(configuration, args)
Expand Down
8 changes: 4 additions & 4 deletions test/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ def test_select_edit_non_existent_profile(mocker, capsys):
assert captured.err.endswith("Profile 'non-existent' does not exist\n")


def test_select_empty(tmpdir):
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(tmpdir)
cloud = CloudSelect(cfgdir)
args = cloud.parse_args([profile])
configuration = cloud.configuration_read()
configuration = cloud.merge(configuration, cloud.configuration_read(args.profile))
Expand All @@ -122,10 +122,10 @@ def test_select_empty(tmpdir):
assert pytest_wrapped_e.value.code == "Error: No instances found"


def test_select_single(tmpdir):
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(tmpdir)
cloud = CloudSelect(cfgdir)
args = cloud.parse_args([profile])
configuration = cloud.configuration_read()
configuration = cloud.merge(configuration, cloud.configuration_read(args.profile))
Expand Down

0 comments on commit e81c777

Please sign in to comment.