Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / test_driver / linux-cirque / test-on-off-cluster.py
1 #!/usr/bin/env python3
2 """
3 Copyright (c) 2020 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
18 import logging
19 import os
20 import time
21 import sys
22
23 from helper.CHIPTestBase import CHIPVirtualHome
24
25 logger = logging.getLogger('CHIPOnOffTest')
26 logger.setLevel(logging.INFO)
27
28 sh = logging.StreamHandler()
29 sh.setFormatter(
30     logging.Formatter(
31         '%(asctime)s [%(name)s] %(levelname)s %(message)s'))
32 logger.addHandler(sh)
33
34 DEVICE_CONFIG = {
35     'device0': {
36         'type': 'CHIP-Server',
37         'base_image': 'chip_server',
38         'capability': ['Thread', 'Interactive'],
39         'rcp_mode': True,
40     },
41     'device1': {
42         'type': 'CHIP-Tool',
43         'base_image': 'chip_tool',
44         'capability': ['Thread', 'Interactive'],
45         'rcp_mode': True,
46     }
47 }
48
49 SETUPPINCODE = 12345678
50 DISCRIMINATOR = 1  # Randomw number, not used
51 CHIP_PORT = 11097
52
53 CIRQUE_URL = "http://localhost:5000"
54
55
56 class TestOnOffCluster(CHIPVirtualHome):
57     def __init__(self, device_config):
58         super().__init__(CIRQUE_URL, device_config)
59         self.logger = logger
60
61     def setup(self):
62         self.initialize_home()
63         self.connect_to_thread_network()
64
65     def test_routine(self):
66         self.run_data_model_test()
67
68     def run_data_model_test(self):
69         server_ip_address = set()
70         tool_device_id = ''
71
72         server_ids = [device['id'] for device in self.non_ap_devices
73                       if device['type'] == 'CHIP-Server']
74         tool_ids = [device['id'] for device in self.non_ap_devices
75                     if device['type'] == 'CHIP-Tool']
76
77         tool_device_id = tool_ids[0]
78
79         for device_id in server_ids:
80             server_ip_address.add(self.get_device_thread_ip(device_id))
81
82         command = "chip-tool onoff {} 1"
83
84         for ip in server_ip_address:
85             ret = self.execute_device_cmd(
86                 tool_device_id, "chip-tool pairing softap {} {} {} {}".format(SETUPPINCODE, DISCRIMINATOR, ip, CHIP_PORT))
87             self.assertEqual(ret['return_code'], '0', "{} command failure: {}".format(
88                 "pairing softap", ret['output']))
89
90             ret = self.execute_device_cmd(tool_device_id, command.format("on"))
91             self.assertEqual(
92                 ret['return_code'], '0', "{} command failure: {}".format("on", ret['output']))
93
94             ret = self.execute_device_cmd(
95                 tool_device_id, command.format("off"))
96             self.assertEqual(
97                 ret['return_code'], '0', "{} command failure: {}".format("off", ret['output']))
98
99             ret = self.execute_device_cmd(
100                 tool_device_id, "chip-tool pairing unpair")
101             self.assertEqual(ret['return_code'], '0', "{} command failure: {}".format(
102                 "pairing unpair", ret['output']))
103
104         time.sleep(1)
105
106         for device_id in server_ids:
107             self.logger.info("checking device log for {}".format(
108                 self.get_device_pretty_id(device_id)))
109             self.assertTrue(self.sequenceMatch(self.get_device_log(device_id).decode('utf-8'), ["Thread initialized.", "LightingManager::InitiateAction(ON_ACTION)", "LightingManager::InitiateAction(OFF_ACTION)"]),
110                             "Datamodel test failed: cannot find matching string from device {}".format(device_id))
111
112
113 if __name__ == "__main__":
114     sys.exit(TestOnOffCluster(DEVICE_CONFIG).run_test())