[LNT] r243407 - Add more unit tests checking for error 400/404 when requesting invalid or unavailable data.

Kristof Beyls kristof.beyls at arm.com
Tue Jul 28 04:31:59 PDT 2015


Author: kbeyls
Date: Tue Jul 28 06:31:58 2015
New Revision: 243407

URL: http://llvm.org/viewvc/llvm-project?rev=243407&view=rev
Log:
Add more unit tests checking for error 400/404 when requesting invalid or unavailable data.

Also fixes one more instance where the server could crash on an invalid URL.
Also replace error code 400 (bad request) with error code 404 (not found) in a
few places where this seems more appropriate.


Modified:
    lnt/trunk/lnt/server/ui/views.py
    lnt/trunk/tests/server/ui/V4Pages.py

Modified: lnt/trunk/lnt/server/ui/views.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/ui/views.py?rev=243407&r1=243406&r2=243407&view=diff
==============================================================================
--- lnt/trunk/lnt/server/ui/views.py (original)
+++ lnt/trunk/lnt/server/ui/views.py Tue Jul 28 06:31:58 2015
@@ -525,14 +525,15 @@ def v4_graph():
             return abort(400)
 
         if not (0 <= field_index < len(ts.sample_fields)):
-            return abort(400)
+            return abort(404)
 
         try:
-            machine = ts.query(ts.Machine).filter(ts.Machine.id == machine_id).one()
+            machine = \
+                ts.query(ts.Machine).filter(ts.Machine.id == machine_id).one()
             test = ts.query(ts.Test).filter(ts.Test.id == test_id).one()
             field = ts.sample_fields[field_index]
         except NoResultFound:
-            return abort(400)
+            return abort(404)
         graph_parameters.append((machine, test, field))
 
     # Order the plots by machine name, test name and then field.
@@ -555,9 +556,13 @@ def v4_graph():
             return abort(400)
 
         if not (0 <= field_index < len(ts.sample_fields)):
-            return abort(400)
+            return abort(404)
 
-        machine = ts.query(ts.Machine).filter(ts.Machine.id == machine_id).one()
+        try:
+            machine = \
+                ts.query(ts.Machine).filter(ts.Machine.id == machine_id).one()
+        except NoResultFound:
+            return abort(404)
         field = ts.sample_fields[field_index]
 
         mean_parameter = (machine, field)
@@ -1061,7 +1066,10 @@ def v4_daily_report(year, month, day):
         filter_machine_regex=filter_machine_regex)
 
     # Build the report.
-    report.build()
+    try:
+        report.build()
+    except ValueError:
+        return abort(400)
 
     return render_template("v4_daily_report.html", ts=ts, report=report,
                            analysis=lnt.server.reporting.analysis)

Modified: lnt/trunk/tests/server/ui/V4Pages.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/ui/V4Pages.py?rev=243407&r1=243406&r2=243407&view=diff
==============================================================================
--- lnt/trunk/tests/server/ui/V4Pages.py (original)
+++ lnt/trunk/tests/server/ui/V4Pages.py Tue Jul 28 06:31:58 2015
@@ -19,6 +19,9 @@ import json
 
 logging.basicConfig(level=logging.DEBUG)
 
+HTTP_BAD_REQUEST = 400
+HTTP_NOT_FOUND = 404
+
 
 def check_code(client, url, expected_code=200, data_to_send=None):
     """Call a flask url, and make sure the return code is good."""
@@ -134,9 +137,8 @@ def main():
 
     # Get a machine overview page.
     check_code(client, '/v4/nts/machine/1')
-
     # Check invalid machine gives error.
-    check_code(client,  '/v4/nts/machine/1000', expected_code=404)
+    check_code(client,  '/v4/nts/machine/9999', expected_code=HTTP_NOT_FOUND)
     # Get a machine overview page in JSON format.
     check_code(client, '/v4/nts/machine/1?json=true')
 
@@ -145,16 +147,23 @@ def main():
 
     # Get an order page.
     check_code(client, '/v4/nts/order/3')
+    # Check invalid order gives error.
+    check_code(client, '/v4/nts/order/9999', expected_code=HTTP_NOT_FOUND)
 
     # Get a run result page (and associated views).
     check_code(client, '/v4/nts/1')
-
     check_code(client, '/v4/nts/1?json=true')
-
     check_code(client, '/v4/nts/1/report')
-
     check_code(client, '/v4/nts/1/text_report')
-
+    # Check invalid run numbers give errors.
+    check_code(client, '/v4/nts/9999',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/nts/9999?json=true',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/nts/9999/report',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/nts/9999/text_report',
+               expected_code=HTTP_NOT_FOUND)
 
     # Get a graph page. This has been changed to redirect.
     check_redirect(client, '/v4/nts/1/graph?test.87=2',
@@ -162,9 +171,22 @@ def main():
 
     # Get the new graph page.
     check_code(client, '/v4/nts/graph?plot.0=1.87.2')
+    # Don't crash when requesting non-existing data
+    check_code(client, '/v4/nts/graph?plot.9999=1.87.2')
+    check_code(client, '/v4/nts/graph?plot.0=9999.87.2',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/nts/graph?plot.0=1.9999.2',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/nts/graph?plot.0=1.87.9999',
+               expected_code=HTTP_NOT_FOUND)
 
     # Get the mean graph page.
     check_code(client, '/v4/nts/graph?mean=1.2')
+    # Don't crash when requesting non-existing data
+    check_code(client, '/v4/nts/graph?mean=9999.2',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/nts/graph?mean=1.9999',
+               expected_code=HTTP_NOT_FOUND)
 
     # Check some variations of the daily report work.
     check_code(client, '/v4/nts/daily_report/2012/4/12')
@@ -180,6 +202,14 @@ def main():
     # a flask URL variable.
     check_redirect(client, '/v4/nts/daily_report?day=15',
                    '/v4/nts/daily_report/\d+/\d+/\d+$')
+    # Don't crash when requesting non-existing data
+    check_code(client, '/v4/nts/daily_report/1999/4/12')
+    check_code(client, '/v4/nts/daily_report/-1/4/12',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/nts/daily_report/2012/13/12',
+               expected_code=HTTP_BAD_REQUEST)
+    check_code(client, '/v4/nts/daily_report/2012/4/32',
+               expected_code=HTTP_BAD_REQUEST)
 
     # check ?filter-machine-regex= filter
     check_nr_machines_reported(client, '/v4/nts/daily_report/2012/4/12', 3)
@@ -188,6 +218,8 @@ def main():
     check_nr_machines_reported(client, '/v4/nts/daily_report/2012/4/12?filter-machine-regex=ma.*[34]$', 1)
     check_nr_machines_reported(client, '/v4/nts/daily_report/2012/4/12?filter-machine-regex=ma.*4', 0)
     # Don't crash on an invalid regular expression:
+    # FIXME - this should probably return HTTP_BAD_REQUEST instead of silently
+    # ignoring the invalid regex.
     check_nr_machines_reported(client, '/v4/nts/daily_report/2012/4/12?filter-machine-regex=?', 3)
 
     # check that a regression seen between 2 consecutive runs that are
@@ -206,6 +238,11 @@ def main():
     # Get a machine overview page.
     check_code(client, '/v4/compile/machine/1')
     check_code(client, '/v4/compile/machine/2')
+    # Don't crash when requesting non-existing data
+    check_code(client, '/v4/compile/machine/9999',
+               expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/compile/machine/-1', expected_code=HTTP_NOT_FOUND)
+    check_code(client, '/v4/compile/machine/a', expected_code=HTTP_NOT_FOUND)
 
     # Get the order summary page.
     check_code(client, '/v4/compile/all_orders')
@@ -218,7 +255,7 @@ def main():
     check_code(client, '/v4/compile/2')
     check_code(client, '/v4/compile/3')
     check_code(client, '/v4/compile/4')
-    check_code(client, '/v4/compile/10', expected_code=404) # This page should not be there.
+    check_code(client, '/v4/compile/9999', expected_code=HTTP_NOT_FOUND)
 
     check_code(client, '/v4/compile/1/report')
 





More information about the llvm-commits mailing list