test_bind.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 = ''
  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.buildconfigspec('cmd_bind')
  24. def test_bind_unbind_with_node(u_boot_console):
  25. tree = u_boot_console.run_command('dm tree')
  26. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  27. assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
  28. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  29. #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
  30. response = u_boot_console.run_command('unbind /bind-test/bind-test-child1')
  31. assert response == ''
  32. tree = u_boot_console.run_command('dm tree')
  33. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  34. assert 'bind-test-child1' not in tree
  35. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  36. #bind child #1. No error expected and all devices should be there
  37. response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
  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 in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
  42. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False)
  43. #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
  44. response = u_boot_console.run_command('unbind /bind-test/bind-test-child2')
  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 'bind-test-child2' not in tree
  50. #Bind child #2. No error expected and all devices should be there
  51. response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus')
  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, False)
  56. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  57. #Unbind parent. No error expected. All devices should be removed and unbound
  58. response = u_boot_console.run_command('unbind /bind-test')
  59. assert response == ''
  60. tree = u_boot_console.run_command('dm tree')
  61. assert 'bind-test' not in tree
  62. assert 'bind-test-child1' not in tree
  63. assert 'bind-test-child2' not in tree
  64. #try binding invalid node with valid driver
  65. response = u_boot_console.run_command('bind /not-a-valid-node simple_bus')
  66. assert response != ''
  67. tree = u_boot_console.run_command('dm tree')
  68. assert 'not-a-valid-node' not in tree
  69. #try binding valid node with invalid driver
  70. response = u_boot_console.run_command('bind /bind-test not_a_driver')
  71. assert response != ''
  72. tree = u_boot_console.run_command('dm tree')
  73. assert 'bind-test' not in tree
  74. #bind /bind-test. Device should come up as well as its children
  75. response = u_boot_console.run_command('bind /bind-test simple_bus')
  76. assert response == ''
  77. tree = u_boot_console.run_command('dm tree')
  78. assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
  79. assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
  80. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  81. response = u_boot_console.run_command('unbind /bind-test')
  82. assert response == ''
  83. def get_next_line(tree, name):
  84. treelines = [x.strip() for x in tree.splitlines() if x.strip()]
  85. child_line = ''
  86. for idx, line in enumerate(treelines):
  87. if ('-- ' + name) in line:
  88. try:
  89. child_line = treelines[idx+1]
  90. except:
  91. pass
  92. break
  93. return child_line
  94. @pytest.mark.buildconfigspec('cmd_bind')
  95. def test_bind_unbind_with_uclass(u_boot_console):
  96. #bind /bind-test
  97. response = u_boot_console.run_command('bind /bind-test simple_bus')
  98. assert response == ''
  99. #make sure bind-test-child2 is there and get its uclass/index pair
  100. tree = u_boot_console.run_command('dm tree')
  101. child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
  102. assert len(child2_line) == 1
  103. child2_uclass = child2_line[0].split()[0]
  104. child2_index = int(child2_line[0].split()[1])
  105. #bind simple_bus as a child of bind-test-child2
  106. response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  107. #check that the child is there and its uclass/index pair is right
  108. tree = u_boot_console.run_command('dm tree')
  109. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  110. assert child_of_child2_line
  111. child_of_child2_index = int(child_of_child2_line.split()[1])
  112. assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
  113. assert child_of_child2_index == child2_index + 1
  114. #unbind the child and check it has been removed
  115. response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index))
  116. assert response == ''
  117. tree = u_boot_console.run_command('dm tree')
  118. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  119. assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
  120. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  121. assert child_of_child2_line == ''
  122. #bind simple_bus as a child of bind-test-child2
  123. response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  124. #check that the child is there and its uclass/index pair is right
  125. tree = u_boot_console.run_command('dm tree')
  126. treelines = [x.strip() for x in tree.splitlines() if x.strip()]
  127. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  128. assert child_of_child2_line
  129. child_of_child2_index = int(child_of_child2_line.split()[1])
  130. assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
  131. assert child_of_child2_index == child2_index + 1
  132. #unbind the child and check it has been removed
  133. response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  134. assert response == ''
  135. tree = u_boot_console.run_command('dm tree')
  136. assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
  137. child_of_child2_line = get_next_line(tree, 'bind-test-child2')
  138. assert child_of_child2_line == ''
  139. #unbind the child again and check it doesn't change the tree
  140. tree_old = u_boot_console.run_command('dm tree')
  141. response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
  142. tree_new = u_boot_console.run_command('dm tree')
  143. assert response == ''
  144. assert tree_old == tree_new
  145. response = u_boot_console.run_command('unbind /bind-test')
  146. assert response == ''