nomr: add lib, fail/sucess suites

This commit is contained in:
Konstantin Koslowski 2023-07-04 12:31:18 +02:00
parent 41aef59775
commit fc5fa31198
4 changed files with 240 additions and 0 deletions

46
nomr/fail.rst Normal file
View file

@ -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

55
nomr/lib/libNom.py Normal file
View file

@ -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()

93
nomr/lib/nom.py Normal file
View file

@ -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 <arg> | rand <arg> | nslookup <host> | ping \
<host> | speedtest <up|down> | 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()

46
nomr/success.rst Normal file
View file

@ -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