1# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6# Recipe for uploading DM results.
7
8
9import calendar
10
11
12DEPS = [
13  'recipe_engine/file',
14  'recipe_engine/json',
15  'recipe_engine/path',
16  'recipe_engine/properties',
17  'recipe_engine/step',
18  'recipe_engine/time',
19  'gsutil',
20]
21
22
23DM_JSON = 'dm.json'
24VERBOSE_LOG = 'verbose.log'
25
26
27def RunSteps(api):
28  builder_name = api.properties['buildername']
29  revision = api.properties['revision']
30
31  results_dir = api.path['start_dir'].join('test')
32
33  # Upload the images. It is *vital* that the images are uploaded first
34  # so they exist whenever the json is processed.
35  image_dest_path = 'gs://%s/dm-images-v1' % api.properties['gs_bucket']
36  for ext in ['.png', '.pdf']:
37    files_to_upload = api.file.glob_paths(
38        'find %s images' % ext,
39        results_dir,
40        '*%s' % ext,
41        test_data=['someimage.png'])
42    # For some reason, glob returns results_dir when it should return nothing.
43    files_to_upload = [f for f in files_to_upload if str(f).endswith(ext)]
44    if len(files_to_upload) > 0:
45      api.gsutil.cp('%s images' % ext, results_dir.join('*%s' % ext),
46                       image_dest_path, multithread=True)
47
48  # Compute the directory to upload results to
49  now = api.time.utcnow()
50  summary_dest_path = '/'.join([
51      'dm-json-v1',
52      str(now.year ).zfill(4),
53      str(now.month).zfill(2),
54      str(now.day  ).zfill(2),
55      str(now.hour ).zfill(2),
56      revision,
57      builder_name,
58      str(int(calendar.timegm(now.utctimetuple())))])
59
60  # Trybot results are further siloed by issue/patchset.
61  issue = api.properties.get('patch_issue')
62  patchset = api.properties.get('patch_set')
63  if issue and patchset:
64    summary_dest_path = '/'.join((
65        'trybot', summary_dest_path, str(issue), str(patchset)))
66
67  summary_dest_path = 'gs://%s/%s' % (api.properties['gs_bucket'],
68                                      summary_dest_path)
69
70  # Directly upload dm.json and verbose.log if it exists
71  json_file = results_dir.join(DM_JSON)
72  log_file = results_dir.join(VERBOSE_LOG)
73
74  api.gsutil.cp('dm.json', json_file,
75                summary_dest_path + '/' + DM_JSON, extra_args=['-Z'])
76
77  files = api.file.listdir('check for optional verbose.log file',
78                           results_dir, test_data=['dm.json', 'verbose.log'])
79  if log_file in files:
80    api.gsutil.cp('verbose.log', log_file,
81                  summary_dest_path + '/' + VERBOSE_LOG, extra_args=['-Z'])
82
83
84def GenTests(api):
85  builder = 'Test-Debian9-GCC-GCE-CPU-AVX2-x86_64-Debug'
86  yield (
87    api.test('normal_bot') +
88    api.properties(buildername=builder,
89                   gs_bucket='skia-infra-gm',
90                   revision='abc123',
91                   path_config='kitchen')
92  )
93
94  yield (
95    api.test('alternate_bucket') +
96    api.properties(buildername=builder,
97                   gs_bucket='skia-infra-gm-alt',
98                   revision='abc123',
99                   path_config='kitchen')
100  )
101
102  yield (
103    api.test('failed_once') +
104    api.properties(buildername=builder,
105                   gs_bucket='skia-infra-gm',
106                   revision='abc123',
107                   path_config='kitchen') +
108    api.step_data('upload .png images', retcode=1)
109  )
110
111  yield (
112    api.test('failed_all') +
113    api.properties(buildername=builder,
114                   gs_bucket='skia-infra-gm',
115                   revision='abc123',
116                   path_config='kitchen') +
117    api.step_data('upload .png images', retcode=1) +
118    api.step_data('upload .png images (attempt 2)', retcode=1) +
119    api.step_data('upload .png images (attempt 3)', retcode=1) +
120    api.step_data('upload .png images (attempt 4)', retcode=1) +
121    api.step_data('upload .png images (attempt 5)', retcode=1)
122  )
123
124  yield (
125      api.test('trybot') +
126      api.properties(
127          buildername=builder,
128          gs_bucket='skia-infra-gm',
129          revision='abc123',
130          path_config='kitchen',
131          patch_storage='gerrit') +
132      api.properties.tryserver(
133          buildername=builder,
134          gerrit_project='skia',
135          gerrit_url='https://skia-review.googlesource.com/',
136      )
137  )
138