2018-05-21 21:02:38 +00:00
|
|
|
import json
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
|
2018-05-23 20:57:26 +00:00
|
|
|
from config_app.config_util.config.baseprovider import BaseProvider
|
2018-05-21 21:02:38 +00:00
|
|
|
|
|
|
|
REAL_FILES = ['test/data/signing-private.gpg', 'test/data/signing-public.gpg', 'test/data/test.pem']
|
|
|
|
|
|
|
|
|
|
|
|
class TestConfigProvider(BaseProvider):
|
2018-08-15 19:32:24 +00:00
|
|
|
""" Implementation of the config provider for testing. Everything is kept in-memory instead on
|
|
|
|
the real file system. """
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.clear()
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def clear(self):
|
|
|
|
self.files = {}
|
|
|
|
self._config = {}
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
@property
|
|
|
|
def provider_id(self):
|
|
|
|
return 'test'
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def update_app_config(self, app_config):
|
|
|
|
self._config = app_config
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def get_config(self):
|
|
|
|
if not 'config.yaml' in self.files:
|
|
|
|
return None
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
return json.loads(self.files.get('config.yaml', '{}'))
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def save_config(self, config_obj):
|
|
|
|
self.files['config.yaml'] = json.dumps(config_obj)
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def config_exists(self):
|
|
|
|
return 'config.yaml' in self.files
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def volume_exists(self):
|
|
|
|
return True
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def volume_file_exists(self, filename):
|
|
|
|
if filename in REAL_FILES:
|
|
|
|
return True
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
return filename in self.files
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def save_volume_file(self, filename, flask_file):
|
|
|
|
self.files[filename] = flask_file.read()
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def write_volume_file(self, filename, contents):
|
|
|
|
self.files[filename] = contents
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def get_volume_file(self, filename, mode='r'):
|
|
|
|
if filename in REAL_FILES:
|
|
|
|
return open(filename, mode=mode)
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
return io.BytesIO(self.files[filename])
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def remove_volume_file(self, filename):
|
|
|
|
self.files.pop(filename, None)
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def list_volume_directory(self, path):
|
|
|
|
paths = []
|
|
|
|
for filename in self.files:
|
|
|
|
if filename.startswith(path):
|
|
|
|
paths.append(filename[len(path) + 1:])
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
return paths
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def requires_restart(self, app_config):
|
|
|
|
return False
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def reset_for_test(self):
|
|
|
|
self._config['SUPER_USERS'] = ['devtable']
|
|
|
|
self.files = {}
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def get_volume_path(self, directory, filename):
|
|
|
|
return os.path.join(directory, filename)
|