Refactor SampleVNF wait method 09/62709/7
authorMytnyk, Volodymyr <volodymyrx.mytnyk@intel.com>
Fri, 21 Sep 2018 15:31:23 +0000 (16:31 +0100)
committerMytnyk, Volodymyr <volodymyrx.mytnyk@intel.com>
Fri, 1 Feb 2019 09:25:14 +0000 (09:25 +0000)
JIRA: YARDSTICK-1439

Change-Id: I26441fe1f6e34a867c1f19d60d2247e888af6c54
Signed-off-by: Mytnyk, Volodymyr <volodymyrx.mytnyk@intel.com>
yardstick/network_services/vnf_generic/vnf/acl_vnf.py
yardstick/network_services/vnf_generic/vnf/cgnapt_vnf.py
yardstick/network_services/vnf_generic/vnf/sample_vnf.py
yardstick/network_services/vnf_generic/vnf/vfw_vnf.py
yardstick/tests/unit/network_services/vnf_generic/vnf/test_sample_vnf.py

index e51d387..46380e2 100644 (file)
@@ -251,3 +251,7 @@ class AclApproxVnf(SampleVNF):
             setup_env_helper_type = AclApproxSetupEnvSetupEnvHelper
 
         super(AclApproxVnf, self).__init__(name, vnfd, setup_env_helper_type, resource_helper_type)
+
+    def wait_for_instantiate(self):
+        """Wait for VNF to initialize"""
+        self.wait_for_initialize()
index bfe628f..62e1509 100644 (file)
@@ -120,3 +120,7 @@ class CgnaptApproxVnf(SampleVNF):
             self.vnf_execute(cmd)
 
         time.sleep(WAIT_FOR_STATIC_NAPT)
+
+    def wait_for_instantiate(self):
+        """Wait for VNF to initialize"""
+        self.wait_for_initialize()
index 6c2e940..6ae393b 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2016-2018 Intel Corporation
+# Copyright (c) 2016-2019 Intel Corporation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -764,6 +764,53 @@ class SampleVNF(GenericVNF):
             # by other VNF output
             self.q_in.put('\r\n')
 
+    def wait_for_initialize(self):
+        buf = []
+        vnf_prompt_found = False
+        prompt_command = '\r\n'
+        script_name = 'non_existent_script_name'
+        done_string = 'Cannot open file "{}"'.format(script_name)
+        time.sleep(self.WAIT_TIME)  # Give some time for config to load
+        while True:
+            if not self._vnf_process.is_alive():
+                raise RuntimeError("%s VNF process died." % self.APP_NAME)
+            while self.q_out.qsize() > 0:
+                buf.append(self.q_out.get())
+                message = ''.join(buf)
+
+                if self.VNF_PROMPT in message and not vnf_prompt_found:
+                    # Once we got VNF promt, it doesn't mean that the VNF is
+                    # up and running/initialized completely. But we can run
+                    # addition (any) VNF command and wait for it to complete
+                    # as it will be finished ONLY at the end of the VNF
+                    # initialization. So, this approach can be used to
+                    # indentify that VNF is completely initialized.
+                    LOG.info("Got %s VNF prompt.", self.APP_NAME)
+                    prompt_command = "run {}\r\n".format(script_name)
+                    self.q_in.put(prompt_command)
+                    # Cut the buffer since we are not interesting to find
+                    # the VNF prompt anymore
+                    prompt_pos = message.find(self.VNF_PROMPT)
+                    buf = [message[prompt_pos + len(self.VNF_PROMPT):]]
+                    vnf_prompt_found = True
+                    continue
+
+                if done_string in message:
+                    LOG.info("%s VNF is up and running.", self.APP_NAME)
+                    self._vnf_up_post()
+                    self.queue_wrapper.clear()
+                    return self._vnf_process.exitcode
+
+                if "PANIC" in message:
+                    raise RuntimeError("Error starting %s VNF." %
+                                       self.APP_NAME)
+
+            LOG.info("Waiting for %s VNF to start.. ", self.APP_NAME)
+            time.sleep(self.WAIT_TIME_FOR_SCRIPT)
+            # Send command again to display the expected prompt in case the
+            # expected text was corrupted by other VNF output
+            self.q_in.put(prompt_command)
+
     def start_collect(self):
         self.resource_helper.start_collect()
 
index 432f30a..5722431 100644 (file)
@@ -57,3 +57,7 @@ class FWApproxVnf(SampleVNF):
             setup_env_helper_type = FWApproxSetupEnvHelper
 
         super(FWApproxVnf, self).__init__(name, vnfd, setup_env_helper_type, resource_helper_type)
+
+    def wait_for_instantiate(self):
+        """Wait for VNF to initialize"""
+        self.wait_for_initialize()
index 405eece..5dae5cd 100644 (file)
@@ -16,6 +16,7 @@ from copy import deepcopy
 
 import unittest
 import mock
+import time
 
 import paramiko
 
@@ -1627,6 +1628,39 @@ class TestSampleVnf(unittest.TestCase):
 
         self.assertEqual(sample_vnf.wait_for_instantiate(), 0)
 
+    @mock.patch.object(time, 'sleep')
+    @mock.patch.object(ssh, 'SSH')
+    def test_wait_for_initialize(self, ssh, *args):
+        test_base.mock_ssh(ssh, exec_result=(1, "", ""))
+        queue_get_list = [
+            'some output',
+            'pipeline> ',
+            'run non_existent_script_name',
+            'Cannot open file "non_existent_script_name"'
+        ]
+        queue_size_list = [
+            0,
+            len(queue_get_list[0]),
+            0,
+            len(queue_get_list[1]),
+            len(queue_get_list[2]),
+            0,
+            len(queue_get_list[3])
+        ]
+        vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
+        sample_vnf = SampleVNF('vnf1', vnfd)
+        sample_vnf.APP_NAME = 'sample1'
+        sample_vnf.WAIT_TIME_FOR_SCRIPT = 0
+        sample_vnf._vnf_process = mock.Mock()
+        sample_vnf._vnf_process.exitcode = 0
+        sample_vnf._vnf_process._is_alive.return_value = 1
+        sample_vnf.queue_wrapper = mock.Mock()
+        sample_vnf.q_in = mock.Mock()
+        sample_vnf.q_out = mock.Mock()
+        sample_vnf.q_out.qsize.side_effect = iter(queue_size_list)
+        sample_vnf.q_out.get.side_effect = iter(queue_get_list)
+        sample_vnf.wait_for_initialize()
+
     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
     def test_vnf_execute_with_queue_data(self, *args):
         queue_size_list = [