-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add profile show commands and associated test, plus some clean up. Cl…
- Loading branch information
1 parent
943de56
commit 97c146b
Showing
7 changed files
with
197 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Copyright (c) 2017 Red Hat, Inc. | ||
# | ||
# This software is licensed to you under the GNU General Public License, | ||
# version 3 (GPLv3). There is NO WARRANTY for this software, express or | ||
# implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3 | ||
# along with this software; if not, see | ||
# https://www.gnu.org/licenses/gpl-3.0.txt. | ||
# | ||
""" NetworkShowCommand is used to show info on a specific network profiles | ||
for system scans | ||
""" | ||
|
||
from __future__ import print_function | ||
import sys | ||
from requests import codes | ||
from cli.utils import pretty_print | ||
from cli.clicommand import CliCommand | ||
import cli.network as network | ||
from cli.request import GET | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class NetworkShowCommand(CliCommand): | ||
""" | ||
This command is for showing a network which can later be used with a scan | ||
to gather facts. | ||
""" | ||
SUBCOMMAND = network.SUBCOMMAND | ||
ACTION = network.SHOW | ||
|
||
def __init__(self, subparsers): | ||
# pylint: disable=no-member | ||
CliCommand.__init__(self, self.SUBCOMMAND, self.ACTION, | ||
subparsers.add_parser(self.ACTION), GET, | ||
network.NETWORK_URI, [codes.ok]) | ||
self.parser.add_argument('--name', dest='name', metavar='NAME', | ||
help='profile name', required=True) | ||
|
||
def _build_req_params(self): | ||
self.req_params = {'name': self.args.name} | ||
|
||
def _handle_response_success(self): | ||
json_data = self.response.json() | ||
if len(json_data) == 1: | ||
cred_entry = json_data[0] | ||
data = pretty_print(cred_entry) | ||
print(data) | ||
else: | ||
print('Profile "%s" does not exist' % self.args.name) | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# | ||
# Copyright (c) 2017 Red Hat, Inc. | ||
# | ||
# This software is licensed to you under the GNU General Public License, | ||
# version 3 (GPLv3). There is NO WARRANTY for this software, express or | ||
# implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3 | ||
# along with this software; if not, see | ||
# https://www.gnu.org/licenses/gpl-3.0.txt. | ||
# | ||
"""Test the CLI module""" | ||
|
||
import unittest | ||
import sys | ||
from io import StringIO | ||
from argparse import ArgumentParser, Namespace | ||
import requests | ||
import requests_mock | ||
from cli.tests_utilities import HushUpStderr, redirect_stdout | ||
from cli.request import BASE_URL, CONNECTION_ERROR_MSG, SSL_ERROR_MSG | ||
from cli.network import NETWORK_URI | ||
from cli.network.show import NetworkShowCommand | ||
|
||
PARSER = ArgumentParser() | ||
SUBPARSER = PARSER.add_subparsers(dest='subcommand') | ||
|
||
|
||
class NetworkShowCliTests(unittest.TestCase): | ||
"""Class for testing the profile show commands for qpc""" | ||
def setUp(self): | ||
# Temporarily disable stderr for these tests, CLI errors clutter up | ||
# nosetests command. | ||
self.orig_stderr = sys.stderr | ||
sys.stderr = HushUpStderr() | ||
|
||
def tearDown(self): | ||
# Restore stderr | ||
sys.stderr = self.orig_stderr | ||
|
||
def test_show_network_ssl_err(self): | ||
"""Testing the show profile command with a connection error | ||
""" | ||
network_out = StringIO() | ||
url = BASE_URL + NETWORK_URI + '?name=profile1' | ||
with requests_mock.Mocker() as mocker: | ||
mocker.get(url, exc=requests.exceptions.SSLError) | ||
nsc = NetworkShowCommand(SUBPARSER) | ||
args = Namespace(name='profile1') | ||
with self.assertRaises(SystemExit): | ||
with redirect_stdout(network_out): | ||
nsc.main(args) | ||
self.assertEqual(network_out.getvalue(), SSL_ERROR_MSG) | ||
|
||
def test_show_network_conn_err(self): | ||
"""Testing the show profile command with a connection error | ||
""" | ||
network_out = StringIO() | ||
url = BASE_URL + NETWORK_URI + '?name=profile1' | ||
with requests_mock.Mocker() as mocker: | ||
mocker.get(url, exc=requests.exceptions.ConnectTimeout) | ||
nsc = NetworkShowCommand(SUBPARSER) | ||
args = Namespace(name='profile1') | ||
with self.assertRaises(SystemExit): | ||
with redirect_stdout(network_out): | ||
nsc.main(args) | ||
self.assertEqual(network_out.getvalue(), | ||
CONNECTION_ERROR_MSG) | ||
|
||
def test_show_network_internal_err(self): | ||
"""Testing the show profile command with an internal error | ||
""" | ||
network_out = StringIO() | ||
url = BASE_URL + NETWORK_URI + '?name=profile1' | ||
with requests_mock.Mocker() as mocker: | ||
mocker.get(url, status_code=500, json={'error': ['Server Error']}) | ||
nsc = NetworkShowCommand(SUBPARSER) | ||
args = Namespace(name='profile1') | ||
with self.assertRaises(SystemExit): | ||
with redirect_stdout(network_out): | ||
nsc.main(args) | ||
self.assertEqual(network_out.getvalue(), 'Server Error') | ||
|
||
def test_show_network_empty(self): | ||
"""Testing the show profile command successfully with empty data | ||
""" | ||
network_out = StringIO() | ||
url = BASE_URL + NETWORK_URI + '?name=profile1' | ||
with requests_mock.Mocker() as mocker: | ||
mocker.get(url, status_code=200, json=[]) | ||
nsc = NetworkShowCommand(SUBPARSER) | ||
args = Namespace(name='profile1') | ||
with self.assertRaises(SystemExit): | ||
with redirect_stdout(network_out): | ||
nsc.main(args) | ||
self.assertEqual(network_out.getvalue(), | ||
'Profile "profile1" does not exist\n') | ||
|
||
def test_show_network_data(self): | ||
"""Testing the show profile command successfully with stubbed data | ||
""" | ||
network_out = StringIO() | ||
url = BASE_URL + NETWORK_URI + '?name=profile1' | ||
auth_entry = {'id': 1, 'name': 'profile1', 'hosts': ['1.2.3.4'], | ||
'credentials': [{'id': 1, 'name': 'auth1'}]} | ||
data = [auth_entry] | ||
with requests_mock.Mocker() as mocker: | ||
mocker.get(url, status_code=200, json=data) | ||
nsc = NetworkShowCommand(SUBPARSER) | ||
args = Namespace(name='profile1') | ||
with redirect_stdout(network_out): | ||
nsc.main(args) | ||
expected = '{"credentials":[{"id":1,"name":"auth1"}],' \ | ||
'"hosts":["1.2.3.4"],"id":1,"name":"profile1"}' | ||
self.assertEqual(network_out.getvalue().replace('\n', '') | ||
.replace(' ', '').strip(), expected) |