Skip to content

Commit

Permalink
Improved coverage of ServerFile class
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Apr 5, 2021
1 parent 69538b7 commit 845d220
Showing 1 changed file with 107 additions and 29 deletions.
136 changes: 107 additions & 29 deletions tests/unittest/test_server_file.py
Expand Up @@ -27,10 +27,12 @@
from ..utils.simplified_test_function import simplified_test_function


TEST_SDF_FILEPATH = 'tests/testfiles/server.yml'
TEST_SDF_FILEPATH_ABS = os.path.abspath(TEST_SDF_FILEPATH)
TEST_SERVERFILE_FILEPATH = 'tests/testfiles/server.yml'
TEST_SERVERFILE_FILEPATH_ABS = os.path.abspath(TEST_SERVERFILE_FILEPATH)
TEST_VAULTFILE_FILEPATH = 'tests/testfiles/vault.yml'
TEST_VAULTFILE_FILEPATH_ABS = os.path.abspath(TEST_VAULTFILE_FILEPATH)

TESTCASES_SDF_INIT = [
TESTCASES_SF_INIT = [

# Testcases for ServerFile.__init__()

Expand All @@ -48,11 +50,11 @@
"Order of positional parameters",
dict(
init_args=(
TEST_SDF_FILEPATH,
TEST_SERVERFILE_FILEPATH,
),
init_kwargs=dict(),
exp_attrs={
'filepath': TEST_SDF_FILEPATH_ABS,
'filepath': TEST_SERVERFILE_FILEPATH_ABS,
},
),
None, None, True
Expand All @@ -62,10 +64,10 @@
dict(
init_args=(),
init_kwargs=dict(
filepath=TEST_SDF_FILEPATH,
filepath=TEST_SERVERFILE_FILEPATH,
),
exp_attrs={
'filepath': TEST_SDF_FILEPATH_ABS,
'filepath': TEST_SERVERFILE_FILEPATH_ABS,
},
),
None, None, True
Expand All @@ -91,14 +93,28 @@
(ServerFileOpenError, "Cannot open server file"),
None, True
),
(
"Server file that references vault file",
dict(
init_args=(),
init_kwargs=dict(
filepath=TEST_SERVERFILE_FILEPATH,
),
exp_attrs={
'filepath': TEST_SERVERFILE_FILEPATH_ABS,
'vault_file': TEST_VAULTFILE_FILEPATH_ABS,
},
),
None, None, True
),
]


@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_SDF_INIT)
TESTCASES_SF_INIT)
@simplified_test_function
def test_SDF_init(testcase, init_args, init_kwargs, exp_attrs):
def test_ServerFile_init(testcase, init_args, init_kwargs, exp_attrs):
"""
Test function for ServerFile.__init__()
"""
Expand All @@ -123,7 +139,7 @@ def test_SDF_init(testcase, init_args, init_kwargs, exp_attrs):
format(attr_name, exp_attr_value, act_attr_value)


TESTCASES_SDF_LOAD = [
TESTCASES_SF_LOAD = [

# Testcases for ServerFile._load_server_file()

Expand Down Expand Up @@ -442,17 +458,17 @@ def test_SDF_init(testcase, init_args, init_kwargs, exp_attrs):

@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_SDF_LOAD)
TESTCASES_SF_LOAD)
@simplified_test_function
def test_SDF_load(testcase, sdf_yaml, exp_data):
def test_ServerFile_load(testcase, sdf_yaml, exp_data):
"""
Test function for ServerFile._load_server_file()
"""

with TempDirectory() as tmp_dir:

# Create the server file
filename = 'tmp_sdf.yaml'
filename = 'tmp_server.yml'
filepath = os.path.join(tmp_dir.path, filename)
if isinstance(sdf_yaml, six.text_type):
sdf_yaml = sdf_yaml.encode('utf-8')
Expand All @@ -470,14 +486,15 @@ def test_SDF_load(testcase, sdf_yaml, exp_data):
assert act_data == exp_data


TESTCASES_SDF_GET_SERVER = [
TESTCASES_SF_GET_SERVER = [

# Testcases for ServerFile.get_server()

# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * sdf_yaml: Content of server file.
# * vf_yaml: Content of vault file, and indicator to use in server file.
# * nick: nickname input parameter for get_server().
# * exp_attrs: Dict with expected attributes of Server result
# of get_server(). Keys: attribute names; values: attribute values.
Expand All @@ -489,6 +506,7 @@ def test_SDF_load(testcase, sdf_yaml, exp_data):
"No servers; non-existing nickname",
dict(
sdf_yaml="servers: {}\n",
vf_yaml=None,
nick='srv',
exp_attrs=None,
),
Expand All @@ -503,6 +521,7 @@ def test_SDF_load(testcase, sdf_yaml, exp_data):
" description: server1\n"
" user_defined:\n"
" stuff: 42\n",
vf_yaml=None,
nick='srv',
exp_attrs=None,
),
Expand All @@ -522,6 +541,7 @@ def test_SDF_load(testcase, sdf_yaml, exp_data):
" description: group1\n"
" members:\n"
" - srv1\n",
vf_yaml=None,
nick='srv',
exp_attrs=None,
),
Expand All @@ -541,6 +561,7 @@ def test_SDF_load(testcase, sdf_yaml, exp_data):
" description: group1\n"
" members:\n"
" - srv1\n",
vf_yaml=None,
nick='srv1',
exp_attrs=dict(
nickname='srv1',
Expand All @@ -565,28 +586,85 @@ def test_SDF_load(testcase, sdf_yaml, exp_data):
" description: group1\n"
" members:\n"
" - srv1\n",
vf_yaml=None,
nick='grp1',
exp_attrs=None,
),
(KeyError, "Server with nickname 'grp1' not found"),
None, True
),
(
"One server with vault file, server exists in vault file",
dict(
sdf_yaml="vault_file: tmp_vault.yml\n"
"servers:\n"
" srv1:\n"
" description: server1\n"
" user_defined:\n"
" stuff: 42\n",
vf_yaml="secrets:\n"
" srv1:\n"
" foo: bar\n",
nick='srv1',
exp_attrs=dict(
nickname='srv1',
description='server1',
contact_name=None,
access_via=None,
user_defined={'stuff': 42},
secrets={'foo': 'bar'},
),
),
None, None, True
),
(
"One server with vault file, server does not exist in vault file",
dict(
sdf_yaml="vault_file: tmp_vault.yml\n"
"servers:\n"
" srv1:\n"
" description: server1\n"
" user_defined:\n"
" stuff: 42\n",
vf_yaml="secrets:\n"
" srv2:\n"
" foo: bar\n",
nick='srv1',
exp_attrs=dict(
nickname='srv1',
description='server1',
contact_name=None,
access_via=None,
user_defined={'stuff': 42},
secrets=None,
),
),
None, None, True
),
]


@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_SDF_GET_SERVER)
TESTCASES_SF_GET_SERVER)
@simplified_test_function
def test_SDF_get_server(testcase, sdf_yaml, nick, exp_attrs):
def test_ServerFile_get_server(testcase, sdf_yaml, vf_yaml, nick, exp_attrs):
"""
Test function for ServerFile.get_server()
"""

with TempDirectory() as tmp_dir:

# Create the vault file, if specified
if vf_yaml:
filename = 'tmp_vault.yml'
filepath = os.path.join(tmp_dir.path, filename)
if isinstance(vf_yaml, six.text_type):
vf_yaml = vf_yaml.encode('utf-8')
tmp_dir.write(filename, vf_yaml)

# Create the server file
filename = 'tmp_sdf.yaml'
filename = 'tmp_server.yml'
filepath = os.path.join(tmp_dir.path, filename)
if isinstance(sdf_yaml, six.text_type):
sdf_yaml = sdf_yaml.encode('utf-8')
Expand All @@ -607,7 +685,7 @@ def test_SDF_get_server(testcase, sdf_yaml, nick, exp_attrs):
assert getattr(act_srv, name) == exp_attrs[name]


TESTCASES_SDF_LIST_SERVERS = [
TESTCASES_SF_LIST_SERVERS = [

# Testcases for ServerFile.list_servers()

Expand Down Expand Up @@ -847,17 +925,17 @@ def test_SDF_get_server(testcase, sdf_yaml, nick, exp_attrs):

@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_SDF_LIST_SERVERS)
TESTCASES_SF_LIST_SERVERS)
@simplified_test_function
def test_SDF_list_servers(testcase, sdf_yaml, nick, exp_srvs_attrs):
def test_ServerFile_list_servers(testcase, sdf_yaml, nick, exp_srvs_attrs):
"""
Test function for ServerFile.list_servers()
"""

with TempDirectory() as tmp_dir:

# Create the server file
filename = 'tmp_sdf.yaml'
filename = 'tmp_server.yml'
filepath = os.path.join(tmp_dir.path, filename)
if isinstance(sdf_yaml, six.text_type):
sdf_yaml = sdf_yaml.encode('utf-8')
Expand Down Expand Up @@ -885,7 +963,7 @@ def test_SDF_list_servers(testcase, sdf_yaml, nick, exp_srvs_attrs):
assert getattr(act_sd, name) == exp_attrs[name]


TESTCASES_SDF_LIST_DEFAULT_SERVERS = [
TESTCASES_SF_LIST_DEFAULT_SERVERS = [

# Testcases for ServerFile.list_default_servers()

Expand Down Expand Up @@ -998,17 +1076,17 @@ def test_SDF_list_servers(testcase, sdf_yaml, nick, exp_srvs_attrs):

@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_SDF_LIST_DEFAULT_SERVERS)
TESTCASES_SF_LIST_DEFAULT_SERVERS)
@simplified_test_function
def test_SDF_list_default_servers(testcase, sdf_yaml, exp_srvs_attrs):
def test_ServerFile_list_default_servers(testcase, sdf_yaml, exp_srvs_attrs):
"""
Test function for ServerFile.list_default_servers()
"""

with TempDirectory() as tmp_dir:

# Create the server file
filename = 'tmp_sdf.yaml'
filename = 'tmp_server.yml'
filepath = os.path.join(tmp_dir.path, filename)
if isinstance(sdf_yaml, six.text_type):
sdf_yaml = sdf_yaml.encode('utf-8')
Expand Down Expand Up @@ -1036,7 +1114,7 @@ def test_SDF_list_default_servers(testcase, sdf_yaml, exp_srvs_attrs):
assert getattr(act_sd, name) == exp_attrs[name]


TESTCASES_SDF_LIST_ALL_SERVERS = [
TESTCASES_SF_LIST_ALL_SERVERS = [

# Testcases for ServerFile.list_all_servers()

Expand Down Expand Up @@ -1230,17 +1308,17 @@ def test_SDF_list_default_servers(testcase, sdf_yaml, exp_srvs_attrs):

@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_SDF_LIST_ALL_SERVERS)
TESTCASES_SF_LIST_ALL_SERVERS)
@simplified_test_function
def test_SDF_list_all_servers(testcase, sdf_yaml, exp_srvs_attrs):
def test_ServerFile_list_all_servers(testcase, sdf_yaml, exp_srvs_attrs):
"""
Test function for ServerFile.list_all_servers()
"""

with TempDirectory() as tmp_dir:

# Create the server file
filename = 'tmp_sdf.yaml'
filename = 'tmp_server.yml'
filepath = os.path.join(tmp_dir.path, filename)
if isinstance(sdf_yaml, six.text_type):
sdf_yaml = sdf_yaml.encode('utf-8')
Expand Down

0 comments on commit 845d220

Please sign in to comment.