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 | |
Paul Belanger | ecb0b84 | 2017-11-18 15:23:29 -0500 | [diff] [blame] | 60 | def test_single_inventory_list(self): |
| 61 | |
| 62 | inventory = self._get_build_inventory('single-inventory-list') |
| 63 | |
| 64 | all_nodes = ('compute', 'controller') |
| 65 | self.assertIn('all', inventory) |
| 66 | self.assertIn('hosts', inventory['all']) |
| 67 | self.assertIn('vars', inventory['all']) |
| 68 | for node_name in all_nodes: |
| 69 | self.assertIn(node_name, inventory['all']['hosts']) |
| 70 | self.assertIn('zuul', inventory['all']['vars']) |
| 71 | z_vars = inventory['all']['vars']['zuul'] |
| 72 | self.assertIn('executor', z_vars) |
| 73 | self.assertIn('src_root', z_vars['executor']) |
| 74 | self.assertIn('job', z_vars) |
| 75 | self.assertEqual(z_vars['job'], 'single-inventory-list') |
| 76 | |
| 77 | self.executor_server.release() |
| 78 | self.waitUntilSettled() |
| 79 | |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 80 | def test_group_inventory(self): |
| 81 | |
| 82 | inventory = self._get_build_inventory('group-inventory') |
| 83 | |
| 84 | all_nodes = ('controller', 'compute1', 'compute2') |
| 85 | self.assertIn('all', inventory) |
| 86 | self.assertIn('hosts', inventory['all']) |
Monty Taylor | a54144a | 2017-05-24 08:20:53 -0500 | [diff] [blame] | 87 | self.assertIn('vars', inventory['all']) |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 88 | for group_name in ('ceph-osd', 'ceph-monitor'): |
| 89 | self.assertIn(group_name, inventory) |
| 90 | for node_name in all_nodes: |
| 91 | self.assertIn(node_name, inventory['all']['hosts']) |
| 92 | self.assertIn(node_name, |
| 93 | inventory['ceph-monitor']['hosts']) |
Monty Taylor | a54144a | 2017-05-24 08:20:53 -0500 | [diff] [blame] | 94 | self.assertIn('zuul', inventory['all']['vars']) |
| 95 | z_vars = inventory['all']['vars']['zuul'] |
| 96 | self.assertIn('executor', z_vars) |
| 97 | self.assertIn('src_root', z_vars['executor']) |
| 98 | self.assertIn('job', z_vars) |
| 99 | self.assertEqual(z_vars['job'], 'group-inventory') |
| 100 | |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 101 | self.executor_server.release() |
| 102 | self.waitUntilSettled() |
Jamie Lennox | d4006d6 | 2017-04-06 10:34:04 +1000 | [diff] [blame] | 103 | |
| 104 | def test_hostvars_inventory(self): |
| 105 | |
| 106 | inventory = self._get_build_inventory('hostvars-inventory') |
| 107 | |
| 108 | all_nodes = ('default', 'fakeuser') |
| 109 | self.assertIn('all', inventory) |
| 110 | self.assertIn('hosts', inventory['all']) |
| 111 | self.assertIn('vars', inventory['all']) |
| 112 | for node_name in all_nodes: |
| 113 | self.assertIn(node_name, inventory['all']['hosts']) |
| 114 | # check if the nodes use the correct username |
| 115 | if node_name == 'fakeuser': |
| 116 | username = 'fakeuser' |
| 117 | else: |
| 118 | username = 'zuul' |
| 119 | self.assertEqual( |
| 120 | inventory['all']['hosts'][node_name]['ansible_user'], username) |
| 121 | |
| 122 | self.executor_server.release() |
| 123 | self.waitUntilSettled() |