tests: Use TEST_WORKER_DIR for file access.

This commit is contained in:
Wyatt Hoodes 2019-07-05 14:07:56 -10:00 committed by Tim Abbott
parent 5f20caa6e0
commit 62566f02d7
7 changed files with 17 additions and 32 deletions

View File

@ -60,8 +60,6 @@ from zilencer.models import get_remote_server_by_uuid
from zerver.decorator import do_two_factor_login from zerver.decorator import do_two_factor_login
from zerver.tornado.event_queue import clear_client_event_queues_for_testing from zerver.tornado.event_queue import clear_client_event_queues_for_testing
from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path
import base64 import base64
import mock import mock
import os import os
@ -708,7 +706,7 @@ class ZulipTestCase(TestCase):
shutil.rmtree(path) shutil.rmtree(path)
def make_import_output_dir(self, exported_from: str) -> str: def make_import_output_dir(self, exported_from: str) -> str:
output_dir = tempfile.mkdtemp(dir=get_or_create_dev_uuid_var_path('test-backend'), output_dir = tempfile.mkdtemp(dir=settings.TEST_WORKER_DIR,
prefix="test-" + exported_from + "-import-") prefix="test-" + exported_from + "-import-")
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
return output_dir return output_dir

View File

@ -5,6 +5,7 @@ import ujson
from django.core import mail from django.core import mail
from django.test import override_settings from django.test import override_settings
from django.conf import settings
from mock import patch, MagicMock from mock import patch, MagicMock
from typing import Any, Dict, List, Mapping from typing import Any, Dict, List, Mapping
@ -25,8 +26,6 @@ from zerver.lib.bot_lib import get_bot_handler
from zulip_bots.custom_exceptions import ConfigValidationError from zulip_bots.custom_exceptions import ConfigValidationError
from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path
class BotTest(ZulipTestCase, UploadSerializeMixin): class BotTest(ZulipTestCase, UploadSerializeMixin):
def get_bot_user(self, email: str) -> UserProfile: def get_bot_user(self, email: str) -> UserProfile:
realm = get_realm("zulip") realm = get_realm("zulip")
@ -894,7 +893,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
profile = get_user(email, get_realm('zulip')) profile = get_user(email, get_realm('zulip'))
self.assertEqual(profile.bot_owner, self.example_user("hamlet")) self.assertEqual(profile.bot_owner, self.example_user("hamlet"))
@override_settings(LOCAL_UPLOADS_DIR=get_or_create_dev_uuid_var_path('test-backend/bot_avatar')) @override_settings(LOCAL_UPLOADS_DIR=os.path.join(settings.TEST_WORKER_DIR, 'bot_avatar'))
def test_patch_bot_avatar(self) -> None: def test_patch_bot_avatar(self) -> None:
self.login(self.example_email('hamlet')) self.login(self.example_email('hamlet'))
bot_info = { bot_info = {

View File

@ -147,8 +147,6 @@ from zerver.tornado.event_queue import (
) )
from zerver.tornado.views import get_events from zerver.tornado.views import get_events
from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path
import mock import mock
import time import time
import ujson import ujson
@ -160,7 +158,7 @@ class LogEventsTest(ZulipTestCase):
log_event(dict()) log_event(dict())
def test_log_event_mkdir(self) -> None: def test_log_event_mkdir(self) -> None:
dir_name = get_or_create_dev_uuid_var_path('test-backend/test-log-dir') dir_name = os.path.join(settings.TEST_WORKER_DIR, "test-log-dir")
try: try:
shutil.rmtree(dir_name) shutil.rmtree(dir_name)

View File

@ -89,10 +89,6 @@ from zerver.lib.test_helpers import (
get_test_image_file, get_test_image_file,
) )
from scripts.lib.zulip_tools import (
get_or_create_dev_uuid_var_path,
)
class QueryUtilTest(ZulipTestCase): class QueryUtilTest(ZulipTestCase):
def _create_messages(self) -> None: def _create_messages(self) -> None:
for email in [self.example_email('cordelia'), for email in [self.example_email('cordelia'),
@ -227,7 +223,7 @@ class ImportExportTest(ZulipTestCase):
self.rm_tree(settings.LOCAL_UPLOADS_DIR) self.rm_tree(settings.LOCAL_UPLOADS_DIR)
def _make_output_dir(self) -> str: def _make_output_dir(self) -> str:
output_dir = get_or_create_dev_uuid_var_path('test-backend/test-export') output_dir = os.path.join(settings.TEST_WORKER_DIR, 'test-export')
self.rm_tree(output_dir) self.rm_tree(output_dir)
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
return output_dir return output_dir
@ -701,7 +697,7 @@ class ImportExportTest(ZulipTestCase):
with patch('logging.info'): with patch('logging.info'):
with self.settings(BILLING_ENABLED=False): with self.settings(BILLING_ENABLED=False):
do_import_realm(get_or_create_dev_uuid_var_path('test-backend/test-export'), do_import_realm(os.path.join(settings.TEST_WORKER_DIR, 'test-export'),
'test-zulip') 'test-zulip')
# sanity checks # sanity checks
@ -958,7 +954,7 @@ class ImportExportTest(ZulipTestCase):
self._export_realm(realm) self._export_realm(realm)
with patch('logging.info'): with patch('logging.info'):
do_import_realm(get_or_create_dev_uuid_var_path('test-backend/test-export'), do_import_realm(os.path.join(settings.TEST_WORKER_DIR, 'test-export'),
'test-zulip') 'test-zulip')
imported_realm = Realm.objects.get(string_id='test-zulip') imported_realm = Realm.objects.get(string_id='test-zulip')
@ -995,7 +991,7 @@ class ImportExportTest(ZulipTestCase):
self._setup_export_files() self._setup_export_files()
self._export_realm(realm) self._export_realm(realm)
with patch('logging.info'): with patch('logging.info'):
do_import_realm(get_or_create_dev_uuid_var_path('test-backend/test-export'), do_import_realm(os.path.join(settings.TEST_WORKER_DIR, 'test-export'),
'test-zulip') 'test-zulip')
imported_realm = Realm.objects.get(string_id='test-zulip') imported_realm = Realm.objects.get(string_id='test-zulip')
test_image_data = open(get_test_image_file('img.png').name, 'rb').read() test_image_data = open(get_test_image_file('img.png').name, 'rb').read()
@ -1052,13 +1048,13 @@ class ImportExportTest(ZulipTestCase):
with patch('logging.info'): with patch('logging.info'):
with self.settings(BILLING_ENABLED=True): with self.settings(BILLING_ENABLED=True):
realm = do_import_realm(get_or_create_dev_uuid_var_path('test-backend/test-export'), realm = do_import_realm(os.path.join(settings.TEST_WORKER_DIR, 'test-export'),
'test-zulip-1') 'test-zulip-1')
self.assertTrue(realm.plan_type, Realm.LIMITED) self.assertTrue(realm.plan_type, Realm.LIMITED)
self.assertTrue(RealmAuditLog.objects.filter( self.assertTrue(RealmAuditLog.objects.filter(
realm=realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED).exists()) realm=realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED).exists())
with self.settings(BILLING_ENABLED=False): with self.settings(BILLING_ENABLED=False):
realm = do_import_realm(get_or_create_dev_uuid_var_path('test-backend/test-export'), realm = do_import_realm(os.path.join(settings.TEST_WORKER_DIR, 'test-export'),
'test-zulip-2') 'test-zulip-2')
self.assertTrue(realm.plan_type, Realm.SELF_HOSTED) self.assertTrue(realm.plan_type, Realm.SELF_HOSTED)
self.assertTrue(RealmAuditLog.objects.filter( self.assertTrue(RealmAuditLog.objects.filter(

View File

@ -10,14 +10,11 @@ from zerver.lib.test_helpers import use_s3_backend, create_s3_buckets
from zerver.models import RealmAuditLog from zerver.models import RealmAuditLog
from zerver.views.public_export import public_only_realm_export from zerver.views.public_export import public_only_realm_export
from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path
import os import os
import re import re
def create_tarball_path() -> str: def create_tarball_path() -> str:
tarball_path = os.path.join(get_or_create_dev_uuid_var_path('test-backend'), tarball_path = os.path.join(settings.TEST_WORKER_DIR, 'test-export.tar.gz')
'test-export.tar.gz')
with open(tarball_path, 'w') as f: with open(tarball_path, 'w') as f:
f.write('zulip!') f.write('zulip!')
return tarball_path return tarball_path

View File

@ -45,10 +45,6 @@ from zerver.models import (
Recipient, Recipient,
) )
from scripts.lib.zulip_tools import (
get_or_create_dev_uuid_var_path,
)
import ujson import ujson
import logging import logging
import shutil import shutil
@ -483,7 +479,9 @@ class SlackImporter(ZulipTestCase):
@mock.patch("zerver.data_import.slack.get_messages_iterator") @mock.patch("zerver.data_import.slack.get_messages_iterator")
def test_convert_slack_workspace_messages(self, mock_get_messages_iterator: mock.Mock, def test_convert_slack_workspace_messages(self, mock_get_messages_iterator: mock.Mock,
mock_message: mock.Mock) -> None: mock_message: mock.Mock) -> None:
output_dir = get_or_create_dev_uuid_var_path('test-backend/test-slack-import') output_dir = os.path.join(settings.TEST_WORKER_DIR, 'test-slack-import')
os.makedirs(output_dir, exist_ok=True)
added_channels = {'random': ('c5', 1), 'general': ('c6', 2)} # type: Dict[str, Tuple[str, int]] added_channels = {'random': ('c5', 1), 'general': ('c6', 2)} # type: Dict[str, Tuple[str, int]]
time = float(timezone_now().timestamp()) time = float(timezone_now().timestamp())

View File

@ -42,8 +42,7 @@ from zerver.lib.cache import get_realm_used_upload_space_cache_key, cache_get
from zerver.lib.create_user import copy_user_settings from zerver.lib.create_user import copy_user_settings
from zerver.lib.users import get_api_key from zerver.lib.users import get_api_key
from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path, \ from scripts.lib.zulip_tools import get_dev_uuid_var_path
get_dev_uuid_var_path
import urllib import urllib
import ujson import ujson
@ -1449,7 +1448,7 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
user_profile = self.example_user("iago") user_profile = self.example_user("iago")
self.assertTrue(user_profile.is_realm_admin) self.assertTrue(user_profile.is_realm_admin)
tarball_path = os.path.join(get_or_create_dev_uuid_var_path('test-backend'), tarball_path = os.path.join(settings.TEST_WORKER_DIR,
'tarball.tar.gz') 'tarball.tar.gz')
with open(tarball_path, 'w') as f: with open(tarball_path, 'w') as f:
f.write('dummy') f.write('dummy')
@ -1743,7 +1742,7 @@ class S3Test(ZulipTestCase):
user_profile = self.example_user("iago") user_profile = self.example_user("iago")
self.assertTrue(user_profile.is_realm_admin) self.assertTrue(user_profile.is_realm_admin)
tarball_path = os.path.join(get_or_create_dev_uuid_var_path('test-backend'), tarball_path = os.path.join(settings.TEST_WORKER_DIR,
'tarball.tar.gz') 'tarball.tar.gz')
with open(tarball_path, 'w') as f: with open(tarball_path, 'w') as f:
f.write('dummy') f.write('dummy')