aboutsummaryrefslogtreecommitdiff
path: root/build-scripts/post-build-lava.py
blob: 08250e39c4e49688f9d80207734c9dbe62d2cfc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
import os
import json
import urllib2
import xmlrpclib

# Map a TARGET_PRODUCT to LAVA parameters.
PRODUCT_MAP = {
    "pandaboard": {
        "test_target": "panda",
        "test_stream": "/anonymous/android-daily/",
        "image_path": "%s%s" % (
            "target/product/",
            "pandaboard")},
    "full_panda": {
        "test_target": "panda",
        "test_stream": "/anonymous/android-daily/",
        "image_path": "%s%s" % (
            "target/product/",
            "panda")},
    "beagleboard": {
        "test_target": "beaglexm",
        "test_stream": "/anonymous/android-daily/",
        "image_path": "%s%s" % (
            "target/product/",
            "beagleboard")},
    "snowball": {
        "test_target": "snowball_sd",
        "test_stream": "/anonymous/android-daily/",
        "image_path": "%s%s" % (
            "target/product/",
            "snowball")},
    "iMX53": {
        "test_target": "mx53loco",
        "test_stream": "/anonymous/android-daily/",
        "image_path": "%s%s" % (
            "target/product/",
            "iMX53")},
    "origen": {
        "test_target": "origen",
        "test_stream": "/anonymous/android-daily/",
        "image_path": "%s%s" % (
            "target/product/",
            "origen")},
}


def gen_lava_android_test_actions(tests=[]):
    actions = []
    if len(tests) == 0:
        return actions
    inst_action = {
                      "command": "lava_android_test_install",
                      "parameters": {
                                    # ensure only unique test names
                                    "tests": list(set(tests))
                                        }
                        }
    actions.append(inst_action)

    for test in tests:
        run_action = {
                      "command": "lava_android_test_run",
                      "parameters": {
                                     "test_name": test
                                         }
                        }
        actions.append(run_action)
    return actions


def lava_android_test_custom_actions(commands=[], cmd_file=None, parser=None):
    parameters = None
    if commands:
        parameters = {'commands': commands}
    elif cmd_file:
        parameters = {'command_file': cmd_file}

    if parameters and parser:
        parameters['parser'] = parser

    action = {"command": "lava_android_test_run_custom",
              "parameters": parameters}
    return action


def gen_test_plan_action():
    test_plan = os.environ.get("LAVA_TEST_PLAN")
    if test_plan == None:
        test_plan = '0xbench, glmark2, monkey'
    test_plans = test_plan.split(',')
    for index in range(len(test_plans)):
        test_plans[index] = test_plans[index].strip()
        if test_plans[index] == "test_android_0xbench":
            test_plans[index] = "0xbench"
    return gen_lava_android_test_actions(test_plans)


def gen_test_actions():
    test_actions = []
    lava_tests_str = os.environ.get("LAVA_TESTS")
    if lava_tests_str is not None:
        lava_tests_ary = lava_tests_str.split(',')
        for lava_test in lava_tests_ary:
            lava_test = lava_test.strip()
            lava_test_upper = lava_test.strip().upper()
            if lava_test_upper == 'LAVA_TEST_PLAN':
                test_actions.extend(gen_test_plan_action())
            elif lava_test_upper.startswith('LT_CMD_FILE_'):
                cmd_file = os.environ.get(lava_test)
                parser = os.environ.get('%s_PARSER' % lava_test)
                test_actions.append(
                        lava_android_test_custom_actions(cmd_file=cmd_file,
                                                          parser=parser))
            elif lava_test_upper.startswith('LT_CMD_'):
                commands = [os.environ.get(lava_test)]
                parser = os.environ.get('%s_PARSER' % lava_test)
                test_actions.append(
                        lava_android_test_custom_actions(commands=commands,
                                                          parser=parser))
            elif lava_test_upper.startswith('LT_CMDS_FILE_'):
                file_url = os.environ.get(lava_test)
                try:
                    fd = urllib2.urlopen(file_url.strip())
                except:
                    print "File to get command list file(%s) for %s." % (
                                                    file_url, lava_test)
                    continue
                commands = []
                for line in fd.readlines():
                    test_cmd = line.strip()
                    if test_cmd:
                        commands.append(test_cmd)
                if commands:
                    parser = os.environ.get('%s_PARSER' % lava_test)
                    test_actions.append(lava_android_test_custom_actions(
                                commands=commands, parser=parser))
            elif lava_test_upper.startswith('LT_CMDS_'):
                test_cmds_names_str = os.environ.get(lava_test)
                test_cmds_names_ary = test_cmds_names_str.split(',')
                commands = []
                for test_cmd_var_name in test_cmds_names_ary:
                    test_cmd = os.environ.get(test_cmd_var_name.strip())
                    if test_cmd:
                        commands.append(test_cmd)
                if commands:
                    parser = os.environ.get('%s_PARSER' % lava_test)
                    test_actions.append(
                        lava_android_test_custom_actions(commands=commands,
                                                          parser=parser))
            else:
                continue
    else:
        #keep compatibility
        test_actions.extend(gen_test_plan_action())

    return test_actions


def main():
    """Script entry point: return some JSON based on calling args.
    We should be called from Jenkins and expect the following to
    be defined: $TARGET_PRODUCT $JOB_NAME $BUILD_NUMBER $BUILD_URL"""

    # Target product name, user defined, e.g. pandaboard
    target_product = os.environ.get("TARGET_PRODUCT")
    # Job name, defined by android-build, e.g. linaro-android_leb-panda
    job_name = os.environ.get("JOB_NAME")
    frontend_job_name = "~" + job_name.replace("_", "/", 1)
    # Build number, defined by android-build, e.g. 61
    build_number = os.environ.get("BUILD_NUMBER")
    # Build url, defined by android-build, e.g.
    # https://android-build.linaro.org/jenkins/job/linaro-android_leb-panda/61/
    build_url = os.environ.get("BUILD_URL")
    # download base URL, this may differ from job URL if we don't host downloads in Jenkins any more
    download_url = "http://snapshots.linaro.org/android/%s/%s/" % (frontend_job_name, build_number)

    # Board-specific parameters
    if target_product not in PRODUCT_MAP:
        # We don't know how to test this job, so skip testing.
        print "Don't know how to test this board. Skip testing."
        return

    actions = [
    {
    "command": "deploy_linaro_android_image",
    "parameters":
      {
        "boot": "%s%s%s" % (download_url, PRODUCT_MAP[target_product]["image_path"], "/boot.tar.bz2"),
        "system":"%s%s%s" % (download_url, PRODUCT_MAP[target_product]["image_path"], "/system.tar.bz2"),
        "data":"%s%s%s" % (download_url, PRODUCT_MAP[target_product]["image_path"], "/userdata.tar.bz2")
      },
    "metadata":
      {
        "android.name": job_name,
        "android.build": '%s' % build_number,
        "android.url": build_url
      }
  },
  {
    "command": "android_install_binaries"
  },
  {
    "command": "boot_linaro_android_image"
  },
]

    actions.extend(gen_test_actions())

    actions.append(
  {
    "command": "submit_results_on_host",
    "parameters":
      {
        "server": "http://validation.linaro.org/lava-server/RPC2/",
        "stream": PRODUCT_MAP[target_product]["test_stream"]
      }
  })

    config = json.dumps({"job_name": build_url,
                         "image_type": 'android',
                         "device_type": PRODUCT_MAP[target_product]["test_target"],
                         "timeout": 18000,
                         "actions": actions
                            },
                        indent=4)

    print config
    lava_user = os.environ.get("LAVA_USER")
    if lava_user == None:
        f = open('/var/run/lava/lava-user')
        lava_user = f.read().strip()
        f.close()

    lava_token = os.environ.get("LAVA_TOKEN")
    if lava_token == None:
        f = open('/var/run/lava/lava-token')
        lava_token = f.read().strip()
        f.close()

    lava_server = os.environ.get("LAVA_SERVER")
    if lava_server == None:
        lava_server = "validation.linaro.org/lava-server/RPC2/"

    server = xmlrpclib.ServerProxy("https://%(lava_user)s:%(lava_token)s@%(lava_server)s" % \
        dict(lava_user=lava_user, lava_token=lava_token, lava_server=lava_server))
    lava_job_id = server.scheduler.submit_job(config)
    lava_server_root = lava_server.rstrip("/")
    if lava_server_root.endswith("/RPC2"):
        lava_server_root = lava_server_root[:-len("/RPC2")]
    print "LAVA Job Id: %s, URL: http://%s/scheduler/job/%s" % (lava_job_id, lava_server_root, lava_job_id)

    json.dump({
        'lava_url': "http://" + lava_server_root,
        'job_id': lava_job_id,
        }, open('out/lava-job-info', 'w'))

if __name__ == "__main__":
        main()