test_bind.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  3. import os.path
  4. import pytest
  5. import re
  6. def in_tree(response, name, uclass, drv, depth, last_child):
  7. lines = [x.strip() for x in response.splitlines()]
  8. leaf = ' ' * 4 * depth;
  9. if not last_child:
  10. leaf = leaf + r'\|'
  11. else:
  12. leaf = leaf + '`'
  13. leaf = leaf + '-- ' + name
  14. line = (r' *{:10.10} [0-9]* \[ [ +] \] {:20.20} {}$'
  15. .format(uclass, drv, leaf))
  16. prog = re.compile(line)
  17. for l in lines:
  18. if prog.match(l):
  19. return True
  20. return False
  21. @pytest.mark.buildconfigspec('cmd_bind')
  22. def test_bind_unbind_with_node(u_boot_console):
  23. tree = u_boot_console.run_command('dm tree')
  24. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  25. assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
  26. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  27. #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
  28. response = u_boot_console.run_command('unbind /bind-test/bind-test-child1')
  29. assert response == ''
  30. tree = u_boot_console.run_command('dm tree')
  31. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  32. assert 'bind-test-child1' not in tree
  33. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  34. #bind child #1. No error expected and all devices should be there
  35. response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
  36. assert response == ''
  37. tree = u_boot_console.run_command('dm tree')
  38. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  39. assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
  40. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False)
  41. #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
  42. response = u_boot_console.run_command('unbind /bind-test/bind-test-child2')
  43. assert response == ''
  44. tree = u_boot_console.run_command('dm tree')
  45. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  46. assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
  47. assert 'bind-test-child2' not in tree
  48. #Bind child #2. No error expected and all devices should be there
  49. response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus')
  50. assert response == ''
  51. tree = u_boot_console.run_command('dm tree')
  52. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  53. assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
  54. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  55. #Unbind parent. No error expected. All devices should be removed and unbound
  56. response = u_boot_console.run_command('unbind /bind-test')
  57. assert response == ''
  58. tree = u_boot_console.run_command('dm tree')
  59. assert 'bind-test' not in tree
  60. assert 'bind-test-child1' not in tree
  61. assert 'bind-test-child2' not in tree
  62. #try binding invalid node with valid driver
  63. response = u_boot_console.run_command('bind /not-a-valid-node simple_bus')
  64. assert response != ''
  65. tree = u_boot_console.run_command('dm tree')
  66. assert 'not-a-valid-node' not in tree
  67. #try binding valid node with invalid driver
  68. response = u_boot_console.run_command('bind /bind-test not_a_driver')
  69. assert response != ''
  70. tree = u_boot_console.run_command('dm tree')
  71. assert 'bind-test' not in tree
  72. #bind /bind-test. Device should come up as well as its children
  73. response = u_boot_console.run_command('bind /bind-test simple_bus')
  74. assert response == ''
  75. tree = u_boot_console.run_command('dm tree')
  76. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  77. assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
  78. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  79. response = u_boot_console.run_command('unbind /bind-test')
  80. assert response == ''
  81. def get_next_line(tree, name):
  82. treelines = [x.strip() for x in tree.splitlines() if x.strip()]
  83. child_line = ''
  84. for idx, line in enumerate(treelines):
  85. if ('-- ' + name) in line:
  86. try:
  87. child_line = treelines[idx+1]
  88. except:
  89. pass
  90. break
  91. return child_line
  92. @pytest.mark.buildconfigspec('cmd_bind')
  93. def test_bind_unbind_with_uclass(u_boot_console):
  94. #bind /bind-test
  95. response = u_boot_console.run_command('bind /bind-test simple_bus')
  96. assert response == ''
  97. #make sure bind-test-child2 is there and get its uclass/index pair
  98. tree = u_boot_console.run_command('dm tree')
  99. child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
  100. assert len(child2_line) == 1
  101. child2_uclass = child2_line[0].split()[0]
  102. child2_index = int(child2_line[0].split()[1])
  103. #bind simple_bus as a child of bind-test-child2
  104. response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  105. #check that the child is there and its uclass/index pair is right
  106. tree = u_boot_console.run_command('dm tree')
  107. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  108. assert child_of_child2_line
  109. child_of_child2_index = int(child_of_child2_line.split()[1])
  110. assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
  111. assert child_of_child2_index == child2_index + 1
  112. #unbind the child and check it has been removed
  113. response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index))
  114. assert response == ''
  115. tree = u_boot_console.run_command('dm tree')
  116. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  117. assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
  118. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  119. assert child_of_child2_line == ''
  120. #bind simple_bus as a child of bind-test-child2
  121. response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  122. #check that the child is there and its uclass/index pair is right
  123. tree = u_boot_console.run_command('dm tree')
  124. treelines = [x.strip() for x in tree.splitlines() if x.strip()]
  125. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  126. assert child_of_child2_line
  127. child_of_child2_index = int(child_of_child2_line.split()[1])
  128. assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
  129. assert child_of_child2_index == child2_index + 1
  130. #unbind the child and check it has been removed
  131. response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  132. assert response == ''
  133. tree = u_boot_console.run_command('dm tree')
  134. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  135. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  136. assert child_of_child2_line == ''
  137. #unbind the child again and check it doesn't change the tree
  138. tree_old = u_boot_console.run_command('dm tree')
  139. response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  140. tree_new = u_boot_console.run_command('dm tree')
  141. assert response == ''
  142. assert tree_old == tree_new
  143. response = u_boot_console.run_command('unbind /bind-test')
  144. assert response == ''