Fix case in filters when Labels is None

When Labels is None it's not managed by tripleo container,
then skip it.
Change-Id: Ib82c4d28c462abb3f1a5ccb7d5137ec6059b2665
This commit is contained in:
Sagi Shnaidman 2019-12-03 15:49:56 +02:00
parent 0a325d8095
commit 1acc95211d
2 changed files with 77 additions and 3 deletions

View File

@ -81,18 +81,22 @@ class FilterModule(object):
installed_containers.append(c_name)
# Don't delete containers not managed by tripleo-ansible
if c['Config']['Labels'].get('managed_by') != 'tripleo_ansible':
if (c['Config']['Labels'] is None
or c['Config']['Labels'].get(
'managed_by') != 'tripleo_ansible'):
to_skip += [c_name]
continue
# Only remove containers managed in this config_id
if c['Config']['Labels'].get('config_id') != config_id:
if (c['Config']['Labels'] is None
or c['Config']['Labels'].get('config_id') != config_id):
to_skip += [c_name]
continue
# Remove containers with no config_data
# e.g. broken config containers
if 'config_data' not in c['Config']['Labels']:
if (c['Config']['Labels'] is not None
and 'config_data' not in c['Config']['Labels']):
to_delete += [c_name]
continue

View File

@ -256,3 +256,73 @@ class TestHelperFilters(tests_base.TestCase):
reverse=True,
any=True)
self.assertEqual(result, expected_list)
def test_needs_delete(self):
data = [
{
'Name': 'mysql',
'Config': {
'Labels': {
'config_id': 'dontdeleteme',
'managed_by': 'triple_ansible',
}
}
},
{
'Name': 'rabbitmq',
'Config': {
'Labels': {
'managed_by': 'tripleo_ansible',
'config_id': 'tripleo_step1',
'container_name': 'rabbitmq',
'name': 'rabbitmq',
}
}
},
{
'Name': 'swift',
'Config': {
'Labels': {
'managed_by': 'tripleo_ansible',
'config_id': 'tripleo_step1',
'container_name': 'swift',
'name': 'swift',
'config_data': 'foo',
}
}
},
{
'Name': 'haproxy',
'Config': {
'Labels': {
'config_id': 'test'
}
}
},
{
'Name': 'tripleo',
'Config': {
'Labels': {
'foo': 'bar'
}
}
},
{
'Name': 'none_tripleo',
'Config': {
'Labels': None,
}
},
]
config = {
'mysql': '',
'rabbitmq': '',
'haproxy': '',
'tripleo': '',
'doesnt_exist': ''
}
expected_list = ['rabbitmq']
result = self.filters.needs_delete(container_infos=data,
config=config,
config_id='tripleo_step1')
self.assertEqual(result, expected_list)