Best Python code snippet using autotest_python
boottool_unittest.py
Source:boottool_unittest.py  
1#!/usr/bin/python2import logging3import os4import sys5import unittest6try:7    import autotest.common as common  # pylint: disable=W06118except ImportError:9    import common  # pylint: disable=W061110from autotest.client.shared.test_utils import mock11from autotest.client.shared import boottool12from autotest.client.tools import boottool as boot_tool13class TestEfiSys(unittest.TestCase):14    def setUp(self):15        self.god = mock.mock_god()16    def tearDown(self):17        self.god.unstub_all()18    def test_efi_path_not_found(self):19        self.god.stub_function(os.path, 'exists')20        self.god.stub_function(sys, 'exit')21        os.path.exists.expect_call('/sys/firmware/efi/vars').and_return(False)22        sys.exit.expect_call(-1)23        # Run24        self.efi_tool = boot_tool.EfiToolSys()25        self.god.check_playback()26class TestBoottool(unittest.TestCase):27    def setUp(self):28        self.god = mock.mock_god()29        # creates a bootloader with _run_boottool mocked out30        self.bt_mock = boottool.boottool()31        self.god.stub_function(self.bt_mock, '_run_grubby_get_return')32        self.god.stub_function(self.bt_mock, '_run_grubby_get_output')33        self.god.stub_function(self.bt_mock, '_run_get_output_err')34        self.god.stub_function(self.bt_mock, '_get_entry_selection')35        self.god.stub_function(self.bt_mock, 'get_info')36        self.god.stub_function(self.bt_mock, 'get_info_lines')37    def tearDown(self):38        self.god.unstub_all()39    def test_get_bootloader(self):40        # set up the recording41        args = [self.bt_mock.path, '--bootloader-probe']42        self.bt_mock._run_get_output_err.expect_call(args).and_return('lilo')43        # run the test44        self.assertEquals(self.bt_mock.get_bootloader(), 'lilo')45        self.god.check_playback()46    def test_get_architecture(self):47        self.god.stub_function(os, 'uname')48        # set up the recording49        os.uname.expect_call().and_return(('Linux', 'foobar.local',50                                           '3.2.7-1.fc16.x86_64',51                                           '#1 SMP Tue Feb 21 01:40:47 UTC 2012',52                                           'x86_64'))53        # run the test54        self.assertEquals(self.bt_mock.get_architecture(), 'x86_64')55        self.god.check_playback()56    def test_get_default_index(self):57        # set up the recording58        self.bt_mock._run_grubby_get_output.expect_call(['--default-index']).and_return(0)59        # run the test60        self.assertEquals(self.bt_mock.get_default_index(), 0)61        self.god.check_playback()62    def test_get_titles(self):63        # set up the recording64        output = ['index=0', 'title=title #1', 'index=1', 'title=title #2']65        self.bt_mock.get_info_lines.expect_call().and_return(output)66        # run the test67        self.assertEquals(self.bt_mock.get_titles(),68                          ['title #1', 'title #2'])69        self.god.check_playback()70    def test_get_entry(self):71        index = 572        RESULT = ("""73index=%s74title="Fedora 16, kernel 3.2.6-3"75kernel=/vmlinuz-3.2.6-3.fc16.x86_6476args="ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"77root=/dev/mapper/vg_foo-lv_root78initrd=/boot/initramfs-3.2.6-3.fc16.x86_64.img79""" % index)80        # set up the recording81        self.bt_mock.get_info.expect_call(index).and_return(RESULT)82        actual_info = self.bt_mock.get_entry(index)83        expected_info = {'index': index,84                         'args': '"ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"',85                         'initrd': '/boot/initramfs-3.2.6-3.fc16.x86_64.img',86                         'kernel': '/vmlinuz-3.2.6-3.fc16.x86_64',87                         'root': '/dev/mapper/vg_foo-lv_root',88                         'title': '"Fedora 16, kernel 3.2.6-3"'}89        self.assertEquals(expected_info, actual_info)90    def test_get_entry_missing_result(self):91        index = 492        RESULT = """93"""94        # set up the recording95        self.bt_mock.get_info.expect_call(index).and_return(RESULT)96        actual_info = self.bt_mock.get_entry(index)97        expected_info = {}98        self.assertEquals(expected_info, actual_info)99    def test_get_entries(self):100        self.god.stub_function(self.bt_mock, '_get_entry_indexes')101        entry_0 = '''102index=0103kernel=/vmlinuz-3.2.9-1.fc16.x86_64104args="ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"105root=/dev/mapper/vg_freedom-lv_root106initrd=/boot/initramfs-3.2.9-1.fc16.x86_64.img107'''108        entry_1 = '''109index=1110kernel=/vmlinuz-3.2.7-1.fc16.x86_64111args="ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"112root=/dev/mapper/vg_freedom-lv_root113initrd=/boot/initramfs-3.2.7-1.fc16.x86_64.img114'''115        entry_2 = '''116index=2117kernel=/vmlinuz-3.2.6-3.fc16.x86_64118args="ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"119root=/dev/mapper/vg_freedom-lv_root120initrd=/boot/initramfs-3.2.6-3.fc16.x86_64.img121'''122        RESULT = entry_0 + entry_1 + entry_2123        entry_indexes = [0, 1, 2]124        # run the test125        self.bt_mock.get_info.expect_call().and_return(RESULT)126        actual_info = self.bt_mock.get_entries()127        expected_info = {0: {'args': '"ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"',128                             'index': 0,129                             'initrd': '/boot/initramfs-3.2.9-1.fc16.x86_64.img',130                             'kernel': '/vmlinuz-3.2.9-1.fc16.x86_64',131                             'root': '/dev/mapper/vg_freedom-lv_root'},132                         1: {'args': '"ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"',133                             'index': 1,134                             'initrd': '/boot/initramfs-3.2.7-1.fc16.x86_64.img',135                             'kernel': '/vmlinuz-3.2.7-1.fc16.x86_64',136                             'root': '/dev/mapper/vg_freedom-lv_root'},137                         2: {'args': '"ro quiet rhgb SYSFONT=latarcyrheb-sun16 LANG=en_US.UTF-8 KEYTABLE=us-acentos"',138                             'index': 2,139                             'initrd': '/boot/initramfs-3.2.6-3.fc16.x86_64.img',140                             'kernel': '/vmlinuz-3.2.6-3.fc16.x86_64',141                             'root': '/dev/mapper/vg_freedom-lv_root'}}142        self.assertEquals(expected_info, actual_info)143        self.god.check_playback()144    def test_set_default(self):145        pass146    def test_add_args(self):147        # set up the recording148        kernel = 10149        args = "some kernel args"150        self.bt_mock._get_entry_selection.expect_call(kernel).and_return(kernel)151        command_arguments = ['--update-kernel=%s' % kernel,152                             '--args=%s' % args]153        self.bt_mock._run_grubby_get_return.expect_call(command_arguments)154        # run the test155        self.bt_mock.add_args(kernel, args)156        self.god.check_playback()157    def test_remove_args(self):158        # set up the recording159        kernel = 12160        args = "some kernel args"161        self.bt_mock._get_entry_selection.expect_call(kernel).and_return(kernel)162        command_arguments = ['--update-kernel=%s' % kernel,163                             '--remove-args=%s' % args]164        self.bt_mock._run_grubby_get_return.expect_call(command_arguments)165        # run the test166        self.bt_mock.remove_args(kernel, args)167        self.god.check_playback()168    def setup_add_kernel(self, oldtitle, path, title, root=None, args=None,169                         initrd=None, default=False, position='end'):170        self.bt_mock.get_titles = self.god.create_mock_function('get_titles')171        self.bt_mock.remove_kernel = self.god.create_mock_function('remove_kernel')172        # set up the recording173        self.bt_mock.get_titles.expect_call().and_return([oldtitle])174        if oldtitle == title:175            self.bt_mock.remove_kernel.expect_call(title)176        parameters = ['--add-kernel=%s' % path, '--title=%s' % title]177        # FIXME: grubby takes no --root parameter178        # if root:179        #    parameters.append('--root=%s' % root)180        if args:181            parameters.append('--args=%s' %182                              self.bt_mock._remove_duplicate_cmdline_args(args))183        if initrd:184            parameters.append('--initrd=%s' % initrd)185        if default:186            parameters.append('--make-default')187        # There's currently an issue with grubby '--add-to-bottom' feature.188        # Because it uses the tail instead of the head of the list to add189        # a new entry, when copying a default entry as a template190        # (--copy-default), it usually copies the "recover" entries that191        # usually go along a regular boot entry, specially on grub2.192        #193        # So, for now, until I fix grubby, we'll *not* respect the position194        # (--position=end) command line option.195        # if position:196        #    parameters.append('--position=%s' % position)197        parameters.append("--copy-default")198        self.bt_mock._run_grubby_get_return.expect_call(parameters)199    def test_add_kernel_basic(self):200        # set up the recording201        self.setup_add_kernel(oldtitle='notmylabel',202                              path='/unittest/kernels/vmlinuz', title='mylabel')203        # run the test204        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',205                                title='mylabel')206        self.god.check_playback()207    def test_add_kernel_removes_old(self):208        # set up the recording209        self.setup_add_kernel(oldtitle='mylabel',210                              path='/unittest/kernels/vmlinuz', title='mylabel')211        # run the test212        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',213                                title='mylabel')214        self.god.check_playback()215    def test_add_kernel_adds_root(self):216        # set up the recording217        self.setup_add_kernel(oldtitle='notmylabel',218                              path='/unittest/kernels/vmlinuz', title='mylabel',219                              root='/unittest/root')220        # run the test221        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',222                                title='mylabel', root='/unittest/root')223        self.god.check_playback()224    def test_add_kernel_adds_args(self):225        # set up the recording226        self.setup_add_kernel(oldtitle='notmylabel',227                              path='/unittest/kernels/vmlinuz', title='mylabel',228                              args='my kernel args')229        # run the test230        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',231                                title='mylabel', args='my kernel args')232        self.god.check_playback()233    def test_add_kernel_args_remove_duplicates(self):234        # set up the recording235        self.setup_add_kernel(oldtitle='notmylabel',236                              path='/unittest/kernels/vmlinuz', title='mylabel',237                              args='param2 param1')238        # run the test239        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',240                                title='mylabel', args='param1 param2 param1')241        self.god.check_playback()242    def test_add_kernel_adds_initrd(self):243        # set up the recording244        self.setup_add_kernel(oldtitle='notmylabel',245                              path='/unittest/kernels/vmlinuz', title='mylabel',246                              initrd='/unittest/initrd')247        # run the test248        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',249                                title='mylabel', initrd='/unittest/initrd')250        self.god.check_playback()251    def test_add_kernel_enables_make_default(self):252        # set up the recording253        self.setup_add_kernel(oldtitle='notmylabel',254                              path='/unittest/kernels/vmlinuz', title='mylabel',255                              default=True)256        # run the test257        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',258                                title='mylabel', default=True)259        self.god.check_playback()260    def test_add_kernel_position(self):261        # set up the recording262        self.setup_add_kernel(oldtitle='notmylabel',263                              path='/unittest/kernels/vmlinuz', title='mylabel',264                              position=5)265        # run the test266        self.bt_mock.add_kernel(path='/unittest/kernels/vmlinuz',267                                title='mylabel', position=5)268        self.god.check_playback()269    def test_remove_kernel(self):270        index = 14271        # set up the recording272        self.bt_mock._get_entry_selection.expect_call(index).and_return(index)273        command_arguments = ['--remove-kernel=%s' % index]274        self.bt_mock._run_grubby_get_return.expect_call(command_arguments)275        # run the test276        self.bt_mock.remove_kernel(index)277        self.god.check_playback()278    def test_boot_once(self):279        self.god.stub_function(self.bt_mock, 'get_bootloader')280        self.god.stub_function(self.bt_mock, '_index_for_title')281        self.god.stub_function(self.bt_mock, 'get_default_title')282        self.god.stub_function(boottool, 'install_grubby_if_missing')283        # set up the recording284        title = 'autotest'285        entry_index = 1286        default_title = 'linux'287        default_index = 0288        info_lines = ['index=%s' % default_index, 'title=%s' % default_title,289                      'index=%s' % entry_index, 'title=%s' % title]290        bootloaders = ('grub2', 'grub', 'yaboot', 'elilo')291        for bootloader in bootloaders:292            self.god.stub_function(self.bt_mock, 'boot_once_%s' % bootloader)293            self.god.stub_function(self.bt_mock, '_init_on_demand')294            self.bt_mock.log = logging295            self.bt_mock.get_info_lines.expect_call().and_return(info_lines)296            self.bt_mock.get_default_title.expect_call().and_return(default_title)297            self.bt_mock.get_bootloader.expect_call().and_return(bootloader)298            if bootloader in ('grub', 'grub2', 'elilo'):299                self.bt_mock._index_for_title.expect_call(title).and_return(entry_index)300            bootloader_func = getattr(self.bt_mock, 'boot_once_%s' % bootloader)301            if bootloader in ('yaboot'):302                arg = title303            else:304                arg = entry_index305            bootloader_func.expect_call(arg)306            # run the test307            self.bt_mock.boot_once('autotest')308            self.god.check_playback()309if __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!!
