~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
"""
 
3
 Copyright (C) International Business Machines Corp., 2006
 
4
 Author: Stefan Berger <stefanb@us.ibm.com>
 
5
 
 
6
 This program is free software; you can redistribute it and/or modify
 
7
 it under the terms of the GNU General Public License as published by
 
8
 the Free Software Foundation; under version 2 of the License.
 
9
 
 
10
 This program is distributed in the hope that it will be useful,
 
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 GNU General Public License for more details.
 
14
 
 
15
 You should have received a copy of the GNU General Public License
 
16
 along with this program; if not, write to the Free Software
 
17
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
"""
 
20
from Test import *
 
21
import xen.util.xsm.xsm as security
 
22
from xen.xm.main import server
 
23
from xen.util import xsconstants
 
24
import re
 
25
 
 
26
try:
 
27
    from acm_config import *
 
28
except:
 
29
    ACM_LABEL_RESOURCES = False
 
30
 
 
31
labeled_resources = {}
 
32
acm_verbose = False
 
33
policy='xm-test'
 
34
 
 
35
 
 
36
def isACMEnabled():
 
37
    return security.on()
 
38
 
 
39
def setCurrentPolicy(plcy):
 
40
    global policy
 
41
    policy = plcy
 
42
 
 
43
def ACMSetPolicy():
 
44
    cmd='xm dumppolicy | grep -E "^POLICY REFERENCE = ' + policy + '.$"'
 
45
    s, o = traceCommand(cmd)
 
46
    if o != "":
 
47
        return
 
48
    s, o = traceCommand("xm setpolicy ACM %s" % (policy))
 
49
    if s != 0:
 
50
        FAIL("Could not load the required policy '%s'.\n"
 
51
             "Start the system without any policy.\n%s" % \
 
52
             (policy, o))
 
53
 
 
54
def ACMPrepareSystem(resources):
 
55
    if isACMEnabled():
 
56
        ACMSetPolicy()
 
57
        ACMLabelResources(resources)
 
58
 
 
59
def ACMLabelResources(resources):
 
60
    for k, v in resources.items():
 
61
        if k == "disk":
 
62
            for vv in v:
 
63
                res = vv.split(',')[0]
 
64
                ACMLabelResource(res)
 
65
 
 
66
# Applications may label resources explicitly by calling this function
 
67
def ACMLabelResource(resource, label='red'):
 
68
    if not isACMEnabled():
 
69
        return
 
70
    if acm_verbose:
 
71
        print "labeling resource %s with label %s" % (resource, label)
 
72
    if not ACM_LABEL_RESOURCES:
 
73
        SKIP("Skipping test since not allowed to label resources in "
 
74
             "test suite")
 
75
    if not isACMResourceLabeled(resource):
 
76
        ACMUnlabelResource(resource)
 
77
        s, o = traceCommand("xm addlabel %s res %s" % (label, resource))
 
78
        if s != 0:
 
79
            FAIL("Could not add label to resource")
 
80
        else:
 
81
            labeled_resources["%s" % resource] = 1
 
82
 
 
83
 
 
84
# Application may remove a label from a resource. It has to call this
 
85
# function and must do so once a resource for re-labeling a resource
 
86
def ACMUnlabelResource(resource):
 
87
    s, o = traceCommand("xm rmlabel res %s" % (resource))
 
88
    labeled_resources["%s" % resource] = 0
 
89
 
 
90
 
 
91
def isACMResourceLabeled(resource):
 
92
    """ Check whether a resource has been labeled using this API
 
93
        and while running the application """
 
94
    try:
 
95
        if labeled_resources["%s" % resource] == 1:
 
96
            if acm_verbose:
 
97
                print "resource %s already labeled!" % resource
 
98
            return True
 
99
    except:
 
100
        return False
 
101
    return False