1#
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the 'License');
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an 'AS IS' BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import copy
17
18# Based on JobModel defined in
19# test/vti/test_serving/gae/webapp/src/proto/model.py
20input_data = {
21    "test_type": 1,
22    "hostname": "my_hostname",
23    "priority": "low",
24    "test_name": "vts/vts",
25    "require_signed_device_build": True,
26    "has_bootloader_img": True,
27    "has_radio_img": True,
28    "device": "my_device",
29    "serial": ["my_serial1", "my_serial2", "my_serial3"],
30
31    # device image information
32    "build_storage_type": 1,
33    "manifest_branch": "my_branch",
34    "build_target": "my_build_target",
35    "build_id": "my_build_id",
36    "pab_account_id": "my_pab_account_id",
37    "shards": 3,
38    "param": "",
39    "status": 1,
40    "period": 24 * 60,  # 1 day
41
42    # GSI information
43    "gsi_storage_type": 1,
44    "gsi_branch": "my_gsi_branch",
45    "gsi_build_target": "my_gsi_build_target",
46    "gsi_build_id": "my_gsi_build_id",
47    "gsi_pab_account_id": "my_gsi_pab_account_id",
48    # gsi_vendor_version: "8.1.0"
49
50    # test suite information
51    "test_storage_type": 1,
52    "test_branch": "my_test_branch",
53    "test_build_target": "my_test_build_target",
54    "test_build_id": "my_test_build_id",
55    "test_pab_account_id": "my_test_pab_account_id",
56
57    #timestamp = ndb.DateTimeProperty(auto_now=False)
58    #heartbeat_stamp = ndb.DateTimeProperty(auto_now=False)
59    "retry_count": 3,
60    "infra_log_url": "infra_log_url",
61
62    #parent_schedule = ndb.KeyProperty(kind="ScheduleModel")
63    "image_package_repo_base": "image_package_repo_base",
64    "report_bucket": ["report_bucket"],
65    "report_spreadsheet_id": ["report_spreadsheet_id"],
66}
67
68expected_output = [
69    'device --set_serial=my_serial1,my_serial2,my_serial3 --from_job_pool --interval=300',
70    'fetch --type=pab --branch=my_branch --target=my_build_target --artifact_name=my_build_target-img-my_build_id.zip --build_id=my_build_id --account_id=my_pab_account_id --fetch_signed_build=True',
71    'fetch --type=pab --branch=my_branch --target=my_build_target --artifact_name=bootloader.img --build_id=my_build_id --account_id=my_pab_account_id',
72    'fetch --type=pab --branch=my_branch --target=my_build_target --artifact_name=radio.img --build_id=my_build_id --account_id=my_pab_account_id',
73    'fetch --type=pab --branch=my_gsi_branch --target=my_gsi_build_target --gsi=True --artifact_name=my_gsi_build_target-img-{build_id}.zip --build_id=my_gsi_build_id --account_id=my_gsi_pab_account_id',
74    'fetch --type=pab --branch=my_test_branch --target=my_test_build_target --artifact_name=android-{{test_suite}}.zip --build_id=my_test_build_id --account_id=my_test_pab_account_id',
75    'info', 'gsispl --version_from_path=boot.img', 'info',
76    [[
77        'flash --current --serial my_serial1 --skip-vbmeta=True ',
78        'adb -s my_serial1 root',
79        'dut --operation=wifi_on --serial=my_serial1 --ap=GoogleGuest',
80        'dut --operation=volume_mute --serial=my_serial1 --version=9.0'
81    ], [
82        'flash --current --serial my_serial2 --skip-vbmeta=True ',
83        'adb -s my_serial2 root',
84        'dut --operation=wifi_on --serial=my_serial2 --ap=GoogleGuest',
85        'dut --operation=volume_mute --serial=my_serial2 --version=9.0'
86    ], [
87        'flash --current --serial my_serial3 --skip-vbmeta=True ',
88        'adb -s my_serial3 root',
89        'dut --operation=wifi_on --serial=my_serial3 --ap=GoogleGuest',
90        'dut --operation=volume_mute --serial=my_serial3 --version=9.0'
91    ]],
92    'test --suite {{test_suite}} --keep-result -- {{test_plan}} --shards 3  --serial my_serial1 --serial my_serial2 --serial my_serial3',
93    'retry --suite {{test_suite}} --count 3 {{retry_plan}} --shards 3 --serial my_serial1 --serial my_serial2 --serial my_serial3{{cleanup_device}}',
94    'upload --src={result_full} --dest=report_bucket/{suite_plan}/{{test_plan}}/{branch}/{target}/my_build_target_{build_id}_{timestamp}/ --report_path=report_bucket/suite_result/{timestamp_year}/{timestamp_month}/{timestamp_day}',
95    'sheet --src {result_zip} --dest report_spreadsheet_id --extra_rows logs,report_bucket/{suite_plan}/{{test_plan}}/{branch}/{target}/my_build_target_{build_id}_{timestamp}/ --primary_abi_only --client_secrets DATA/vtslab-gcs.json',
96    'device --update=stop',
97]
98
99
100def GenerateInputData(test_name):
101    """Returns an input data dict for a given `test_name`."""
102    new_data = copy.copy(input_data)
103    new_data["test_name"] = test_name
104    return new_data
105
106
107def GenerateOutputData(test_name):
108    """Returns an output data list for a given `test_name`."""
109    test_suite, test_plan = test_name.split("/")
110
111    def ReplaceChars(line):
112        line = line.replace('{{test_suite}}', test_suite)
113        line = line.replace('{{test_plan}}', test_plan)
114        if test_plan != "cts-on-gsi":
115            line = line.replace(' --primary_abi_only', '')
116        if (test_suite == "cts" or test_suite == "gts" or test_suite == "sts"
117                or test_plan.startswith("cts-")):
118            line = line.replace('--shards', "--shard-count")
119            if test_suite == "vts":
120                line = line.replace('{{retry_plan}}',
121                                    '--retry_plan=%s-retry' % test_plan)
122            else:
123                line = line.replace('{{retry_plan}}', '--retry_plan=retry')
124            line = line.replace('{{cleanup_device}}',
125                                ' --cleanup_devices=True')
126        else:
127            line = line.replace('{{retry_plan}}', '')
128            line = line.replace('{{cleanup_device}}', '')
129        return line
130
131    def RecursivelyApply(input_list, func):
132        for number, item in enumerate(input_list):
133            if type(item) is list:
134                input_list[number] = RecursivelyApply(input_list[number], func)
135            elif type(item) is str:
136                input_list[number] = func(item)
137            else:
138                return None
139        return input_list
140
141    return RecursivelyApply(copy.copy(expected_output), ReplaceChars)
142