]> git.ipfire.org Git - thirdparty/pdns.git/blame - regression-tests.dnsdist/test_HealthChecks.py
Merge pull request #8795 from omoerbeek/rec-lua-docs-policytag
[thirdparty/pdns.git] / regression-tests.dnsdist / test_HealthChecks.py
CommitLineData
98650fde
RG
1#!/usr/bin/env python
2import base64
3import time
4import dns
5from dnsdisttests import DNSDistTest
6
7class HealthCheckTest(DNSDistTest):
8 _consoleKey = DNSDistTest.generateConsoleKey()
9 _consoleKeyB64 = base64.b64encode(_consoleKey).decode('ascii')
10 _config_params = ['_consoleKeyB64', '_consolePort', '_testServerPort']
11 _config_template = """
12 setKey("%s")
13 controlSocket("127.0.0.1:%d")
14 newServer{address="127.0.0.1:%d"}
15 """
16
17 def getBackendStatus(self):
18 return self.sendConsoleCommand("if getServer(0):isUp() then return 'up' else return 'down' end").strip("\n")
19
20class TestDefaultHealthCheck(HealthCheckTest):
21 # this test suite uses a different responder port
22 # because we need fresh counters
23 _testServerPort = 5380
24
25 def testDefault(self):
26 """
27 HealthChecks: Default
28 """
29 before = TestDefaultHealthCheck._healthCheckCounter
e4fb91e8 30 time.sleep(1.5)
98650fde
RG
31 self.assertGreater(TestDefaultHealthCheck._healthCheckCounter, before)
32 self.assertEquals(self.getBackendStatus(), 'up')
33
34 self.sendConsoleCommand("getServer(0):setUp()")
35 self.assertEquals(self.getBackendStatus(), 'up')
36
37 before = TestDefaultHealthCheck._healthCheckCounter
e4fb91e8 38 time.sleep(1.5)
98650fde
RG
39 self.assertEquals(TestDefaultHealthCheck._healthCheckCounter, before)
40
41 self.sendConsoleCommand("getServer(0):setDown()")
42 self.assertEquals(self.getBackendStatus(), 'down')
43
44 before = TestDefaultHealthCheck._healthCheckCounter
e4fb91e8 45 time.sleep(1.5)
98650fde
RG
46 self.assertEquals(TestDefaultHealthCheck._healthCheckCounter, before)
47
48 self.sendConsoleCommand("getServer(0):setAuto()")
49 # we get back the previous state, which was up
50 self.assertEquals(self.getBackendStatus(), 'up')
51
52 before = TestDefaultHealthCheck._healthCheckCounter
e4fb91e8 53 time.sleep(1.5)
98650fde
RG
54 self.assertGreater(TestDefaultHealthCheck._healthCheckCounter, before)
55 self.assertEquals(self.getBackendStatus(), 'up')
56
57 self.sendConsoleCommand("getServer(0):setDown()")
58 self.assertEquals(self.getBackendStatus(), 'down')
59 self.sendConsoleCommand("getServer(0):setAuto(false)")
98650fde
RG
60
61 before = TestDefaultHealthCheck._healthCheckCounter
e4fb91e8 62 time.sleep(1.5)
98650fde
RG
63 self.assertGreater(TestDefaultHealthCheck._healthCheckCounter, before)
64 self.assertEquals(self.getBackendStatus(), 'up')
65
66class TestHealthCheckForcedUP(HealthCheckTest):
67 # this test suite uses a different responder port
68 # because we need fresh counters
69 _testServerPort = 5381
70
71 _config_template = """
72 setKey("%s")
73 controlSocket("127.0.0.1:%d")
74 srv = newServer{address="127.0.0.1:%d"}
75 srv:setUp()
76 """
77
78 def testForcedUp(self):
79 """
80 HealthChecks: Forced UP
81 """
82 before = TestHealthCheckForcedUP._healthCheckCounter
e4fb91e8 83 time.sleep(1.5)
98650fde
RG
84 self.assertEquals(TestHealthCheckForcedUP._healthCheckCounter, before)
85 self.assertEquals(self.getBackendStatus(), 'up')
86
87class TestHealthCheckForcedDown(HealthCheckTest):
88 # this test suite uses a different responder port
89 # because we need fresh counters
90 _testServerPort = 5382
91
92 _config_template = """
93 setKey("%s")
94 controlSocket("127.0.0.1:%d")
95 srv = newServer{address="127.0.0.1:%d"}
96 srv:setDown()
97 """
98
99 def testForcedDown(self):
100 """
101 HealthChecks: Forced Down
102 """
103 before = TestHealthCheckForcedDown._healthCheckCounter
e4fb91e8 104 time.sleep(1.5)
98650fde
RG
105 self.assertEquals(TestHealthCheckForcedDown._healthCheckCounter, before)
106
107class TestHealthCheckCustomName(HealthCheckTest):
108 # this test suite uses a different responder port
109 # because it uses a different health check name
110 _testServerPort = 5383
111
112 _healthCheckName = 'powerdns.com.'
113 _config_params = ['_consoleKeyB64', '_consolePort', '_testServerPort', '_healthCheckName']
114 _config_template = """
115 setKey("%s")
116 controlSocket("127.0.0.1:%d")
117 srv = newServer{address="127.0.0.1:%d", checkName='%s'}
118 """
119
120 def testAuto(self):
121 """
122 HealthChecks: Custom name
123 """
124 before = TestHealthCheckCustomName._healthCheckCounter
e4fb91e8 125 time.sleep(1.5)
98650fde
RG
126 self.assertGreater(TestHealthCheckCustomName._healthCheckCounter, before)
127 self.assertEquals(self.getBackendStatus(), 'up')
128
129class TestHealthCheckCustomNameNoAnswer(HealthCheckTest):
130 # this test suite uses a different responder port
131 # because it uses a different health check configuration
132 _testServerPort = 5384
133
e44df0f1 134 _answerUnexpected = False
98650fde
RG
135 _config_template = """
136 setKey("%s")
137 controlSocket("127.0.0.1:%d")
138 srv = newServer{address="127.0.0.1:%d", checkName='powerdns.com.'}
139 """
140
141 def testAuto(self):
142 """
143 HealthChecks: Custom name not expected by the responder
144 """
145 before = TestHealthCheckCustomNameNoAnswer._healthCheckCounter
e4fb91e8 146 time.sleep(1.5)
98650fde
RG
147 self.assertEquals(TestHealthCheckCustomNameNoAnswer._healthCheckCounter, before)
148 self.assertEquals(self.getBackendStatus(), 'down')
149
150class TestHealthCheckCustomFunction(HealthCheckTest):
151 # this test suite uses a different responder port
152 # because it uses a different health check configuration
153 _testServerPort = 5385
e44df0f1 154 _answerUnexpected = False
98650fde
RG
155
156 _healthCheckName = 'powerdns.com.'
157 _config_template = """
158 setKey("%s")
159 controlSocket("127.0.0.1:%d")
160
161 function myHealthCheckFunction(qname, qtype, qclass, dh)
162 dh:setCD(true)
163
d3ec24f9 164 return newDNSName('powerdns.com.'), DNSQType.AAAA, qclass
98650fde
RG
165 end
166
167 srv = newServer{address="127.0.0.1:%d", checkName='powerdns.org.', checkFunction=myHealthCheckFunction}
168 """
169
170 def testAuto(self):
171 """
172 HealthChecks: Custom function
173 """
174 before = TestHealthCheckCustomFunction._healthCheckCounter
e4fb91e8 175 time.sleep(1.5)
98650fde
RG
176 self.assertGreater(TestHealthCheckCustomFunction._healthCheckCounter, before)
177 self.assertEquals(self.getBackendStatus(), 'up')