From 3577d7e0a16cebb94ccdaa66c9c5d14ef7439e72 Mon Sep 17 00:00:00 2001 From: Aleksandr Bochev Date: Tue, 10 Jul 2018 18:34:53 +0200 Subject: [PATCH 1/2] fixed emulator is unauthorized performed avd creation just once --- src/app.py | 30 ++++++++++++++++++++++++------ src/tests/unit/test_app.py | 21 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/app.py b/src/app.py index 1ba4db4..e431e7e 100644 --- a/src/app.py +++ b/src/app.py @@ -49,12 +49,22 @@ def convert_str_to_bool(str: str) -> bool: 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_VERSION = get_or_raise('ANDROID_VERSION') API_LEVEL = get_or_raise('API_LEVEL') PROCESSOR = get_or_raise('PROCESSOR') SYS_IMG = get_or_raise('SYS_IMG') IMG_TYPE = get_or_raise('IMG_TYPE') +INIT_FILE = os.getenv('INIT_FILE', "/root/init") logger.info('Android version: {version} \n' 'API level: {level} \n' @@ -86,9 +96,10 @@ def prepare_avd(device: str, avd_name: str): 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};' \ - '{img_type};{sys_img}" -d {device} -p {path}'.format(name=avd_name, img_type=IMG_TYPE, sys_img=SYS_IMG, - api_lvl=API_LEVEL, device=device_name_bash, - path=avd_path) + '{img_type};{sys_img}" -d {device} -p {path}'.format(name=avd_name, img_type=IMG_TYPE, + sys_img=SYS_IMG, + api_lvl=API_LEVEL, device=device_name_bash, + path=avd_path) logger.info('Command to create avd: {command}'.format(command=creation_cmd)) 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) logger.info('AVD name: {avd}'.format(avd=avd_name)) - logger.info('Preparing emulator...') - prepare_avd(device, avd_name) + if not is_initialized(): + logger.info('Preparing emulator...') + prepare_avd(device, avd_name) + finish_initialization() logger.info('Run emulator...') dp_size = os.getenv('DATAPARTITION', '550m') with open("/root/android_emulator/config.ini", "a") as cfg: 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))) if appium: subprocess.Popen(cmd.split()) @@ -202,5 +219,6 @@ def run(): else: result = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate() + if __name__ == '__main__': run() diff --git a/src/tests/unit/test_app.py b/src/tests/unit/test_app.py index 80da2b2..ef4210d 100644 --- a/src/tests/unit/test_app.py +++ b/src/tests/unit/test_app.py @@ -1,5 +1,6 @@ """Unit test to test app.""" import os +import time from unittest import TestCase import mock @@ -67,7 +68,7 @@ class TestApp(TestCase): @mock.patch('src.app.prepare_avd') @mock.patch('builtins.open') @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: os.environ['APPIUM'] = str(False) app.run() @@ -75,3 +76,21 @@ class TestApp(TestCase): self.assertTrue(mocked_open.called) self.assertTrue(mocked_subprocess.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) From bf65a73eb18332817dbfe2b9d4fc9d060de94ec4 Mon Sep 17 00:00:00 2001 From: Aleksandr Bochev Date: Tue, 10 Jul 2018 18:36:38 +0200 Subject: [PATCH 2/2] removed time dependency --- src/tests/unit/test_app.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tests/unit/test_app.py b/src/tests/unit/test_app.py index ef4210d..9b48fa5 100644 --- a/src/tests/unit/test_app.py +++ b/src/tests/unit/test_app.py @@ -1,6 +1,5 @@ """Unit test to test app.""" import os -import time from unittest import TestCase import mock