forked from kimchi-project/kimchi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvmusertests.py
82 lines (70 loc) · 2.59 KB
/
kvmusertests.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Project Kimchi
#
# Copyright IBM Corp, 2015-2016
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import platform
import threading
import libvirt
import psutil
from wok.plugins.kimchi.config import get_libvirt_path
from wok.rollbackcontext import RollbackContext
KVMUSERTEST_VM_NAME = 'KVMUSERTEST_VM'
class UserTests(object):
SIMPLE_VM_XML = """
<domain type='kvm'>
<name>%(name)s</name>
<memory unit='KiB'>262144</memory>
<os>
<type arch='%(arch)s'>hvm</type>
<boot dev='hd'/>
</os>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<cpu mode='host-passthrough'>
</cpu>
</domain>"""
lock = threading.Lock()
user = None
@classmethod
def probe_user(cls):
with cls.lock:
if cls.user:
return cls.user
arch = 'ppc64' if platform.machine() == 'ppc64le' else platform.machine()
xml = cls.SIMPLE_VM_XML % {'name': KVMUSERTEST_VM_NAME, 'arch': arch}
with RollbackContext() as rollback:
with cls.lock:
conn = libvirt.open(None)
rollback.prependDefer(conn.close)
f = libvirt.VIR_DOMAIN_START_AUTODESTROY
dom = conn.createXML(xml, flags=f)
rollback.prependDefer(dom.destroy)
filename = get_libvirt_path() + '/qemu/%s.pid' % KVMUSERTEST_VM_NAME
with open(filename) as f:
pidStr = f.read()
p = psutil.Process(int(pidStr))
# bug fix #357
# in psutil 2.0 and above versions, username will be a method,
# not a string
if callable(p.username):
cls.user = p.username()
else:
cls.user = p.username
return cls.user
if __name__ == '__main__':
ut = UserTests()
print(ut.probe_user())