]>
git.ipfire.org Git - thirdparty/pdns.git/blob - regression-tests.recursor-dnssec/test_Carbon.py
7 from queue
import Queue
9 from recursortests
import RecursorTest
11 class TestCarbon(RecursorTest
):
13 _carbonNamespace
= 'NS'
14 _carbonInstance
= 'Instance'
15 _carbonServerName
= "carbonname1"
17 _carbonServer1Port
= 8000
18 _carbonServer2Port
= 8001
19 _carbonQueue1
= Queue()
20 _carbonQueue2
= Queue()
22 _config_template
= """
27 carbon-server=127.0.0.1:%s,127.0.01:%s
28 """ % (_carbonNamespace
, _carbonInstance
, _carbonInterval
, _carbonServerName
, _carbonServer1Port
, _carbonServer2Port
)
32 # we don't need all the auth stuff
36 confdir
= os
.path
.join('configs', cls
._confdir
)
37 cls
.createConfigDir(confdir
)
39 cls
.generateRecursorConfig(confdir
)
40 cls
.startRecursor(confdir
, cls
._recursorPort
)
43 def tearDownClass(cls
):
44 cls
.tearDownRecursor()
47 def CarbonResponder(cls
, port
):
48 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
49 sock
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEPORT
, 1)
51 sock
.bind(("127.0.0.1", port
))
52 except socket
.error
as e
:
53 print("Error binding in the Carbon responder: %s" % str(e
))
58 (conn
, _
) = sock
.accept()
62 data
= conn
.recv(4096)
67 if port
== cls
._carbonServer
1Port
:
68 cls
._carbonQueue
1.put(lines
, True, timeout
=2.0)
70 cls
._carbonQueue
2.put(lines
, True, timeout
=2.0)
71 if threading
.currentThread().name
in cls
._carbonCounters
:
72 cls
._carbonCounters
[threading
.currentThread().name
] += 1
74 cls
._carbonCounters
[threading
.currentThread().name
] = 1
80 def startResponders(cls
):
81 cls
._CarbonResponder
1 = threading
.Thread(name
='Carbon Responder 1', target
=cls
.CarbonResponder
, args
=[cls
._carbonServer
1Port
])
82 cls
._CarbonResponder
1.setDaemon(True)
83 cls
._CarbonResponder
1.start()
85 cls
._CarbonResponder
2 = threading
.Thread(name
='Carbon Responder 2', target
=cls
.CarbonResponder
, args
=[cls
._carbonServer
2Port
])
86 cls
._CarbonResponder
2.setDaemon(True)
87 cls
._CarbonResponder
2.start()
91 Carbon: send data to 2 carbon servers
93 # wait for the carbon data to be sent
94 time
.sleep(self
._carbonInterval
+ 1)
96 # check if the servers have received our data
97 # we will block for a short while if the data is not already there,
98 # and an exception will be raised after the timeout
100 data1
= self
._carbonQueue
1.get(block
=True, timeout
=2.0)
102 data2
= self
._carbonQueue
2.get(block
=True, timeout
=2.0)
105 self
.assertTrue(data1
)
106 self
.assertTrue(len(data1
.splitlines()) > 1)
107 expectedStart
= b
"%s.%s.%s." % (self
._carbonNamespace
.encode('UTF8'), self
._carbonServerName
.encode('UTF-8'), self
._carbonInstance
.encode('UTF8'))
108 for line
in data1
.splitlines():
109 self
.assertTrue(line
.startswith(expectedStart
))
110 parts
= line
.split(b
' ')
111 self
.assertEqual(len(parts
), 3)
112 self
.assertTrue(parts
[1].isdigit())
113 self
.assertTrue(parts
[2].isdigit())
114 self
.assertTrue(int(parts
[2]) <= int(after
))
116 self
.assertTrue(data2
)
117 self
.assertTrue(len(data2
.splitlines()) > 1)
118 expectedStart
= b
"%s.%s.%s." % (self
._carbonNamespace
.encode('UTF8'), self
._carbonServerName
.encode('UTF-8'), self
._carbonInstance
.encode('UTF8'))
119 for line
in data2
.splitlines():
120 self
.assertTrue(line
.startswith(expectedStart
))
121 parts
= line
.split(b
' ')
122 self
.assertEqual(len(parts
), 3)
123 self
.assertTrue(parts
[1].isdigit())
124 self
.assertTrue(parts
[2].isdigit())
125 self
.assertTrue(int(parts
[2]) <= int(after
))
127 # make sure every carbon server has received at least one connection
128 for key
in self
._carbonCounters
:
129 value
= self
._carbonCounters
[key
]
130 self
.assertTrue(value
>= 1)