Signed-off-by: Jincheng Miao <jmiao@xxxxxxxxxx> --- repos/domain/guest_time.py | 98 ++++++++++++++++++++++++++++++++ repos/domain/set_guest_time.py | 122 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+), 0 deletions(-) create mode 100644 repos/domain/guest_time.py create mode 100644 repos/domain/set_guest_time.py diff --git a/repos/domain/guest_time.py b/repos/domain/guest_time.py new file mode 100644 index 0000000..4111e99 --- /dev/null +++ b/repos/domain/guest_time.py @@ -0,0 +1,98 @@ +#!/usr/bin/evn python +# To test guest time + +import time +import libxml2 + +import libvirt +from libvirt import libvirtError + +from src import sharedmod +from utils import utils + +required_params = ('guestname', 'username', 'userpassword') +optional_params = {} + +GET_TIME = 'date +%s' +DELTA = 3 + +def get_guest_mac(dom): + """ get guest's MAC address by parsing XML + """ + doc = libxml2.parseDoc(dom.XMLDesc()) + cont=doc.xpathNewContext() + macs = cont.xpathEval("/domain/devices/interface/mac/@address") + if macs == None: + return None + mac = macs[0].content + return mac + +def check_guest_status(domobj): + """ check guest current status + """ + state = domobj.info()[0] + if state == libvirt.VIR_DOMAIN_SHUTOFF or \ + state == libvirt.VIR_DOMAIN_SHUTDOWN: + return False + else: + return True + +def check_guest_time(t1, t2): + """ check result, the acceptable error within delta + """ + diff = abs(t1 - t2) + if diff > DELTA: + return False + else: + return True + +def guest_time(params): + """ test guest time + """ + logger = params['logger'] + guestname = params['guestname'] + username = params['username'] + userpassword = params['userpassword'] + + conn = sharedmod.libvirtobj['conn'] + + domobj = conn.lookupByName(guestname) + + # Check domain status + if check_guest_status(domobj): + pass + else: + domobj.create() + time.sleep(90) + + # get guest MAC + mac = get_guest_mac(domobj) + if mac == None: + logger.error("Failed to get guest's MAC address") + return 1 + else: + logger.info("guest's MAC is %s" % mac) + + ipaddr = utils.mac_to_ip(mac, 180) + if mac == None: + logger.error("Failed to get guest's IP address") + return 1 + else: + logger.info("guest's IP is %s" % ipaddr) + + try: + t1 = utils.remote_exec(ipaddr, username, userpassword, GET_TIME) + + t2 = domobj.getTime()['seconds'] + except libvirtError, e: + logger.error("API error message: %s, error code is %s" \ + % (e.message, e.get_error_code())) + return 1 + + if check_guest_time(long(t1), t2): + logger.info("checking guest time: %s" % t2) + else: + logger.error("checking guest time: failed") + return 1 + + return 0 \ No newline at end of file diff --git a/repos/domain/set_guest_time.py b/repos/domain/set_guest_time.py new file mode 100644 index 0000000..2aee8a4 --- /dev/null +++ b/repos/domain/set_guest_time.py @@ -0,0 +1,122 @@ +#!/usr/bin/evn python +# To test setting guest time + +import time +import libxml2 + +import libvirt +from libvirt import libvirtError + +from src import sharedmod +from utils import utils + +required_params = ('guestname', 'username', 'userpassword') +optional_params = {'flags' : 0} + +GET_TIME = 'date +%s' +DELTA = 30 + +def parse_flags(flags): + """ parse flags + """ + if flags == 'sync': + return 1 + elif flags == 0: + return 0 + else: + return -1 + +def get_guest_mac(dom): + """ get guest's MAC address by parsing XML + """ + doc = libxml2.parseDoc(dom.XMLDesc()) + cont=doc.xpathNewContext() + macs = cont.xpathEval("/domain/devices/interface/mac/@address") + if macs == None: + return None + mac = macs[0].content + return mac + +def check_guest_status(domobj): + """ check guest current status + """ + state = domobj.info()[0] + if state == libvirt.VIR_DOMAIN_SHUTOFF or \ + state == libvirt.VIR_DOMAIN_SHUTDOWN: + return False + else: + return True + +def check_guest_time(t1, t2): + """ check result, the acceptable error within delta + """ + diff = abs(t1 - t2) + if diff > DELTA: + return False + else: + return True + +def set_guest_time(params): + """ test setting guest time + """ + logger = params['logger'] + guestname = params['guestname'] + username = params['username'] + userpassword = params['userpassword'] + f = params.get('flags', 0) + flags = parse_flags(f) + + if flags == -1: + logger.error("unrecongnized flags: %s" % f) + return 1 + + conn = sharedmod.libvirtobj['conn'] + + domobj = conn.lookupByName(guestname) + + # Check domain status + if check_guest_status(domobj): + pass + else: + domobj.create() + time.sleep(90) + + # get guest MAC + mac = get_guest_mac(domobj) + if mac == None: + logger.error("Failed to get guest's MAC address") + return 1 + else: + logger.info("guest's MAC is %s" % mac) + + ipaddr = utils.mac_to_ip(mac, 180) + if mac == None: + logger.error("Failed to get guest's IP address") + return 1 + else: + logger.info("guest's IP is %s" % ipaddr) + + seconds = 100 + + try: + domobj.setTime({'seconds': seconds, 'nseconds': 0}, flags) + + sec = long(utils.remote_exec(ipaddr, username, userpassword, GET_TIME)) + + except libvirtError, e: + logger.error("API error message: %s, error code is %s" \ + % (e.message, e.get_error_code())) + return 1 + + if flags == 1: + logger.info("guest time is synchronized to %s" % sec) + return 0 + + if check_guest_time(seconds, sec): + logger.info("guest time is set to %s: pass" % seconds) + else: + logger.error("guest time %s is not matched with what we expected %s" % + (sec, seconds)) + return 1 + + return 0 \ No newline at end of file -- 1.7.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list