~ressu/+junk/xen-ubuntu

« back to all changes in this revision

Viewing changes to tools/xm-test/lib/XmTestLib/network_utils.py

  • Committer: sami at haahtinen
  • Author(s): Bastian Blank
  • Date: 2010-09-03 15:14:28 UTC
  • Revision ID: sami@haahtinen.name-20100903151428-f88eg54n2fdnak41
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# Copyright (C) International Business Machines Corp., 2005
 
4
# Author: Murillo F. Bernardes <mfb@br.ibm.com>
 
5
 
 
6
from XmTestLib import *
 
7
 
 
8
def count_eth(console):
 
9
    try:
 
10
        run = console.runCmd("ifconfig -a | grep eth")
 
11
    except ConsoleError, e:
 
12
        FAIL(str(e))
 
13
    return len(run['output'].splitlines())
 
14
 
 
15
def get_state(domain_name, number):
 
16
    s, o = traceCommand("xm network-list %s | awk '/^%d/ {print $5}'" %
 
17
                        (domain_name, number))
 
18
    print o
 
19
    
 
20
    if s != 0:
 
21
        FAIL("network-list failed")
 
22
    if o == "":
 
23
        return 0
 
24
    else:
 
25
        return int(o)
 
26
 
 
27
def network_attach(domain_name, console, bridge=None):
 
28
    eths_before = count_eth(console)
 
29
    if bridge:
 
30
        status, output = traceCommand("xm network-attach %s bridge=%s"
 
31
                                      % (domain_name, bridge))
 
32
    else:
 
33
        status, output = traceCommand("xm network-attach %s" % domain_name)
 
34
    if status != 0:
 
35
        return -1, "xm network-attach returned invalid %i != 0" % status
 
36
 
 
37
    eths_after = count_eth(console)
 
38
    if (eths_after != (eths_before+1)):
 
39
        return -2, "Network device is not actually connected to domU"
 
40
 
 
41
    return 0, None 
 
42
 
 
43
def network_detach(domain_name, console, num=0):
 
44
    eths_before = count_eth(console)
 
45
    status, output = traceCommand("xm network-detach %s %d" % (domain_name, num))
 
46
    if status != 0:
 
47
        return -1, "xm network-detach returned invalid %i != 0" % status
 
48
 
 
49
    for i in range(10):
 
50
        if get_state(domain_name, num) == 0:
 
51
            break
 
52
        time.sleep(1)
 
53
    else:
 
54
        FAIL("network-detach failed: device did not disappear")
 
55
 
 
56
    eths_after = count_eth(console)
 
57
    if eths_after != (eths_before-1):
 
58
        return -2, "Network device was not actually disconnected from domU"
 
59
 
 
60
    return 0, None