From fc5fa31198be6ad9e125c0d36c57ad1c0053661c Mon Sep 17 00:00:00 2001 From: Konstantin Koslowski Date: Tue, 4 Jul 2023 12:31:18 +0200 Subject: [PATCH] nomr: add lib, fail/sucess suites --- nomr/fail.rst | 46 +++++++++++++++++++++++ nomr/lib/libNom.py | 55 +++++++++++++++++++++++++++ nomr/lib/nom.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++ nomr/success.rst | 46 +++++++++++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 nomr/fail.rst create mode 100644 nomr/lib/libNom.py create mode 100644 nomr/lib/nom.py create mode 100644 nomr/success.rst diff --git a/nomr/fail.rst b/nomr/fail.rst new file mode 100644 index 0000000..098198b --- /dev/null +++ b/nomr/fail.rst @@ -0,0 +1,46 @@ +.. default-role:: code + +Settings +======== + +.. code:: robotframework + + *** Settings *** + Resource nom.resource + + +Test Cases +========== + +inactive +-------- + + success + res success + + fail + res fail + + echo + res echo foo + res echo bar + +.. code:: robotframework + + *** Test Cases *** + nslookup + res nslookup unifi.nom 192.168.11.4 + res nslookup innocence.nom 192.168.11.11 + res nslookup hifiberry1.nom 192.168.11.31 + res nslookup voronberry.nom 192.168.11.35 + res nslookup hhi.fraunhofer.de 193.174.67.39 + res nslookup google.de any + res nslookup wrong.nom False + + ping + res ping unifi.nom 1 + res ping google.de 20 + + speedtest + res speedtest up 25 + res speedtest down 100 diff --git a/nomr/lib/libNom.py b/nomr/lib/libNom.py new file mode 100644 index 0000000..d7ff230 --- /dev/null +++ b/nomr/lib/libNom.py @@ -0,0 +1,55 @@ +import os.path +import subprocess +import sys + + +class libNom(object): + def __init__(self): + self._lib_path = os.path.join(os.path.dirname(__file__), + 'nom.py') + def nom_success(self): + self._run_command('success') + + def nom_fail(self): + self._run_command('fail') + + def nom_echo(self, arg): + self._run_command('echo', arg) + + def nom_rand(self, arg): + self._run_command('rand', arg) + + def nom_nslookup(self, arg): + self._run_command('nslookup', arg) + + def nom_ping(self, arg): + self._run_command('ping', arg) + + def nom_speedtest(self, arg): + self._run_command('speedtest', arg) + + def result_is(self, expected_result): + if expected_result == "any" and str(self._result) != "False": + print(f"any: {self._result}") + return + if expected_result != self._result: + raise AssertionError("Expected result: '%s' but result: '%s'." + % (expected_result, self._result)) + + def result_leq(self, expected_result): + if not float(self._result) <= float(expected_result): + raise AssertionError("Expected result leq '%s' but result '%s'." + % (expected_result, self._result)) + + + def result_geq(self, expected_result): + if not float(self._result) >= float(expected_result): + raise AssertionError("Expected result geq '%s' but result '%s'." + % (expected_result, self._result)) + + def _run_command(self, command, *args): + command = [sys.executable, self._lib_path, command] + list(args) + process = subprocess.Popen(command, + universal_newlines=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + self._result = process.communicate()[0].strip() diff --git a/nomr/lib/nom.py b/nomr/lib/nom.py new file mode 100644 index 0000000..1e3d51a --- /dev/null +++ b/nomr/lib/nom.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +import os.path +import sys +import random +import dns.resolver +from ping3 import ping +import speedtest + + +class Nom(object): + def __init__(self): + pass + + def __enter__(self): + return self + + def __exit__(self, *exc_info): + pass + + def help(self): + file = os.path.basename(sys.argv[0]) + print(f"Usage: {file} [success | fail | echo | rand | nslookup | ping \ + | speedtest | help]") + + def success(self): + ret = True + return ret + + def fail(self): + ret = False + return ret + + def rand(self, arg): + ret = random.randint(0, int(arg)) + return ret + + def echo(self, arg): + return arg + + def nslookup(self, arg): + ret = False + try: + res = dns.resolver.resolve(arg, "A") + ret = ",".join(str(r) for r in res) + except: + pass + + # print(f"nslookup {arg}: {ret}") + return ret + + def ping(self, arg): + ret = False + try: + ret = ping(arg, unit='ms') + except Exception as ex: + print(f"Exception Type:{type(ex).__name__}, args:{ex.args}") + + # print(f"ping {arg}: {ret}") + return ret + + def speedtest(self, arg): + ret = 0.0 + s = speedtest.Speedtest() + s.get_best_server() + threads = None + if arg == "up": + res = s.upload(threads=threads) + elif arg == "down": + res = s.download(threads=threads) + + ret = float(res)/1E6 + # print(f"speedtest {arg}: {ret}") + return ret + + +def main(): + try: + action = sys.argv[1] + except IndexError: + action = "help" + args = sys.argv[2:] + + with Nom() as N: + try: + f = getattr(N, action) + print(f(*args)) + except Exception as ex: + print(f"Exception Type:{type(ex).__name__}, args:{ex.args}") + N.help() + + +if __name__ == "__main__": + main() diff --git a/nomr/success.rst b/nomr/success.rst new file mode 100644 index 0000000..4714686 --- /dev/null +++ b/nomr/success.rst @@ -0,0 +1,46 @@ +.. default-role:: code + +Settings +======== + +.. code:: robotframework + + *** Settings *** + Resource nom.resource + + +Test Cases +========== + +inactive +-------- + + success + res success + + fail + res fail + + echo + res echo foo + res echo bar + +.. code:: robotframework + + *** Test Cases *** + nslookup + res nslookup unifi.nom 192.168.11.4 + res nslookup innocence.nom 192.168.11.11 + res nslookup hifiberry1.nom 192.168.11.31 + res nslookup voronberry.nom 192.168.11.35 + res nslookup hhi.fraunhofer.de 193.174.67.39 + res nslookup google.de any + res nslookup wrong.nom False + + ping + res ping unifi.nom 1 + res ping google.de 20 + + speedtest + res speedtest up 25 + res speedtest down 85