-
Notifications
You must be signed in to change notification settings - Fork 115
/
index.js
262 lines (238 loc) · 7.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const request = require('request-promise');
const SUPERVISOR_PORT = 48484;
const fs = require('fs');
const retry = require('bluebird-retry');
module.exports = {
title: 'Device Tree tests',
deviceType: {
type: 'object',
required: ['slug'],
properties: {
slug: {
type: 'string',
enum: [
'raspberrypi3',
'raspberrypi3-64',
'raspberrypi4-64',
'raspberry-pi2',
'raspberry-pi',
],
},
},
},
tests: [
{
title: 'DToverlay & DTparam tests',
run: async function (test) {
let ip = await this.worker.ip(this.link);
let targetState
// Export the GPIO pin
const exportPin = async () => {
return await this.worker.executeCommandInHostOS(
`echo 4 >/sys/class/gpio/export`,
this.link
)
}
// Check value of GPIO pin and unexport the GPIO pin
const getPinValue = async () => {
const pinValue = await this.worker.executeCommandInHostOS(
`cat /sys/class/gpio/gpio4/value`,
this.link
)
await this.worker.executeCommandInHostOS(
`echo 4 >/sys/class/gpio/unexport`,
this.link
)
return pinValue
}
// After applying Dtoverlay, the GPIO pins becomes unavailable as drivers take control over the pin
// Hence, sysfs can't be used to query the value of the GPIO pin hence the user of /sys/kernel/debug/gpio
const getPinValueThroughDebug = async () => {
const getValue = fs.readFileSync(`${__dirname}/getValue.sh`).toString();
return await this.worker.executeCommandInHostOS(
`cd /tmp && ${getValue}`,
this.link,
);
}
const applySupervisorConfig = async (direction) => {
// Wait for supervisor API to start
await this.utils.waitUntil(async () => {
return (
(await request({
method: 'GET',
uri: `http://${ip}:${SUPERVISOR_PORT}/ping`,
})) === 'OK'
);
}, false);
targetState = {
local: {
name: 'local',
config: {
HOST_CONFIG_dtoverlay: `"gpio-key,gpio=4,active_low=0,gpio_pull=${direction}"`,
HOST_CONFIG_dtparam:
'"i2c_arm=on","spi=on","audio=on","foo=bar","level=42"',
HOST_CONFIG_gpu_mem: '64',
SUPERVISOR_PERSISTENT_LOGGING: 'true',
SUPERVISOR_LOCAL_MODE: 'true',
},
apps: {},
},
dependent: {
apps: [],
devices: [],
},
};
await this.context
.get()
.worker.executeCommandInHostOS(
'touch /tmp/reboot-check',
this.link,
);
// Setting the device tree variables using Supervisor API
// This request reboots the DUT automatically
const setTargetState = await request({
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
json: true,
body: targetState,
uri: `http://${ip}:${SUPERVISOR_PORT}/v2/local/target-state`,
});
test.same(
setTargetState,
{ status: 'success', message: 'OK' },
'DToverlay & DTparam configured successfully',
);
await this.utils.waitUntil(async () => {
test.comment('Waiting for DUT to come back online after reboot...');
return (
(await this.context
.get()
.worker.executeCommandInHostOS(
'[[ ! -f /tmp/reboot-check ]] && echo "pass"',
this.link,
)) === 'pass'
);
}, false);
// IP of the device sometimes change after reboots, hence initalising again
// Commenting this to check if the IP really changes or not after reboot
// If it does, then it's a bug, because the IP shouldn't change.
// If not, then remove the snippet
// Leviathan issue: https://github.com/balena-os/leviathan/issues/781
// ip = await this.worker.ip(this.link);
await this.utils.waitUntil(async () => {
test.comment('Waiting for supervisor to be ready after reboot...');
return (
(await request({
method: 'GET',
uri: `http://${ip}:${SUPERVISOR_PORT}/ping`,
})) === 'OK'
);
}, false);
return targetState
}
// Start of the device-tree practical test
await exportPin(4)
if (await getPinValue(4) === "0") {
test.true(true, "Pin 4 was Low when the test started")
const targetState = await applySupervisorConfig("up")
test.equal(await getPinValueThroughDebug(4), '"hi"', "Pin 4 set to High after applying dtoverlay")
} else {
test.true(true, "Pin 4 is High as expected")
const targetState = await applySupervisorConfig("down")
test.equal(await getPinValueThroughDebug(4), '"lo"', "Pin 4 set to Low after applying dtoverlay")
}
// Get the current target state of device
const currentState = await request({
method: 'GET',
uri: `http://${ip}:${SUPERVISOR_PORT}/v2/local/target-state`,
json: true,
});
// Making sure currentState of the DUT matches the target state that was being set.
test.equal(
currentState.state.local.config.HOST_CONFIG_dtoverlay,
targetState.local.config.HOST_CONFIG_dtoverlay,
'DToverlay successfully set in target state',
);
test.equal(
currentState.state.local.config.HOST_CONFIG_dtparam,
targetState.local.config.HOST_CONFIG_dtparam,
'DTparam successfully set in target state',
);
const dtoverlay = fs.readFileSync(`${__dirname}/dtoverlay.sh`).toString();
const dtparam = fs.readFileSync(`${__dirname}/dtparam.sh`).toString();
const dtOverlayConfigTxt = await this.context
.get()
.worker.executeCommandInHostOS(
`cd /tmp && ${dtoverlay}`,
this.link,
);
test.equal(
dtOverlayConfigTxt,
targetState.local.config.HOST_CONFIG_dtoverlay,
'DToverlays successfully configured in config.txt',
);
const dtParamConfigTxt = await this.context
.get()
.worker.executeCommandInHostOS(
`cd /tmp && ${dtparam}`,
this.link,
);
test.equal(
dtParamConfigTxt,
targetState.local.config.HOST_CONFIG_dtparam,
'DTparams successfully configured in config.txt',
);
/* Static binary currently shared by RPI maintainer
* See: https://github.com/raspberrypi/Raspberry-Pi-OS-64bit/issues/67#issuecomment-653209729
* and https://github.com/raspberrypi/utils/issues/70#issuecomment-1940952517 for the way ahead
*
* vcdbg has been deprecated, needs to be replaced with vclog
* Related: https://balena.fibery.io/Work/Project/Replace-vcdbg-with-vclog-in-BalenaOS-device-Tree-tests-330
*/
let attempt = 0
await retry(async () => {
attempt++
test.comment(`Sending vcdbg to DUT ${attempt}`);
await this.context.get().worker.sendFile(`${__dirname}/vcdbg`, `/tmp/vcdbg`, this.link);
console.log('vcdbg was successfully sent to the target')
}, { retries: 5, interval: 2000 });
test.is(
await this.context
.get()
.worker.executeCommandInHostOS(
`chmod +x /tmp/vcdbg && /tmp/vcdbg log msg 2>&1 | grep -q -i "File read:" ; echo $?`,
this.link,
),
'0',
'vcdbg static binary runs successfully'
);
test.is(
await this.context
.get()
.worker.executeCommandInHostOS(
'/tmp/vcdbg log msg 2>&1 | grep -q -i "Failed to load" ; echo $?',
this.link,
),
'1',
'vcdbg logs should be clean of device-tree or overlay load failures'
);
},
},
],
};