Source code for mylinux.libs.e2eData

import json
import os
import shutil
import re
import six

[docs]class E2E_data: """Hellper class for integration tests. This class will simulate mylinux data structure for the executing tests. Attributes: filesPath (str): Path where temporary tests will be located. packagesPath (str): Relative path in fielsPath to package location. scriptsPath (str): Relative path -//- installedPath (str): Relative path -//- Example of usage:: E2E_data.setUp( installed={ u'package': { u'configState': u'error', u'created': u'%d-%d-%d %d:%d:%d.%d', ... }, ... }, packages={ 'package': { 'info': {'class': 'class', ... }, 'scripts': { 'config-purge': 'printf CP...', 'config-install': 'printf CI...', ... } }, ... } ) E2E-test-data.tearDown() """ filesPath = 'E2E-test-data' packagesPath = filesPath + '/packages' scriptsPath = filesPath + '/scripts' installedPath = filesPath + '/installed.json' @classmethod
[docs] def tearDown(cls): """ Remove and clean testing file structure. """ if os.path.exists(cls.filesPath): shutil.rmtree(cls.filesPath)
@classmethod
[docs] def setUp(cls, installed={}, scripts={}, packages={}): """Setup testing file structure. Args: installed (dic): Dictionary if installed data. scripts (dic): All main scripts names and data. packages (dic): List package names and his structures. Raises: ValueError: If testing structure already exist. """ if not os.path.exists(cls.filesPath): os.makedirs(cls.filesPath) os.makedirs(cls.packagesPath) os.makedirs(cls.scriptsPath) with open(cls.installedPath, 'w') as f: json.dump(installed, f, indent=4) else: raise ValueError('tearDownClass did not clean <filesPath> !!!') for scriptName, scriptData in six.iteritems(scripts): open(cls.scriptsPath + '/' + scriptName, 'w').write(scriptData) for packageName, package in six.iteritems(packages): newPackagePath = cls.packagesPath + '/' + packageName os.makedirs(newPackagePath) os.makedirs(newPackagePath + '/scripts') if 'info' in package: with open(newPackagePath + '/info.json', 'w') as f: json.dump(package['info'], f, indent=4) if 'scripts' in package: for scriptName, scriptData in six.iteritems(package['scripts']): with open(newPackagePath + '/scripts/' + scriptName, 'w') as f: f.write(scriptData)
@classmethod
[docs] def getData(cls): """Get all data info from filesPath. Return: Dictionary of all files in files structure. Example of returning data:: { '<ROOT>/installed.json': { u'package': { u'configState': u'None', u'created': u'%d-%d-%d %d:%d:%d.%d', u'lastChange': u'%d-%d-%d %d:%d:%d.%d', u'packageState': u'None'}, } }, '<ROOT>/packages/package/info.json': { u'class': u'class', u'info': u'info', u'module': u'module' }, '<ROOT>/packages/package/scripts/config-install': ['printf CI...'], '<ROOT>/packages/package/scripts/config-purge': ['printf CP...'], '<ROOT>/packages/package/scripts/package-install': ['printf PI...'], '<ROOT>/packages/package/scripts/package-purge': ['printf PP...'], '<ROOT>/packages/package/scripts/test.py': ['printf T...'] } """ data = {} for path, dirs, files in os.walk(cls.filesPath): for file in files: filePath = os.path.join(path, file) dataKey = filePath.replace(cls.filesPath,'<ROOT>') if 'json' == filePath.split('.')[-1]: with open(filePath) as f: if 'installed.json' == filePath.split('/')[-1]: string = re.sub(r'\d*-\d*-\d* \d*:\d*:\d*.\d*','%d-%d-%d %d:%d:%d.%d',f.read()) data[dataKey] = json.loads(string) else: data[dataKey] = json.load(f) else: with open(filePath) as f: data[dataKey] = f.readlines() return data