Skip to content

Commit

Permalink
[CI] Allow test suite applications to be launched with custom command…
Browse files Browse the repository at this point in the history
… line options (#17469)
  • Loading branch information
carol-apple authored Apr 20, 2022
1 parent 57471cb commit 160de2a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
28 changes: 24 additions & 4 deletions scripts/tests/chiptest/accessories.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import sys
import threading
from xmlrpc.server import SimpleXMLRPCServer
Expand Down Expand Up @@ -57,10 +58,13 @@ def killAll(self):
for accessory in self.__accessories.values():
accessory.kill()

def start(self, name, discriminator):
def start(self, name, args):
accessory = self.__accessories[name]
if accessory:
return accessory.start(discriminator)
# The args param comes directly from the sys.argv[1:] of Start.py and should contain a list of strings in
# key-value pair, e.g. [option1, value1, option2, value2, ...]
options = self.__createCommandLineOptions(args)
return accessory.start(options)
return False

def stop(self, name):
Expand All @@ -69,10 +73,13 @@ def stop(self, name):
return accessory.stop()
return False

def reboot(self, name, discriminator):
def reboot(self, name, args):
accessory = self.__accessories[name]
if accessory:
return accessory.stop() and accessory.start(discriminator)
# The args param comes directly from the sys.argv[1:] of Reboot.py and should contain a list of strings in
# key-value pair, e.g. [option1, value1, option2, value2, ...]
options = self.__createCommandLineOptions(args)
return accessory.stop() and accessory.start(options)
return False

def factoryResetAll(self):
Expand Down Expand Up @@ -116,3 +123,16 @@ def __startXMLRPCServer(self):

def __stopXMLRPCServer(self):
self.server.shutdown()

def __createCommandLineOptions(self, args):
if not args:
return {}

# args should contain a list of strings in key-value pair, e.g. [option1, value1, option2, value2, ...]
if (len(args) % 2) != 0:
logging.warning("Unexpected command line options %r - not key/value pairs (odd length)" % (args,))
return {}

# Create a dictionary from the key-value pair list
options = {args[i]: args[i+1] for i in range(0, len(args), 2)}
return options
38 changes: 18 additions & 20 deletions scripts/tests/chiptest/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ def __init__(self, runner, command):
self.cv_stopped = threading.Condition()
self.stopped = False
self.lastLogIndex = 0
self.kvs = '/tmp/chip_kvs'

def start(self, discriminator):
def start(self, options=None):
if not self.process:
# Make sure to assign self.process before we do any operations that
# might fail, so attempts to kill us on failure actually work.
self.process, self.outpipe, errpipe = self.__startServer(
self.runner, self.command, discriminator)
self.runner, self.command, options)
self.waitForAnyAdvertisement()
self.__updateSetUpCode()
with self.cv_stopped:
Expand All @@ -65,18 +66,9 @@ def stop(self):
return True
return False

def reboot(self, discriminator):
if self.process:
self.stop()
self.start(discriminator)
return True
return False

def factoryReset(self):
storage = '/tmp/chip_kvs'
if os.path.exists(storage):
os.unlink(storage)

if os.path.exists(self.kvs):
os.unlink(self.kvs)
return True

def waitForAnyAdvertisement(self):
Expand Down Expand Up @@ -108,12 +100,18 @@ def wait(self, timeout=None):
while self.stopped:
self.cv_stopped.wait()

def __startServer(self, runner, command, discriminator):
logging.debug(
'Executing application under test with discriminator %s.' %
discriminator)
app_cmd = command + ['--discriminator', str(discriminator)]
app_cmd = app_cmd + ['--interface-id', str(-1)]
def __startServer(self, runner, command, options):
app_cmd = command + ['--interface-id', str(-1)]

if not options:
logging.debug('Executing application under test with default args')
else:
logging.debug('Executing application under test with the following args:')
for key, value in options.items():
logging.debug(' %s: %s' % (key, value))
app_cmd = app_cmd + [key, value]
if key == '--KVS':
self.kvs = value
return runner.RunSubprocess(app_cmd, name='APP ', wait=False)

def __waitFor(self, waitForString, server_process, outpipe):
Expand Down Expand Up @@ -239,7 +237,7 @@ def Run(self, runner, apps_register, paths: ApplicationPaths, pics_file: str):
# Remove server application storage (factory reset),
# so it will be commissionable again.
app.factoryReset()
app.start(str(randrange(1, 4096)))
app.start()
pairing_cmd = tool_cmd + ['pairing', 'qrcode', TEST_NODE_ID, app.setupCode]
if sys.platform != 'darwin':
pairing_cmd.append('--paa-trust-store-path')
Expand Down
4 changes: 2 additions & 2 deletions src/app/tests/suites/commands/system/SystemCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CHIP_ERROR SystemCommands::Start(uint16_t discriminator)
constexpr const char * scriptName = "Start.py";

char command[128];
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s %u", scriptDir, scriptName, discriminator) >= 0,
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s --discriminator %u", scriptDir, scriptName, discriminator) >= 0,
CHIP_ERROR_INTERNAL);
return RunInternal(command);
}
Expand All @@ -53,7 +53,7 @@ CHIP_ERROR SystemCommands::Reboot(uint16_t discriminator)
constexpr const char * scriptName = "Reboot.py";

char command[128];
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s %u", scriptDir, scriptName, discriminator) >= 0,
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s --discriminator %u", scriptDir, scriptName, discriminator) >= 0,
CHIP_ERROR_INTERNAL);
return RunInternal(command);
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/tests/suites/commands/system/scripts/Reboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
if sys.platform == 'linux':
IP = '10.10.10.5'

# Passing in sys.argv[1:] gets rid of the script name with the remaining values in the list as
# key-value pairs, e.g. [option1, value1, option2, value2, ...]
with xmlrpc.client.ServerProxy('http://' + IP + ':' + str(PORT) + '/', allow_none=True) as proxy:
proxy.reboot('default', sys.argv[1])
proxy.reboot('default', sys.argv[1:])
4 changes: 3 additions & 1 deletion src/app/tests/suites/commands/system/scripts/Start.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
if sys.platform == 'linux':
IP = '10.10.10.5'

# Passing in sys.argv[1:] gets rid of the script name with the remaining values in the list as
# key-value pairs, e.g. [option1, value1, option2, value2, ...]
with xmlrpc.client.ServerProxy('http://' + IP + ':' + str(PORT) + '/', allow_none=True) as proxy:
proxy.start('default', sys.argv[1])
proxy.start('default', sys.argv[1:])

0 comments on commit 160de2a

Please sign in to comment.