blob: b7e35ebd26a4b1f1f1039fe36ff5fa12a7164a60 [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
Ricardo Carrillo Cruz6eda4392017-12-27 19:34:47 +010040 def _get_setup_inventory(self, name):
41 build = self.getBuildByName(name)
42 setup_inv_path = os.path.join(build.jobdir.root, 'ansible',
43 'setup-inventory.yaml')
44 return yaml.safe_load(open(setup_inv_path, 'r'))
45
Monty Taylor0d926122017-05-24 08:07:56 -050046 def test_single_inventory(self):
47
48 inventory = self._get_build_inventory('single-inventory')
49
50 all_nodes = ('ubuntu-xenial',)
51 self.assertIn('all', inventory)
52 self.assertIn('hosts', inventory['all'])
Monty Taylora54144a2017-05-24 08:20:53 -050053 self.assertIn('vars', inventory['all'])
Monty Taylor0d926122017-05-24 08:07:56 -050054 for node_name in all_nodes:
55 self.assertIn(node_name, inventory['all']['hosts'])
Monty Taylora54144a2017-05-24 08:20:53 -050056 self.assertIn('zuul', inventory['all']['vars'])
57 z_vars = inventory['all']['vars']['zuul']
58 self.assertIn('executor', z_vars)
59 self.assertIn('src_root', z_vars['executor'])
60 self.assertIn('job', z_vars)
61 self.assertEqual(z_vars['job'], 'single-inventory')
Monty Taylor0d926122017-05-24 08:07:56 -050062
63 self.executor_server.release()
64 self.waitUntilSettled()
65
Paul Belangerecb0b842017-11-18 15:23:29 -050066 def test_single_inventory_list(self):
67
68 inventory = self._get_build_inventory('single-inventory-list')
69
70 all_nodes = ('compute', 'controller')
71 self.assertIn('all', inventory)
72 self.assertIn('hosts', inventory['all'])
73 self.assertIn('vars', inventory['all'])
74 for node_name in all_nodes:
75 self.assertIn(node_name, inventory['all']['hosts'])
76 self.assertIn('zuul', inventory['all']['vars'])
77 z_vars = inventory['all']['vars']['zuul']
78 self.assertIn('executor', z_vars)
79 self.assertIn('src_root', z_vars['executor'])
80 self.assertIn('job', z_vars)
81 self.assertEqual(z_vars['job'], 'single-inventory-list')
82
83 self.executor_server.release()
84 self.waitUntilSettled()
85
Monty Taylor0d926122017-05-24 08:07:56 -050086 def test_group_inventory(self):
87
88 inventory = self._get_build_inventory('group-inventory')
89
90 all_nodes = ('controller', 'compute1', 'compute2')
91 self.assertIn('all', inventory)
92 self.assertIn('hosts', inventory['all'])
Monty Taylora54144a2017-05-24 08:20:53 -050093 self.assertIn('vars', inventory['all'])
Monty Taylor0d926122017-05-24 08:07:56 -050094 for group_name in ('ceph-osd', 'ceph-monitor'):
95 self.assertIn(group_name, inventory)
96 for node_name in all_nodes:
97 self.assertIn(node_name, inventory['all']['hosts'])
98 self.assertIn(node_name,
99 inventory['ceph-monitor']['hosts'])
Monty Taylora54144a2017-05-24 08:20:53 -0500100 self.assertIn('zuul', inventory['all']['vars'])
101 z_vars = inventory['all']['vars']['zuul']
102 self.assertIn('executor', z_vars)
103 self.assertIn('src_root', z_vars['executor'])
104 self.assertIn('job', z_vars)
105 self.assertEqual(z_vars['job'], 'group-inventory')
106
Monty Taylor0d926122017-05-24 08:07:56 -0500107 self.executor_server.release()
108 self.waitUntilSettled()
Jamie Lennoxd4006d62017-04-06 10:34:04 +1000109
110 def test_hostvars_inventory(self):
111
112 inventory = self._get_build_inventory('hostvars-inventory')
113
114 all_nodes = ('default', 'fakeuser')
115 self.assertIn('all', inventory)
116 self.assertIn('hosts', inventory['all'])
117 self.assertIn('vars', inventory['all'])
118 for node_name in all_nodes:
119 self.assertIn(node_name, inventory['all']['hosts'])
120 # check if the nodes use the correct username
121 if node_name == 'fakeuser':
122 username = 'fakeuser'
123 else:
124 username = 'zuul'
125 self.assertEqual(
126 inventory['all']['hosts'][node_name]['ansible_user'], username)
127
Tobias Henkelc5043212017-09-08 08:53:47 +0200128 # check if the nodes use the correct or no ansible_connection
129 if node_name == 'windows':
130 self.assertEqual(
131 inventory['all']['hosts'][node_name]['ansible_connection'],
132 'winrm')
133 else:
134 self.assertEqual(
135 'local',
136 inventory['all']['hosts'][node_name]['ansible_connection'])
137
Jamie Lennoxd4006d62017-04-06 10:34:04 +1000138 self.executor_server.release()
139 self.waitUntilSettled()
Ricardo Carrillo Cruz6eda4392017-12-27 19:34:47 +0100140
141 def test_setup_inventory(self):
142
143 setup_inventory = self._get_setup_inventory('hostvars-inventory')
144 inventory = self._get_build_inventory('hostvars-inventory')
145
146 self.assertIn('all', inventory)
147 self.assertIn('hosts', inventory['all'])
148
149 self.assertIn('default', setup_inventory['all']['hosts'])
150 self.assertIn('fakeuser', setup_inventory['all']['hosts'])
151 self.assertIn('windows', setup_inventory['all']['hosts'])
152 self.assertNotIn('network', setup_inventory['all']['hosts'])
153 self.assertIn('default', inventory['all']['hosts'])
154 self.assertIn('fakeuser', inventory['all']['hosts'])
155 self.assertIn('windows', inventory['all']['hosts'])
156 self.assertIn('network', inventory['all']['hosts'])
157
158 self.executor_server.release()
159 self.waitUntilSettled()