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)")
61 before
= TestDefaultHealthCheck
._healthCheckCounter
63 self
.assertGreater(TestDefaultHealthCheck
._healthCheckCounter
, before
)
64 self
.assertEquals(self
.getBackendStatus(), 'up')
66 class TestHealthCheckForcedUP(HealthCheckTest
):
67 # this test suite uses a different responder port
68 # because we need fresh counters
69 _testServerPort
= 5381
71 _config_template
= """
73 controlSocket("127.0.0.1:%d")
74 srv = newServer{address="127.0.0.1:%d"}
78 def testForcedUp(self
):
80 HealthChecks: Forced UP
82 before
= TestHealthCheckForcedUP
._healthCheckCounter
84 self
.assertEquals(TestHealthCheckForcedUP
._healthCheckCounter
, before
)
85 self
.assertEquals(self
.getBackendStatus(), 'up')
87 class TestHealthCheckForcedDown(HealthCheckTest
):
88 # this test suite uses a different responder port
89 # because we need fresh counters
90 _testServerPort
= 5382
92 _config_template
= """
94 controlSocket("127.0.0.1:%d")
95 srv = newServer{address="127.0.0.1:%d"}
99 def testForcedDown(self
):
101 HealthChecks: Forced Down
103 before
= TestHealthCheckForcedDown
._healthCheckCounter
105 self
.assertEquals(TestHealthCheckForcedDown
._healthCheckCounter
, before
)
107 class TestHealthCheckCustomName(HealthCheckTest
):
108 # this test suite uses a different responder port
109 # because it uses a different health check name
110 _testServerPort
= 5383
112 _healthCheckName
= 'powerdns.com.'
113 _config_params
= ['_consoleKeyB64', '_consolePort', '_testServerPort', '_healthCheckName']
114 _config_template
= """
116 controlSocket("127.0.0.1:%d")
117 srv = newServer{address="127.0.0.1:%d", checkName='%s'}
122 HealthChecks: Custom name
124 before
= TestHealthCheckCustomName
._healthCheckCounter
126 self
.assertGreater(TestHealthCheckCustomName
._healthCheckCounter
, before
)
127 self
.assertEquals(self
.getBackendStatus(), 'up')
129 class TestHealthCheckCustomNameNoAnswer(HealthCheckTest
):
130 # this test suite uses a different responder port
131 # because it uses a different health check configuration
132 _testServerPort
= 5384
134 _answerUnexpected
= False
135 _config_template
= """
137 controlSocket("127.0.0.1:%d")
138 srv = newServer{address="127.0.0.1:%d", checkName='powerdns.com.'}
143 HealthChecks: Custom name not expected by the responder
145 before
= TestHealthCheckCustomNameNoAnswer
._healthCheckCounter
147 self
.assertEquals(TestHealthCheckCustomNameNoAnswer
._healthCheckCounter
, before
)
148 self
.assertEquals(self
.getBackendStatus(), 'down')
150 class TestHealthCheckCustomFunction(HealthCheckTest
):
151 # this test suite uses a different responder port
152 # because it uses a different health check configuration
153 _testServerPort
= 5385
154 _answerUnexpected
= False
156 _healthCheckName
= 'powerdns.com.'
157 _config_template
= """
159 controlSocket("127.0.0.1:%d")
161 function myHealthCheckFunction(qname, qtype, qclass, dh)
164 return newDNSName('powerdns.com.'), DNSQType.AAAA, qclass
167 srv = newServer{address="127.0.0.1:%d", checkName='powerdns.org.', checkFunction=myHealthCheckFunction}
172 HealthChecks: Custom function
174 before
= TestHealthCheckCustomFunction
._healthCheckCounter
176 self
.assertGreater(TestHealthCheckCustomFunction
._healthCheckCounter
, before
)
177 self
.assertEquals(self
.getBackendStatus(), 'up')