Debian Codegraph @

tests/api/test_health.py

raw symbols SCIP
1 import time
2
3 import consul.check
4
5 Check = consul.check.Check
6
7
8 class TestHealth:
9 def test_health_service(self, consul_obj) -> None:
10 c, _consul_version = consul_obj
11
12 # check there are no nodes for the service 'foo'
13 _index, nodes = c.health.service("foo")
14 assert nodes == []
15
16 # register two nodes, one with a long ttl, the other shorter
17 c.agent.service.register("foo", service_id="foo:1", check=Check.ttl("10s"), tags=["tag:foo:1"])
18 c.agent.service.register("foo", service_id="foo:2", check=Check.ttl("1s"))
19
20 time.sleep(0.2)
21
22 # check the nodes show for the /health/service endpoint
23 _index, nodes = c.health.service("foo")
24 assert [node["Service"]["ID"] for node in nodes] == ["foo:1", "foo:2"]
25
26 # but that they aren't passing their health check
27 _index, nodes = c.health.service("foo", passing=True)
28 assert nodes == []
29
30 # ping the two node's health check
31 c.agent.check.ttl_pass("service:foo:1")
32 c.agent.check.ttl_pass("service:foo:2")
33
34 time.sleep(0.2)
35
36 # both nodes are now available
37 _index, nodes = c.health.service("foo", passing=True)
38 assert [node["Service"]["ID"] for node in nodes] == ["foo:1", "foo:2"]
39
40 # wait until the short ttl node fails
41 time.sleep(3)
42
43 # only one node available
44 _index, nodes = c.health.service("foo", passing=True)
45 assert [node["Service"]["ID"] for node in nodes] == ["foo:1"]
46
47 # ping the failed node's health check
48 c.agent.check.ttl_pass("service:foo:2")
49
50 time.sleep(0.2)
51
52 # check both nodes are available
53 _index, nodes = c.health.service("foo", passing=True)
54 assert [node["Service"]["ID"] for node in nodes] == ["foo:1", "foo:2"]
55
56 # check that tag works
57 _index, nodes = c.health.service("foo", tag="tag:foo:1")
58 assert [node["Service"]["ID"] for node in nodes] == ["foo:1"]
59
60 # deregister the nodes
61 c.agent.service.deregister("foo:1")
62 c.agent.service.deregister("foo:2")
63
64 time.sleep(0.2)
65
66 _index, nodes = c.health.service("foo")
67 assert nodes == []
68
69 def test_health_state(self, consul_obj) -> None:
70 c, _consul_version = consul_obj
71
72 # The empty string is for the Serf Health Status check, which has an
73 # empty ServiceID
74 _index, nodes = c.health.state("any")
75 assert [node["ServiceID"] for node in nodes] == [""]
76
77 # register two nodes, one with a long ttl, the other shorter
78 c.agent.service.register("foo", service_id="foo:1", check=Check.ttl("10s"))
79 c.agent.service.register("foo", service_id="foo:2", check=Check.ttl("1s"))
80
81 time.sleep(0.2)
82
83 # check the nodes show for the /health/state/any endpoint
84 _index, nodes = c.health.state("any")
85 assert {node["ServiceID"] for node in nodes} == {"", "foo:1", "foo:2"}
86
87 # but that they aren't passing their health check
88 _index, nodes = c.health.state("passing")
89 assert "foo" not in [node["ServiceID"] for node in nodes]
90
91 # ping the two node's health check
92 c.agent.check.ttl_pass("service:foo:1")
93 c.agent.check.ttl_pass("service:foo:2")
94
95 time.sleep(0.2)
96
97 # both nodes are now available
98 _index, nodes = c.health.state("passing")
99 assert {node["ServiceID"] for node in nodes} == {"", "foo:1", "foo:2"}
100
101 # wait until the short ttl node fails
102 time.sleep(3)
103
104 # only one node available
105 _index, nodes = c.health.state("passing")
106 assert {node["ServiceID"] for node in nodes} == {"", "foo:1"}
107
108 # ping the failed node's health check
109 c.agent.check.ttl_pass("service:foo:2")
110
111 time.sleep(0.2)
112
113 # check both nodes are available
114 _index, nodes = c.health.state("passing")
115 assert {node["ServiceID"] for node in nodes} == {"", "foo:1", "foo:2"}
116
117 # deregister the nodes
118 c.agent.service.deregister("foo:1")
119 c.agent.service.deregister("foo:2")
120
121 time.sleep(0.2)
122
123 _index, nodes = c.health.state("any")
124 assert [node["ServiceID"] for node in nodes] == [""]
125
126 def test_health_node(self, consul_obj) -> None:
127 c, _consul_version = consul_obj
128 # grab local node name
129 node = c.agent.self()["Config"]["NodeName"]
130 _index, checks = c.health.node(node)
131 assert node in [check["Node"] for check in checks]
132
133 def test_health_checks(self, consul_obj) -> None:
134 c, _consul_version = consul_obj
135
136 c.agent.service.register("foobar", service_id="foobar", check=Check.ttl("10s"))
137
138 time.sleep(40 / 1000.00)
139
140 _index, checks = c.health.checks("foobar")
141
142 assert [check["ServiceID"] for check in checks] == ["foobar"]
143 assert [check["CheckID"] for check in checks] == ["service:foobar"]
144
145 c.agent.service.deregister("foobar")
146
147 time.sleep(40 / 1000.0)
148
149 _index, checks = c.health.checks("foobar")
150 assert len(checks) == 0