test_ab.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # SPDX-License-Identifier: GPL-2.0
  2. # (C) Copyright 2018 Texas Instruments, <www.ti.com>
  3. # Test A/B update commands.
  4. import os
  5. import pytest
  6. import u_boot_utils
  7. class ABTestDiskImage(object):
  8. """Disk Image used by the A/B tests."""
  9. def __init__(self, u_boot_console):
  10. """Initialize a new ABTestDiskImage object.
  11. Args:
  12. u_boot_console: A U-Boot console.
  13. Returns:
  14. Nothing.
  15. """
  16. filename = 'test_ab_disk_image.bin'
  17. persistent = u_boot_console.config.persistent_data_dir + '/' + filename
  18. self.path = u_boot_console.config.result_dir + '/' + filename
  19. with u_boot_utils.persistent_file_helper(u_boot_console.log, persistent):
  20. if os.path.exists(persistent):
  21. u_boot_console.log.action('Disk image file ' + persistent +
  22. ' already exists')
  23. else:
  24. u_boot_console.log.action('Generating ' + persistent)
  25. fd = os.open(persistent, os.O_RDWR | os.O_CREAT)
  26. os.ftruncate(fd, 524288)
  27. os.close(fd)
  28. cmd = ('sgdisk', persistent)
  29. u_boot_utils.run_and_log(u_boot_console, cmd)
  30. cmd = ('sgdisk', '--new=1:64:512', '--change-name=1:misc',
  31. persistent)
  32. u_boot_utils.run_and_log(u_boot_console, cmd)
  33. cmd = ('sgdisk', '--load-backup=' + persistent)
  34. u_boot_utils.run_and_log(u_boot_console, cmd)
  35. cmd = ('cp', persistent, self.path)
  36. u_boot_utils.run_and_log(u_boot_console, cmd)
  37. di = None
  38. @pytest.fixture(scope='function')
  39. def ab_disk_image(u_boot_console):
  40. global di
  41. if not di:
  42. di = ABTestDiskImage(u_boot_console)
  43. return di
  44. @pytest.mark.boardspec('sandbox')
  45. @pytest.mark.buildconfigspec('android_ab')
  46. @pytest.mark.buildconfigspec('cmd_ab_select')
  47. @pytest.mark.requiredtool('sgdisk')
  48. def test_ab(ab_disk_image, u_boot_console):
  49. """Test the 'ab_select' command."""
  50. u_boot_console.run_command('host bind 0 ' + ab_disk_image.path)
  51. output = u_boot_console.run_command('ab_select slot_name host 0#misc')
  52. assert 're-initializing A/B metadata' in output
  53. assert 'Attempting slot a, tries remaining 7' in output
  54. output = u_boot_console.run_command('printenv slot_name')
  55. assert 'slot_name=a' in output
  56. output = u_boot_console.run_command('ab_select slot_name host 0:1')
  57. assert 'Attempting slot b, tries remaining 7' in output
  58. output = u_boot_console.run_command('printenv slot_name')
  59. assert 'slot_name=b' in output