TMP_SNAPSHOTS defined in config file

This commit is contained in:
Cayo Puigdefabregas 2020-10-13 15:37:21 +02:00
parent c1595aa305
commit ae98d3154c
4 changed files with 22 additions and 15 deletions

View File

@ -44,6 +44,9 @@ class DevicehubConfig(Config):
"""The minimum version of ereuse.org workbench that this devicehub """The minimum version of ereuse.org workbench that this devicehub
accepts. we recommend not changing this value. accepts. we recommend not changing this value.
""" """
TMP_SNAPSHOTS = config('TMP_SNAPSHOTS', '/tmp/snapshots')
"""This var is for save a snapshots in json format when fail something"""
API_DOC_CONFIG_TITLE = 'Devicehub' API_DOC_CONFIG_TITLE = 'Devicehub'
API_DOC_CONFIG_VERSION = '0.2' API_DOC_CONFIG_VERSION = '0.2'
API_DOC_CONFIG_COMPONENTS = { API_DOC_CONFIG_COMPONENTS = {

View File

@ -1,3 +1,5 @@
""" This is the view for Snapshots """
import os import os
import json import json
from time import time from time import time
@ -18,19 +20,17 @@ from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
SUPPORTED_WORKBENCH = StrictVersion('11.0') SUPPORTED_WORKBENCH = StrictVersion('11.0')
TMP_SNAPSHOTS = 'tmp/snapshots'
def save_json(req_json, tmp_snapshots):
def save_json(req_json):
""" """
This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory
The file need to be saved with one name format with the stamptime and uuid joins The file need to be saved with one name format with the stamptime and uuid joins
""" """
name_file = "{uuid}_{time}.json".format(uuid=req_json.get('uuid', ''), time=int(time())) name_file = "{uuid}_{time}.json".format(uuid=req_json.get('uuid', ''), time=int(time()))
path_name = os.path.join(TMP_SNAPSHOTS, name_file) path_name = os.path.join(tmp_snapshots, name_file)
if not os.path.isdir(TMP_SNAPSHOTS): if not os.path.isdir(tmp_snapshots):
os.system('mkdir -p {}'.format(TMP_SNAPSHOTS)) os.system('mkdir -p {}'.format(tmp_snapshots))
with open(path_name, 'w') as snapshot_file: with open(path_name, 'w') as snapshot_file:
snapshot_file.write(json.dumps(req_json)) snapshot_file.write(json.dumps(req_json))
@ -42,7 +42,8 @@ class ActionView(View):
def post(self): def post(self):
"""Posts an action.""" """Posts an action."""
json = request.get_json(validate=False) json = request.get_json(validate=False)
path_snapshot = save_json(json) tmp_snapshots = app.config['TMP_SNAPSHOTS']
path_snapshot = save_json(json, tmp_snapshots)
if not json or 'type' not in json: if not json or 'type' not in json:
raise ValidationError('Resource needs a type.') raise ValidationError('Resource needs a type.')
# todo there should be a way to better get subclassess resource # todo there should be a way to better get subclassess resource

View File

@ -30,6 +30,7 @@ class TestConfig(DevicehubConfig):
SQLALCHEMY_DATABASE_URI = 'postgresql://dhub:ereuse@localhost/dh_test' SQLALCHEMY_DATABASE_URI = 'postgresql://dhub:ereuse@localhost/dh_test'
TESTING = True TESTING = True
SERVER_NAME = 'localhost' SERVER_NAME = 'localhost'
TMP_SNAPSHOTS = '/tmp/snapshots'
@pytest.fixture(scope='session') @pytest.fixture(scope='session')

View File

@ -26,7 +26,7 @@ from ereuse_devicehub.resources.device.sync import MismatchBetweenProperties, \
from ereuse_devicehub.resources.enums import ComputerChassis, SnapshotSoftware from ereuse_devicehub.resources.enums import ComputerChassis, SnapshotSoftware
from ereuse_devicehub.resources.tag import Tag from ereuse_devicehub.resources.tag import Tag
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.action.views import TMP_SNAPSHOTS, save_json from ereuse_devicehub.resources.action.views import save_json
from tests.conftest import file from tests.conftest import file
@ -481,17 +481,18 @@ def test_pc_2(user: UserClient):
@pytest.mark.mvp @pytest.mark.mvp
def test_save_snapshot_in_file(): def test_save_snapshot_in_file(app: Devicehub):
""" This test check if works the function save_snapshot_in_file """ """ This test check if works the function save_snapshot_in_file """
tmp_snapshots = app.config['TMP_SNAPSHOTS']
snapshot_no_hid = file('basic.snapshot.nohid') snapshot_no_hid = file('basic.snapshot.nohid')
save_json(snapshot_no_hid) save_json(snapshot_no_hid, tmp_snapshots)
uuid = snapshot_no_hid['uuid'] uuid = snapshot_no_hid['uuid']
files = [x for x in os.listdir(TMP_SNAPSHOTS) if uuid in x] files = [x for x in os.listdir(tmp_snapshots) if uuid in x]
snapshot = {'software': '', 'version': '', 'uuid': ''} snapshot = {'software': '', 'version': '', 'uuid': ''}
if files: if files:
path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0]) path_snapshot = os.path.join(tmp_snapshots, files[0])
with open(path_snapshot) as file_snapshot: with open(path_snapshot) as file_snapshot:
snapshot = json.loads(file_snapshot.read()) snapshot = json.loads(file_snapshot.read())
@ -503,8 +504,9 @@ def test_save_snapshot_in_file():
@pytest.mark.mvp @pytest.mark.mvp
def test_backup_snapshot_with_errors(user: UserClient): def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient):
""" This test check if the file snapshot is create when some snapshot is wrong """ """ This test check if the file snapshot is create when some snapshot is wrong """
tmp_snapshots = app.config['TMP_SNAPSHOTS']
snapshot_no_hid = file('basic.snapshot.badly_formed') snapshot_no_hid = file('basic.snapshot.badly_formed')
uuid = snapshot_no_hid['uuid'] uuid = snapshot_no_hid['uuid']
@ -512,9 +514,9 @@ def test_backup_snapshot_with_errors(user: UserClient):
with pytest.raises(KeyError): with pytest.raises(KeyError):
response = user.post(res=Snapshot, data=snapshot_no_hid) response = user.post(res=Snapshot, data=snapshot_no_hid)
files = [x for x in os.listdir(TMP_SNAPSHOTS) if uuid in x] files = [x for x in os.listdir(tmp_snapshots) if uuid in x]
if files: if files:
path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0]) path_snapshot = os.path.join(tmp_snapshots, files[0])
with open(path_snapshot) as file_snapshot: with open(path_snapshot) as file_snapshot:
snapshot = json.loads(file_snapshot.read()) snapshot = json.loads(file_snapshot.read())