def stale_openstack_instances(ctx, instances, locked_nodes):
for (name, instance) in instances.iteritems():
i = OpenStackInstance(name)
- if (i.get_created() > config['max_job_time'] + OPENSTACK_DELAY):
+ if not i.exists():
+ log.debug("stale-openstack: {instance} disappeared, ignored"
+ .format(instance=name))
+ continue
+ if (i.get_created() >
+ config['max_job_time'] + OPENSTACK_DELAY):
log.info(
"stale-openstack: destroying instance {instance}"
" because it was created {created} seconds ago"
def stale_openstack_volumes(ctx, volumes):
now = datetime.datetime.now()
for volume in volumes:
- volume = json.loads(sh("openstack volume show -f json " +
- volume['ID']))
+ try:
+ volume = json.loads(sh("openstack volume show -f json " +
+ volume['ID']))
+ except subprocess.CalledProcessError:
+ log.debug("stale-openstack: {id} disappeared, ignored"
+ .format(id=volume['ID']))
+ continue
volume = dict(map(lambda v: (v['Field'], v['Value']), volume))
created_at = datetime.datetime.strptime(
volume['created_at'], '%Y-%m-%dT%H:%M:%S.%f')
import json
import datetime
+import subprocess
from mock import patch, Mock, DEFAULT
nuke.stale_openstack_volumes(ctx, volume_list)
m['openstack_delete_volume'].assert_called_with(id)
+ #
+ # A volume that no longer exists is ignored
+ #
+ def sh(cmd):
+ raise subprocess.CalledProcessError('ERROR', 'FAIL')
+
+ with patch.multiple(
+ nuke,
+ sh=sh,
+ openstack_delete_volume=DEFAULT,
+ ) as m:
+ nuke.stale_openstack_volumes(ctx, volume_list)
+ m['openstack_delete_volume'].assert_not_called()
+
def test_stale_openstack_nodes(self):
ctx = Mock()
ctx.teuthology_config = config
#
with patch.multiple(
nuke.OpenStackInstance,
+ exists=lambda _: True,
get_created=lambda _: 1,
__getitem__=lambda _, key: name,
destroy=DEFAULT,
#
with patch.multiple(
nuke.OpenStackInstance,
+ exists=lambda _: True,
get_created=lambda _: 1000000000,
__getitem__=lambda _, key: name,
destroy=DEFAULT,
})
m['destroy'].assert_called_with()
#
+ # An instance that turns out to not exist any longer
+ # is ignored.
+ #
+ with patch.multiple(
+ nuke.OpenStackInstance,
+ exists=lambda _: False,
+ __getitem__=lambda _, key: name,
+ destroy=DEFAULT,
+ ) as m:
+ nuke.stale_openstack_instances(ctx, {
+ name: { 'id': 'UUID', },
+ }, {
+ misc.canonicalize_hostname(name, user=None): {},
+ })
+ m['destroy'].assert_not_called()
+ #
# An instance created but not locked after a while is
# destroyed.
#
with patch.multiple(
nuke.OpenStackInstance,
+ exists=lambda _: True,
get_created=lambda _: nuke.OPENSTACK_DELAY + 1,
__getitem__=lambda _, key: name,
destroy=DEFAULT,
#
with patch.multiple(
nuke.OpenStackInstance,
+ exists=lambda _: True,
get_created=lambda _: nuke.OPENSTACK_DELAY + 1,
__getitem__=lambda _, key: name,
destroy=DEFAULT,