Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MemoryError: Added workspace and client for testing available storage #205

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ If you plan to run `mergin` command multiple times and you wish to avoid logging
you can use "login" command to get authorization token.
It will ask for password and then output environment variable with auth token. The returned token
is not permanent - it will expire after several hours.
```
```bash
$ mergin --username john login
Password: topsecret
Login successful!
Expand All @@ -160,15 +160,15 @@ it is possible to run other commands without specifying username/password.
### Installing deps

Python 3.7+ required. Create `mergin/deps` folder where [geodiff](https://github.com/MerginMaps/geodiff) lib is supposed to be and install dependencies:
```
```bash
rm -r mergin/deps
mkdir mergin/deps
pip install python-dateutil pytz
pip install pygeodiff --target=mergin/deps
```

For using mergin client with its dependencies packaged locally run:
```
```bash
pip install wheel
python3 setup.py sdist bdist_wheel
mkdir -p mergin/deps
Expand All @@ -180,13 +180,15 @@ For using mergin client with its dependencies packaged locally run:
### Tests
For running test do:

```
```bash
cd mergin
export TEST_MERGIN_URL=<url> # testing server
export TEST_API_USERNAME=<username>
export TEST_API_PASSWORD=<pwd>
export TEST_API_USERNAME2=<username2>
export TEST_API_PASSWORD2=<pwd2>
# workspace name with controlled available storage space (e.g. 20MB), default value: testpluginstorage
export TEST_STORAGE_WORKSPACE=<workspacename>
pip install pytest pytest-cov coveralls
pytest --cov-report html --cov=mergin mergin/test/
```
35 changes: 22 additions & 13 deletions mergin/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
TMP_DIR = tempfile.gettempdir()
TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data")
CHANGED_SCHEMA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "modified_schema")
STORAGE_WORKSPACE = os.environ.get("TEST_STORAGE_WORKSPACE", "testpluginstorage")


@pytest.fixture(scope="function")
Expand All @@ -65,14 +66,21 @@ def mc2():
return client


@pytest.fixture(scope="function")
def mcStorage():
client = create_client(API_USER, USER_PWD)
create_workspace_for_client(client, STORAGE_WORKSPACE)
return client


def create_client(user, pwd):
assert SERVER_URL and SERVER_URL.rstrip("/") != "https://app.merginmaps.com" and user and pwd
return MerginClient(SERVER_URL, login=user, password=pwd)


def create_workspace_for_client(mc):
def create_workspace_for_client(mc: MerginClient, workspace_name=None):
try:
mc.create_workspace(mc.username())
mc.create_workspace(workspace_name or mc.username())
except ClientError:
return

Expand Down Expand Up @@ -736,33 +744,34 @@ def test_set_editor_access(mc):
assert API_USER2 not in access["writersnames"]


def test_available_storage_validation(mc):
def test_available_storage_validation(mcStorage):
"""
Testing of storage limit - applies to user pushing changes into own project (namespace matching username).
This test also tests giving read and write access to another user. Additionally tests also uploading of big file.
"""
test_project = "test_available_storage_validation"
test_project_fullname = API_USER + "/" + test_project
test_project_fullname = STORAGE_WORKSPACE + "/" + test_project

# cleanups
project_dir = os.path.join(TMP_DIR, test_project, API_USER)
cleanup(mc, test_project_fullname, [project_dir])
cleanup(mcStorage, test_project_fullname, [project_dir])

# create new (empty) project on server
mc.create_project(test_project)
# if namespace is not provided, function is creating project with username
mcStorage.create_project(test_project_fullname)

# download project
mc.download_project(test_project_fullname, project_dir)
mcStorage.download_project(test_project_fullname, project_dir)

# get info about storage capacity
storage_remaining = 0

if mc.server_type() == ServerType.OLD:
user_info = mc.user_info()
if mcStorage.server_type() == ServerType.OLD:
user_info = mcStorage.user_info()
storage_remaining = user_info["storage"] - user_info["disk_usage"]
else:
for workspace in mc.workspaces_list():
if workspace["name"] == API_USER:
for workspace in mcStorage.workspaces_list():
if workspace["name"] == STORAGE_WORKSPACE:
storage_remaining = workspace["storage"] - workspace["disk_usage"]
break

Expand All @@ -774,15 +783,15 @@ def test_available_storage_validation(mc):
# try to upload
got_right_err = False
try:
mc.push_project(project_dir)
mcStorage.push_project(project_dir)
except ClientError as e:
# Expecting "You have reached a data limit" 400 server error msg.
assert "You have reached a data limit" in str(e)
got_right_err = True
assert got_right_err

# Expecting empty project
project_info = get_project_info(mc, API_USER, test_project)
project_info = get_project_info(mcStorage, API_USER, test_project)
assert project_info["version"] == "v0"
assert project_info["disk_usage"] == 0

Expand Down
Loading