summaryrefslogtreecommitdiff
path: root/common/scripts/lkp.py
blob: 8d220011d04c9a6b192fc8cca81f086a239e1e2c (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
#!/usr/bin/env python
#
# Run LKP test suite on Linaro ubuntu
#
# Copyright (C) 2012 - 2014, Linaro Limited.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
# USA.
#
# Author: Chase Qi <chase.qi@linaro.org>
#
import os
import sys
import platform
import glob
import json
import pwd
import shutil
from subprocess import call

LKPPath = str(sys.argv[1])
print 'LKP test suite path: %s' % (LKPPath)
WD = str(sys.argv[2])
print 'Working directory: %s' % (WD)
LOOPS = int(sys.argv[3])
JOB = str(sys.argv[4])
print 'Going to run %s %s times' % (JOB, LOOPS)
HostName = platform.node()
KernelVersion = platform.release()
DIST = str.lower(platform.dist()[0])
CONFIG = 'defconfig'


def test_result(TestCommand, TestCaseID):
    # For each step of test run, print pass or fail to test log.
    if call(TestCommand) == 0:
        print '%s pass' % (TestCaseID)
        return True
    else:
        print '%s fail' % (TestCaseID)
        return False


def find_user(name):
    # Create user 'lkp' if it doesn't exist.
    try:
        return pwd.getpwnam(name)
    except KeyError:
        return None


# pre-config.
if not find_user('lkp'):
    print 'creating user lkp...'
    call(['useradd', '--create-home', '--home-dir', '/home/lkp', 'lkp'])
else:
    print 'User lkp already exists.'

if not os.path.exists('/home/lkp'):
    call(['mkdir', '-p', '/home/lkp'])

call(['chown', '-R', 'lkp:lkp', '/home/lkp'])

f = open('/etc/apt/sources.list.d/multiverse.list', 'w')
f.write('deb http://ports.ubuntu.com/ubuntu-ports/ vivid multiverse\n')
f.close()
call(['apt-get', 'update'])

# Split test job.
if not os.path.exists(WD + '/' + JOB):
    os.makedirs(WD + '/' + JOB)
SplitJob = [LKPPath + '/sbin/split-job', '--output', WD + '/' + JOB,
            LKPPath + '/jobs/' + JOB + '.yaml']
print 'Splitting job %s with command: %s' % (JOB, SplitJob)
if not test_result(SplitJob, 'split-job-' + JOB):
    sys.exit(1)

# Setup test job.
SubTests = glob.glob(WD + '/' + JOB + '/*.yaml')
print 'Sub-tests of %s: %s' % (JOB, SubTests)
SetupLocal = [LKPPath + '/bin/setup-local', SubTests[0]]
print 'Set up %s test with command: %s' % (JOB, SetupLocal)
if not test_result(SetupLocal, 'setup-local-' + JOB):
    sys.exit(1)

# Delete test results from last lava-test-shell-run.
if os.path.exists('/result/'):
    shutil.rmtree('/result/', ignore_errors=True)

# Run tests.
for SubTest in SubTests:
    COUNT = 1
    DONE = True
    SubTestCaseID = os.path.basename(SubTest)[:-5]
    ResultRoot = str('/'.join(['/result', JOB,
                               SubTestCaseID[int(len(JOB) + 1):], HostName,
                               DIST, CONFIG, KernelVersion]))
    while (COUNT <= LOOPS):
        # Use suffix for mutiple runs.
        if LOOPS > 1:
            SUFFIX = '-run' + str(COUNT)
        else:
            SUFFIX = ''

        RunLocal = [LKPPath + '/bin/run-local', SubTest]
        print 'Running test %s%s with command: %s' % (SubTestCaseID, SUFFIX,
                                                      RunLocal)
        if not test_result(RunLocal, 'run-local-' + SubTestCaseID + SUFFIX):
            DONE = False
            break

        # For each run, decode JOB.json to pick up the scores produced by the
        # benchmark itself.
        ResultFile = ResultRoot + '/' + str(COUNT - 1) + '/' + JOB + '.json'
        if not os.path.isfile(ResultFile):
            print '%s not found' % (ResultFile)
        else:
            JsonData = open(ResultFile)
            DICT = json.load(JsonData)
            for item in DICT:
                call(['lava-test-case', SubTestCaseID + '-' + item + SUFFIX,
                      '--result', 'pass', '--measurement', str(DICT[item][0])])
            JsonData.close()

        COUNT = COUNT + 1

    # For mutiple runs, if all runs are completed and results found, decode
    # avg.json.
    if LOOPS > 1 and DONE:
        AvgFile = ResultRoot + '/' + 'avg.json'
        if not os.path.isfile(ResultFile):
            print '%s not found' % (ResultFile)
        elif not os.path.isfile(AvgFile):
            print '%s not found' % (AvgFile)
        else:
            JsonData = open(ResultFile)
            AvgJsonData = open(AvgFile)
            DICT = json.load(JsonData)
            AvgDict = json.load(AvgJsonData)
            for item in DICT:
                if item in AvgDict:
                    call(['lava-test-case',
                          SubTestCaseID + '-' + item + '-avg', '--result',
                          'pass', '--measurement', str(AvgDict[item])])
            JsonData.close()
            AvgJsonData.close()

    # Compress and attach raw data.
    call(['tar', 'caf', 'lkp-result-' + JOB + '.tar.xz', '/result/' + JOB])
    call(['lava-test-run-attach', 'lkp-result-' + JOB + '.tar.xz'])

if not DONE:
    sys.exit(1)
else:
    sys.exit(0)