blob: 1c41f5fa55762a23948da3ccbfc7b0e969a00116 [file] [log] [blame]
Monty Taylor0d926122017-05-24 08:07:56 -05001#!/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
17import os
18
19import yaml
20
21from tests.base import ZuulTestCase
22
23
24class 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 Taylora54144a2017-05-24 08:20:53 -050047 self.assertIn('vars', inventory['all'])
Monty Taylor0d926122017-05-24 08:07:56 -050048 for node_name in all_nodes:
49 self.assertIn(node_name, inventory['all']['hosts'])
Monty Taylora54144a2017-05-24 08:20:53 -050050 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 Taylor0d926122017-05-24 08:07:56 -050056
57 self.executor_server.release()
58 self.waitUntilSettled()
59
Paul Belangerecb0b842017-11-18 15:23:29 -050060 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 Taylor0d926122017-05-24 08:07:56 -050080 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 Taylora54144a2017-05-24 08:20:53 -050087 self.assertIn('vars', inventory['all'])
Monty Taylor0d926122017-05-24 08:07:56 -050088 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 Taylora54144a2017-05-24 08:20:53 -050094 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 Taylor0d926122017-05-24 08:07:56 -0500101 self.executor_server.release()
102 self.waitUntilSettled()
Jamie Lennoxd4006d62017-04-06 10:34:04 +1000103
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()