You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by dr...@apache.org on 2017/11/15 20:46:05 UTC

[trafficserver] branch master updated: modified uDNS AuTest extension

This is an automated email from the ASF dual-hosted git repository.

dragon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 41876c8  modified uDNS AuTest extension
41876c8 is described below

commit 41876c82636b4ce1b00142187237c31a4fb19107
Author: Jesse Zhang <ma...@gmail.com>
AuthorDate: Mon Nov 13 11:38:25 2017 -0600

    modified uDNS AuTest extension
    
    addRecords to uDNS extension
    Documentation for uDNS, Condition.PluginExists and other minor fixes
    Got rid of an unnecessary print statement
    
    autopep and got rid of another print statement
---
 tests/README.md                                | 98 ++++++++++++++++++++++++--
 tests/gold_tests/autest-site/microDNS.test.ext | 86 ++++++++++++++--------
 tests/gold_tests/redirect/redirect.test.py     | 23 ++++--
 tests/gold_tests/redirect/zone.json            |  8 +++
 tests/gold_tests/remap/remap_http.test.py      |  4 +-
 5 files changed, 172 insertions(+), 47 deletions(-)

diff --git a/tests/README.md b/tests/README.md
index 0994da3..d2ca0c3 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -66,9 +66,9 @@ tr.Processes.Default.Env=ts.Env
 #### Variables
 These are the current variable that are define dynamically 
 
-port - the ipv4 port to listen on
-portv6 - the ipv4 port to listen on
-manager_port - the manager port used. This is set even is select_port is False
+port - the ipv4 port to listen on  
+portv6 - the ipv4 port to listen on  
+manager_port - the manager port used. This is set even is select_port is False  
 admin_port - the admin port used. This is set even is select_port is False
 
 #### File objects
@@ -165,7 +165,7 @@ Test.Setup.ts.CopyConfig('config/records_8090.config','records.config',ts1)
 Test.Setup.ts.CopyConfig('config/records_8090.config','records.config',Test.Processes.ts1)
 ```
 
-## Setup Origin Server
+## Setting up Origin Server
 ### Test.MakeOriginServer(Name)
  * name - A name for this instance of Origin Server.
  
@@ -193,11 +193,85 @@ ts.Disk.remap_config.AddLine(
     'map http://www.example.com http://127.0.0.1:{0}'.format(server.Variables.Port)
 )
 ```
+
+## Setting up DNS
+### Test.MakeDNServer(name)
+ * name - A name for this instance of the DNS.
+
+ This function returns a AuTest process object that launches the python-based microDNS (uDNS). uDNS is a mock DNS which responds to DNS queries. uDNS needs to be setup for the tests that require made-up domains. The server reads a JSON-formatted data file that contains mappings of domain to IP addresses. uDNS responds with the approriate IP addresses if the requested domain is in uDNS' mappings, otherwise responds with default IP address 127.0.0.1.  
+
+ * addRecords(records=None, jsonFile=None)
+ 
+ This function adds records using either a dictionary, *records*, or a json file, *jsonFile*.  
+
+ The supplied dictionary must be in the form of ```{ 'domain A': [IP1, IP2], 'domain B': [IP3, IP4] }```.  
+
+ The supplied json file must take the form of 
+ ```
+ {
+     "mappings": [
+         {domain A: [IP1, IP2]},
+         {domain B: [IP3, IP4]}
+     ]
+ }
+ ```
+
+ ### Examples
+ There are 3 ways to utilize uDNS -
+
+ Easy way if everything is done on localhost:  
+ *uDNS by default returns 127.0.0.1 for any unknown mappings*
+
+ ```python
+    # create TrafficServer and uDNS processes
+    ts = Test.MakeATSProcess("ts")
+    dns = Test.MakeDNServer("dns")
+
+    ts.Disk.records_config.update({
+        'proxy.config.dns.nameservers': '127.0.0.1:{0}'.format(dns.Variables.Port), # let TrafficServer know where the DNS is located
+        'proxy.config.url_remap.remap_required': 0  # need this so TrafficServer won't return error upon not finding the domain in its remap file
+    })
+ ```
+
+ Using the *addRecords* method:
+ ```python
+    # create TrafficServer and uDNS processes
+    ts = Test.MakeATSProcess("ts")
+    dns = Test.MakeDNServer("dns")
+
+    ts.Disk.records_config.update({
+        'proxy.config.dns.nameservers': '127.0.0.1:{0}'.format(dns.Variables.Port), # let TrafficServer know where the DNS is located
+        'proxy.config.url_remap.remap_required': 0  # need this so TrafficServer won't return error upon not finding the domain in its remap file
+    })
+
+    dns.addRecords(records={"foo.com":["127.0.0.1", "127.0.1.1"]})
+    # AND/OR
+    dns.addRecords(jsonFile="zone.json") # where zone.json is in the format described above
+ ```
+
+ Without disabling remap_required:
+ ```python
+    # create TrafficServer and uDNS processes
+    ts = Test.MakeATSProcess("ts")
+    dns = Test.MakeDNServer("dns")
+
+    ts.Disk.records_config.update({
+        'proxy.config.dns.nameservers': '127.0.0.1:{0}'.format(dns.Variables.Port) # let TrafficServer know where the DNS is located
+    })
+
+    # if we don't disable remap_required, we can also just remap a domain to a domain recognized by DNS
+    ts.Disk.remap_config.AddLine(
+        'map http://example.com http://foo.com'
+    )
+
+    dns.addRecords(records={"foo.com":["127.0.0.1", "127.0.1.1"]})
+ ```
+
 ## Condition Testing
 ### Condition.HasATSFeature(feature)
  * feature - The feature to test for
  
- This function test for Traffic server for possible feature it has been compiled with. Current Features you can test for are:
+ This function tests for Traffic server for possible feature it has been compiled with. Current Features you can test for are:
  * TS_HAS_LIBZ
  * TS_HAS_LZMA
  * TS_HAS_JEMALLOC
@@ -240,7 +314,7 @@ Test.SkipUnless(
 ### Condition.HasCurlFeature(feature)
  * feature - The feature to test for
  
- This function test for Curl for possible feature it has been compiled with. Consult Curl documenation for feature set.
+ This function tests for Curl for possible feature it has been compiled with. Consult Curl documenation for feature set.
                                 
 ### Example
 ```python
@@ -249,3 +323,15 @@ Test.SkipUnless(
     Condition.HasCurlFeature('http2'),
 )
 ```
+
+### Condition.PluginExists(pluginname)
+ * pluginname - The plugin to test for
+ 
+ This function tests for existence of a certain plugin in TrafficServer.
+                                
+### Example
+```python
+Test.SkipUnless(
+    Condition.PluginExists('a-plugin.so'),
+)
+```
diff --git a/tests/gold_tests/autest-site/microDNS.test.ext b/tests/gold_tests/autest-site/microDNS.test.ext
index 58dbfc7..fcae605 100644
--- a/tests/gold_tests/autest-site/microDNS.test.ext
+++ b/tests/gold_tests/autest-site/microDNS.test.ext
@@ -18,52 +18,75 @@
 
 from ports import get_port
 import json
+import os
 
 
 # AddRecord registers a list of ip address against hostname
-def AddRecord(self,filename,hostname,list_ip_addr):
+def AddRecord(hostname, list_ip_addr):
 
     record = dict()
     record[hostname] = list_ip_addr
     return record
 
+# dict in format {'domain': [IPs]}
+# json file in the same mappings/otherwise format that uDNS takes
 
-#adds transaction in json format to the specified file
-def addRecordtoDNS(self,filename,hostname,list_ip_addr=[]):
-    jsondata=None
-    JFile = os.path.join(self.Variables.DataDir, filename)
-    rec = AddRecord(self,filename,hostname,list_ip_addr)
-    if not os.path.exists(os.path.dirname(JFile)):
-        os.makedirs(os.path.dirname(JFile))
-    if os.path.exists(JFile):
-        jf = open(JFile,'r')
-        jsondata = json.load(jf)
-
-    if jsondata == None:
-        jsondata = dict()
-        jsondata["mappings"]=list()
-        jsondata["mappings"].append(rec)
-        jsondata["otherwise"]=list()
-        jsondata["otherwise"].append("127.0.0.1")
-        jsondata["otherwise"].append("127.1.1.1")
+
+def addRecords(self, records=None, jsonFile=None):
+    jsondata = None
+
+    # at this point the default file and fields should have been created
+    if os.path.exists(self.Variables.zone_file):
+        with open(self.Variables.zone_file, 'r') as f:
+            jsondata = json.load(f)
     else:
-        jsondata["mappings"].append(rec)
-    with open(JFile,'w+') as jf:
-        jf.write(json.dumps(jsondata))
+        raise FileNotFoundError("default zone file doesn't exist, but it should.")
+
+    if records:
+        for domain in records:
+            record = AddRecord(domain, records[domain])
+
+            jsondata["mappings"].append(record)
+
+    if jsonFile:
+        jsonFile = os.path.join(self.TestDirectory, jsonFile)
+        self.Setup.Copy(jsonFile, self.Variables.DataDir)
+
+        with open(jsonFile, 'r') as f:
+            entries = json.load(f)
 
+            if entries:
+                # not copying over entries['otherwise']. dont see the need to
+                for record in entries["mappings"]:
+                    jsondata["mappings"].append(record)
 
+    with open(self.Variables.zone_file, 'w+') as f:
+        f.write(json.dumps(jsondata))
 
 
-def MakeDNServer(obj, name,filename,IP='127.0.0.1',port=False,rr=False,options={}):
-    server_path= os.path.join(obj.Variables.AtsTestToolsDir,'microDNS/uDNS.py')
+def MakeDNServer(obj, name, filename="dns_file.json", port=False, IP='127.0.0.1', rr=False, options={}):
+    server_path = os.path.join(obj.Variables.AtsTestToolsDir, 'microDNS/uDNS.py')
     data_dir = os.path.join(obj.RunDirectory, name)
-    filepath = os.path.join(data_dir,filename)
+    filepath = os.path.join(data_dir, filename)
+    obj.Variables.zone_file = filepath
+
+    # provided file doesn't exist, so we create it and fill it in with the 'otherwise' values
+    if not os.path.exists(os.path.dirname(filepath)):
+        os.makedirs(os.path.dirname(filepath))
+
+        jsondata = {'otherwise': ['127.0.0.1'], 'mappings': []}
+
+        with open(filepath, 'w') as f:
+            f.write(json.dumps(jsondata))
+
     # create Process
     p = obj.Processes.Process(name)
     if (port == False):
-        port=get_port(p,"Port")
-    command = "python3 {3} {0} {1} {2}".format(IP, port,filepath,server_path)
+        port = get_port(p, "Port")
+    command = "python3 {3} {0} {1} {2}".format(IP, port, filepath, server_path)
 
+    if rr:
+        command += " --rr"
 
     # create process
     p.Command = command
@@ -71,10 +94,11 @@ def MakeDNServer(obj, name,filename,IP='127.0.0.1',port=False,rr=False,options={
     p.Variables.DataDir = data_dir
     p.ReturnCode = None
     p.Ready = When.PortOpen(port)
-    AddMethodToInstance(p,AddRecord)
-    AddMethodToInstance(p,addRecordtoDNS)
+    AddMethodToInstance(p, AddRecord)
+    AddMethodToInstance(p, addRecords)
 
     return p
 
-AddTestRunSet(MakeDNServer,name="MakeDNServer")
-AddTestRunSet(MakeDNServer,name="MakeDNS")
+
+AddTestRunSet(MakeDNServer, name="MakeDNServer")
+AddTestRunSet(MakeDNServer, name="MakeDNS")
diff --git a/tests/gold_tests/redirect/redirect.test.py b/tests/gold_tests/redirect/redirect.test.py
index ffb686d..230651a 100644
--- a/tests/gold_tests/redirect/redirect.test.py
+++ b/tests/gold_tests/redirect/redirect.test.py
@@ -21,7 +21,6 @@ Test.Summary = '''
 Test basic redirection
 '''
 
-# TODO figure out how to use this
 MAX_REDIRECT = 99
 
 Test.SkipUnless(
@@ -33,12 +32,15 @@ Test.ContinueOnFail = True
 ts = Test.MakeATSProcess("ts")
 redirect_serv = Test.MakeOriginServer("re_server")
 dest_serv = Test.MakeOriginServer("dest_server")
+dns = Test.MakeDNServer("dns")
 
 ts.Disk.records_config.update({
     'proxy.config.http.redirection_enabled': 1,
     'proxy.config.http.number_of_redirections': MAX_REDIRECT,
-    'proxy.config.http.cache.http': 0  # ,
-    # 'proxy.config.diags.debug.enabled': 1
+    'proxy.config.http.cache.http': 0,
+    'proxy.config.dns.nameservers': '127.0.0.1:{0}'.format(dns.Variables.Port),
+    'proxy.config.dns.resolv_conf': 'NULL',
+    'proxy.config.url_remap.remap_required': 0  # need this so the domain gets a chance to be evaluated through DNS
 })
 
 redirect_request_header = {"headers": "GET /redirect HTTP/1.1\r\nHost: *\r\n\r\n", "timestamp": "5678", "body": ""}
@@ -50,14 +52,21 @@ dest_response_header = {"headers": "HTTP/1.1 204 No Content\r\n\r\n", "timestamp
 redirect_serv.addResponse("sessionfile.log", redirect_request_header, redirect_response_header)
 dest_serv.addResponse("sessionfile.log", dest_request_header, dest_response_header)
 
-ts.Disk.remap_config.AddLine(
-    'map http://127.0.0.1:{0} http://127.0.0.1:{1}'.format(ts.Variables.port, redirect_serv.Variables.Port)
-)
+# we don't really need these two lines, since DNS by default will redirect any unknown domains back to 127.0.0.1
+# dns.addRecords(records={"iwillredirect.com":["127.0.0.1"]})
+# dns.addRecords(jsonFile="zone.json")
+
+# if we don't disable remap_required, we can also just remap a domain to the domain recognized by DNS
+# ts.Disk.remap_config.AddLine(
+#     'map http://example.com http://iwillredirect.com:{1}/redirect'.format(redirect_serv.Variables.Port)
+# )
 
 tr = Test.AddTestRun()
-tr.Processes.Default.Command = 'curl -i http://127.0.0.1:{0}/redirect'.format(ts.Variables.port)
+tr.Processes.Default.Command = 'curl -i --proxy 127.0.0.1:{0} "http://iwillredirect.com:{1}/redirect"'.format(
+    ts.Variables.port, redirect_serv.Variables.Port)
 tr.Processes.Default.StartBefore(ts)
 tr.Processes.Default.StartBefore(redirect_serv)
 tr.Processes.Default.StartBefore(dest_serv)
+tr.Processes.Default.StartBefore(dns)
 tr.Processes.Default.Streams.stdout = "gold/redirect.gold"
 tr.Processes.Default.ReturnCode = 0
diff --git a/tests/gold_tests/redirect/zone.json b/tests/gold_tests/redirect/zone.json
new file mode 100644
index 0000000..cfe67a0
--- /dev/null
+++ b/tests/gold_tests/redirect/zone.json
@@ -0,0 +1,8 @@
+{ 
+    "mappings": [
+      {"iwillredirect.com.": ["127.0.0.1"]}
+    ],
+  
+    "otherwise": ["127.0.0.1"]
+  }
+  
\ No newline at end of file
diff --git a/tests/gold_tests/remap/remap_http.test.py b/tests/gold_tests/remap/remap_http.test.py
index 2543df2..54ed5db 100644
--- a/tests/gold_tests/remap/remap_http.test.py
+++ b/tests/gold_tests/remap/remap_http.test.py
@@ -28,7 +28,7 @@ Test.ContinueOnFail = True
 # Define default ATS
 ts = Test.MakeATSProcess("ts")
 server = Test.MakeOriginServer("server")
-dns = Test.MakeDNServer("dns", filename="dns_file.json")
+dns = Test.MakeDNServer("dns")
 
 Test.testName = ""
 request_header = {"headers": "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n", "timestamp": "1469733493.993", "body": ""}
@@ -61,8 +61,6 @@ ts.Disk.remap_config.AddLine(
     'map http://testDNS.com http://audrey.hepburn.com:{0}'.format(server.Variables.Port)
 )
 
-# dns.addRecordtoDNS(filename="dns_file.json",hostname="wonderwoman",list_ip_addr=["127.0.0.1","127.0.1.1"])
-dns.addRecordtoDNS(filename="dns_file.json", hostname="audrey.hepburn.com", list_ip_addr=["127.0.0.1", "127.0.1.1"])
 # call localhost straight
 tr = Test.AddTestRun()
 tr.Processes.Default.Command = 'curl "http://127.0.0.1:{0}/" --verbose'.format(ts.Variables.port)

-- 
To stop receiving notification emails like this one, please contact
['"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>'].