Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
chore(style): Fix code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjw committed Sep 9, 2020
1 parent 06ff74c commit 724f722
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
10 changes: 5 additions & 5 deletions tests/__init__.py
Expand Up @@ -20,8 +20,8 @@

paramiko.client.SSHClient = MockSSHClient

from .test_arguments import TestArguments
from .test_login import TestLogin
from .test_prometheus import TestPrometheus
from .test_scrape import TestScrape
from .test_server import TestServer
from .test_arguments import TestArguments # noqa
from .test_login import TestLogin # noqa
from .test_prometheus import TestPrometheus # noqa
from .test_scrape import TestScrape # noqa
from .test_server import TestServer # noqa
10 changes: 8 additions & 2 deletions tests/mock_sshclient.py
Expand Up @@ -14,10 +14,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from paramiko.ssh_exception import AuthenticationException, NoValidConnectionsError
from paramiko.ssh_exception import AuthenticationException, \
NoValidConnectionsError

PROMPT = "ZySH> ".encode("utf8")


class MockSSHClient:
mock_sessions = {}

Expand All @@ -27,7 +29,9 @@ def __init__(self):

def connect(self, hostname, username, password):
if (hostname, username, password) in self.mock_sessions:
self.current_session = self.mock_sessions[(hostname, username, password)]
self.current_session = self.mock_sessions[(hostname,
username,
password)]
return

for session_key in self.mock_sessions.keys():
Expand All @@ -52,6 +56,7 @@ def reset(cls):
def add_session(cls, host, user, password, session):
cls.mock_sessions[(host, user, password)] = session


class MockSSHSession:
def __init__(self):
self.cmds = {}
Expand All @@ -78,6 +83,7 @@ def read(self, count):
self.recv_buffer = self.recv_buffer[count:]
return data


class MockChannel:
def __init__(self):
self.eof_received = False
7 changes: 5 additions & 2 deletions tests/test_login.py
Expand Up @@ -40,7 +40,10 @@ def setUp(self):
MockSSHClient.reset()

session = MockSSHSession()
MockSSHClient.add_session("192.168.1.1", "admin", "testpassword", session)
MockSSHClient.add_session("192.168.1.1",
"admin",
"testpassword",
session)

def test_correct_password(self):
session = login("192.168.1.1", "admin", "testpassword")
Expand All @@ -57,5 +60,5 @@ def test_logout(self):
"testpassword")

logout(session)

self.assertIsNone(session.current_session)
3 changes: 2 additions & 1 deletion tests/test_prometheus.py
Expand Up @@ -31,6 +31,7 @@ class TestPrometheus(unittest.TestCase):
def test_values(self):
prom = prometheus(XDSL, IFCONFIG)

self.assertIn("""zyxel_line_rate{bearer=0,stream="up"} 7833000""", prom)
self.assertIn("""zyxel_line_rate{bearer=0,stream="up"} 7833000""",
prom)
self.assertIn("""zyxel_packets{stream="tx",iface="ppp2.3"}"""
+ """ 7334759""", prom)
5 changes: 4 additions & 1 deletion tests/test_scrape.py
Expand Up @@ -32,7 +32,10 @@ def setUp(self):
session = MockSSHSession()
session.add_cmd("ifconfig\n", IFCONFIG)
session.add_cmd("xdslctl info\n", XDSL)
MockSSHClient.add_session("192.168.1.1", "admin", "testpassword", session)
MockSSHClient.add_session("192.168.1.1",
"admin",
"testpassword",
session)

def test_scrape_ifconfig(self):
session = login("192.168.1.1",
Expand Down
5 changes: 4 additions & 1 deletion tests/test_server.py
Expand Up @@ -43,7 +43,10 @@ def setUp(self):
session = MockSSHSession()
session.add_cmd("ifconfig\n", IFCONFIG)
session.add_cmd("xdslctl info\n", XDSL)
MockSSHClient.add_session("192.168.1.1", "testuser", "testpassword", session)
MockSSHClient.add_session("192.168.1.1",
"testuser",
"testpassword",
session)

def test_index(self):
handler = MockHandler()
Expand Down
25 changes: 17 additions & 8 deletions zyxelprometheus/prometheus.py
Expand Up @@ -42,6 +42,7 @@
("zyxel_dropped", "Bytes sent/received.", dropped_re),
]


def prometheus(xdsl, ifconfig):
output = []
if xdsl is not None:
Expand All @@ -53,16 +54,23 @@ def prometheus(xdsl, ifconfig):
continue
output.append("# HELP zyxel_line_rate The line rate.")
output.append("# TYPE zyxel_line_rate gauge")
output.append(f"""zyxel_line_rate{{bearer={bearer},stream="up"}} {line_rate_up}""")
output.append(f"""zyxel_line_rate{{bearer={bearer},stream="down"}} {line_rate_down}""")
output.append(f"""zyxel_line_rate{{bearer={bearer},stream="up"}}"""
+ f""" {line_rate_up}""")
output.append(
f"""zyxel_line_rate{{bearer={bearer},stream="down"}}"""
+ f""" {line_rate_down}""")

max_line_rate = max_line_rate_re.search(xdsl)
line_rate_up = int(max_line_rate.group("upstream"))*1000
line_rate_down = int(max_line_rate.group("downstream"))*1000
output.append("# HELP zyxel_max_line_rate The maxiumum attainable line rate.")
output.append("# TYPE zyxel_max_line_rate gauge")
output.append(f"""zyxel_max_line_rate{{stream="up"}} {line_rate_up}""")
output.append(f"""zyxel_max_line_rate{{stream="down"}} {line_rate_down}""")
output.append(
"# HELP zyxel_max_line_rate The maxiumum attainable line rate.")
output.append(
"# TYPE zyxel_max_line_rate gauge")
output.append(
f"""zyxel_max_line_rate{{stream="up"}} {line_rate_up}""")
output.append(
f"""zyxel_max_line_rate{{stream="down"}} {line_rate_down}""")

if ifconfig is not None:
for iface in iface_re.finditer(ifconfig):
Expand All @@ -74,8 +82,9 @@ def prometheus(xdsl, ifconfig):
for groups in metric_re.finditer(iface_stats):
metric_stream = groups.group(1).lower()
metric_value = int(groups.group(2))
output.append(f"""{metric}{{stream="{metric_stream}",iface="{iface_name}"}}"""
+ f""" {metric_value}""")
output.append(
f"""{metric}{{stream="{metric_stream}","""
+ f"""iface="{iface_name}"}} {metric_value}""")

return "\n".join(output)

Expand Down
2 changes: 2 additions & 0 deletions zyxelprometheus/scrape.py
Expand Up @@ -18,6 +18,7 @@

PROMPT = "ZySH> "


def _read_to_prompt(stdout):
endtime = time.time() + 5
chars = []
Expand All @@ -33,6 +34,7 @@ def _read_to_prompt(stdout):
break
return "".join(chars)


def scrape_xdsl(session):
stdin, stdout, stderr = session.exec_command("", get_pty=True)
_read_to_prompt(stdout)
Expand Down

0 comments on commit 724f722

Please sign in to comment.