-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource storage older than X days deletion (#392)
* add parameter for resource_storage deletion to delete only files older than x days Co-authored-by: anikaweinmann <[email protected]>
- Loading branch information
1 parent
7033dbd
commit 38e5eb9
Showing
3 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
Tests: Resource storage test case | ||
""" | ||
from flask.json import loads as json_load | ||
from datetime import datetime, timedelta | ||
import unittest | ||
import os | ||
|
||
|
@@ -42,12 +43,11 @@ | |
) | ||
|
||
__license__ = "GPLv3" | ||
__author__ = "Sören Gebbert" | ||
__author__ = "Sören Gebbert, Anika Weinmann" | ||
__copyright__ = ( | ||
"Copyright 2016-2018, Sören Gebbert and mundialis GmbH & Co. KG" | ||
"Copyright 2016-2022, Sören Gebbert and mundialis GmbH & Co. KG" | ||
) | ||
__maintainer__ = "Sören Gebbert" | ||
__email__ = "[email protected]" | ||
__maintainer__ = "mundialis" | ||
|
||
|
||
class ResourceStorageTestCase(ActiniaResourceTestCaseBase): | ||
|
@@ -70,7 +70,6 @@ def test_resource_storage(self): | |
rv = self.server.get( | ||
URL_PREFIX + "/resource_storage", headers=self.admin_auth_header | ||
) | ||
print(rv.data) | ||
self.assertEqual( | ||
rv.status_code, | ||
200, | ||
|
@@ -90,7 +89,6 @@ def test_resource_storage(self): | |
rv = self.server.delete( | ||
URL_PREFIX + "/resource_storage", headers=self.admin_auth_header | ||
) | ||
print(rv.data) | ||
self.assertEqual( | ||
rv.status_code, | ||
200, | ||
|
@@ -103,7 +101,6 @@ def test_resource_storage(self): | |
rv = self.server.get( | ||
URL_PREFIX + "/resource_storage", headers=self.admin_auth_header | ||
) | ||
print(rv.data) | ||
self.assertEqual( | ||
rv.status_code, | ||
200, | ||
|
@@ -128,7 +125,6 @@ def test_resource_storage_error_1(self): | |
rv = self.server.get( | ||
URL_PREFIX + "/resource_storage", headers=self.admin_auth_header | ||
) | ||
print(rv.data) | ||
self.assertEqual( | ||
rv.status_code, | ||
400, | ||
|
@@ -146,7 +142,6 @@ def test_resource_storage_error_2(self): | |
rv = self.server.delete( | ||
URL_PREFIX + "/resource_storage", headers=self.admin_auth_header | ||
) | ||
print(rv.data) | ||
self.assertEqual( | ||
rv.status_code, | ||
400, | ||
|
@@ -156,6 +151,82 @@ def test_resource_storage_error_2(self): | |
rv.mimetype, "application/json", "Wrong mimetype %s" % rv.mimetype | ||
) | ||
|
||
def test_resource_storage_delete_olderthan(self): | ||
|
||
global_config.GRASS_RESOURCE_DIR = "/tmp/rstorage_tmp" | ||
global_config.GRASS_RESOURCE_QUOTA = 1 | ||
try: | ||
os.mkdir(global_config.GRASS_RESOURCE_DIR) | ||
except Exception: # more precise exception gladly accepted | ||
pass | ||
|
||
admin_resource_path = os.path.join( | ||
global_config.GRASS_RESOURCE_DIR, | ||
self.admin_id, | ||
) | ||
try: | ||
os.mkdir(admin_resource_path) | ||
except Exception: # more precise exception gladly accepted | ||
pass | ||
|
||
# create files from specified date | ||
now = datetime.now() | ||
before_15days = now - timedelta(days=15) | ||
file1 = os.path.join(admin_resource_path, "file1.txt") | ||
file2 = os.path.join(admin_resource_path, "file2.txt") | ||
date1 = now.strftime("%Y-%m-%d %H:%M:%S") | ||
date2 = before_15days.strftime("%Y-%m-%d %H:%M:%S") | ||
cmd_touch_file1 = f"touch -d '{date1}' {file1}" | ||
cmd_touch_file2 = f"touch -d '{date2}' {file2}" | ||
os.system(cmd_touch_file1) | ||
os.system(cmd_touch_file2) | ||
created_files = os.listdir(admin_resource_path) | ||
self.assertIn( | ||
"file1.txt", | ||
created_files, | ||
"'file1.txt' not in resource path after creation", | ||
) | ||
self.assertIn( | ||
"file2.txt", | ||
created_files, | ||
"'file2.txt' not in resource path after creation", | ||
) | ||
|
||
# request resource storage | ||
rv = self.server.get( | ||
URL_PREFIX + "/resource_storage", headers=self.admin_auth_header | ||
) | ||
self.assertEqual( | ||
rv.status_code, | ||
200, | ||
"HTML status code is wrong %i" % rv.status_code, | ||
) | ||
|
||
# delete files older than 10 days | ||
rv = self.server.delete( | ||
URL_PREFIX + "/resource_storage?olderthan=10", | ||
headers=self.admin_auth_header, | ||
) | ||
self.assertEqual( | ||
rv.status_code, | ||
200, | ||
"HTML status code is wrong %i" % rv.status_code, | ||
) | ||
# check files | ||
files = os.listdir(admin_resource_path) | ||
self.assertIn("file1.txt", files, "'file1.txt' not in resource path") | ||
self.assertNotIn("file2.txt", files, "'file2.txt' in resource path") | ||
|
||
# clean up resource storage | ||
rv = self.server.delete( | ||
URL_PREFIX + "/resource_storage", headers=self.admin_auth_header | ||
) | ||
self.assertEqual( | ||
rv.status_code, | ||
200, | ||
"HTML status code is wrong %i" % rv.status_code, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |