sandbox: dts: Add compatible string for bind-test node
[platform/kernel/u-boot.git] / test / py / tests / test_bind.py
1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3
4 import os.path
5 import pytest
6 import re
7
8 def in_tree(response, name, uclass, drv, depth, last_child):
9         lines = [x.strip() for x in response.splitlines()]
10         leaf = ' ' * 4 * depth;
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
24
25 @pytest.mark.buildconfigspec('cmd_bind')
26 def test_bind_unbind_with_node(u_boot_console):
27
28         tree = u_boot_console.run_command('dm tree')
29         assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
30         assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
31         assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
32
33         #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
34         response = u_boot_console.run_command('unbind  /bind-test/bind-test-child1')
35         assert response == ''
36         tree = u_boot_console.run_command('dm tree')
37         assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
38         assert 'bind-test-child1' not in tree
39         assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
40
41         #bind child #1. No error expected and all devices should be there
42         response = u_boot_console.run_command('bind  /bind-test/bind-test-child1 phy_sandbox')
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 in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False)
48
49         #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
50         response = u_boot_console.run_command('unbind  /bind-test/bind-test-child2')
51         assert response == ''
52         tree = u_boot_console.run_command('dm tree')
53         assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
54         assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
55         assert 'bind-test-child2' not in tree
56
57
58         #Bind child #2. No error expected and all devices should be there
59         response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus')
60         assert response == ''
61         tree = u_boot_console.run_command('dm tree')
62         assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
63         assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
64         assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
65
66         #Unbind parent. No error expected. All devices should be removed and unbound
67         response = u_boot_console.run_command('unbind  /bind-test')
68         assert response == ''
69         tree = u_boot_console.run_command('dm tree')
70         assert 'bind-test' not in tree
71         assert 'bind-test-child1' not in tree
72         assert 'bind-test-child2' not in tree
73
74         #try binding invalid node with valid driver
75         response = u_boot_console.run_command('bind  /not-a-valid-node simple_bus')
76         assert response != ''
77         tree = u_boot_console.run_command('dm tree')
78         assert 'not-a-valid-node' not in tree
79
80         #try binding valid node with invalid driver
81         response = u_boot_console.run_command('bind  /bind-test not_a_driver')
82         assert response != ''
83         tree = u_boot_console.run_command('dm tree')
84         assert 'bind-test' not in tree
85
86         #bind /bind-test. Device should come up as well as its children
87         response = u_boot_console.run_command('bind  /bind-test simple_bus')
88         assert response == ''
89         tree = u_boot_console.run_command('dm tree')
90         assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
91         assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
92         assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
93
94         response = u_boot_console.run_command('unbind  /bind-test')
95         assert response == ''
96
97 def get_next_line(tree, name):
98         treelines = [x.strip() for x in tree.splitlines() if x.strip()]
99         child_line = ''
100         for idx, line in enumerate(treelines):
101                 if ('-- ' + name) in line:
102                         try:
103                                 child_line = treelines[idx+1]
104                         except:
105                                 pass
106                         break
107         return child_line
108
109 @pytest.mark.buildconfigspec('cmd_bind')
110 def test_bind_unbind_with_uclass(u_boot_console):
111         #bind /bind-test
112         response = u_boot_console.run_command('bind  /bind-test simple_bus')
113         assert response == ''
114
115         #make sure bind-test-child2 is there and get its uclass/index pair
116         tree = u_boot_console.run_command('dm tree')
117         child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
118         assert len(child2_line) == 1
119
120         child2_uclass = child2_line[0].split()[0]
121         child2_index = int(child2_line[0].split()[1])
122
123         #bind simple_bus as a child of bind-test-child2
124         response = u_boot_console.run_command('bind  {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
125
126         #check that the child is there and its uclass/index pair is right
127         tree = u_boot_console.run_command('dm tree')
128
129         child_of_child2_line = get_next_line(tree, 'bind-test-child2')
130         assert child_of_child2_line
131         child_of_child2_index = int(child_of_child2_line.split()[1])
132         assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
133         assert child_of_child2_index == child2_index + 1
134
135         #unbind the child and check it has been removed
136         response = u_boot_console.run_command('unbind  simple_bus {}'.format(child_of_child2_index))
137         assert response == ''
138         tree = u_boot_console.run_command('dm tree')
139         assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
140         assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
141         child_of_child2_line = get_next_line(tree, 'bind-test-child2')
142         assert child_of_child2_line == ''
143
144         #bind simple_bus as a child of bind-test-child2
145         response = u_boot_console.run_command('bind  {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
146
147         #check that the child is there and its uclass/index pair is right
148         tree = u_boot_console.run_command('dm tree')
149         treelines = [x.strip() for x in tree.splitlines() if x.strip()]
150
151         child_of_child2_line = get_next_line(tree, 'bind-test-child2')
152         assert child_of_child2_line
153         child_of_child2_index = int(child_of_child2_line.split()[1])
154         assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
155         assert child_of_child2_index == child2_index + 1
156
157         #unbind the child and check it has been removed
158         response = u_boot_console.run_command('unbind  {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
159         assert response == ''
160
161         tree = u_boot_console.run_command('dm tree')
162         assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
163
164         child_of_child2_line = get_next_line(tree, 'bind-test-child2')
165         assert child_of_child2_line == ''
166
167         #unbind the child again and check it doesn't change the tree
168         tree_old = u_boot_console.run_command('dm tree')
169         response = u_boot_console.run_command('unbind  {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
170         tree_new = u_boot_console.run_command('dm tree')
171
172         assert response == ''
173         assert tree_old == tree_new
174
175         response = u_boot_console.run_command('unbind  /bind-test')
176         assert response == ''