Skip to content

Commit ff8190d

Browse files
committed
[Python] Convert tests in src/controller/python/ to asyncio
1 parent c862a24 commit ff8190d

16 files changed

+127
-112
lines changed

src/controller/python/chip/utils/CommissioningBuildingBlocks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async def AddNOCForNewFabricFromExisting(commissionerDevCtrl, newFabricDevCtrl,
167167

168168
csrForAddNOC = await commissionerDevCtrl.SendCommand(existingNodeId, 0, opCreds.Commands.CSRRequest(CSRNonce=os.urandom(32)))
169169

170-
chainForAddNOC = newFabricDevCtrl.IssueNOCChain(csrForAddNOC, newNodeId)
170+
chainForAddNOC = await newFabricDevCtrl.IssueNOCChain(csrForAddNOC, newNodeId)
171171
if (chainForAddNOC.rcacBytes is None or
172172
chainForAddNOC.icacBytes is None or
173173
chainForAddNOC.nocBytes is None or chainForAddNOC.ipkBytes is None):
@@ -225,7 +225,7 @@ async def UpdateNOC(devCtrl, existingNodeId, newNodeId):
225225
return False
226226
csrForUpdateNOC = await devCtrl.SendCommand(
227227
existingNodeId, 0, opCreds.Commands.CSRRequest(CSRNonce=os.urandom(32), isForUpdateNOC=True))
228-
chainForUpdateNOC = devCtrl.IssueNOCChain(csrForUpdateNOC, newNodeId)
228+
chainForUpdateNOC = await devCtrl.IssueNOCChain(csrForUpdateNOC, newNodeId)
229229
if (chainForUpdateNOC.rcacBytes is None or
230230
chainForUpdateNOC.icacBytes is None or
231231
chainForUpdateNOC.nocBytes is None or chainForUpdateNOC.ipkBytes is None):

src/controller/python/chip/yaml/runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ async def run_action(self, dev_ctrl: ChipDeviceController) -> _ActionResult:
665665
return _ActionResult(status=_ActionStatus.SUCCESS, response=_GetCommissionerNodeIdResult(dev_ctrl.nodeId))
666666

667667
try:
668-
dev_ctrl.CommissionWithCode(self._setup_payload, self._node_id)
668+
await dev_ctrl.CommissionWithCode(self._setup_payload, self._node_id)
669669
return _ActionResult(status=_ActionStatus.SUCCESS, response=None)
670670
except ChipStackError:
671671
return _ActionResult(status=_ActionStatus.ERROR, response=None)

src/controller/python/py_matter_yamltest_repl_adapter/matter_yamltest_repl_adapter/runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async def start(self):
5959
# device with the provided node id.
6060
if self._node_id_to_commission is not None:
6161
# Magic value is the defaults expected for YAML tests.
62-
dev_ctrl.CommissionWithCode('MT:-24J0AFN00KA0648G00', self._node_id_to_commission)
62+
await dev_ctrl.CommissionWithCode('MT:-24J0AFN00KA0648G00', self._node_id_to_commission)
6363

6464
self._chip_stack = chip_stack
6565
self._certificate_authority_manager = certificate_authority_manager

src/controller/python/test/test_scripts/base.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def CreateNewFabricController(self):
234234
async def TestRevokeCommissioningWindow(self, ip: str, setuppin: int, nodeid: int):
235235
await self.devCtrl.SendCommand(
236236
nodeid, 0, Clusters.AdministratorCommissioning.Commands.OpenBasicCommissioningWindow(180), timedRequestTimeoutMs=10000)
237-
if not self.TestPaseOnly(ip=ip, setuppin=setuppin, nodeid=nodeid, devCtrl=self.devCtrl2):
237+
if not await self.TestPaseOnly(ip=ip, setuppin=setuppin, nodeid=nodeid, devCtrl=self.devCtrl2):
238238
return False
239239

240240
await self.devCtrl2.SendCommand(
@@ -248,17 +248,17 @@ async def TestRevokeCommissioningWindow(self, ip: str, setuppin: int, nodeid: in
248248
nodeid, 0, Clusters.AdministratorCommissioning.Commands.RevokeCommissioning(), timedRequestTimeoutMs=10000)
249249
return True
250250

251-
def TestEnhancedCommissioningWindow(self, ip: str, nodeid: int):
252-
params = self.devCtrl.OpenCommissioningWindow(nodeid=nodeid, timeout=600, iteration=10000, discriminator=3840, option=1)
253-
return self.TestPaseOnly(ip=ip, nodeid=nodeid, setuppin=params.setupPinCode, devCtrl=self.devCtrl2)
251+
async def TestEnhancedCommissioningWindow(self, ip: str, nodeid: int):
252+
params = await self.devCtrl.OpenCommissioningWindow(nodeid=nodeid, timeout=600, iteration=10000, discriminator=3840, option=1)
253+
return await self.TestPaseOnly(ip=ip, nodeid=nodeid, setuppin=params.setupPinCode, devCtrl=self.devCtrl2)
254254

255-
def TestPaseOnly(self, ip: str, setuppin: int, nodeid: int, devCtrl=None):
255+
async def TestPaseOnly(self, ip: str, setuppin: int, nodeid: int, devCtrl=None):
256256
if devCtrl is None:
257257
devCtrl = self.devCtrl
258258
self.logger.info(
259259
"Attempting to establish PASE session with device id: {} addr: {}".format(str(nodeid), ip))
260260
try:
261-
devCtrl.EstablishPASESessionIP(ip, setuppin, nodeid)
261+
await devCtrl.EstablishPASESessionIP(ip, setuppin, nodeid)
262262
except ChipStackException:
263263
self.logger.info(
264264
"Failed to establish PASE session with device id: {} addr: {}".format(str(nodeid), ip))
@@ -267,11 +267,11 @@ def TestPaseOnly(self, ip: str, setuppin: int, nodeid: int, devCtrl=None):
267267
"Successfully established PASE session with device id: {} addr: {}".format(str(nodeid), ip))
268268
return True
269269

270-
def TestCommissionOnly(self, nodeid: int):
270+
async def TestCommissionOnly(self, nodeid: int):
271271
self.logger.info(
272272
"Commissioning device with id {}".format(nodeid))
273273
try:
274-
self.devCtrl.Commission(nodeid)
274+
await self.devCtrl.Commission(nodeid)
275275
except ChipStackException:
276276
self.logger.info(
277277
"Failed to commission device with id {}".format(str(nodeid)))
@@ -280,17 +280,17 @@ def TestCommissionOnly(self, nodeid: int):
280280
"Successfully commissioned device with id {}".format(str(nodeid)))
281281
return True
282282

283-
def TestKeyExchangeBLE(self, discriminator: int, setuppin: int, nodeid: int):
283+
async def TestKeyExchangeBLE(self, discriminator: int, setuppin: int, nodeid: int):
284284
self.logger.info(
285285
"Conducting key exchange with device {}".format(discriminator))
286-
if not self.devCtrl.ConnectBLE(discriminator, setuppin, nodeid):
286+
if not await self.devCtrl.ConnectBLE(discriminator, setuppin, nodeid):
287287
self.logger.info(
288288
"Failed to finish key exchange with device {}".format(discriminator))
289289
return False
290290
self.logger.info("Device finished key exchange.")
291291
return True
292292

293-
def TestCommissionFailure(self, nodeid: int, failAfter: int):
293+
async def TestCommissionFailure(self, nodeid: int, failAfter: int):
294294
self.devCtrl.ResetTestCommissioner()
295295
a = self.devCtrl.SetTestCommissionerSimulateFailureOnStage(failAfter)
296296
if not a:
@@ -299,43 +299,43 @@ def TestCommissionFailure(self, nodeid: int, failAfter: int):
299299

300300
self.logger.info(
301301
"Commissioning device, expecting failure after stage {}".format(failAfter))
302-
self.devCtrl.Commission(nodeid)
302+
await self.devCtrl.Commission(nodeid)
303303
return self.devCtrl.CheckTestCommissionerCallbacks() and self.devCtrl.CheckTestCommissionerPaseConnection(nodeid)
304304

305-
def TestCommissionFailureOnReport(self, nodeid: int, failAfter: int):
305+
async def TestCommissionFailureOnReport(self, nodeid: int, failAfter: int):
306306
self.devCtrl.ResetTestCommissioner()
307307
a = self.devCtrl.SetTestCommissionerSimulateFailureOnReport(failAfter)
308308
if not a:
309309
# We're not going to hit this stage during commissioning so no sense trying, just say it was fine.
310310
return True
311311
self.logger.info(
312312
"Commissioning device, expecting failure on report for stage {}".format(failAfter))
313-
self.devCtrl.Commission(nodeid)
313+
await self.devCtrl.Commission(nodeid)
314314
return self.devCtrl.CheckTestCommissionerCallbacks() and self.devCtrl.CheckTestCommissionerPaseConnection(nodeid)
315315

316-
def TestCommissioning(self, ip: str, setuppin: int, nodeid: int):
316+
async def TestCommissioning(self, ip: str, setuppin: int, nodeid: int):
317317
self.logger.info("Commissioning device {}".format(ip))
318318
try:
319-
self.devCtrl.CommissionIP(ip, setuppin, nodeid)
319+
await self.devCtrl.CommissionIP(ip, setuppin, nodeid)
320320
except ChipStackException:
321321
self.logger.exception(
322322
"Failed to finish commissioning device {}".format(ip))
323323
return False
324324
self.logger.info("Commissioning finished.")
325325
return True
326326

327-
def TestCommissioningWithSetupPayload(self, setupPayload: str, nodeid: int, discoveryType: int = 2):
327+
async def TestCommissioningWithSetupPayload(self, setupPayload: str, nodeid: int, discoveryType: int = 2):
328328
self.logger.info("Commissioning device with setup payload {}".format(setupPayload))
329329
try:
330-
self.devCtrl.CommissionWithCode(setupPayload, nodeid, chip.discovery.DiscoveryType(discoveryType))
330+
await self.devCtrl.CommissionWithCode(setupPayload, nodeid, chip.discovery.DiscoveryType(discoveryType))
331331
except ChipStackException:
332332
self.logger.exception(
333333
"Failed to finish commissioning device {}".format(setupPayload))
334334
return False
335335
self.logger.info("Commissioning finished.")
336336
return True
337337

338-
def TestOnNetworkCommissioning(self, discriminator: int, setuppin: int, nodeid: int, ip_override: str = None):
338+
async def TestOnNetworkCommissioning(self, discriminator: int, setuppin: int, nodeid: int, ip_override: str = None):
339339
self.logger.info("Testing discovery")
340340
device = self.TestDiscovery(discriminator=discriminator)
341341
if not device:
@@ -345,7 +345,7 @@ def TestOnNetworkCommissioning(self, discriminator: int, setuppin: int, nodeid:
345345
if ip_override:
346346
address = ip_override
347347
self.logger.info("Testing commissioning")
348-
if not self.TestCommissioning(address, setuppin, nodeid):
348+
if not await self.TestCommissioning(address, setuppin, nodeid):
349349
self.logger.info("Failed to finish commissioning")
350350
return False
351351
return True
@@ -792,7 +792,7 @@ async def TestMultiFabric(self, ip: str, setuppin: int, nodeid: int):
792792
self.controllerNodeId, self.paaTrustStorePath)
793793

794794
try:
795-
self.devCtrl2.CommissionIP(ip, setuppin, nodeid)
795+
await self.devCtrl2.CommissionIP(ip, setuppin, nodeid)
796796
except ChipStackException:
797797
self.logger.exception(
798798
"Failed to finish key exchange with device {}".format(ip))

src/controller/python/test/test_scripts/commissioning_failure_test.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
GROUP_ID = 0
4747

4848

49-
def main():
49+
async def main():
5050
optParser = OptionParser()
5151
optParser.add_option(
5252
"-t",
@@ -98,32 +98,32 @@ def main():
9898
# TODO: Start at stage 2 once handling for arming failsafe on pase is done.
9999
if options.report:
100100
for testFailureStage in range(3, 21):
101-
FailIfNot(test.TestPaseOnly(ip=options.deviceAddress1,
102-
setuppin=20202021,
103-
nodeid=1),
101+
FailIfNot(await test.TestPaseOnly(ip=options.deviceAddress1,
102+
setuppin=20202021,
103+
nodeid=1),
104104
"Failed to establish PASE connection with device")
105-
FailIfNot(test.TestCommissionFailureOnReport(1, testFailureStage),
105+
FailIfNot(await test.TestCommissionFailureOnReport(1, testFailureStage),
106106
"Commissioning failure tests failed for simulated report failure on stage {}".format(testFailureStage))
107107

108108
else:
109109
for testFailureStage in range(3, 21):
110-
FailIfNot(test.TestPaseOnly(ip=options.deviceAddress1,
111-
setuppin=20202021,
112-
nodeid=1),
110+
FailIfNot(await test.TestPaseOnly(ip=options.deviceAddress1,
111+
setuppin=20202021,
112+
nodeid=1),
113113
"Failed to establish PASE connection with device")
114-
FailIfNot(test.TestCommissionFailure(1, testFailureStage),
114+
FailIfNot(await test.TestCommissionFailure(1, testFailureStage),
115115
"Commissioning failure tests failed for simulated stage failure on stage {}".format(testFailureStage))
116116

117117
# Ensure we can still commission for real
118-
FailIfNot(test.TestPaseOnly(ip=options.deviceAddress1,
119-
setuppin=20202021,
120-
nodeid=1),
118+
FailIfNot(await test.TestPaseOnly(ip=options.deviceAddress1,
119+
setuppin=20202021,
120+
nodeid=1),
121121
"Failed to establish PASE connection with device")
122-
FailIfNot(test.TestCommissionFailure(1, 0), "Failed to commission device")
122+
FailIfNot(await test.TestCommissionFailure(1, 0), "Failed to commission device")
123123

124124
logger.info("Testing on off cluster")
125-
FailIfNot(asyncio.run(test.TestOnOffCluster(nodeid=1,
126-
endpoint=LIGHTING_ENDPOINT_ID)), "Failed to test on off cluster")
125+
FailIfNot(await test.TestOnOffCluster(nodeid=1,
126+
endpoint=LIGHTING_ENDPOINT_ID), "Failed to test on off cluster")
127127

128128
timeoutTicker.stop()
129129

@@ -136,7 +136,9 @@ def main():
136136

137137
if __name__ == "__main__":
138138
try:
139-
main()
139+
loop = asyncio.get_event_loop()
140+
loop.run_until_complete(main())
141+
loop.close()
140142
except Exception as ex:
141143
logger.exception(ex)
142144
TestFail("Exception occurred when running tests.")

src/controller/python/test/test_scripts/commissioning_test.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
GROUP_ID = 0
4848

4949

50-
def main():
50+
async def main():
5151
optParser = OptionParser()
5252
optParser.add_option(
5353
"-t",
@@ -133,15 +133,15 @@ def main():
133133

134134
if options.deviceAddress:
135135
logger.info("Testing commissioning (IP)")
136-
FailIfNot(test.TestCommissioning(ip=options.deviceAddress,
137-
setuppin=20202021,
138-
nodeid=options.nodeid),
136+
FailIfNot(await test.TestCommissioning(ip=options.deviceAddress,
137+
setuppin=20202021,
138+
nodeid=options.nodeid),
139139
"Failed to finish commissioning")
140140
elif options.setupPayload:
141141
logger.info("Testing commissioning (w/ Setup Payload)")
142-
FailIfNot(test.TestCommissioningWithSetupPayload(setupPayload=options.setupPayload,
143-
nodeid=options.nodeid,
144-
discoveryType=options.discoveryType),
142+
FailIfNot(await test.TestCommissioningWithSetupPayload(setupPayload=options.setupPayload,
143+
nodeid=options.nodeid,
144+
discoveryType=options.discoveryType),
145145
"Failed to finish commissioning")
146146
else:
147147
TestFail("Must provide device address or setup payload to commissioning the device")
@@ -164,7 +164,9 @@ def main():
164164

165165
if __name__ == "__main__":
166166
try:
167-
main()
167+
loop = asyncio.get_event_loop()
168+
loop.run_until_complete(main())
169+
loop.close()
168170
except Exception as ex:
169171
logger.exception(ex)
170172
TestFail("Exception occurred when running tests.")

src/controller/python/test/test_scripts/commissioning_window_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ async def main():
8989
"Failed to finish network commissioning")
9090

9191
logger.info("Commissioning DUT from first commissioner")
92-
FailIfNot(test.TestPaseOnly(ip=options.deviceAddress, setuppin=20202021, nodeid=1),
92+
FailIfNot(await test.TestPaseOnly(ip=options.deviceAddress, setuppin=20202021, nodeid=1),
9393
"Unable to establish PASE connection to device")
94-
FailIfNot(test.TestCommissionOnly(nodeid=1), "Unable to commission device")
94+
FailIfNot(await test.TestCommissionOnly(nodeid=1), "Unable to commission device")
9595

9696
logger.info("Creating controller on a new fabric")
9797
FailIfNot(test.CreateNewFabricController(), "Unable to create new controller")
@@ -103,7 +103,7 @@ async def main():
103103
"RevokeCommissioning test failed")
104104

105105
logger.info("Test Enhanced Commissioning Window")
106-
FailIfNot(test.TestEnhancedCommissioningWindow(ip=options.deviceAddress, nodeid=1), "EnhancedCommissioningWindow open failed")
106+
FailIfNot(await test.TestEnhancedCommissioningWindow(ip=options.deviceAddress, nodeid=1), "EnhancedCommissioningWindow open failed")
107107

108108
timeoutTicker.stop()
109109

src/controller/python/test/test_scripts/example_python_commissioning_flow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def get_csr_nonce(self) -> bytes:
6868

6969
async def get_commissionee_credentials(self, request: commissioning.GetCommissioneeCredentialsRequest) -> commissioning.GetCommissioneeCredentialsResponse:
7070
node_id = random.randint(100000, 999999)
71-
nocChain = self._devCtrl.IssueNOCChain(Clusters.OperationalCredentials.Commands.CSRResponse(
71+
nocChain = await self._devCtrl.IssueNOCChain(Clusters.OperationalCredentials.Commands.CSRResponse(
7272
NOCSRElements=request.csr_elements, attestationSignature=request.attestation_signature), nodeId=node_id)
7373
return commissioning.GetCommissioneeCredentialsResponse(
7474
rcac=nocChain.rcacBytes,

src/controller/python/test/test_scripts/failsafe_tests.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
GROUP_ID = 0
4747

4848

49-
def main():
49+
async def main():
5050
optParser = OptionParser()
5151
optParser.add_option(
5252
"-t",
@@ -95,12 +95,12 @@ def main():
9595
"Failed to finish network commissioning")
9696

9797
logger.info("Testing commissioning")
98-
FailIfNot(test.TestCommissioning(ip=options.deviceAddress,
99-
setuppin=20202021,
100-
nodeid=1),
98+
FailIfNot(await test.TestCommissioning(ip=options.deviceAddress,
99+
setuppin=20202021,
100+
nodeid=1),
101101
"Failed to finish key exchange")
102102

103-
FailIfNot(asyncio.run(test.TestFailsafe(nodeid=1)), "Failed failsafe test")
103+
FailIfNot(await test.TestFailsafe(nodeid=1), "Failed failsafe test")
104104

105105
timeoutTicker.stop()
106106

@@ -113,7 +113,9 @@ def main():
113113

114114
if __name__ == "__main__":
115115
try:
116-
main()
116+
loop = asyncio.get_event_loop()
117+
loop.run_until_complete(main())
118+
loop.close()
117119
except Exception as ex:
118120
logger.exception(ex)
119121
TestFail("Exception occurred when running tests.")

0 commit comments

Comments
 (0)