From 1acc95211d018033858f15e33dd063fca1991d3b Mon Sep 17 00:00:00 2001
From: Sagi Shnaidman <sshnaidm@redhat.com>
Date: Tue, 3 Dec 2019 15:49:56 +0200
Subject: [PATCH] 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
---
 .../ansible_plugins/filter/helpers.py         | 10 ++-
 .../tests/plugins/filter/test_helpers.py      | 70 +++++++++++++++++++
 2 files changed, 77 insertions(+), 3 deletions(-)

diff --git a/tripleo_ansible/ansible_plugins/filter/helpers.py b/tripleo_ansible/ansible_plugins/filter/helpers.py
index 985f74a5f..7a6f0b427 100644
--- a/tripleo_ansible/ansible_plugins/filter/helpers.py
+++ b/tripleo_ansible/ansible_plugins/filter/helpers.py
@@ -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
 
diff --git a/tripleo_ansible/tests/plugins/filter/test_helpers.py b/tripleo_ansible/tests/plugins/filter/test_helpers.py
index 701482caa..88760e210 100644
--- a/tripleo_ansible/tests/plugins/filter/test_helpers.py
+++ b/tripleo_ansible/tests/plugins/filter/test_helpers.py
@@ -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)