Simplify get_info method
Operators and end-users only really care about if the container is running or not. So remove the LXD_POWER_STATES dictionary and only check to see if the container.status is "Running". Update the approiate unit test. Change-Id: I5088cbc40fdb3c12e7f65c144b4fc185e1004c2d Signed-off-by: Chuck Short <chuck.short@canonical.com>
This commit is contained in:
@@ -32,7 +32,7 @@ MockResponse = collections.namedtuple('Response', ['status_code'])
|
||||
|
||||
MockContainer = collections.namedtuple('Container', ['name'])
|
||||
MockContainerState = collections.namedtuple(
|
||||
'ContainerState', ['status_code', 'memory'])
|
||||
'ContainerState', ['status', 'memory'])
|
||||
|
||||
|
||||
def fake_connection_info(volume, location, iqn, auth=False, transport=None):
|
||||
@@ -104,7 +104,7 @@ class LXDDriverTest(test.NoDBTestCase):
|
||||
def test_get_info(self):
|
||||
container = mock.Mock()
|
||||
container.state.return_value = MockContainerState(
|
||||
100, {'usage': 4000, 'usage_peak': 4500})
|
||||
'Running', {'usage': 4000, 'usage_peak': 4500})
|
||||
self.client.containers.get.return_value = container
|
||||
|
||||
ctx = context.get_admin_context()
|
||||
|
||||
@@ -23,7 +23,6 @@ for nova-lxd.
|
||||
import ddt
|
||||
import mock
|
||||
|
||||
from nova.compute import power_state
|
||||
from nova import exception
|
||||
from nova import test
|
||||
from pylxd.deprecated import exceptions as lxd_exceptions
|
||||
@@ -108,42 +107,6 @@ class SessionContainerTest(test.NoDBTestCase):
|
||||
self.session.container_running, instance
|
||||
)
|
||||
|
||||
def test_container_state(self):
|
||||
"""
|
||||
container_state translates LXD container status into
|
||||
what nova understands. Verify that status_code sends
|
||||
a power_state.RUNNING and a 108 sends a
|
||||
power_state.CRASHED.
|
||||
"""
|
||||
calls = []
|
||||
self.assertEqual(calls, self.ml.method_calls)
|
||||
|
||||
@stubs.annotated_data(
|
||||
('api_fail', True, lxd_exceptions.APIError('Fake', 500),
|
||||
{'state': power_state.NOSTATE, 'mem': 0, 'max_mem': 0})
|
||||
)
|
||||
def test_container_state_fail(self, tag, container_defined, side_effect,
|
||||
expected):
|
||||
"""
|
||||
container_state translates LXD container status into
|
||||
what nova understands. If the API sends an APIError
|
||||
then raise an power_state.NOSTATE, same if the
|
||||
the container goes missing.
|
||||
"""
|
||||
instance = stubs._fake_instance()
|
||||
if container_defined:
|
||||
self.ml.container_defined.return_value = container_defined
|
||||
self.ml.container_state.side_effect = (
|
||||
lxd_exceptions.APIError('Fake', 500))
|
||||
self.assertEqual(
|
||||
expected,
|
||||
self.session.container_state(instance))
|
||||
if not container_defined:
|
||||
self.ml.container_defined.return_value = container_defined
|
||||
self.assertEqual(
|
||||
expected,
|
||||
self.session.container_state(instance))
|
||||
|
||||
def test_container_config(self):
|
||||
"""
|
||||
container_config returns a dictionary representation
|
||||
|
||||
@@ -46,6 +46,7 @@ from nova.virt.lxd import utils as container_utils
|
||||
|
||||
from nova.compute import arch
|
||||
from nova.compute import hv_type
|
||||
from nova.compute import power_state
|
||||
from nova.compute import vm_mode
|
||||
from nova.virt import hardware
|
||||
from oslo_utils import units
|
||||
@@ -211,12 +212,16 @@ class LXDDriver(driver.ComputeDriver):
|
||||
"""Return an InstanceInfo object for the instance."""
|
||||
container = self.client.containers.get(instance.name)
|
||||
state = container.state()
|
||||
power_state = session.LXD_POWER_STATES[state.status_code]
|
||||
mem_kb = state.memory['usage'] >> 10
|
||||
max_mem_kb = state.memory['usage_peak'] >> 10
|
||||
return hardware.InstanceInfo(
|
||||
state=power_state, max_mem_kb=max_mem_kb, mem_kb=mem_kb,
|
||||
num_cpu=instance.flavor.vcpus, cpu_time_ns=0)
|
||||
state=(
|
||||
power_state.RUNNING if state.status == 'Running'
|
||||
else power_state.SHUTDOWN),
|
||||
max_mem_kb=max_mem_kb,
|
||||
mem_kb=mem_kb,
|
||||
num_cpu=instance.flavor.vcpus,
|
||||
cpu_time_ns=0)
|
||||
|
||||
def list_instances(self):
|
||||
"""Return a list of all instance names."""
|
||||
|
||||
@@ -19,7 +19,6 @@ from nova import context as nova_context
|
||||
from nova import exception
|
||||
from nova import i18n
|
||||
from nova import rpc
|
||||
from nova.compute import power_state
|
||||
|
||||
from oslo_log import log as logging
|
||||
from oslo_service import loopingcall
|
||||
@@ -35,24 +34,6 @@ _LI = i18n._LI
|
||||
CONF = nova.conf.CONF
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
LXD_POWER_STATES = {
|
||||
100: power_state.RUNNING,
|
||||
101: power_state.RUNNING,
|
||||
102: power_state.SHUTDOWN,
|
||||
103: power_state.RUNNING,
|
||||
104: power_state.SHUTDOWN,
|
||||
105: power_state.NOSTATE,
|
||||
106: power_state.NOSTATE,
|
||||
107: power_state.SHUTDOWN,
|
||||
108: power_state.CRASHED,
|
||||
109: power_state.SUSPENDED,
|
||||
110: power_state.SUSPENDED,
|
||||
111: power_state.SUSPENDED,
|
||||
200: power_state.RUNNING,
|
||||
400: power_state.CRASHED,
|
||||
401: power_state.NOSTATE
|
||||
}
|
||||
|
||||
|
||||
class LXDAPISession(object):
|
||||
"""The session to invoke the LXD API session."""
|
||||
@@ -156,42 +137,6 @@ class LXDAPISession(object):
|
||||
{'instance': instance.name, 'reason': e},
|
||||
instance=instance)
|
||||
|
||||
def container_state(self, instance):
|
||||
"""Determine container_state and translate state
|
||||
|
||||
:param instance: nova instance object
|
||||
:return: nova power_state
|
||||
|
||||
"""
|
||||
LOG.debug('container_state called for instance', instance=instance)
|
||||
try:
|
||||
mem = 0
|
||||
max_mem = 0
|
||||
|
||||
client = self.get_session()
|
||||
|
||||
(state, data) = client.container_state(instance.name)
|
||||
state = LXD_POWER_STATES[data['metadata']['status_code']]
|
||||
|
||||
container_state = self.container_info(instance)
|
||||
mem = int(container_state['memory']['usage']) >> 10
|
||||
max_mem = int(container_state['memory']['usage_peak']) >> 10
|
||||
|
||||
except lxd_exceptions.APIError as ex:
|
||||
msg = _('Failed to communicate with LXD API %(instance)s:'
|
||||
' %(reason)s') % {'instance': instance.name,
|
||||
'reason': ex}
|
||||
LOG.error(msg)
|
||||
state = power_state.NOSTATE
|
||||
except Exception as e:
|
||||
with excutils.save_and_reraise_exception():
|
||||
LOG.error(_LE('Error from LXD during container_state'
|
||||
'%(instance)s: %(reason)s'),
|
||||
{'instance': instance.name, 'reason': e},
|
||||
instance=instance)
|
||||
state = power_state.NOSTATE
|
||||
return {'state': state, 'mem': mem, 'max_mem': max_mem}
|
||||
|
||||
def container_config(self, instance):
|
||||
"""Fetches the configuration of a given LXD container
|
||||
|
||||
|
||||
Reference in New Issue
Block a user