forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
82 lines (63 loc) · 2.36 KB
/
test_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from unittest import TestCase
from unittest.mock import patch
from paramiko import SSHException
from pyinfra.api import Config, State
from pyinfra.api.connect import connect_all
from pyinfra.api.exceptions import NoGroupError, NoHostError, PyinfraError
from ..paramiko_util import PatchSSHTestCase
from ..util import make_inventory
class TestInventoryApi(TestCase):
def test_inventory_creation(self):
inventory = make_inventory()
# Check length
assert len(inventory.hosts) == 2
# Get a host
host = inventory.get_host("somehost")
assert host.data.ssh_user == "vagrant"
# Check our group data
assert inventory.get_group_data("test_group") == {
"group_data": "hello world",
}
def test_tuple_host_group_inventory_creation(self):
inventory = make_inventory(
hosts=[
("somehost", {"some_data": "hello"}),
],
tuple_group=(
[
("somehost", {"another_data": "world"}),
],
{
"tuple_group_data": "word",
},
),
)
# Check host data
host = inventory.get_host("somehost")
assert host.data.some_data == "hello"
assert host.data.another_data == "world"
# Check group data
assert host.data.tuple_group_data == "word"
def test_host_and_group_errors(self):
inventory = make_inventory()
with self.assertRaises(NoHostError):
inventory.get_host("i-dont-exist")
with self.assertRaises(NoGroupError):
inventory.get_group("i-dont-exist")
class TestStateApi(PatchSSHTestCase):
@patch("pyinfra.connectors.base.raise_if_bad_type", lambda *args, **kwargs: None)
def test_fail_percent(self):
inventory = make_inventory(
(
"somehost",
("thinghost", {"ssh_hostname": SSHException}),
"anotherhost",
),
)
state = State(inventory, Config(FAIL_PERCENT=1))
# Ensure we would fail at this point
with self.assertRaises(PyinfraError) as context:
connect_all(state)
assert context.exception.args[0] == "Over 1% of hosts failed (33%)"
# Ensure the other two did connect
assert len(state.active_hosts) == 2