Merge pull request #92 from red-avtovo/mymaster
Initialise emulator just once
This commit is contained in:
commit
852daa4e32
30
src/app.py
30
src/app.py
|
@ -49,12 +49,22 @@ def convert_str_to_bool(str: str) -> bool:
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
|
|
||||||
|
|
||||||
|
def is_initialized() -> bool:
|
||||||
|
return os.path.exists(INIT_FILE)
|
||||||
|
|
||||||
|
|
||||||
|
def finish_initialization():
|
||||||
|
file = open(INIT_FILE, 'w+')
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
|
||||||
ANDROID_HOME = get_or_raise('ANDROID_HOME')
|
ANDROID_HOME = get_or_raise('ANDROID_HOME')
|
||||||
ANDROID_VERSION = get_or_raise('ANDROID_VERSION')
|
ANDROID_VERSION = get_or_raise('ANDROID_VERSION')
|
||||||
API_LEVEL = get_or_raise('API_LEVEL')
|
API_LEVEL = get_or_raise('API_LEVEL')
|
||||||
PROCESSOR = get_or_raise('PROCESSOR')
|
PROCESSOR = get_or_raise('PROCESSOR')
|
||||||
SYS_IMG = get_or_raise('SYS_IMG')
|
SYS_IMG = get_or_raise('SYS_IMG')
|
||||||
IMG_TYPE = get_or_raise('IMG_TYPE')
|
IMG_TYPE = get_or_raise('IMG_TYPE')
|
||||||
|
INIT_FILE = os.getenv('INIT_FILE', "/root/init")
|
||||||
|
|
||||||
logger.info('Android version: {version} \n'
|
logger.info('Android version: {version} \n'
|
||||||
'API level: {level} \n'
|
'API level: {level} \n'
|
||||||
|
@ -86,9 +96,10 @@ def prepare_avd(device: str, avd_name: str):
|
||||||
|
|
||||||
avd_path = '/'.join([ANDROID_HOME, 'android_emulator'])
|
avd_path = '/'.join([ANDROID_HOME, 'android_emulator'])
|
||||||
creation_cmd = 'avdmanager create avd -f -n {name} -b {img_type}/{sys_img} -k "system-images;android-{api_lvl};' \
|
creation_cmd = 'avdmanager create avd -f -n {name} -b {img_type}/{sys_img} -k "system-images;android-{api_lvl};' \
|
||||||
'{img_type};{sys_img}" -d {device} -p {path}'.format(name=avd_name, img_type=IMG_TYPE, sys_img=SYS_IMG,
|
'{img_type};{sys_img}" -d {device} -p {path}'.format(name=avd_name, img_type=IMG_TYPE,
|
||||||
api_lvl=API_LEVEL, device=device_name_bash,
|
sys_img=SYS_IMG,
|
||||||
path=avd_path)
|
api_lvl=API_LEVEL, device=device_name_bash,
|
||||||
|
path=avd_path)
|
||||||
logger.info('Command to create avd: {command}'.format(command=creation_cmd))
|
logger.info('Command to create avd: {command}'.format(command=creation_cmd))
|
||||||
subprocess.check_call(creation_cmd, shell=True)
|
subprocess.check_call(creation_cmd, shell=True)
|
||||||
|
|
||||||
|
@ -186,14 +197,20 @@ def run():
|
||||||
avd_name = '{device}_{version}'.format(device=device.replace(' ', '_').lower(), version=ANDROID_VERSION)
|
avd_name = '{device}_{version}'.format(device=device.replace(' ', '_').lower(), version=ANDROID_VERSION)
|
||||||
logger.info('AVD name: {avd}'.format(avd=avd_name))
|
logger.info('AVD name: {avd}'.format(avd=avd_name))
|
||||||
|
|
||||||
logger.info('Preparing emulator...')
|
if not is_initialized():
|
||||||
prepare_avd(device, avd_name)
|
logger.info('Preparing emulator...')
|
||||||
|
prepare_avd(device, avd_name)
|
||||||
|
finish_initialization()
|
||||||
|
|
||||||
logger.info('Run emulator...')
|
logger.info('Run emulator...')
|
||||||
dp_size = os.getenv('DATAPARTITION', '550m')
|
dp_size = os.getenv('DATAPARTITION', '550m')
|
||||||
with open("/root/android_emulator/config.ini", "a") as cfg:
|
with open("/root/android_emulator/config.ini", "a") as cfg:
|
||||||
cfg.write('\ndisk.dataPartition.size={dp}'.format(dp=dp_size))
|
cfg.write('\ndisk.dataPartition.size={dp}'.format(dp=dp_size))
|
||||||
cmd = 'emulator/emulator @{name} -gpu off -verbose'.format(name=avd_name)
|
|
||||||
|
if not is_initialized():
|
||||||
|
cmd = 'emulator/emulator @{name} -gpu off -verbose -wipe-data'.format(name=avd_name)
|
||||||
|
else:
|
||||||
|
cmd = 'emulator/emulator @{name} -gpu off -verbose'.format(name=avd_name)
|
||||||
appium = convert_str_to_bool(str(os.getenv('APPIUM', False)))
|
appium = convert_str_to_bool(str(os.getenv('APPIUM', False)))
|
||||||
if appium:
|
if appium:
|
||||||
subprocess.Popen(cmd.split())
|
subprocess.Popen(cmd.split())
|
||||||
|
@ -202,5 +219,6 @@ def run():
|
||||||
else:
|
else:
|
||||||
result = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()
|
result = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run()
|
run()
|
||||||
|
|
|
@ -67,7 +67,7 @@ class TestApp(TestCase):
|
||||||
@mock.patch('src.app.prepare_avd')
|
@mock.patch('src.app.prepare_avd')
|
||||||
@mock.patch('builtins.open')
|
@mock.patch('builtins.open')
|
||||||
@mock.patch('subprocess.Popen')
|
@mock.patch('subprocess.Popen')
|
||||||
def test_run_withhout_appium(self, mocked_avd, mocked_open, mocked_subprocess):
|
def test_run_without_appium(self, mocked_avd, mocked_open, mocked_subprocess):
|
||||||
with mock.patch('src.app.appium_run') as mocked_appium:
|
with mock.patch('src.app.appium_run') as mocked_appium:
|
||||||
os.environ['APPIUM'] = str(False)
|
os.environ['APPIUM'] = str(False)
|
||||||
app.run()
|
app.run()
|
||||||
|
@ -75,3 +75,21 @@ class TestApp(TestCase):
|
||||||
self.assertTrue(mocked_open.called)
|
self.assertTrue(mocked_open.called)
|
||||||
self.assertTrue(mocked_subprocess.called)
|
self.assertTrue(mocked_subprocess.called)
|
||||||
self.assertFalse(mocked_appium.called)
|
self.assertFalse(mocked_appium.called)
|
||||||
|
|
||||||
|
@mock.patch('builtins.open')
|
||||||
|
@mock.patch('subprocess.Popen')
|
||||||
|
@mock.patch('os.path.exists', mock.MagicMock(return_value=False))
|
||||||
|
def test_run_first_run(self, mocked_open, mocked_subprocess):
|
||||||
|
with mock.patch('src.app.prepare_avd') as mocked_prepare_avd:
|
||||||
|
app.run()
|
||||||
|
self.assertFalse(app.is_initialized())
|
||||||
|
self.assertTrue(mocked_prepare_avd.called)
|
||||||
|
|
||||||
|
@mock.patch('builtins.open')
|
||||||
|
@mock.patch('subprocess.Popen')
|
||||||
|
@mock.patch('os.path.exists', mock.MagicMock(return_value=True))
|
||||||
|
def test_run_next_run(self, mocked_open, mocked_subprocess):
|
||||||
|
with mock.patch('src.app.prepare_avd') as mocked_prepare_avd:
|
||||||
|
app.run()
|
||||||
|
self.assertTrue(app.is_initialized())
|
||||||
|
self.assertFalse(mocked_prepare_avd.called)
|
||||||
|
|
Loading…
Reference in a new issue