test_bind.py 8.1 KB

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