Skip to content

Commit 4759969

Browse files
vivien-applepull[bot]
authored andcommitted
[CI] Add FactoryReset command supports (#14622)
* [Test Runner] Add factoryReset command * [YAML] Add FactoryReset command to SystemCommands * Update generated code
1 parent 34da03c commit 4759969

File tree

8 files changed

+91
-16
lines changed

8 files changed

+91
-16
lines changed

scripts/tests/chiptest/accessories.py

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ def reboot(self, name, discriminator):
8181
return accessory.stop() and accessory.start(discriminator)
8282
return False
8383

84+
def factoryResetAll(self):
85+
for accessory in self.__accessories.values():
86+
accessory.factoryReset()
87+
88+
def factoryReset(self, name):
89+
accessory = self.__accessories[name]
90+
if accessory:
91+
return accessory.factoryReset()
92+
return False
93+
8494
def ping(self):
8595
return True
8696

@@ -90,6 +100,7 @@ def __startXMLRPCServer(self):
90100
self.server.register_function(self.start, 'start')
91101
self.server.register_function(self.stop, 'stop')
92102
self.server.register_function(self.reboot, 'reboot')
103+
self.server.register_function(self.factoryReset, 'factoryReset')
93104
self.server.register_function(self.ping, 'ping')
94105

95106
self.server_thread = threading.Thread(target=self.__handle_request)

scripts/tests/chiptest/test_definition.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ def reboot(self, discriminator):
6464
return True
6565
return False
6666

67+
def factoryReset(self):
68+
storage = '/tmp/chip_kvs'
69+
if platform.system() == 'Darwin':
70+
storage = str(Path.home()) + '/Documents/chip.store'
71+
72+
if os.path.exists(storage):
73+
os.unlink(storage)
74+
75+
return True
76+
6777
def poll(self):
6878
# When the server is manually stopped, process polling is overriden so the other
6979
# processes that depends on the accessory beeing alive does not stop.
@@ -192,16 +202,8 @@ def Run(self, runner, apps_register, paths: ApplicationPaths):
192202
if os.path.exists(f):
193203
os.unlink(f)
194204

195-
# Remove server all_clusters_app or tv_app storage, so it will be commissionable again
196-
if platform.system() == 'Linux':
197-
if os.path.exists('/tmp/chip_kvs'):
198-
os.unlink('/tmp/chip_kvs')
199-
200-
if platform.system() == "Darwin":
201-
if os.path.exists(str(Path.home()) + '/Documents/chip.store'):
202-
os.unlink(str(Path.home()) + '/Documents/chip.store')
203-
204205
app = App(runner, app_cmd)
206+
app.factoryReset() # Remove server application storage, so it will be commissionable again
205207
app.start(str(randrange(1, 4096)))
206208
apps_register.add("default", app)
207209

@@ -216,4 +218,5 @@ def Run(self, runner, apps_register, paths: ApplicationPaths):
216218
raise
217219
finally:
218220
apps_register.killAll()
221+
apps_register.factoryResetAll()
219222
apps_register.removeAll()

src/app/tests/suites/TestSystemCommands.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ tests:
3939
values:
4040
- name: "discriminator"
4141
value: 2222
42+
43+
- label: "Factory Reset the accessory"
44+
command: "FactoryReset"

src/app/tests/suites/commands/system/SystemCommands.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ CHIP_ERROR SystemCommands::Start(uint16_t discriminator)
3434
char command[128];
3535
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s %u", scriptDir, scriptName, discriminator) >= 0,
3636
CHIP_ERROR_INTERNAL);
37-
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
38-
return ContinueOnChipMainThread();
37+
return RunInternal(command);
3938
}
4039

4140
CHIP_ERROR SystemCommands::Stop()
@@ -45,9 +44,7 @@ CHIP_ERROR SystemCommands::Stop()
4544

4645
char command[128];
4746
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s", scriptDir, scriptName) >= 0, CHIP_ERROR_INTERNAL);
48-
49-
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
50-
return ContinueOnChipMainThread();
47+
return RunInternal(command);
5148
}
5249

5350
CHIP_ERROR SystemCommands::Reboot(uint16_t discriminator)
@@ -58,7 +55,21 @@ CHIP_ERROR SystemCommands::Reboot(uint16_t discriminator)
5855
char command[128];
5956
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s %u", scriptDir, scriptName, discriminator) >= 0,
6057
CHIP_ERROR_INTERNAL);
58+
return RunInternal(command);
59+
}
60+
61+
CHIP_ERROR SystemCommands::FactoryReset()
62+
{
63+
const char * scriptDir = getScriptsFolder();
64+
constexpr const char * scriptName = "FactoryReset.py";
6165

66+
char command[128];
67+
VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s", scriptDir, scriptName) >= 0, CHIP_ERROR_INTERNAL);
68+
return RunInternal(command);
69+
}
70+
71+
CHIP_ERROR SystemCommands::RunInternal(const char * command)
72+
{
6273
VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL);
6374
return ContinueOnChipMainThread();
6475
}

src/app/tests/suites/commands/system/SystemCommands.h

+4
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ class SystemCommands
3131
CHIP_ERROR Start(uint16_t discriminator);
3232
CHIP_ERROR Stop();
3333
CHIP_ERROR Reboot(uint16_t discriminator);
34+
CHIP_ERROR FactoryReset();
35+
36+
private:
37+
CHIP_ERROR RunInternal(const char * command);
3438
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env -S python3 -B
2+
3+
# Copyright (c) 2022 Project CHIP Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import sys
18+
import xmlrpc.client
19+
20+
IP = '127.0.0.1'
21+
PORT = 9000
22+
23+
if sys.platform == 'linux':
24+
IP = '10.10.10.5'
25+
26+
with xmlrpc.client.ServerProxy('http://' + IP + ':' + str(PORT) + '/', allow_none=True) as proxy:
27+
proxy.factoryReset('default')

src/app/zap-templates/common/simulated-clusters/clusters/SystemCommands.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@ const Reboot = {
4242
response : { arguments : [] }
4343
};
4444

45+
const FactoryReset = {
46+
name : 'FactoryReset',
47+
arguments : [],
48+
response : { arguments : [] }
49+
};
50+
4551
const SystemCommands = {
4652
name : 'SystemCommands',
47-
commands : [ Start, Stop, Reboot ],
53+
commands : [ Start, Stop, Reboot, FactoryReset ],
4854
};
4955

5056
//

zzz_generated/chip-tool/zap-generated/test/Commands.h

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)