Changes:
* get_alias method now has optional default argument, value is set
when alias is not specified in cli args
* get_mreq() method returns dict with machine requirements for match
* breakpoint() is a new feature for config_only mode, execution is
paused until user presses Enter
* add_host(), add_interface() new methods for creating mreq dict,
also initializes HostAPI and InterfaceAPI methods
* match() runs match algorithm and prepares test environment,
returns True if task part should be executed
* deprecated methods removed
* m_id and if_id are automatically generated
Signed-off-by: Jiri Prochazka <jprochaz(a)redhat.com>
---
lnst/Controller/Task.py | 240 ++++++++++++++++++++++++++----------------------
1 file changed, 128 insertions(+), 112 deletions(-)
diff --git a/lnst/Controller/Task.py b/lnst/Controller/Task.py
index 5462d44..af9c201 100644
--- a/lnst/Controller/Task.py
+++ b/lnst/Controller/Task.py
@@ -32,20 +32,105 @@ except:
# The handle to be imported from each task
ctl = None
+def get_alias(alias, default=None):
+ return ctl.get_alias(alias, default)
+
+def get_mreq():
+ return ctl.get_mreq()
+
+def wait(seconds):
+ return ctl.wait(seconds)
+
+def get_module(name, options={}):
+ return ctl.get_module(name, options)
+
+def breakpoint():
+ if ctl.get_run_mode != "config_only":
+ return
+ raw_input("Press enter to continue: ")
+
+def add_host(params=None):
+ m_id = ctl.gen_m_id()
+ ctl.mreq[m_id] = {'interfaces' : {}, 'params' : {}}
+ handle = HostAPI(ctl, m_id)
+ ctl.add_host(m_id, handle)
+ return handle
+
+def match():
+ ctl.cleanup_slaves()
+
+ if ctl.first_run:
+ ctl.first_run = False
+ ctl.set_machine_requirements()
+
+ if ctl.prepare_test_env():
+ if ctl.get_run_mode == "match_setup":
+ return False
+ if ctl.packet_capture():
+ ctl.start_packet_capture()
+ return True
+ else:
+ if ctl._ctl._multi_match:
+ if ctl.prepare_test_env():
+ if ctl.get_run_mode == "match_setup":
+ return False
+ if ctl.packet_capture():
+ ctl.start_packet_capture()
+ return True
+ else:
+ return False
+ else:
+ return False
+
+
class TaskError(Exception): pass
class ControllerAPI(object):
""" An API class representing the controller. """
- def __init__(self, ctl, hosts):
+ def __init__(self, ctl):
self._ctl = ctl
+ self._run_mode = ctl._get_run_mode()
self._result = True
+ self.first_run = True
+ self._m_id_seq = 0
+ self.mreq = {}
self._perf_repo_api = PerfRepoAPI()
self._hosts = {}
+
+ def get_mreq(self):
+ return self.mreq
+
+ def cleanup_slaves(self):
+ self._ctl._cleanup_slaves()
+
+ def get_run_mode(self):
+ return self._run_mode
+
+ def set_machine_requirements(self):
+ self._ctl.set_machine_requirements()
+
+ def packet_capture(self):
+ return self._ctl._packet_capture
+
+ def start_packet_capture(self):
+ self._ctl._start_packet_capture()
+
+ def prepare_test_env(self):
+ return self._ctl.prepare_test_env()
+
+ def gen_m_id(self):
+ self._m_id_seq += 1
+ return "m_id_%s" % self._m_id_seq
+
+ def add_host(self, host_id, handle):
+ self._hosts[host_id] = handle
+
+ def init_hosts(self, hosts):
for host_id, host in hosts.iteritems():
- self._hosts[host_id] = HostAPI(self, host_id, host)
+ self._hosts[host_id].init_host(host)
def _run_command(self, command):
"""
@@ -58,27 +143,6 @@ class ControllerAPI(object):
self._result = self._result and res["passed"]
return res
- def get_host(self, host_id):
- """
- Get an API handle for the host from the recipe spec with
- a specific id.
-
- :param host_id: id of the host as defined in the recipe
- :type host_id: string
-
- :return: The host handle.
- :rtype: HostAPI
-
- :raises TaskError: If there is no host with such id.
- """
- if host_id not in self._hosts:
- raise TaskError("Host '%s' not found." % host_id)
-
- return self._hosts[host_id]
-
- def get_hosts(self):
- return self._hosts
-
def get_module(self, name, options={}):
"""
Initialize a module to be run on a host.
@@ -104,7 +168,7 @@ class ControllerAPI(object):
cmd = {"type": "ctl_wait", "seconds":
int(seconds)}
return self._ctl._run_command(cmd)
- def get_alias(self, alias):
+ def get_alias(self, alias, default=None):
"""
Get the value of user defined alias.
@@ -115,9 +179,13 @@ class ControllerAPI(object):
:rtype: string
"""
try:
- return self._ctl._get_alias(alias)
+ val = self._ctl._get_alias(alias)
+ if val is None:
+ return default
+ else:
+ return val
except XmlTemplateError:
- return None
+ return default
def get_aliases(self):
"""
@@ -177,21 +245,47 @@ class ControllerAPI(object):
class HostAPI(object):
""" An API class representing a host machine. """
- def __init__(self, ctl, host_id, host):
+ def __init__(self, ctl, host_id):
self._ctl = ctl
self._id = host_id
- self._m = host
+ self._m = None
self._ifaces = {}
+ self._if_id_seq = 0
+ self._bg_id_seq = 0
+
+ def _gen_if_id(self):
+ self._if_id_seq += 1
+ return "if_id_%s" % self._if_id_seq
+
+ def init_host(self, host):
+ self._m = host
+ self.init_ifaces()
+
+ def init_ifaces(self):
for interface in self._m.get_interfaces():
if interface.get_id() is None:
continue
- self._ifaces[interface.get_id()] = InterfaceAPI(interface, self)
+ self._ifaces[interface.get_id()].init_iface(interface)
- self._bg_id_seq = 0
+ def add_interface(self, label, netns=None, params=None):
+ m_id = self.get_id()
+ if_id = self._gen_if_id()
+
+ self._ctl.mreq[m_id]['interfaces'][if_id] = {}
+ self._ctl.mreq[m_id]['interfaces'][if_id]['network'] = label
+ self._ctl.mreq[m_id]['interfaces'][if_id]['netns'] = netns
+
+ if params:
+ self._ctl.mreq[m_id]['interfaces'][if_id]['params'] = params
+ else:
+ self._ctl.mreq[m_id]['interfaces'][if_id]['params'] = {}
+
+ self._ifaces[if_id] = InterfaceAPI(None, self)
+ return self._ifaces[if_id]
def get_id(self):
- return self._m.get_id()
+ return self._id
def config(self, option, value, persistent=False, netns=None):
"""
@@ -287,75 +381,6 @@ class HostAPI(object):
cmd_res = self._ctl._run_command(cmd)
return ProcessAPI(self._ctl, self._id, cmd_res, bg_id, cmd["netns"])
- def get_interfaces(self):
- return self._ifaces
-
- def get_interface(self, if_id):
- return self._ifaces[if_id]
-
- @deprecated
- def get_devname(self, if_id):
- """
- Returns devname of the interface.
-
- :param if_id: which interface
- :type if_id: string
-
- :return: Device name (e.g., eth0).
- :rtype: str
- """
- iface = self._ifaces[if_id]
- return iface.get_devname()
-
- @deprecated
- def get_hwaddr(self, if_id):
- """
- Returns hwaddr of the interface.
-
- :param if_id: which interface
- :type if_id: string
-
- :return: HW address (e.g., 00:11:22:33:44:55:FF).
- :rtype: str
- """
- iface = self._ifaces[if_id]
- return iface.get_hwaddr()
-
- @deprecated
- def get_ip(self, if_id, addr_number=0):
- """
- Returns an IP address of the interface.
-
- :param if_id: which interface
- :type if_id: string
-
- :param addr_number: which address
- :type addr_number: int
-
- :return: IP address (e.g., 192.168.1.10).
- :rtype: str
- """
- iface = self._ifaces[if_id]
- return iface.get_ip_addr(addr_number)
-
- @deprecated
- def get_prefix(self, if_id, addr_number=0):
- """
- Returns an IP address prefix (netmask)
- of the interface.
-
- :param if_id: which interface
- :type if_id: string
-
- :param addr_number: which address
- :type addr_number: int
-
- :return: netmask (e.g., 24).
- :rtype: str
- """
- iface = self._ifaces[if_id]
- return iface.get_ip_prefix(addr_number)
-
def sync_resources(self, modules=[], tools=[]):
res_table = self._ctl._ctl._resource_table
sync_table = {'module': {}, 'tools': {}}
@@ -470,6 +495,9 @@ class InterfaceAPI(object):
self._if = interface
self._host = host
+ def init_iface(self, interface):
+ self._if = interface
+
def get_id(self):
return self._if.get_id()
@@ -494,21 +522,9 @@ class InterfaceAPI(object):
def get_ips(self):
return VolatileValue(self._if.get_addresses)
- @deprecated
- def get_ip_addr(self, ip_index=0):
- return self.get_ip(ip_index)
-
- @deprecated
- def get_ip_addrs(self):
- return self.get_ips()
-
def get_prefix(self, ip_index=0):
return VolatileValue(self._if.get_prefix, ip_index)
- @deprecated
- def get_ip_prefix(self, ip_index=0):
- return self.get_prefix(ip_index)
-
def get_mtu(self):
return VolatileValue(self._if.get_mtu)
--
2.4.11