Skip to content

Commit

Permalink
tests: convert source tests to pytests
Browse files Browse the repository at this point in the history
  - Convert qpc/source tests to pytest:
      tests_source_add.py
      tests_source_clear.py
      tests_source_edit.py
      tests_source_list.py
      tests_source_show.py
  • Loading branch information
abellotti committed Jul 17, 2023
1 parent c5f12fa commit b45a46d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 167 deletions.
113 changes: 57 additions & 56 deletions qpc/source/tests_source_add.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Test the CLI module."""

import logging
import os
import sys
import unittest
from argparse import ArgumentParser, ArgumentTypeError, Namespace
from io import StringIO

import pytest
import requests
import requests_mock

Expand All @@ -22,17 +23,17 @@
TMP_HOSTFILE = "/tmp/testhostsfile"


class SourceAddCliTests(unittest.TestCase):
class TestSourceAddCli:
"""Class for testing the source add commands for qpc."""

@classmethod
def setUpClass(cls):
def setup_class(cls):
"""Set up test case."""
argument_parser = ArgumentParser()
subparser = argument_parser.add_subparsers(dest="subcommand")
cls.command = SourceAddCommand(subparser)

def setUp(self):
def setup_method(self, _test_method):
"""Create test setup."""
write_server_config(DEFAULT_CONFIG)
# Temporarily disable stderr for these tests, CLI errors clutter up
Expand All @@ -45,7 +46,7 @@ def setUp(self):
test_hostfile.write("1.2.3.4\n")
test_hostfile.write("1.2.3.[1:10]\n")

def tearDown(self):
def teardown_method(self, _test_method):
"""Remove test case setup."""
# Restore stderr
sys.stderr = self.orig_stderr
Expand All @@ -54,13 +55,13 @@ def tearDown(self):

def test_add_req_args_err(self):
"""Testing the add source command required flags."""
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
sys.argv = ["/bin/qpc", "source", "add", "--name", "source1"]
CLI().main()

def test_add_process_file(self):
"""Testing the add source command process file."""
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
sys.argv = [
"/bin/qpc",
"source",
Expand All @@ -79,31 +80,31 @@ def test_add_process_file(self):
def test_validate_port_string(self):
"""Testing the add source command with port validation non-integer."""
source_out = StringIO()
with self.assertRaises(ArgumentTypeError):
with pytest.raises(ArgumentTypeError):
with redirect_stdout(source_out):
validate_port("ff")
self.assertTrue("Port value ff" in source_out.getvalue())
assert "Port value ff" in source_out.getvalue()

def test_validate_port_bad_type(self):
"""Testing the add source command with port validation bad type."""
source_out = StringIO()
with self.assertRaises(ArgumentTypeError):
with pytest.raises(ArgumentTypeError):
with redirect_stdout(source_out):
validate_port(["ff"])
self.assertTrue("Port value ff" in source_out.getvalue())
assert "Port value ff" in source_out.getvalue()

def test_validate_port_range_err(self):
"""Test the add source command with port validation out of range."""
source_out = StringIO()
with self.assertRaises(ArgumentTypeError):
with pytest.raises(ArgumentTypeError):
with redirect_stdout(source_out):
validate_port("65537")
self.assertTrue("Port value 65537" in source_out.getvalue())
assert "Port value 65537" in source_out.getvalue()

def test_validate_port_good(self):
"""Testing the add source command with port validation success."""
val = validate_port("80")
self.assertEqual(80, val)
assert val == 80

def test_add_source_name_dup(self):
"""Testing the add source command duplicate name."""
Expand All @@ -124,11 +125,11 @@ def test_add_source_name_dup(self):
hosts=["1.2.3.4"],
port=22,
)
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(source_out):
self.command.main(args)
self.command.main(args)
self.assertTrue(
assert (
"source with this name already exists." in source_out.getvalue()
)

Expand All @@ -148,12 +149,12 @@ def test_add_source_cred_less(self):
type="network",
port=22,
)
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(source_out):
self.command.main(args)
self.assertTrue(
"An error occurred while processing "
'the "--cred" input' in source_out.getvalue()
assert (
'An error occurred while processing the "--cred" input'
in source_out.getvalue()
)

def test_add_source_cred_err(self):
Expand All @@ -170,12 +171,12 @@ def test_add_source_cred_err(self):
type="network",
port=22,
)
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(source_out):
self.command.main(args)
self.assertTrue(
"An error occurred while processing "
'the "--cred" input' in source_out.getvalue()
assert (
'An error occurred while processing the "--cred" input'
in source_out.getvalue()
)

def test_add_source_ssl_err(self):
Expand All @@ -192,10 +193,10 @@ def test_add_source_ssl_err(self):
type="network",
port=22,
)
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(source_out):
self.command.main(args)
self.assertEqual(source_out.getvalue(), CONNECTION_ERROR_MSG)
assert source_out.getvalue() == CONNECTION_ERROR_MSG

def test_add_source_conn_err(self):
"""Testing the add source command with a connection error."""
Expand All @@ -211,15 +212,15 @@ def test_add_source_conn_err(self):
type="network",
port=22,
)
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(source_out):
self.command.main(args)
self.assertEqual(source_out.getvalue(), CONNECTION_ERROR_MSG)
assert source_out.getvalue() == CONNECTION_ERROR_MSG

##################################################
# Network Source Test
##################################################
def test_add_source_net_one_host(self):
def test_add_source_net_one_host(self, caplog):
"""Testing add network source command successfully with one host."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -236,12 +237,12 @@ def test_add_source_net_one_host(self):
type="network",
port=22,
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

def test_add_source_net_valid_hosts(self):
def test_add_source_net_valid_hosts(self, caplog):
"""Testing add network source command with hosts in valid formats."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -267,12 +268,12 @@ def test_add_source_net_valid_hosts(self):
type="network",
port=22,
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

def test_add_source_with_paramiko(self):
def test_add_source_with_paramiko(self, caplog):
"""Testing add network source command with use_paramiko set to true."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -291,10 +292,10 @@ def test_add_source_with_paramiko(self):
port=22,
)

with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

def test_add_source_with_paramiko_and_ssl(self):
"""Testing add network source command with use_paramiko set to true."""
Expand All @@ -316,11 +317,11 @@ def test_add_source_with_paramiko_and_ssl(self):
type="network",
port=22,
)
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(source_out):
self.command.main(args)

def test_add_source_one_excludehost(self):
def test_add_source_one_excludehost(self, caplog):
"""Testing the add network source command with one exclude host."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -338,12 +339,12 @@ def test_add_source_one_excludehost(self):
exclude_hosts=["1.2.3.4"],
port=22,
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

def test_add_source_exclude_hosts(self):
def test_add_source_exclude_hosts(self, caplog):
"""Testing add network source command with many valid exclude hosts."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand Down Expand Up @@ -376,15 +377,15 @@ def test_add_source_exclude_hosts(self):
type="network",
port=22,
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

##################################################
# Vcenter Source Test
##################################################
def test_add_source_vc(self):
def test_add_source_vc(self, caplog):
"""Testing the add vcenter source command successfully."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -397,12 +398,12 @@ def test_add_source_vc(self):
args = Namespace(
name="source1", cred=["cred1"], hosts=["1.2.3.4"], type="vcenter"
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

def test_add_source_with_ssl_params(self):
def test_add_source_with_ssl_params(self, caplog):
"""Testing add vcenter source command with all ssl params."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -422,15 +423,15 @@ def test_add_source_with_ssl_params(self):
type="vcenter",
port=22,
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

##################################################
# Satellite Source Test
##################################################
def test_add_source_sat(self):
def test_add_source_sat(self, caplog):
"""Testing the add satellite source command successfully."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -443,12 +444,12 @@ def test_add_source_sat(self):
args = Namespace(
name="source1", cred=["cred1"], hosts=["1.2.3.4"], type="satellite"
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

def test_add_source_sat_no_ssl(self):
def test_add_source_sat_no_ssl(self, caplog):
"""Testing the add satellite with ssl_cert_verify set to false."""
get_cred_url = get_server_location() + CREDENTIAL_URI + "?name=cred1"
cred_results = [{"id": 1, "name": "cred1"}]
Expand All @@ -465,7 +466,7 @@ def test_add_source_sat_no_ssl(self):
type="satellite",
ssl_cert_verify="false",
)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.SOURCE_ADDED % "source1"
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text
Loading

0 comments on commit b45a46d

Please sign in to comment.