summaryrefslogtreecommitdiffstats
path: root/tests/carbon_tests/emitter_test.py
diff options
context:
space:
mode:
authorMark Beierl <mark.beierl@emc.com>2016-07-14 16:20:44 -0400
committerMark Beierl <mark.beierl@emc.com>2016-07-14 16:20:44 -0400
commit2227414bd57f4b7f5f275d915fa8f6a2aa21f8f7 (patch)
treeb7ed9d0e356d27383396d680b059b4ab18069915 /tests/carbon_tests/emitter_test.py
parentce3f927c2bc98eaf124e96f99cbcc33fbebabeed (diff)
Separation of test and source
Moving the test files into their own top-level directory to keep things clean Change-Id: Ic50b881045bc59b003807923424345b335dd5c95 Signed-off-by: Mark Beierl <mark.beierl@emc.com>
Diffstat (limited to 'tests/carbon_tests/emitter_test.py')
-rw-r--r--tests/carbon_tests/emitter_test.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/carbon_tests/emitter_test.py b/tests/carbon_tests/emitter_test.py
new file mode 100644
index 0000000..fe19ed2
--- /dev/null
+++ b/tests/carbon_tests/emitter_test.py
@@ -0,0 +1,66 @@
+##############################################################################
+# Copyright (c) 2015 EMC and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+from storperf.carbon import converter
+from storperf.carbon.emitter import CarbonMetricTransmitter
+from time import sleep
+import SocketServer
+import json
+import threading
+import unittest
+
+
+class MetricsHandler(SocketServer.BaseRequestHandler):
+
+ def handle(self):
+ # Echo the back to the client
+ CarbonMetricTransmitterTest.response = self.request.recv(1024)
+ return
+
+
+class MetricsServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
+ pass
+
+
+class CarbonMetricTransmitterTest(unittest.TestCase):
+ listen_port = 0
+ response = None
+
+ def setUp(self):
+
+ address = ('localhost', 0)
+ server = MetricsServer(address, MetricsHandler)
+ ip, self.listen_port = server.server_address
+
+ t = threading.Thread(target=server.serve_forever)
+ t.setDaemon(True)
+ t.start()
+
+ def test_transmit_metrics(self):
+
+ testconv = converter.Converter()
+ json_object = json.loads("""{"timestamp" : "12345", "key":"value" }""")
+ result = testconv.convert_json_to_flat(json_object, "host.run-name")
+
+ emitter = CarbonMetricTransmitter()
+ emitter.carbon_port = self.listen_port
+ emitter.transmit_metrics(result)
+
+ count = 0
+
+ while (CarbonMetricTransmitterTest.response is None and count < 10):
+ count += 1
+ sleep(0.1)
+
+ self.assertEqual("host.run-name.key value 12345\n",
+ CarbonMetricTransmitterTest.response,
+ CarbonMetricTransmitterTest.response)
+
+if __name__ == '__main__':
+ unittest.main()