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