]>
Commit | Line | Data |
---|---|---|
98650fde RG |
1 | #!/usr/bin/env python |
2 | import base64 | |
3 | import time | |
4 | import dns | |
5 | from dnsdisttests import DNSDistTest | |
6 | ||
7 | class 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 | ||
20 | class 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 | |
30 | time.sleep(1) | |
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 | |
38 | time.sleep(1) | |
39 | self.assertEquals(TestDefaultHealthCheck._healthCheckCounter, before) | |
40 | ||
41 | self.sendConsoleCommand("getServer(0):setDown()") | |
42 | self.assertEquals(self.getBackendStatus(), 'down') | |
43 | ||
44 | before = TestDefaultHealthCheck._healthCheckCounter | |
45 | time.sleep(1) | |
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 | |
53 | time.sleep(1) | |
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)") | |
60 | # we specified that the new state should be up until the next health check | |
61 | self.assertEquals(self.getBackendStatus(), 'down') | |
62 | ||
63 | before = TestDefaultHealthCheck._healthCheckCounter | |
64 | time.sleep(1) | |
65 | self.assertGreater(TestDefaultHealthCheck._healthCheckCounter, before) | |
66 | self.assertEquals(self.getBackendStatus(), 'up') | |
67 | ||
68 | class TestHealthCheckForcedUP(HealthCheckTest): | |
69 | # this test suite uses a different responder port | |
70 | # because we need fresh counters | |
71 | _testServerPort = 5381 | |
72 | ||
73 | _config_template = """ | |
74 | setKey("%s") | |
75 | controlSocket("127.0.0.1:%d") | |
76 | srv = newServer{address="127.0.0.1:%d"} | |
77 | srv:setUp() | |
78 | """ | |
79 | ||
80 | def testForcedUp(self): | |
81 | """ | |
82 | HealthChecks: Forced UP | |
83 | """ | |
84 | before = TestHealthCheckForcedUP._healthCheckCounter | |
85 | time.sleep(1) | |
86 | self.assertEquals(TestHealthCheckForcedUP._healthCheckCounter, before) | |
87 | self.assertEquals(self.getBackendStatus(), 'up') | |
88 | ||
89 | class TestHealthCheckForcedDown(HealthCheckTest): | |
90 | # this test suite uses a different responder port | |
91 | # because we need fresh counters | |
92 | _testServerPort = 5382 | |
93 | ||
94 | _config_template = """ | |
95 | setKey("%s") | |
96 | controlSocket("127.0.0.1:%d") | |
97 | srv = newServer{address="127.0.0.1:%d"} | |
98 | srv:setDown() | |
99 | """ | |
100 | ||
101 | def testForcedDown(self): | |
102 | """ | |
103 | HealthChecks: Forced Down | |
104 | """ | |
105 | before = TestHealthCheckForcedDown._healthCheckCounter | |
106 | time.sleep(1) | |
107 | self.assertEquals(TestHealthCheckForcedDown._healthCheckCounter, before) | |
108 | ||
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 | |
113 | ||
114 | _healthCheckName = 'powerdns.com.' | |
115 | _config_params = ['_consoleKeyB64', '_consolePort', '_testServerPort', '_healthCheckName'] | |
116 | _config_template = """ | |
117 | setKey("%s") | |
118 | controlSocket("127.0.0.1:%d") | |
119 | srv = newServer{address="127.0.0.1:%d", checkName='%s'} | |
120 | """ | |
121 | ||
122 | def testAuto(self): | |
123 | """ | |
124 | HealthChecks: Custom name | |
125 | """ | |
126 | before = TestHealthCheckCustomName._healthCheckCounter | |
127 | time.sleep(1) | |
128 | self.assertGreater(TestHealthCheckCustomName._healthCheckCounter, before) | |
129 | self.assertEquals(self.getBackendStatus(), 'up') | |
130 | ||
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 | |
135 | ||
136 | _healthCheckAnswerUnexpected = False | |
137 | _config_template = """ | |
138 | setKey("%s") | |
139 | controlSocket("127.0.0.1:%d") | |
140 | srv = newServer{address="127.0.0.1:%d", checkName='powerdns.com.'} | |
141 | """ | |
142 | ||
143 | def testAuto(self): | |
144 | """ | |
145 | HealthChecks: Custom name not expected by the responder | |
146 | """ | |
147 | before = TestHealthCheckCustomNameNoAnswer._healthCheckCounter | |
148 | time.sleep(1) | |
149 | self.assertEquals(TestHealthCheckCustomNameNoAnswer._healthCheckCounter, before) | |
150 | self.assertEquals(self.getBackendStatus(), 'down') | |
151 | ||
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 | _healthCheckAnswerUnexpected = False | |
157 | ||
158 | _healthCheckName = 'powerdns.com.' | |
159 | _config_template = """ | |
160 | setKey("%s") | |
161 | controlSocket("127.0.0.1:%d") | |
162 | ||
163 | function myHealthCheckFunction(qname, qtype, qclass, dh) | |
164 | dh:setCD(true) | |
165 | ||
166 | return newDNSName('powerdns.com.'), dnsdist.AAAA, qclass | |
167 | end | |
168 | ||
169 | srv = newServer{address="127.0.0.1:%d", checkName='powerdns.org.', checkFunction=myHealthCheckFunction} | |
170 | """ | |
171 | ||
172 | def testAuto(self): | |
173 | """ | |
174 | HealthChecks: Custom function | |
175 | """ | |
176 | before = TestHealthCheckCustomFunction._healthCheckCounter | |
177 | time.sleep(1) | |
178 | self.assertGreater(TestHealthCheckCustomFunction._healthCheckCounter, before) | |
179 | self.assertEquals(self.getBackendStatus(), 'up') |