Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2017 Red Hat, Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | import os |
| 18 | |
| 19 | import yaml |
| 20 | |
| 21 | from tests.base import ZuulTestCase |
| 22 | |
| 23 | |
| 24 | class TestInventory(ZuulTestCase): |
| 25 | |
| 26 | tenant_config_file = 'config/inventory/main.yaml' |
| 27 | |
| 28 | def setUp(self): |
| 29 | super(TestInventory, self).setUp() |
| 30 | self.executor_server.hold_jobs_in_build = True |
| 31 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 32 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 33 | self.waitUntilSettled() |
| 34 | |
| 35 | def _get_build_inventory(self, name): |
| 36 | build = self.getBuildByName(name) |
| 37 | inv_path = os.path.join(build.jobdir.root, 'ansible', 'inventory.yaml') |
| 38 | return yaml.safe_load(open(inv_path, 'r')) |
| 39 | |
| 40 | def test_single_inventory(self): |
| 41 | |
| 42 | inventory = self._get_build_inventory('single-inventory') |
| 43 | |
| 44 | all_nodes = ('ubuntu-xenial',) |
| 45 | self.assertIn('all', inventory) |
| 46 | self.assertIn('hosts', inventory['all']) |
Monty Taylor | a54144a | 2017-05-24 08:20:53 -0500 | [diff] [blame] | 47 | self.assertIn('vars', inventory['all']) |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 48 | for node_name in all_nodes: |
| 49 | self.assertIn(node_name, inventory['all']['hosts']) |
Monty Taylor | a54144a | 2017-05-24 08:20:53 -0500 | [diff] [blame] | 50 | self.assertIn('zuul', inventory['all']['vars']) |
| 51 | z_vars = inventory['all']['vars']['zuul'] |
| 52 | self.assertIn('executor', z_vars) |
| 53 | self.assertIn('src_root', z_vars['executor']) |
| 54 | self.assertIn('job', z_vars) |
| 55 | self.assertEqual(z_vars['job'], 'single-inventory') |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 56 | |
| 57 | self.executor_server.release() |
| 58 | self.waitUntilSettled() |
| 59 | |
| 60 | def test_group_inventory(self): |
| 61 | |
| 62 | inventory = self._get_build_inventory('group-inventory') |
| 63 | |
| 64 | all_nodes = ('controller', 'compute1', 'compute2') |
| 65 | self.assertIn('all', inventory) |
| 66 | self.assertIn('hosts', inventory['all']) |
Monty Taylor | a54144a | 2017-05-24 08:20:53 -0500 | [diff] [blame] | 67 | self.assertIn('vars', inventory['all']) |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 68 | for group_name in ('ceph-osd', 'ceph-monitor'): |
| 69 | self.assertIn(group_name, inventory) |
| 70 | for node_name in all_nodes: |
| 71 | self.assertIn(node_name, inventory['all']['hosts']) |
| 72 | self.assertIn(node_name, |
| 73 | inventory['ceph-monitor']['hosts']) |
Monty Taylor | a54144a | 2017-05-24 08:20:53 -0500 | [diff] [blame] | 74 | self.assertIn('zuul', inventory['all']['vars']) |
| 75 | z_vars = inventory['all']['vars']['zuul'] |
| 76 | self.assertIn('executor', z_vars) |
| 77 | self.assertIn('src_root', z_vars['executor']) |
| 78 | self.assertIn('job', z_vars) |
| 79 | self.assertEqual(z_vars['job'], 'group-inventory') |
| 80 | |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 81 | self.executor_server.release() |
| 82 | self.waitUntilSettled() |