Repository: trafficserver
Updated Branches:
  refs/heads/master 078a9a8c0 -> f98ad39f5


Add tests for reloading the hosts file


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/f98ad39f
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/f98ad39f
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/f98ad39f

Branch: refs/heads/master
Commit: f98ad39f52826f825905266e784ac88e7252f26a
Parents: c857fe7
Author: Thomas Jackson <[email protected]>
Authored: Tue Jul 7 18:59:38 2015 -0700
Committer: Thomas Jackson <[email protected]>
Committed: Tue Jul 7 19:19:31 2015 -0700

----------------------------------------------------------------------
 ci/tsqa/tests/test_hostdb.py | 92 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 82 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f98ad39f/ci/tsqa/tests/test_hostdb.py
----------------------------------------------------------------------
diff --git a/ci/tsqa/tests/test_hostdb.py b/ci/tsqa/tests/test_hostdb.py
index e17d79a..25ac640 100644
--- a/ci/tsqa/tests/test_hostdb.py
+++ b/ci/tsqa/tests/test_hostdb.py
@@ -21,10 +21,41 @@ Test hostdb
 import os
 import requests
 import time
-import tsqa.test_cases
+import logging
+import SocketServer
 
+import tsqa.test_cases
 import helpers
 
+log = logging.getLogger(__name__)
+
+
+class EchoServerIpHandler(SocketServer.BaseRequestHandler):
+    """
+    A subclass of RequestHandler which will return a connection uuid
+    """
+
+    def handle(self):
+        # Receive the data in small chunks and retransmit it
+        while True:
+            data = self.request.recv(4096).strip()
+            if data:
+                log.debug('Sending data back to the client')
+            else:
+                log.debug('Client disconnected')
+                break
+            resp = ('HTTP/1.1 200 OK\r\n'
+                    'Content-Length: 0\r\n'
+                    'Content-Type: text/html; charset=UTF-8\r\n'
+                    'Connection: keep-alive\r\n'
+                    'X-Server-Ip: {server_ip}\r\n'
+                    'X-Server-Port: {server_port}\r\n'
+                    '\r\n'.format(
+                        server_ip=self.request.getsockname()[0],
+                        server_port=self.request.getsockname()[0],
+                    ))
+            self.request.sendall(resp)
+
 
 class TestHostDBPartiallyFailedDNS(helpers.EnvironmentCase):
     '''
@@ -95,30 +126,71 @@ class TestHostDBHostsFile(helpers.EnvironmentCase, 
tsqa.test_cases.HTTPBinCase):
     '''
     @classmethod
     def setUpEnv(cls, env):
-        hosts_file_path = os.path.join(env.layout.prefix, 'hosts')
-        with open(hosts_file_path, 'w') as fh:
-            fh.write('127.0.0.1 local')
+        cls.hosts_file_path = os.path.join(env.layout.prefix, 'hosts')
+        with open(cls.hosts_file_path, 'w') as fh:
+            fh.write('127.0.0.1 local\n')
+            fh.write('127.0.0.2 local2\n')
 
         cls.configs['records.config']['CONFIG'].update({
             'proxy.config.http.response_server_enabled': 2,  # only add server 
headers when there weren't any
             'proxy.config.hostdb.lookup_timeout': 2,
             'proxy.config.url_remap.remap_required': 1,
             'proxy.config.http.connect_attempts_max_retries': 1,
-            'proxy.config.hostdb.host_file.interval': 30,
-            'proxy.config.hostdb.host_file.path': hosts_file_path,
+            'proxy.config.hostdb.host_file.interval': 1,
+            'proxy.config.hostdb.host_file.path': cls.hosts_file_path,
             'proxy.config.diags.debug.enabled': 1,
             'proxy.config.diags.debug.tags': 'hostdb',
         })
-
-        cls.configs['remap.config'].add_line('map http://local/ 
http://local:{0}/'.format(cls.http_endpoint.address[1]))
+        # create a socket server
+        cls.socket_server = 
tsqa.endpoint.SocketServerDaemon(EchoServerIpHandler)
+        cls.socket_server.start()
+        cls.socket_server.ready.wait()
+        cls.configs['remap.config'].add_line('map http://local/ 
http://local:{0}/'.format(cls.socket_server.port))
+        cls.configs['remap.config'].add_line('map http://local2/ 
http://local2:{0}/'.format(cls.socket_server.port))
+        cls.configs['remap.config'].add_line('map http://local3/ 
http://local3:{0}/'.format(cls.socket_server.port))
 
 
     def test_basic(self):
+        '''
+        Test basic fnctionality of hosts files
+        '''
         # TODO add stat, then wait for the stat to increment
-        time.sleep(5)  # wait for the continuation to load the hosts file
+        time.sleep(2)  # wait for the continuation to load the hosts file
         ret = requests.get(
             'http://local/get',
             proxies=self.proxies,
         )
         self.assertEqual(ret.status_code, 200)
-        self.assertEqual('127.0.0.1', ret.json()['origin'])
+        self.assertEqual('127.0.0.1', ret.headers['X-Server-Ip'])
+
+        ret = requests.get(
+            'http://local2/get',
+            proxies=self.proxies,
+        )
+        self.assertEqual(ret.status_code, 200)
+        self.assertEqual('127.0.0.2', ret.headers['X-Server-Ip'])
+
+    def test_reload(self):
+        '''
+        Test that changes to hosts file get loaded within host_file.interval
+        '''
+        # TODO add stat, then wait for the stat to increment
+        time.sleep(2)  # wait for the continuation to load the hosts file
+        ret = requests.get(
+            'http://local3/get',
+            proxies=self.proxies,
+        )
+        self.assertEqual(ret.status_code, 502)
+
+        with open(self.hosts_file_path, 'a') as fh:
+            fh.write('127.0.0.3 local3\n')
+
+        # TODO add stat, then wait for the stat to increment, with a timeout
+        time.sleep(2)
+
+        ret = requests.get(
+            'http://local3/get',
+            proxies=self.proxies,
+        )
+        self.assertEqual(ret.status_code, 200)
+        self.assertEqual('127.0.0.3', ret.headers['X-Server-Ip'])

Reply via email to