Best Python code snippet using autotest_python
setup_job_unittest.py
Source:setup_job_unittest.py
1#!/usr/bin/python2#pylint: disable-msg=C01113import logging4import os5import shutil6import StringIO7import sys8import unittest9import common10from autotest_lib.client.bin import job, setup_job11from autotest_lib.client.bin import utils12from autotest_lib.client.common_lib import base_job13from autotest_lib.client.common_lib import logging_manager, logging_config14from autotest_lib.client.common_lib import base_job_unittest15from autotest_lib.client.common_lib.test_utils import mock16class setup_job_test_case(unittest.TestCase):17 """Generic job TestCase class that defines a standard job setUp and18 tearDown, with some standard stubs."""19 job_class = setup_job.setup_job20 def setUp(self):21 self.god = mock.mock_god()22 self.god.stub_with(setup_job.setup_job, '_get_environ_autodir',23 classmethod(lambda cls: '/adir'))24 self.job = self.job_class.__new__(self.job_class)25 self.job._job_directory = base_job_unittest.stub_job_directory26 self.job.args = []27 def tearDown(self):28 self.god.unstub_all()29class test_find_base_directories(30 base_job_unittest.test_find_base_directories.generic_tests,31 setup_job_test_case):32 def test_autodir_equals_clientdir(self):33 autodir, clientdir, _ = self.job._find_base_directories()34 self.assertEqual(autodir, '/adir')35 self.assertEqual(clientdir, '/adir')36 def test_serverdir_is_none(self):37 _, _, serverdir = self.job._find_base_directories()38 self.assertEqual(serverdir, None)39class abstract_test_init(base_job_unittest.test_init.generic_tests):40 """Generic client job mixin used when defining variations on the41 job.__init__ generic tests."""42 PUBLIC_ATTRIBUTES = (43 base_job_unittest.test_init.generic_tests.PUBLIC_ATTRIBUTES44 - set(['bootloader', 'control', 'drop_caches',45 'drop_caches_between_iterations', 'harness', 'hosts', 'logging',46 'machines', 'num_tests_failed', 'num_tests_run', 'profilers',47 'sysinfo', 'user', 'warning_loggers', 'warning_manager']))48class test_init_minimal_options(abstract_test_init, setup_job_test_case):49 def call_init(self):50 # TODO(jadmanski): refactor more of the __init__ code to not need to51 # stub out countless random APIs52 self.god.stub_function_to_return(setup_job.os, 'mkdir', None)53 self.god.stub_function_to_return(setup_job.os.path, 'exists', True)54 self.god.stub_function_to_return(self.job, '_load_state', None)55 self.god.stub_function_to_return(setup_job.logging_manager,56 'configure_logging', None)57 class manager:58 def start_logging(self):59 return None60 self.god.stub_function_to_return(setup_job.logging_manager,61 'get_logging_manager', manager())62 class options:63 tag = ''64 verbose = False65 cont = False66 harness = 'stub'67 hostname = None68 user = None69 log = False70 output_dir = False71 self.job.__init__(options)72class dummy(object):73 """A simple placeholder for attributes"""74 pass75class first_line_comparator(mock.argument_comparator):76 def __init__(self, first_line):77 self.first_line = first_line78 def is_satisfied_by(self, parameter):79 return self.first_line == parameter.splitlines()[0]80class test_setup_job(unittest.TestCase):81 def setUp(self):82 # make god83 self.god = mock.mock_god()84 # need to set some environ variables85 self.autodir = "autodir"86 os.environ['AUTODIR'] = self.autodir87 # set up some variables88 self.jobtag = "jobtag"89 # get rid of stdout and logging90 sys.stdout = StringIO.StringIO()91 logging_manager.configure_logging(logging_config.TestingConfig())92 logging.disable(logging.CRITICAL)93 def dummy_configure_logging(*args, **kwargs):94 pass95 self.god.stub_with(logging_manager, 'configure_logging',96 dummy_configure_logging)97 real_get_logging_manager = logging_manager.get_logging_manager98 def get_logging_manager_no_fds(manage_stdout_and_stderr=False,99 redirect_fds=False):100 return real_get_logging_manager(manage_stdout_and_stderr, False)101 self.god.stub_with(logging_manager, 'get_logging_manager',102 get_logging_manager_no_fds)103 # stub out some stuff104 self.god.stub_function(os.path, 'exists')105 self.god.stub_function(os.path, 'isdir')106 self.god.stub_function(os, 'makedirs')107 self.god.stub_function(os, 'mkdir')108 self.god.stub_function(os, 'remove')109 self.god.stub_function(shutil, 'rmtree')110 self.god.stub_function(shutil, 'copyfile')111 self.god.stub_function(setup_job, 'open')112 self.god.stub_function(utils, 'system')113 self.god.stub_class_method(job.base_client_job,114 '_cleanup_debugdir_files')115 self.god.stub_class_method(job.base_client_job, '_cleanup_results_dir')116 self.god.stub_with(base_job.job_directory, '_ensure_valid',117 lambda *_: None)118 def tearDown(self):119 sys.stdout = sys.__stdout__120 self.god.unstub_all()121 def _setup_pre_record_init(self):122 resultdir = os.path.join(self.autodir, 'results', self.jobtag)123 tmpdir = os.path.join(self.autodir, 'tmp')124 job.base_client_job._cleanup_debugdir_files.expect_call()125 job.base_client_job._cleanup_results_dir.expect_call()126 return resultdir127 def construct_job(self):128 # will construct class instance using __new__129 self.job = setup_job.setup_job.__new__(setup_job.setup_job)130 resultdir = self._setup_pre_record_init()131 # finish constructor132 options = dummy()133 options.tag = self.jobtag134 options.log = False135 options.verbose = False136 options.hostname = 'localhost'137 options.user = 'my_user'138 options.output_dir = False139 self.job.__init__(options)140 # check141 self.god.check_playback()142 def get_partition_mock(self, devname):143 """144 Create a mock of a partition object and return it.145 """146 class mock(object):147 device = devname148 get_mountpoint = self.god.create_mock_function('get_mountpoint')149 return mock150 def test_constructor_first_run(self):151 self.construct_job()152 def test_constructor_continuation(self):153 self.construct_job()154 def test_setup_dirs_raise(self):155 self.construct_job()156 # setup157 results_dir = 'foo'158 tmp_dir = 'bar'159 # record160 os.path.exists.expect_call(tmp_dir).and_return(True)161 os.path.isdir.expect_call(tmp_dir).and_return(False)162 # test163 self.assertRaises(ValueError, self.job.setup_dirs, results_dir, tmp_dir)164 self.god.check_playback()165 def test_setup_dirs(self):166 self.construct_job()167 # setup168 results_dir1 = os.path.join(self.job.resultdir, 'build')169 results_dir2 = os.path.join(self.job.resultdir, 'build.2')170 results_dir3 = os.path.join(self.job.resultdir, 'build.3')171 tmp_dir = 'bar'172 # record173 os.path.exists.expect_call(tmp_dir).and_return(False)174 os.mkdir.expect_call(tmp_dir)175 os.path.isdir.expect_call(tmp_dir).and_return(True)176 os.path.exists.expect_call(results_dir1).and_return(True)177 os.path.exists.expect_call(results_dir2).and_return(True)178 os.path.exists.expect_call(results_dir3).and_return(False)179 os.path.exists.expect_call(results_dir3).and_return(False)180 os.mkdir.expect_call(results_dir3)181 # test182 self.assertEqual(self.job.setup_dirs(None, tmp_dir),183 (results_dir3, tmp_dir))184 self.god.check_playback()185if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!