aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/bots/infra_tests.py
blob: 7150e0408e1e77820e3bbf8b9be584dd422e9262 (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
#!/usr/bin/env python
#
# Copyright 2016 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


"""Run all infrastructure-related tests."""


import os
import subprocess
import sys


INFRA_BOTS_DIR = os.path.dirname(os.path.realpath(__file__))
SKIA_DIR = os.path.abspath(os.path.join(INFRA_BOTS_DIR, os.pardir, os.pardir))


def test(cmd, cwd):
  try:
    subprocess.check_output(cmd, cwd=cwd, stderr=subprocess.STDOUT)
  except subprocess.CalledProcessError as e:
    return e.output


def python_unit_tests(train):
  if train:
    return None
  return test(
      ['python', '-m', 'unittest', 'discover', '-s', '.', '-p', '*_test.py'],
      INFRA_BOTS_DIR)


def recipe_test(train):
  cmd = [
      'python', os.path.join(INFRA_BOTS_DIR, 'recipes.py'), 'test']
  if train:
    cmd.append('train')
  else:
    cmd.append('run')
  return test(cmd, SKIA_DIR)


def gen_tasks_test(train):
  cmd = ['go', 'run', 'gen_tasks.go']
  if not train:
    cmd.append('--test')
  try:
    output = test(cmd, INFRA_BOTS_DIR)
  except OSError:
    return ('Failed to run "%s"; do you have Go installed on your machine?'
            % ' '.join(cmd))
  if output and 'cannot find package "go.skia.org/infra' in output:
    return ('Failed to run gen_tests.go:\n\n%s\nMaybe you need to run:\n\n'
            '$ go get -u go.skia.org/infra/...' % output)
  return output


def main():
  train = False
  if '--train' in sys.argv:
    train = True

  tests = (
      python_unit_tests,
      recipe_test,
      gen_tasks_test,
  )
  errs = []
  for t in tests:
    err = t(train)
    if err:
      errs.append(err)

  if len(errs) > 0:
    print >> sys.stderr, 'Test failures:\n'
    for err in errs:
      print >> sys.stderr, '=============================='
      print >> sys.stderr, err
      print >> sys.stderr, '=============================='
    sys.exit(1)

  if train:
    print 'Trained tests successfully.'
  else:
    print 'All tests passed!'


if __name__ == '__main__':
  main()