blob: 04dcb051b9e9128d183c373b596c9fabb5742220 [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
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 Taylora54144a2017-05-24 08:20:53 -050067 self.assertIn('vars', inventory['all'])
Monty Taylor0d926122017-05-24 08:07:56 -050068 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 Taylora54144a2017-05-24 08:20:53 -050074 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 Taylor0d926122017-05-24 08:07:56 -050081 self.executor_server.release()
82 self.waitUntilSettled()
Jamie Lennoxd4006d62017-04-06 10:34:04 +100083
84 def test_hostvars_inventory(self):
85
86 inventory = self._get_build_inventory('hostvars-inventory')
87
88 all_nodes = ('default', 'fakeuser')
89 self.assertIn('all', inventory)
90 self.assertIn('hosts', inventory['all'])
91 self.assertIn('vars', inventory['all'])
92 for node_name in all_nodes:
93 self.assertIn(node_name, inventory['all']['hosts'])
94 # check if the nodes use the correct username
95 if node_name == 'fakeuser':
96 username = 'fakeuser'
97 else:
98 username = 'zuul'
99 self.assertEqual(
100 inventory['all']['hosts'][node_name]['ansible_user'], username)
101
102 self.executor_server.release()
103 self.waitUntilSettled()