From 567a494ccb40031dbda9ff9def4363c845d30df5 Mon Sep 17 00:00:00 2001 From: butomo1989 Date: Thu, 9 Nov 2017 11:25:21 +0100 Subject: [PATCH] Using new usage process of new SDK --- src/app.py | 39 +++++++++++++++++++------------------- src/tests/unit/test_app.py | 26 +++++++++++++------------ src/tests/unit/test_avd.py | 38 +++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 53 deletions(-) diff --git a/src/app.py b/src/app.py index aeaad8a..f3af9e5 100644 --- a/src/app.py +++ b/src/app.py @@ -4,22 +4,24 @@ import json import logging import os import subprocess -import errno -from src import CONFIG_FILE, ROOT, CHROME_DRIVER +from src import CHROME_DRIVER, CONFIG_FILE, ROOT from src import log log.init() logger = logging.getLogger('app') + def symlink_force(target, link_name): try: os.symlink(target, link_name) except OSError as e: + import errno if e.errno == errno.EEXIST: os.remove(link_name) os.symlink(target, link_name) + def get_or_raise(env: str) -> str: """ Check if needed environment variables are given. @@ -69,22 +71,9 @@ def prepare_avd(device: str, avd_name: str): :param device: Device name :param avd_name: Name of android virtual device / emulator """ - cmd = 'echo no | android create avd -f -n {name} -t android-{api} -b {img_type}{sys_img}'.format( - name=avd_name, api=API_LEVEL, img_type='google_apis/' if IMG_TYPE == 'google_apis' else '', - sys_img=SYS_IMG) - # Link emulator skins - skin_rsc_path = os.path.join(ROOT, 'devices', 'skins') - logger.info('Skin ressource path: {rsc}'.format(rsc=skin_rsc_path)) - skin_dst_path = os.path.join(ANDROID_HOME, 'platforms', 'android-{api}'.format(api=API_LEVEL), 'skins') - logger.info('Skin destination path: {dst}'.format(dst=skin_dst_path)) - for s in os.listdir(skin_rsc_path): - symlink_force(os.path.join(skin_rsc_path, s), os.path.join(skin_dst_path, s)) - - # Hardware and its skin device_name_bash = device.replace(' ', '\ ') skin_name = device.replace(' ', '_').lower() - logger.info('Device name in bash: {db}, Skin name: {skin}'.format(db=device_name_bash, skin=skin_name)) # For custom hardware profile profile_dst_path = os.path.join(ROOT, '.android', 'devices.xml') @@ -95,10 +84,19 @@ def prepare_avd(device: str, avd_name: str): logger.info('Hardware profile destination path: {dst}'.format(dst=profile_dst_path)) symlink_force(profile_src_path, profile_dst_path) - # Append command - cmd += ' -d {device} -s {skin}'.format(device=device_name_bash, skin=skin_name) - logger.info('AVD creation command: {cmd}'.format(cmd=cmd)) - subprocess.check_call(cmd, shell=True) + 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) + logger.info('Command to create avd: {command}'.format(command=creation_cmd)) + subprocess.check_call(creation_cmd, shell=True) + + skin_path = '/'.join([ANDROID_HOME, 'devices', 'skins', skin_name]) + config_path = '/'.join([avd_path, 'config.ini']) + with open(config_path, 'a') as file: + file.write('skin.path={sp}'.format(sp=skin_path)) + logger.info('Skin was added in config.ini') def appium_run(avd_name: str): @@ -184,8 +182,9 @@ def run(): logger.info('Preparing emulator...') prepare_avd(device, avd_name) + logger.info('Run emulator...') - cmd = 'emulator -avd {name}'.format(name=avd_name) + cmd = 'emulator -avd {name} -gpu off'.format(name=avd_name) subprocess.Popen(cmd.split()) appium = convert_str_to_bool(str(os.getenv('APPIUM', False))) diff --git a/src/tests/unit/test_app.py b/src/tests/unit/test_app.py index c31ac2d..d6e8c15 100644 --- a/src/tests/unit/test_app.py +++ b/src/tests/unit/test_app.py @@ -10,22 +10,24 @@ from src import app class TestApp(TestCase): """Unit test class to test other methods in the app.""" - #create symlink @classmethod - def test_symlink_correct(self): - os.mknod(os.path.join("./","testFile1.txt")) - app.symlink_force(os.path.join("./","testFile1.txt"),os.path.join("./","link_testFile1.txt")) - os.remove(os.path.join("./","testFile1.txt")) - os.remove(os.path.join("./","link_testFile1.txt")) + def test_symlink(self): + res = os.path.join('testFile1.txt') + dest = os.path.join('link_testFile1.txt') + open(res, 'a').close() + app.symlink_force(res, dest) + os.remove(res) + os.remove(dest) - #link already exist @classmethod def test_symlink_already_exist(self): - os.mknod(os.path.join("./","testFile2.txt")) - os.mknod(os.path.join("./","link_testFile2.txt")) - app.symlink_force(os.path.join("./","testFile2.txt"),os.path.join("./","link_testFile2.txt")) - os.remove(os.path.join("./","testFile2.txt")) - os.remove(os.path.join("./","link_testFile2.txt")) + res = os.path.join('testFile1.txt') + dest = os.path.join('link_testFile1.txt') + open(res, 'a').close() + open(dest, 'a').close() + app.symlink_force(res, dest) + os.remove(res) + os.remove(dest) def test_valid_env(self): key = 'ENV_1' diff --git a/src/tests/unit/test_avd.py b/src/tests/unit/test_avd.py index 800d291..c6ca33b 100644 --- a/src/tests/unit/test_avd.py +++ b/src/tests/unit/test_avd.py @@ -8,34 +8,30 @@ from src import app @mock.patch('subprocess.check_call') -@mock.patch('os.symlink') class TestAvd(TestCase): """Unit test class to test method create_avd.""" def setUp(self): self.avd_name = 'test_avd' - def test_nexus_avd_as_default(self, mocked_suprocess, mocked_sys_link): - with mock.patch('os.listdir') as mocked_list_dir: - mocked_list_dir.return_value = ['file1', 'file2'] - self.assertFalse(mocked_list_dir.called) - self.assertFalse(mocked_sys_link.called) - self.assertFalse(mocked_suprocess.called) - app.prepare_avd('Nexus 5', self.avd_name) - self.assertTrue(mocked_list_dir.called) - self.assertTrue(mocked_sys_link.called) - self.assertTrue(mocked_suprocess.called) + @mock.patch("builtins.open", create=True) + def test_nexus_avd_as_default(self, mocked_suprocess, mocked_open): + self.assertFalse(mocked_suprocess.called) + self.assertFalse(mocked_open.called) + app.prepare_avd('Nexus 5', self.avd_name) + self.assertTrue(mocked_suprocess.called) + self.assertTrue(mocked_open.called) - def test_samsung_avd(self, mocked_suprocess, mocked_sys_link): - with mock.patch('os.listdir') as mocked_list_dir: - mocked_list_dir.return_value = ['file1', 'file2'] - self.assertFalse(mocked_list_dir.called) - self.assertFalse(mocked_sys_link.called) - self.assertFalse(mocked_suprocess.called) - app.prepare_avd('Samsung Galaxy S6', self.avd_name) - self.assertTrue(mocked_list_dir.called) - self.assertTrue(mocked_sys_link.called) - self.assertTrue(mocked_suprocess.called) + @mock.patch('os.symlink') + @mock.patch("builtins.open", create=True) + def test_samsung_avd(self, mocked_suprocess, mocked_sys_link, mocked_open): + self.assertFalse(mocked_sys_link.called) + self.assertFalse(mocked_suprocess.called) + self.assertFalse(mocked_open.called) + app.prepare_avd('Samsung Galaxy S6', self.avd_name) + self.assertTrue(mocked_sys_link.called) + self.assertTrue(mocked_suprocess.called) + self.assertTrue(mocked_open.called) def tearDown(self): if os.getenv('DEVICE'):