test_gpt.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright (c) 2017 Alison Chaiken
  2. # Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: GPL-2.0
  5. # Test GPT manipulation commands.
  6. import os
  7. import pytest
  8. import u_boot_utils
  9. """
  10. These tests rely on a 4 MB disk image, which is automatically created by
  11. the test.
  12. """
  13. class GptTestDiskImage(object):
  14. """Disk Image used by the GPT tests."""
  15. def __init__(self, u_boot_console):
  16. """Initialize a new GptTestDiskImage object.
  17. Args:
  18. u_boot_console: A U-Boot console.
  19. Returns:
  20. Nothing.
  21. """
  22. filename = 'test_gpt_disk_image.bin'
  23. persistent = u_boot_console.config.persistent_data_dir + '/' + filename
  24. self.path = u_boot_console.config.result_dir + '/' + filename
  25. if os.path.exists(persistent):
  26. u_boot_console.log.action('Disk image file ' + persistent +
  27. ' already exists')
  28. else:
  29. u_boot_console.log.action('Generating ' + persistent)
  30. fd = os.open(persistent, os.O_RDWR | os.O_CREAT)
  31. os.ftruncate(fd, 4194304)
  32. os.close(fd)
  33. cmd = ('sgdisk', '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
  34. persistent)
  35. u_boot_utils.run_and_log(u_boot_console, cmd)
  36. cmd = ('sgdisk', '--new=1:2048:2560', persistent)
  37. u_boot_utils.run_and_log(u_boot_console, cmd)
  38. cmd = ('sgdisk', '--new=2:4096:4608', persistent)
  39. u_boot_utils.run_and_log(u_boot_console, cmd)
  40. cmd = ('sgdisk', '-l', persistent)
  41. u_boot_utils.run_and_log(u_boot_console, cmd)
  42. cmd = ('cp', persistent, self.path)
  43. u_boot_utils.run_and_log(u_boot_console, cmd)
  44. gtdi = None
  45. @pytest.fixture(scope='function')
  46. def state_disk_image(u_boot_console):
  47. """pytest fixture to provide a GptTestDiskImage object to tests.
  48. This is function-scoped because it uses u_boot_console, which is also
  49. function-scoped. However, we don't need to actually do any function-scope
  50. work, so this simply returns the same object over and over each time."""
  51. global gtdi
  52. if not gtdi:
  53. gtdi = GptTestDiskImage(u_boot_console)
  54. return gtdi
  55. @pytest.mark.boardspec('sandbox')
  56. @pytest.mark.buildconfigspec('cmd_gpt')
  57. @pytest.mark.requiredtool('sgdisk')
  58. def test_gpt_guid(state_disk_image, u_boot_console):
  59. """Test the gpt guid command."""
  60. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  61. output = u_boot_console.run_command('gpt guid host 0')
  62. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  63. @pytest.mark.boardspec('sandbox')
  64. @pytest.mark.buildconfigspec('cmd_gpt')
  65. @pytest.mark.requiredtool('sgdisk')
  66. def test_gpt_save_guid(state_disk_image, u_boot_console):
  67. """Test the gpt guid command to save GUID into a string."""
  68. if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
  69. pytest.skip('gpt command not supported')
  70. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  71. output = u_boot_console.run_command('gpt guid host 0 newguid')
  72. output = u_boot_console.run_command('printenv newguid')
  73. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  74. @pytest.mark.boardspec('sandbox')
  75. @pytest.mark.buildconfigspec('cmd_gpt')
  76. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  77. @pytest.mark.requiredtool('sgdisk')
  78. def test_gpt_rename_partition(state_disk_image, u_boot_console):
  79. """Test the gpt rename command to write partition names."""
  80. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  81. u_boot_console.run_command('gpt rename host 0 1 first')
  82. output = u_boot_console.run_command('gpt read host 0')
  83. assert 'name first' in output
  84. u_boot_console.run_command('gpt rename host 0 2 second')
  85. output = u_boot_console.run_command('gpt read host 0')
  86. assert 'name second' in output
  87. @pytest.mark.boardspec('sandbox')
  88. @pytest.mark.buildconfigspec('cmd_gpt')
  89. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  90. @pytest.mark.buildconfigspec('cmd_part')
  91. @pytest.mark.requiredtool('sgdisk')
  92. def test_gpt_swap_partitions(state_disk_image, u_boot_console):
  93. """Test the gpt swap command to exchange two partition names."""
  94. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  95. output = u_boot_console.run_command('part list host 0')
  96. assert '0x000007ff "first"' in output
  97. assert '0x000017ff "second"' in output
  98. u_boot_console.run_command('gpt swap host 0 first second')
  99. output = u_boot_console.run_command('part list host 0')
  100. assert '0x000007ff "second"' in output
  101. assert '0x000017ff "first"' in output