backend: Block network access to test suites.

In this commit we basically start to override the request method of
httplib2.Http() to raise an exception whenever it is called i.e.
a trial is made to access the network from test suits.

Fixes: #1472.
This commit is contained in:
Aditya Bansal 2017-07-04 16:42:40 +05:30 committed by Tim Abbott
parent caa8a3f7fc
commit 26c345f059
2 changed files with 35 additions and 1 deletions

View File

@ -79,6 +79,25 @@ if you're working on new database migrations. To do this, run:
[lxc-sf]: https://github.com/fgrehm/vagrant-lxc/wiki/FAQ#help-my-shared-folders-have-the-wrong-owner
### Internet access inside test suits
Zulip has a policy of requiring its backend tests to not depend upon Internet
connectivity. We currently enforce this policy by overriding library functions
which might be used to access internet (eg. `httplib2.Http().request`). Though
this might not be a full proof blockade to internet access but we intend to
improve upon this in near time.
- If you are seeing test failures with tracebacks as below, you should
probably mock the element trying to access the network. You can consult our
[guide](/docs/testing-with-django.htmls#mocks-and-stubs) on
mocking if you want to learn how to write mockups.
```
File "tools/test-backend", line 120, in internet_guard
raise Exception("Zulip doesn't allow network access to test suits."
Exception: Zulip doesn't allow network access to test suits.
You need to mock any network access calls in testsuits.
```
## Schema and initial data changes

View File

@ -2,13 +2,14 @@
from __future__ import print_function
from __future__ import absolute_import
from typing import List
from typing import List, Any
import glob
import optparse
import os
import sys
import subprocess
import ujson
import httplib2
# check for the venv
from lib import sanity_check
@ -110,7 +111,21 @@ def write_failed_tests(failed_tests):
with open(FAILED_TEST_PATH, 'w') as f:
ujson.dump(failed_tests, f)
def block_internet():
# type: () -> None
# We are blocking internet currently by assuming mostly any test would use
# httplib2 to access internet.
def internet_guard(*args, **kwargs):
# type: (*Any, **Any) -> None
raise Exception("Zulip doesn't allow network access to test suits. "
"You need to mock any network access calls in test"
"suits. Checkout our docs on testing here for details."
"https://zulip.readthedocs.io/en/latest/testing.html#internet-access-inside-test-suits")
httplib2.Http.request = internet_guard
if __name__ == "__main__":
block_internet()
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.dirname(TOOLS_DIR))
sys.path.insert(0, os.path.dirname(TOOLS_DIR))