From aa822eed7aa7ff4b6e822113c7a597cec6bd3ef8 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Wed, 8 Nov 2017 13:02:12 -0700 Subject: [PATCH] Make orchestra.console a subpackage Signed-off-by: Zack Cerza --- teuthology/orchestra/console/__init__.py | 5 ++ .../{console.py => console/physical.py} | 90 +------------------ teuthology/orchestra/console/virtual.py | 86 ++++++++++++++++++ teuthology/orchestra/test/test_console.py | 20 ++--- 4 files changed, 105 insertions(+), 96 deletions(-) create mode 100644 teuthology/orchestra/console/__init__.py rename teuthology/orchestra/{console.py => console/physical.py} (80%) create mode 100644 teuthology/orchestra/console/virtual.py diff --git a/teuthology/orchestra/console/__init__.py b/teuthology/orchestra/console/__init__.py new file mode 100644 index 0000000000..5615a7d784 --- /dev/null +++ b/teuthology/orchestra/console/__init__.py @@ -0,0 +1,5 @@ +import physical +import virtual + +PhysicalConsole = physical.PhysicalConsole +VirtualConsole = virtual.VirtualConsole diff --git a/teuthology/orchestra/console.py b/teuthology/orchestra/console/physical.py similarity index 80% rename from teuthology/orchestra/console.py rename to teuthology/orchestra/console/physical.py index c59c366531..d6546ec6b7 100644 --- a/teuthology/orchestra/console.py +++ b/teuthology/orchestra/console/physical.py @@ -6,18 +6,11 @@ import subprocess import sys import time -import teuthology.lock.query -import teuthology.lock.util -from teuthology.config import config - -from ..exceptions import ConsoleError +import teuthology.orchestra.remote -import remote +from teuthology.config import config -try: - import libvirt -except ImportError: - libvirt = None +from teuthology.exceptions import ConsoleError log = logging.getLogger(__name__) @@ -29,7 +22,7 @@ class PhysicalConsole(): def __init__(self, name, ipmiuser=None, ipmipass=None, ipmidomain=None, logfile=None, timeout=20): self.name = name - self.shortname = remote.getShortName(name) + self.shortname = teuthology.orchestra.remote.getShortName(name) self.timeout = timeout self.logfile = None self.ipmiuser = ipmiuser or config.ipmi_user @@ -306,78 +299,3 @@ class PhysicalConsole(): self.has_conserver = False proc = start() return proc - - -class VirtualConsole(): - """ - Virtual Console (set from getRemoteConsole) - """ - def __init__(self, name): - if libvirt is None: - raise RuntimeError("libvirt not found") - - self.shortname = remote.getShortName(name) - status_info = teuthology.lock.query.get_status(self.shortname) - try: - if teuthology.lock.query.is_vm(status=status_info): - phys_host = status_info['vm_host']['name'].split('.')[0] - except TypeError: - raise RuntimeError("Cannot create a virtual console for %s", name) - self.connection = libvirt.open(phys_host) - for i in self.connection.listDomainsID(): - d = self.connection.lookupByID(i) - if d.name() == self.shortname: - self.vm_domain = d - break - return - - def check_power(self, state, timeout=None): - """ - Return true if vm domain state indicates power is on. - """ - return self.vm_domain.info[0] in [libvirt.VIR_DOMAIN_RUNNING, - libvirt.VIR_DOMAIN_BLOCKED, - libvirt.VIR_DOMAIN_PAUSED] - - def check_status(self, timeout=None): - """ - Return true if running. - """ - return self.vm_domain.info()[0] == libvirt.VIR_DOMAIN_RUNNING - - def power_cycle(self): - """ - Simiulate virtual machine power cycle - """ - self.vm_domain.info().destroy() - self.vm_domain.info().create() - - def hard_reset(self): - """ - Simiulate hard reset - """ - self.vm_domain.info().destroy() - - def power_on(self): - """ - Simiulate power on - """ - self.vm_domain.info().create() - - def power_off(self): - """ - Simiulate power off - """ - self.vm_domain.info().destroy() - - def power_off_for_interval(self, interval=30): - """ - Simiulate power off for an interval. - """ - log.info('Power off {s} for {i} seconds'.format( - s=self.shortname, i=interval)) - self.vm_domain.info().destroy() - time.sleep(interval) - self.vm_domain.info().create() - log.info('Power off for {i} seconds completed'.format( - s=self.shortname, i=interval)) diff --git a/teuthology/orchestra/console/virtual.py b/teuthology/orchestra/console/virtual.py new file mode 100644 index 0000000000..a89f51a18f --- /dev/null +++ b/teuthology/orchestra/console/virtual.py @@ -0,0 +1,86 @@ +import logging +import time +try: + import libvirt +except ImportError: + libvirt = None + +import teuthology.lock.query +import teuthology.lock.util + +log = logging.getLogger(__name__) + + +class VirtualConsole(): + """ + Virtual Console (set from getRemoteConsole) + """ + def __init__(self, name): + if libvirt is None: + raise RuntimeError("libvirt not found") + + self.shortname = remote.getShortName(name) + status_info = teuthology.lock.query.get_status(self.shortname) + try: + if teuthology.lock.query.is_vm(status=status_info): + phys_host = status_info['vm_host']['name'].split('.')[0] + except TypeError: + raise RuntimeError("Cannot create a virtual console for %s", name) + self.connection = libvirt.open(phys_host) + for i in self.connection.listDomainsID(): + d = self.connection.lookupByID(i) + if d.name() == self.shortname: + self.vm_domain = d + break + return + + def check_power(self, state, timeout=None): + """ + Return true if vm domain state indicates power is on. + """ + return self.vm_domain.info[0] in [libvirt.VIR_DOMAIN_RUNNING, + libvirt.VIR_DOMAIN_BLOCKED, + libvirt.VIR_DOMAIN_PAUSED] + + def check_status(self, timeout=None): + """ + Return true if running. + """ + return self.vm_domain.info()[0] == libvirt.VIR_DOMAIN_RUNNING + + def power_cycle(self): + """ + Simiulate virtual machine power cycle + """ + self.vm_domain.info().destroy() + self.vm_domain.info().create() + + def hard_reset(self): + """ + Simiulate hard reset + """ + self.vm_domain.info().destroy() + + def power_on(self): + """ + Simiulate power on + """ + self.vm_domain.info().create() + + def power_off(self): + """ + Simiulate power off + """ + self.vm_domain.info().destroy() + + def power_off_for_interval(self, interval=30): + """ + Simiulate power off for an interval. + """ + log.info('Power off {s} for {i} seconds'.format( + s=self.shortname, i=interval)) + self.vm_domain.info().destroy() + time.sleep(interval) + self.vm_domain.info().create() + log.info('Power off for {i} seconds completed'.format( + s=self.shortname, i=interval)) diff --git a/teuthology/orchestra/test/test_console.py b/teuthology/orchestra/test/test_console.py index 1e37abd73c..dd219fc1af 100644 --- a/teuthology/orchestra/test/test_console.py +++ b/teuthology/orchestra/test/test_console.py @@ -88,7 +88,7 @@ class TestPhysicalConsole(TestConsole): def test_spawn_log_conserver(self): with patch( - 'teuthology.orchestra.console.psutil.subprocess.Popen', + 'teuthology.orchestra.console.physical.psutil.subprocess.Popen', autospec=True, ) as m_popen: m_popen.return_value.pid = 42 @@ -107,7 +107,7 @@ class TestPhysicalConsole(TestConsole): def test_spawn_log_ipmi(self): with patch( - 'teuthology.orchestra.console.psutil.subprocess.Popen', + 'teuthology.orchestra.console.physical.psutil.subprocess.Popen', autospec=True, ) as m_popen: m_popen.return_value.pid = 42 @@ -126,7 +126,7 @@ class TestPhysicalConsole(TestConsole): def test_spawn_log_fallback(self): with patch( - 'teuthology.orchestra.console.psutil.subprocess.Popen', + 'teuthology.orchestra.console.physical.psutil.subprocess.Popen', autospec=True, ) as m_popen: m_popen.return_value.pid = 42 @@ -146,7 +146,7 @@ class TestPhysicalConsole(TestConsole): def test_get_console_conserver(self): with patch( - 'teuthology.orchestra.console.psutil.subprocess.Popen', + 'teuthology.orchestra.console.physical.psutil.subprocess.Popen', autospec=True, ) as m_popen: m_popen.return_value.pid = 42 @@ -155,7 +155,7 @@ class TestPhysicalConsole(TestConsole): cons = self.klass(self.hostname) assert cons.has_conserver is True with patch( - 'teuthology.orchestra.console.pexpect.spawn', + 'teuthology.orchestra.console.physical.pexpect.spawn', autospec=True, ) as m_spawn: cons._get_console() @@ -165,7 +165,7 @@ class TestPhysicalConsole(TestConsole): def test_get_console_ipmitool(self): with patch( - 'teuthology.orchestra.console.psutil.subprocess.Popen', + 'teuthology.orchestra.console.physical.psutil.subprocess.Popen', autospec=True, ) as m_popen: m_popen.return_value.pid = 42 @@ -174,7 +174,7 @@ class TestPhysicalConsole(TestConsole): cons = self.klass(self.hostname) assert cons.has_conserver is True with patch( - 'teuthology.orchestra.console.pexpect.spawn', + 'teuthology.orchestra.console.physical.pexpect.spawn', autospec=True, ) as m_spawn: cons.has_conserver = False @@ -184,7 +184,7 @@ class TestPhysicalConsole(TestConsole): def test_get_console_fallback(self): with patch( - 'teuthology.orchestra.console.psutil.subprocess.Popen', + 'teuthology.orchestra.console.physical.psutil.subprocess.Popen', autospec=True, ) as m_popen: m_popen.return_value.pid = 42 @@ -193,7 +193,7 @@ class TestPhysicalConsole(TestConsole): cons = self.klass(self.hostname) assert cons.has_conserver is True with patch( - 'teuthology.orchestra.console.pexpect.spawn', + 'teuthology.orchestra.console.physical.pexpect.spawn', autospec=True, ) as m_spawn: cons.has_conserver = True @@ -206,7 +206,7 @@ class TestPhysicalConsole(TestConsole): def test_disable_conserver(self): with patch( - 'teuthology.orchestra.console.psutil.subprocess.Popen', + 'teuthology.orchestra.console.physical.psutil.subprocess.Popen', autospec=True, ) as m_popen: m_popen.return_value.pid = 42 -- 2.39.5