5 from dnsdisttests
import DNSDistTest
7 class HealthCheckTest(DNSDistTest
):
8 _consoleKey
= DNSDistTest
.generateConsoleKey()
9 _consoleKeyB64
= base64
.b64encode(_consoleKey
).decode('ascii')
10 _config_params
= ['_consoleKeyB64', '_consolePort', '_testServerPort']
11 _config_template
= """
13 controlSocket("127.0.0.1:%d")
14 newServer{address="127.0.0.1:%d"}
17 def getBackendStatus(self
):
18 return self
.sendConsoleCommand("if getServer(0):isUp() then return 'up' else return 'down' end").strip("\n")
20 class TestDefaultHealthCheck(HealthCheckTest
):
21 # this test suite uses a different responder port
22 # because we need fresh counters
23 _testServerPort
= 5380
25 def testDefault(self
):
29 before
= TestDefaultHealthCheck
._healthCheckCounter
31 self
.assertGreater(TestDefaultHealthCheck
._healthCheckCounter
, before
)
32 self
.assertEquals(self
.getBackendStatus(), 'up')
34 self
.sendConsoleCommand("getServer(0):setUp()")
35 self
.assertEquals(self
.getBackendStatus(), 'up')
37 before
= TestDefaultHealthCheck
._healthCheckCounter
39 self
.assertEquals(TestDefaultHealthCheck
._healthCheckCounter
, before
)
41 self
.sendConsoleCommand("getServer(0):setDown()")
42 self
.assertEquals(self
.getBackendStatus(), 'down')
44 before
= TestDefaultHealthCheck
._healthCheckCounter
46 self
.assertEquals(TestDefaultHealthCheck
._healthCheckCounter
, before
)
48 self
.sendConsoleCommand("getServer(0):setAuto()")
49 # we get back the previous state, which was up
50 self
.assertEquals(self
.getBackendStatus(), 'up')
52 before
= TestDefaultHealthCheck
._healthCheckCounter
54 self
.assertGreater(TestDefaultHealthCheck
._healthCheckCounter
, before
)
55 self
.assertEquals(self
.getBackendStatus(), 'up')
57 self
.sendConsoleCommand("getServer(0):setDown()")
58 self
.assertEquals(self
.getBackendStatus(), 'down')
59 self
.sendConsoleCommand("getServer(0):setAuto(false)")
60 # we specified that the new state should be up until the next health check
61 self
.assertEquals(self
.getBackendStatus(), 'down')
63 before
= TestDefaultHealthCheck
._healthCheckCounter
65 self
.assertGreater(TestDefaultHealthCheck
._healthCheckCounter
, before
)
66 self
.assertEquals(self
.getBackendStatus(), 'up')
68 class TestHealthCheckForcedUP(HealthCheckTest
):
69 # this test suite uses a different responder port
70 # because we need fresh counters
71 _testServerPort
= 5381
73 _config_template
= """
75 controlSocket("127.0.0.1:%d")
76 srv = newServer{address="127.0.0.1:%d"}
80 def testForcedUp(self
):
82 HealthChecks: Forced UP
84 before
= TestHealthCheckForcedUP
._healthCheckCounter
86 self
.assertEquals(TestHealthCheckForcedUP
._healthCheckCounter
, before
)
87 self
.assertEquals(self
.getBackendStatus(), 'up')
89 class TestHealthCheckForcedDown(HealthCheckTest
):
90 # this test suite uses a different responder port
91 # because we need fresh counters
92 _testServerPort
= 5382
94 _config_template
= """
96 controlSocket("127.0.0.1:%d")
97 srv = newServer{address="127.0.0.1:%d"}
101 def testForcedDown(self
):
103 HealthChecks: Forced Down
105 before
= TestHealthCheckForcedDown
._healthCheckCounter
107 self
.assertEquals(TestHealthCheckForcedDown
._healthCheckCounter
, before
)
109 class TestHealthCheckCustomName(HealthCheckTest
):
110 # this test suite uses a different responder port
111 # because it uses a different health check name
112 _testServerPort
= 5383
114 _healthCheckName
= 'powerdns.com.'
115 _config_params
= ['_consoleKeyB64', '_consolePort', '_testServerPort', '_healthCheckName']
116 _config_template
= """
118 controlSocket("127.0.0.1:%d")
119 srv = newServer{address="127.0.0.1:%d", checkName='%s'}
124 HealthChecks: Custom name
126 before
= TestHealthCheckCustomName
._healthCheckCounter
128 self
.assertGreater(TestHealthCheckCustomName
._healthCheckCounter
, before
)
129 self
.assertEquals(self
.getBackendStatus(), 'up')
131 class TestHealthCheckCustomNameNoAnswer(HealthCheckTest
):
132 # this test suite uses a different responder port
133 # because it uses a different health check configuration
134 _testServerPort
= 5384
136 _answerUnexpected
= False
137 _config_template
= """
139 controlSocket("127.0.0.1:%d")
140 srv = newServer{address="127.0.0.1:%d", checkName='powerdns.com.'}
145 HealthChecks: Custom name not expected by the responder
147 before
= TestHealthCheckCustomNameNoAnswer
._healthCheckCounter
149 self
.assertEquals(TestHealthCheckCustomNameNoAnswer
._healthCheckCounter
, before
)
150 self
.assertEquals(self
.getBackendStatus(), 'down')
152 class TestHealthCheckCustomFunction(HealthCheckTest
):
153 # this test suite uses a different responder port
154 # because it uses a different health check configuration
155 _testServerPort
= 5385
156 _answerUnexpected
= False
158 _healthCheckName
= 'powerdns.com.'
159 _config_template
= """
161 controlSocket("127.0.0.1:%d")
163 function myHealthCheckFunction(qname, qtype, qclass, dh)
166 return newDNSName('powerdns.com.'), dnsdist.AAAA, qclass
169 srv = newServer{address="127.0.0.1:%d", checkName='powerdns.org.', checkFunction=myHealthCheckFunction}
174 HealthChecks: Custom function
176 before
= TestHealthCheckCustomFunction
._healthCheckCounter
178 self
.assertGreater(TestHealthCheckCustomFunction
._healthCheckCounter
, before
)
179 self
.assertEquals(self
.getBackendStatus(), 'up')