Debian Codegraph @

test/units/inventory/test_host.py

raw
1 # Copyright 2015 Marius Gedminas <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 # for __setstate__/__getstate__ tests
19
20 from __future__ import annotations
21
22 import unittest
23
24
25 from ansible.inventory.group import Group
26 from ansible.inventory.host import Host
27
28
29 class TestHost(unittest.TestCase):
30 ansible_port = 22
31
32 def setUp(self):
33 self.hostA = Host('a')
34 self.hostB = Host('b')
35
36 def test_equality(self):
37 self.assertEqual(self.hostA, self.hostA)
38 self.assertNotEqual(self.hostA, self.hostB)
39 self.assertNotEqual(self.hostA, Host('a'))
40
41 def test_hashability(self):
42 # equality implies the hash values are the same
43 self.assertEqual(hash(self.hostA), hash(Host('a')))
44
45 def test_get_vars(self):
46 host_vars = self.hostA.get_vars()
47 self.assertIsInstance(host_vars, dict)
48
49 def test_repr(self):
50 host_repr = repr(self.hostA)
51 self.assertIsInstance(host_repr, str)
52
53 def test_add_group(self):
54 group = Group('some_group')
55 group_len = len(self.hostA.groups)
56 self.hostA.add_group(group)
57 self.assertEqual(len(self.hostA.groups), group_len + 1)
58
59 def test_get_groups(self):
60 group = Group('some_group')
61 self.hostA.add_group(group)
62 groups = self.hostA.get_groups()
63 self.assertEqual(len(groups), 1)
64 for _group in groups:
65 self.assertIsInstance(_group, Group)
66
67 def test_equals_none(self):
68 other = None
69 assert not (self.hostA == other)
70 assert not (other == self.hostA)
71 assert self.hostA != other
72 assert other != self.hostA
73 self.assertNotEqual(self.hostA, other)
74
75
76 class TestHostWithPort(TestHost):
77 ansible_port = 8822
78
79 def setUp(self):
80 self.hostA = Host(name='a', port=self.ansible_port)
81 self.hostB = Host(name='b', port=self.ansible_port)
82
83 def test_get_vars_ansible_port(self):
84 host_vars = self.hostA.get_vars()
85 self.assertEqual(host_vars['ansible_port'], self.ansible_port)