Skip to content

Commit

Permalink
[CI] Add FactoryReset command supports (#14622)
Browse files Browse the repository at this point in the history
* [Test Runner] Add factoryReset command

* [YAML] Add FactoryReset command to SystemCommands

* Update generated code
  • Loading branch information
vivien-apple authored and pull[bot] committed Sep 1, 2023
1 parent e3c862b commit 58408ff
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 16 deletions.
11 changes: 11 additions & 0 deletions scripts/tests/chiptest/accessories.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def reboot(self, name, discriminator):
return accessory.stop() and accessory.start(discriminator)
return False

def factoryResetAll(self):
for accessory in self.__accessories.values():
accessory.factoryReset()

def factoryReset(self, name):
accessory = self.__accessories[name]
if accessory:
return accessory.factoryReset()
return False

def ping(self):
return True

Expand All @@ -90,6 +100,7 @@ def __startXMLRPCServer(self):
self.server.register_function(self.start, 'start')
self.server.register_function(self.stop, 'stop')
self.server.register_function(self.reboot, 'reboot')
self.server.register_function(self.factoryReset, 'factoryReset')
self.server.register_function(self.ping, 'ping')

self.server_thread = threading.Thread(target=self.__handle_request)
Expand Down
21 changes: 12 additions & 9 deletions scripts/tests/chiptest/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def reboot(self, discriminator):
return True
return False

def factoryReset(self):
storage = '/tmp/chip_kvs'
if platform.system() == 'Darwin':
storage = str(Path.home()) + '/Documents/chip.store'

if os.path.exists(storage):
os.unlink(storage)

return True

def poll(self):
# When the server is manually stopped, process polling is overriden so the other
# processes that depends on the accessory beeing alive does not stop.
Expand Down Expand Up @@ -192,16 +202,8 @@ def Run(self, runner, apps_register, paths: ApplicationPaths):
if os.path.exists(f):
os.unlink(f)

# Remove server all_clusters_app or tv_app storage, so it will be commissionable again
if platform.system() == 'Linux':
if os.path.exists('/tmp/chip_kvs'):
os.unlink('/tmp/chip_kvs')

if platform.system() == "Darwin":
if os.path.exists(str(Path.home()) + '/Documents/chip.store'):
os.unlink(str(Path.home()) + '/Documents/chip.store')

app = App(runner, app_cmd)
app.factoryReset() # Remove server application storage, so it will be commissionable again
app.start(str(randrange(1, 4096)))
apps_register.add("default", app)

Expand All @@ -216,4 +218,5 @@ def Run(self, runner, apps_register, paths: ApplicationPaths):
raise
finally:
apps_register.killAll()
apps_register.factoryResetAll()
apps_register.removeAll()
3 changes: 3 additions & 0 deletions src/app/tests/suites/TestSystemCommands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ tests:
values:
- name: "discriminator"
value: 2222

- label: "Factory Reset the accessory"
command: "FactoryReset"
21 changes: 16 additions & 5 deletions src/app/tests/suites/commands/system/SystemCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ CHIP_ERROR SystemCommands::Start(uint16_t discriminator)
char command[128];
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s %u", scriptDir, scriptName, discriminator) >= 0,
CHIP_ERROR_INTERNAL);
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
return ContinueOnChipMainThread();
return RunInternal(command);
}

CHIP_ERROR SystemCommands::Stop()
Expand All @@ -45,9 +44,7 @@ CHIP_ERROR SystemCommands::Stop()

char command[128];
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s", scriptDir, scriptName) >= 0, CHIP_ERROR_INTERNAL);

VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
return ContinueOnChipMainThread();
return RunInternal(command);
}

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

CHIP_ERROR SystemCommands::FactoryReset()
{
const char * scriptDir = getScriptsFolder();
constexpr const char * scriptName = "FactoryReset.py";

char command[128];
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s", scriptDir, scriptName) >= 0, CHIP_ERROR_INTERNAL);
return RunInternal(command);
}

CHIP_ERROR SystemCommands::RunInternal(const char * command)
{
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
return ContinueOnChipMainThread();
}
4 changes: 4 additions & 0 deletions src/app/tests/suites/commands/system/SystemCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ class SystemCommands
CHIP_ERROR Start(uint16_t discriminator);
CHIP_ERROR Stop();
CHIP_ERROR Reboot(uint16_t discriminator);
CHIP_ERROR FactoryReset();

private:
CHIP_ERROR RunInternal(const char * command);
};
27 changes: 27 additions & 0 deletions src/app/tests/suites/commands/system/scripts/FactoryReset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env -S python3 -B

# Copyright (c) 2022 Project CHIP Authors
#
# 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.

import sys
import xmlrpc.client

IP = '127.0.0.1'
PORT = 9000

if sys.platform == 'linux':
IP = '10.10.10.5'

with xmlrpc.client.ServerProxy('http://' + IP + ':' + str(PORT) + '/', allow_none=True) as proxy:
proxy.factoryReset('default')
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ const Reboot = {
response : { arguments : [] }
};

const FactoryReset = {
name : 'FactoryReset',
arguments : [],
response : { arguments : [] }
};

const SystemCommands = {
name : 'SystemCommands',
commands : [ Start, Stop, Reboot ],
commands : [ Start, Stop, Reboot, FactoryReset ],
};

//
Expand Down
12 changes: 11 additions & 1 deletion zzz_generated/chip-tool/zap-generated/test/Commands.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58408ff

Please sign in to comment.