Browse Source

Treat manage_vlans as a bool

Host info is being read in as a dictionary of strings,
pass manage_vlans into oslo.configs Boolean class as it
was intended to be True/False.

Change-Id: I465da006f5e3e05be7fab60f43d612e13cd754fc
Derek Higgins 5 months ago
parent
commit
7bdfdb7f59

+ 5
- 0
networking_ansible/config.py View File

@@ -14,6 +14,7 @@
14 14
 #    under the License.
15 15
 
16 16
 from oslo_config import cfg
17
+from oslo_config import types
17 18
 from oslo_log import log as logging
18 19
 
19 20
 CONF = cfg.CONF
@@ -30,6 +31,7 @@ def build_ansible_inventory():
30 31
     # TODO(radez): consider take advantage of ansible inventory grouping
31 32
 
32 33
     driver_tag = 'ansible:'
34
+    booleans = ['manage_vlans']
33 35
     inventory = {}
34 36
 
35 37
     for conffile in CONF.config_file:
@@ -50,6 +52,9 @@ def build_ansible_inventory():
50 52
         for host in hosts:
51 53
             dev_id = host.partition(driver_tag)[2]
52 54
             dev_cfg = {k: v[0] for k, v in hosts[host].items()}
55
+            for b in booleans:
56
+                if b in dev_cfg:
57
+                    dev_cfg[b] = types.Boolean()(dev_cfg[b])
53 58
             inventory[dev_id] = dev_cfg
54 59
 
55 60
     LOG.info('Ansible Host List: %s', ', '.join(inventory))

+ 5
- 2
networking_ansible/tests/unit/ml2/test_mech_driver.py View File

@@ -238,6 +238,7 @@ class TestML2PluginIntegration(NetAnsibleML2Base):
238 238
             'ansible_pass=password\n',
239 239
         ] for host in HOSTS
240 240
     }
241
+    CONFIG_CONTENT['ansible:otherhost'].append('manage_vlans=False')
241 242
 
242 243
     LOCAL_LINK_INFORMATION = [{
243 244
         'switch_info': HOSTS[0],
@@ -304,7 +305,8 @@ class TestML2PluginIntegration(NetAnsibleML2Base):
304 305
                 'create_network',
305 306
                 host,
306 307
                 int(self.network_spec[provider_net.SEGMENTATION_ID]))
307
-            for host in self.HOSTS]
308
+            for host in self.HOSTS if 'manage_vlans=False' not in
309
+            self.CONFIG_CONTENT['ansible:%s' % host]]
308 310
         self.assertItemsEqual(
309 311
             expected_calls,
310 312
             m_run_task.call_args_list)
@@ -321,7 +323,8 @@ class TestML2PluginIntegration(NetAnsibleML2Base):
321 323
                 'delete_network',
322 324
                 host,
323 325
                 int(self.network_spec[provider_net.SEGMENTATION_ID]))
324
-            for host in self.HOSTS]
326
+            for host in self.HOSTS if 'manage_vlans=False' not in
327
+            self.CONFIG_CONTENT['ansible:%s' % host]]
325 328
         self.assertItemsEqual(
326 329
             expected_calls,
327 330
             m_run_task.call_args_list)

+ 24
- 1
networking_ansible/tests/unit/test_config.py View File

@@ -19,12 +19,22 @@ from networking_ansible.tests.unit import base
19 19
 
20 20
 
21 21
 class MockedConfigParser(mock.Mock):
22
+
22 23
     def __init__(self, conffile, sections):
23 24
         super(MockedConfigParser, self).__init__()
25
+        self.conffile = conffile
24 26
         self.sections = sections
25 27
 
26 28
     def parse(self):
27
-        self.sections.update({'ansible:testhost': {}})
29
+        section_data = {'ansible:testhost': {}}
30
+        if self.conffile == 'foo2':
31
+            section_data = {
32
+                'ansible:h1': {'manage_vlans': ['0']},
33
+                'ansible:h2': {'manage_vlans': ['true']},
34
+                'ansible:h3': {'manage_vlans': ['false']},
35
+            }
36
+
37
+        self.sections.update(section_data)
28 38
 
29 39
     @staticmethod
30 40
     def _parse_file(values, namespace):
@@ -60,3 +70,16 @@ class TestBuildAnsibleInventory(base.BaseTestCase):
60 70
 
61 71
         self.assertEqual(self.inventory,
62 72
                          self.ansconfig.build_ansible_inventory())
73
+
74
+    @mock.patch('networking_ansible.config.cfg.ConfigParser',
75
+                MockedConfigParser)
76
+    def test_build_ansible_inventory_from_file(self):
77
+        self.test_config_files = ['foo2']
78
+        self.setup_config()
79
+
80
+        ansible_inventory = self.ansconfig.build_ansible_inventory()
81
+
82
+        hosts = ansible_inventory['all']['hosts']
83
+        self.assertEqual({'manage_vlans': False}, hosts['h1'])
84
+        self.assertEqual({'manage_vlans': True}, hosts['h2'])
85
+        self.assertEqual({'manage_vlans': False}, hosts['h3'])

Loading…
Cancel
Save