aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xm-test/tests/network-attach/network_utils.py
blob: 0d1c2a726bcea64fbd98b4bef8de8110d2d4235b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python

# Copyright (C) International Business Machines Corp., 2005
# Author: Murillo F. Bernardes <mfb@br.ibm.com>

from XmTestLib import *

def count_eth(console):
    try:
        run = console.runCmd("ifconfig -a | grep eth")
    except ConsoleError, e:
        FAIL(str(e))
    return len(run['output'].splitlines())

def get_state(domain_name, number):
    s, o = traceCommand("xm network-list %s | awk '/^%d/ {print $5}'" %
                        (domain_name, number))
    print o
    
    if s != 0:
        FAIL("network-list failed")
    if o == "":
        return 0
    else:
        return int(o)

def network_attach(domain_name, console):
    eths_before = count_eth(console)
    status, output = traceCommand("xm network-attach %s" % domain_name)
    if status != 0:
        return -1, "xm network-attach returned invalid %i != 0" % status

    eths_after = count_eth(console)
    if (eths_after != (eths_before+1)):
        return -2, "Network device is not actually connected to domU"

    return 0, None 

def network_detach(domain_name, console, num=0):
    eths_before = count_eth(console)
    status, output = traceCommand("xm network-detach %s %d" % (domain_name, num))
    if status != 0:
        return -1, "xm network-detach returned invalid %i != 0" % status

    for i in range(10):
        if get_state(domain_name, num) == 0:
            break
        time.sleep(1)
    else:
        FAIL("network-detach failed: device did not disappear")

    eths_after = count_eth(console)
    if eths_after != (eths_before-1):
        return -2, "Network device was not actually disconnected from domU"

    return 0, None