Adapt StatefulConnection class to advance image

Test test_established_tcp_session_after_re_attachinging_sg fails when it
is executed on advance images because launching commands in background
could not be done using the exec_command method
For cirros images it works fine, so the method used will depend on
the tempest configuration

Change-Id: Idf915c34ae6f08c2d1ac86f4c192824090f9d6e6
diff --git a/neutron_tempest_plugin/common/utils.py b/neutron_tempest_plugin/common/utils.py
index 898e4b9..4ccec72 100644
--- a/neutron_tempest_plugin/common/utils.py
+++ b/neutron_tempest_plugin/common/utils.py
@@ -29,11 +29,14 @@
 
 from tempest.lib import exceptions
 
+from neutron_tempest_plugin import config
+
 
 SCHEMA_PORT_MAPPING = {
     "http": 80,
     "https": 443,
 }
+CONF = config.CONF
 
 
 class classproperty(object):
@@ -161,13 +164,20 @@
         return 'attempt_{}'.format(str(self.test_attempt).zfill(3))
 
     def _start_connection(self):
+        if CONF.neutron_plugin_options.default_image_is_advanced:
+            server_exec_method = self.server_ssh.execute_script
+            client_exec_method = self.client_ssh.execute_script
+        else:
+            server_exec_method = self.server_ssh.exec_command
+            client_exec_method = self.client_ssh.exec_command
+
         self.server_ssh.exec_command(
                 'echo "{}" > input.txt'.format(self.test_str))
-        self.server_ssh.exec_command('tail -f input.txt | nc -lp '
+        server_exec_method('tail -f input.txt | nc -lp '
                 '{} &> output.txt &'.format(self.port))
         self.client_ssh.exec_command(
                 'echo "{}" > input.txt'.format(self.test_str))
-        self.client_ssh.exec_command('tail -f input.txt | nc {} {} &>'
+        client_exec_method('tail -f input.txt | nc {} {} &>'
                 'output.txt &'.format(self.ip, self.port))
 
     def _test_connection(self):
@@ -203,9 +213,11 @@
                 self._test_connection, timeout=timeout, sleep=sleep_timer)
 
     def __exit__(self, type, value, traceback):
-        self.server_ssh.exec_command('sudo killall nc || killall nc')
+        self.server_ssh.exec_command('sudo killall nc || killall nc || '
+                                     'echo "True"')
         self.server_ssh.exec_command(
                 'sudo killall tail || killall tail || echo "True"')
-        self.client_ssh.exec_command('sudo killall nc || killall nc')
+        self.client_ssh.exec_command('sudo killall nc || killall nc || '
+                                     'echo "True"')
         self.client_ssh.exec_command(
                 'sudo killall tail || killall tail || echo "True"')