From: Ondrej Lichtner <olichtne(a)redhat.com>
After some more use I decided to go back on by decision to make IpParam
check if the value is a BaseIpAddress object and instead use the
ipaddress factory method again.
I also changed how HostnameParam works. Instead of accepting Ip
addresses AND hostnames it should only accept hostnames.
To have a mixed option I also added the HostnameOrIpParam class.
Finally I made the necessary modifications to the Iperf and Ping test
modules.
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
lnst/Common/Parameters.py | 27 +++++++++++++++++----------
lnst/Tests/Iperf.py | 6 +++---
lnst/Tests/Ping.py | 4 ++--
3 files changed, 22 insertions(+), 15 deletions(-)
diff --git a/lnst/Common/Parameters.py b/lnst/Common/Parameters.py
index 7cfc451..00c7832 100644
--- a/lnst/Common/Parameters.py
+++ b/lnst/Common/Parameters.py
@@ -17,7 +17,7 @@ olichtne(a)redhat.com (Ondrej Lichtner)
import copy
import re
from lnst.Common.DeviceRef import DeviceRef
-from lnst.Common.IpAddress import BaseIpAddress
+from lnst.Common.IpAddress import BaseIpAddress, ipaddress
from lnst.Common.LnstError import LnstError
class ParamError(LnstError):
@@ -62,25 +62,32 @@ class BoolParam(Param):
class IpParam(Param):
def type_check(self, value):
- if isinstance(value, BaseIpAddress):
- return value
- else:
+ try:
+ return ipaddress(value)
+ except LnstError:
raise ParamError("Value must be a BaseIpAddress object")
class HostnameParam(Param):
def type_check(self, value):
- if isinstance(value, BaseIpAddress):
- return value
-
- if len(value) > 255:
- raise ParamError("Value must be a BaseIpAddress object or a valid
hostname")
+ if not isinstance(value, str) or len(value) > 255:
+ raise ParamError("Value must be a valid hostname string")
hostname_re = ("^([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9])"
"(\.([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9]))*$")
if re.match(hostname_re, value, re.IGNORECASE):
return value
else:
- raise ParamError("Value must be a BaseIpAddress object or a valid
hostname")
+ raise ParamError("Value must be a valid hostname string")
+
+class HostnameOrIpParam(IpParam, HostnameParam):
+ def type_check(self, value):
+ try:
+ return IpParam.type_check(self, value)
+ except:
+ try:
+ return HostnameParam.type_check(self, value)
+ except:
+ raise ParamError("Value must be a valid hostname string, ipaddress
string or a BaseIpAddress object.")
class DeviceParam(Param):
def type_check(self, value):
diff --git a/lnst/Tests/Iperf.py b/lnst/Tests/Iperf.py
index 213ee04..10bf974 100644
--- a/lnst/Tests/Iperf.py
+++ b/lnst/Tests/Iperf.py
@@ -6,7 +6,7 @@ import time
import subprocess
import json
from lnst.Common.Parameters import IntParam, IpParam, StrParam, Param, BoolParam
-from lnst.Common.Parameters import HostnameParam
+from lnst.Common.Parameters import HostnameOrIpParam
from lnst.Common.Utils import is_installed
from lnst.Tests.BaseTestModule import BaseTestModule, TestModuleError
@@ -53,7 +53,7 @@ class IperfBase(BaseTestModule):
return True
class IperfServer(IperfBase):
- bind = HostnameParam()
+ bind = IpParam()
port = IntParam()
cpu_bind = IntParam()
opts = StrParam()
@@ -86,7 +86,7 @@ class IperfServer(IperfBase):
class IperfClient(IperfBase):
- server = HostnameParam(mandatory=True)
+ server = HostnameOrIpParam(mandatory=True)
duration = IntParam(default=10)
udp = BoolParam(default=False)
sctp = BoolParam(default=False)
diff --git a/lnst/Tests/Ping.py b/lnst/Tests/Ping.py
index e115cb9..a81cf1c 100644
--- a/lnst/Tests/Ping.py
+++ b/lnst/Tests/Ping.py
@@ -1,7 +1,7 @@
import re
import logging
import subprocess
-from lnst.Common.Parameters import IntParam, FloatParam, IpParam, DeviceOrIpParam
+from lnst.Common.Parameters import IntParam, FloatParam, HostnameOrIpParam,
DeviceOrIpParam
from lnst.Common.ExecCmd import exec_cmd
from lnst.Common.Utils import is_installed
from lnst.Common.IpAddress import Ip6Address
@@ -9,7 +9,7 @@ from lnst.Tests.BaseTestModule import BaseTestModule, TestModuleError
class Ping(BaseTestModule):
"""Port of old IcmpPing test modules"""
- dst = IpParam(mandatory=True)
+ dst = HostnameOrIpParam(mandatory=True)
count = IntParam(default=10)
interval = FloatParam(default=1.0)
interface = DeviceOrIpParam(mandatory=False)
--
2.19.0