test_gpt.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. self.path = u_boot_console.config.persistent_data_dir + '/' + filename
  24. if os.path.exists(self.path):
  25. u_boot_console.log.action('Disk image file ' + self.path +
  26. ' already exists')
  27. else:
  28. u_boot_console.log.action('Generating ' + self.path)
  29. fd = os.open(self.path, os.O_RDWR | os.O_CREAT)
  30. os.ftruncate(fd, 4194304)
  31. os.close(fd)
  32. sgdisk = '/sbin/sgdisk'
  33. cmd = (sgdisk, '-U', '375a56f7-d6c9-4e81-b5f0-09d41ca89efe',
  34. self.path)
  35. u_boot_utils.run_and_log(u_boot_console, cmd)
  36. cmd = (sgdisk, '--new=1:2048:2560', self.path)
  37. u_boot_utils.run_and_log(u_boot_console, cmd)
  38. cmd = (sgdisk, '--new=2:4096:4608', self.path)
  39. u_boot_utils.run_and_log(u_boot_console, cmd)
  40. cmd = (sgdisk, '-l', self.path)
  41. u_boot_utils.run_and_log(u_boot_console, cmd)
  42. gtdi = None
  43. @pytest.fixture(scope='function')
  44. def state_disk_image(u_boot_console):
  45. """pytest fixture to provide a GptTestDiskImage object to tests.
  46. This is function-scoped because it uses u_boot_console, which is also
  47. function-scoped. However, we don't need to actually do any function-scope
  48. work, so this simply returns the same object over and over each time."""
  49. global gtdi
  50. if not gtdi:
  51. gtdi = GptTestDiskImage(u_boot_console)
  52. return gtdi
  53. @pytest.mark.boardspec('sandbox')
  54. @pytest.mark.buildconfigspec('cmd_gpt')
  55. def test_gpt_guid(state_disk_image, u_boot_console):
  56. """Test the gpt guid command."""
  57. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  58. output = u_boot_console.run_command('gpt guid host 0')
  59. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  60. @pytest.mark.boardspec('sandbox')
  61. @pytest.mark.buildconfigspec('cmd_gpt')
  62. def test_gpt_save_guid(state_disk_image, u_boot_console):
  63. """Test the gpt guid command to save GUID into a string."""
  64. if u_boot_console.config.buildconfig.get('config_cmd_gpt', 'n') != 'y':
  65. pytest.skip('gpt command not supported')
  66. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  67. output = u_boot_console.run_command('gpt guid host 0 newguid')
  68. output = u_boot_console.run_command('printenv newguid')
  69. assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output
  70. @pytest.mark.boardspec('sandbox')
  71. @pytest.mark.buildconfigspec('cmd_gpt')
  72. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  73. def test_gpt_rename_partition(state_disk_image, u_boot_console):
  74. """Test the gpt rename command to write partition names."""
  75. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  76. u_boot_console.run_command('gpt rename host 0 1 first')
  77. output = u_boot_console.run_command('gpt read host 0')
  78. assert 'name first' in output
  79. u_boot_console.run_command('gpt rename host 0 2 second')
  80. output = u_boot_console.run_command('gpt read host 0')
  81. assert 'name second' in output
  82. @pytest.mark.boardspec('sandbox')
  83. @pytest.mark.buildconfigspec('cmd_gpt')
  84. @pytest.mark.buildconfigspec('cmd_gpt_rename')
  85. @pytest.mark.buildconfigspec('cmd_part')
  86. def test_gpt_swap_partitions(state_disk_image, u_boot_console):
  87. """Test the gpt swap command to exchange two partition names."""
  88. u_boot_console.run_command('host bind 0 ' + state_disk_image.path)
  89. output = u_boot_console.run_command('part list host 0')
  90. assert '0x000007ff "first"' in output
  91. assert '0x000017ff "second"' in output
  92. u_boot_console.run_command('gpt swap host 0 first second')
  93. output = u_boot_console.run_command('part list host 0')
  94. assert '0x000007ff "second"' in output
  95. assert '0x000017ff "first"' in output