Revision 2a410f76 snf-tools/synnefo_tools/burnin.py

b/snf-tools/synnefo_tools/burnin.py
58 58
from kamaki.clients.image import ImageClient
59 59
from kamaki.clients import ClientError
60 60

  
61
import fabric.api as fabric
62

  
63 61
from vncauthproxy.d3des import generate_response as d3des_generate_response
64 62

  
65 63
# Use backported unittest functionality if Python < 2.7
......
95 93

  
96 94

  
97 95
# --------------------------------------------------------------------
96
# Global functions
97
def _ssh_execute(hostip, username, password, command):
98
    """Execute a command via ssh"""
99
    ssh = paramiko.SSHClient()
100
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
101
    try:
102
        ssh.connect(hostip, username=username, password=password)
103
    except socket.error:
104
        raise AssertionError
105
    try:
106
        stdin, stdout, stderr = ssh.exec_command(command)
107
    except paramiko.SSHException:
108
        raise AssertionError
109
    status = stdout.channel.recv_exit_status()
110
    output = stdout.readlines()
111
    ssh.close()
112
    return output, status
113

  
114

  
115
# --------------------------------------------------------------------
98 116
# BurninTestReulst class
99 117
class BurninTestResult(unittest.TextTestResult):
100 118
    def addSuccess(self, test):
......
396 414
        self.assertEquals(ret, 0)
397 415

  
398 416
    def _get_hostname_over_ssh(self, hostip, username, password):
399
        ssh = paramiko.SSHClient()
400
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
401
        try:
402
            ssh.connect(hostip, username=username, password=password)
403
        except socket.error:
404
            raise AssertionError
405
        stdin, stdout, stderr = ssh.exec_command("hostname")
406
        lines = stdout.readlines()
417
        lines, status = _ssh_execute(
418
            hostip, username, password, "hostname")
407 419
        self.assertEqual(len(lines), 1)
408 420
        return lines[0]
409 421

  
......
469 481
            ssh = paramiko.SSHClient()
470 482
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
471 483
            ssh.connect(hostip, username=username, password=password)
484
            ssh.close()
472 485
        except socket.error:
473 486
            raise AssertionError
474 487

  
......
1176 1189
        myPass = self.password['A']
1177 1190

  
1178 1191
        log.info("SSH in server A as %s/%s" % (loginname, myPass))
1192
        command = "ifconfig eth1 %s" % self.priv_ip["A"]
1193
        output, status = _ssh_execute(
1194
            hostip, loginname, myPass, command)
1179 1195

  
1180
        res = False
1181

  
1182
        if loginname != "root":
1183
            with fabric.settings(
1184
                    fabric.hide('warnings', 'running'),
1185
                    warn_only=True,
1186
                    host_string=hostip,
1187
                    user=loginname, password=myPass):
1188
                conf_res = fabric.sudo('ifconfig eth1 %s' % self.priv_ip["A"])
1189
                if len(conf_res) == 0:
1190
                    res = True
1191

  
1192
        else:
1193
            with fabric.settings(
1194
                    fabric.hide('warnings', 'running'),
1195
                    warn_only=True,
1196
                    host_string=hostip,
1197
                    user=loginname, password=myPass):
1198
                conf_res = fabric.run('ifconfig eth1 %s' % self.priv_ip["A"])
1199
                if len(conf_res) == 0:
1200
                    res = True
1201

  
1202
        self.assertTrue(res)
1196
        self.assertEquals(status, 0)
1203 1197

  
1204 1198
    def test_003b_setup_interface_B(self):
1205 1199
        """Setup eth1 for server B"""
......
1226 1220
        myPass = self.password['B']
1227 1221

  
1228 1222
        log.info("SSH in server B as %s/%s" % (loginname, myPass))
1223
        command = "ifconfig eth1 %s" % self.priv_ip["B"]
1224
        output, status = _ssh_execute(
1225
            hostip, loginname, myPass, command)
1229 1226

  
1230
        res = False
1231

  
1232
        if loginname != "root":
1233
            with fabric.settings(
1234
                    fabric.hide('warnings', 'running'),
1235
                    warn_only=True,
1236
                    host_string=hostip,
1237
                    user=loginname, password=myPass):
1238
                conf_res = fabric.sudo('ifconfig eth1 %s' % self.priv_ip["B"])
1239
                if len(conf_res) == 0:
1240
                    res = True
1241

  
1242
        else:
1243
            with fabric.settings(
1244
                    fabric.hide('warnings', 'running'),
1245
                    warn_only=True,
1246
                    host_string=hostip,
1247
                    user=loginname, password=myPass):
1248
                conf_res = fabric.run('ifconfig eth1 %s' % self.priv_ip["B"])
1249
                if len(conf_res) == 0:
1250
                    res = True
1251

  
1252
        self.assertTrue(res)
1227
        self.assertEquals(status, 0)
1253 1228

  
1254 1229
    def test_003c_test_connection_exists(self):
1255 1230
        """Ping server B from server A to test if connection exists"""
......
1275 1250

  
1276 1251
        myPass = self.password['A']
1277 1252

  
1278
        try:
1279
            ssh = paramiko.SSHClient()
1280
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
1281
            ssh.connect(hostip, username=loginname, password=myPass)
1282
        except socket.error:
1283
            raise AssertionError
1284

  
1285 1253
        cmd = "if ping -c 2 -w 3 %s >/dev/null; \
1286 1254
               then echo \'True\'; fi;" % self.priv_ip["B"]
1287
        stdin, stdout, stderr = ssh.exec_command(cmd)
1288
        lines = stdout.readlines()
1255
        lines, status = _ssh_execute(
1256
            hostip, loginname, myPass, cmd)
1289 1257

  
1290 1258
        exists = False
1291 1259

  

Also available in: Unified diff