1 # Copyright 2015-2017 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
21 from tools import tasks
23 class IVnf(tasks.Process):
33 Initialization method.
35 Purpose of this method is to initialize all
36 common Vnf data, no services should be started by
37 this call (use ``start`` method instead).
39 self._number = IVnf._number_vnfs
40 self._logger.debug('Initializing %s. VM with index %s',
41 self._number + 1, self._number)
42 IVnf._number_vnfs = IVnf._number_vnfs + 1
43 self._log_prefix = 'vnf_%d_cmd : ' % self._number
44 self._login_active = False
51 self._logger.info('Killing VNF...')
53 # force termination of VNF and wait for it to terminate; It will avoid
54 # sporadic reboot of host. (caused by hugepages or DPDK ports)
55 super(IVnf, self).kill(signal='-9', sleep=10)
57 def login(self, dummy_timeout=120):
59 Login to GUEST instance.
61 This can be used after booting the machine
63 :param timeout: Timeout to wait for login to complete.
65 :returns: True if login is active
67 raise NotImplementedError()
69 def execute(self, cmd, delay=0):
71 execute ``cmd`` with given ``delay``.
73 This method makes asynchronous call to guest system
74 and waits given ``delay`` before returning. Can be
75 used with ``wait`` method to create synchronous call.
77 :param cmd: Command to execute on guest system.
78 :param delay: Delay (in seconds) to wait after sending
79 command before returning. Please note that
80 this value can be floating point which
81 allows to pass milliseconds.
85 self._logger.debug('%s%s', self._log_prefix, cmd)
87 # ensure that output from previous commands is flushed
89 while not self._child.expect(r'.+', timeout=0.1):
91 except (pexpect.TIMEOUT, pexpect.EOF):
94 self._child.sendline(cmd)
97 def wait(self, prompt='', timeout=30):
99 wait for ``prompt`` on guest system for given ``timeout``.
101 This method ends based on two conditions:
102 * ``prompt`` has been detected
103 * ``timeout`` has been reached.
105 :param prompt: method end condition. If ``prompt``
106 won't be detected during given timeout,
107 method will return False.
108 :param timeout: Time to wait for prompt (in seconds).
109 Please note that this value can be floating
110 point which allows to pass milliseconds.
112 :returns: output of executed command
114 self._child.expect(prompt, timeout=timeout)
115 return self._child.before
117 def execute_and_wait(self, cmd, timeout=30, prompt=''):
119 execute ``cmd`` with given ``timeout``.
121 This method makes synchronous call to guest system
122 and waits till ``cmd`` execution is finished
123 (based on ``prompt value) or ''timeout'' has
126 :param cmd: Command to execute on guest system.
127 :param timeout: Timeout till the end of execution is not
129 :param prompt: method end condition. If ``prompt``
130 won't be detected during given timeout,
131 method will return False. If no argument
132 or None value will be passed, default
133 ``prompt`` passed in __init__
136 :returns: output of executed command
139 return self.wait(prompt=prompt, timeout=timeout)
141 def validate_start(self, _dummyresult):
142 """ Validate call of VNF start()
144 if self._child and self._child.isalive():
149 def validate_stop(self, result):
150 """ Validate call of VNF stop()
152 return not self.validate_start(result)
155 def validate_execute_and_wait(result, _dummy_cmd, _dummy_timeout=30, _dummy_prompt=''):
156 """ Validate command execution within VNF
158 return len(result) > 0
161 def validate_login(result):
162 """ Validate successful login into guest
167 def reset_vnf_counter():
169 Reset internal VNF counters
171 This method is static
173 IVnf._number_vnfs = 0