/
bodhi-ci
executable file
·1344 lines (1102 loc) · 47.9 KB
/
bodhi-ci
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# Copyright © 2018 Red Hat, Inc.
#
# This file is part of Bodhi.
#
# 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.
"""Bodhi's CI command tool."""
import asyncio
import datetime
import functools
import multiprocessing
import os
import shutil
import signal
import subprocess
import sys
import typing
import uuid
import click
CONTAINER_NAME = 'bodhi-ci'
# We label the containers we run so it's easy to find them when we run _stop_all_jobs() at the end.
# UUID is used so that one bodhi-ci process does not stop jobs started by a different one.
CONTAINER_LABEL = 'purpose=bodhi-ci-{}'.format(uuid.uuid4())
# This template is used to generate the summary lines that are printed out at the end.
LABEL_TEMPLATE = '{:>8}-{:<27}'
PROJECT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
RELEASES = ('f28', 'f29', 'rawhide', 'pip')
def _set_concurrency(ctx, param, value):
"""
Set up the concurrency_semaphore.
Args:
ctx (click.core.Context): The Click context, unused.
param (click.core.Option): The option being handled. Unused.
value (str): The value of the -j flag.
Returns:
str: The value of the -j flag.
"""
Job.concurrency_semaphore = asyncio.Semaphore(value=value)
# We don't want too many integration test jobs to run at once because they consume
# many system resources and will fail due to timeouts. Therefore, let's set another semaphore
# to limit how many of these can run at once that cannot be greater than an arbitrary value.
# The arbitrary value was chosen because it seems to work in a Vagrant guest on a
# Lenovo T480s.
IntegrationJob.extra_concurrency_semaphore = asyncio.Semaphore(value=max(1, value / 4))
return value
def _set_container_runtime(ctx, param, value):
"""
Set up the container_runtime variable.
Args:
ctx (click.core.Context): The Click context, unused.
param (click.core.Option): The option being handled. Unused.
value (str): The value of the --container-runtime flag.
Returns:
str: The value of the --container-runtime flag.
"""
global CONTAINER_NAME
global container_runtime
container_runtime = value
if value == 'podman':
# Workaround for https://github.com/containers/buildah/issues/1034
CONTAINER_NAME = 'localhost/{}'.format(CONTAINER_NAME)
return value
def _set_global(ctx, param, value):
"""
Set up a global variable based on click input.
Args:
ctx (click.core.Context): The Click context, unused.
param (click.core.Option): The option being handled. Used to find the global we are setting.
value (str): The value of the flag.
Returns:
bool: The value of the flag.
"""
globals()[param.name] = value
return value
archive_option = click.option(
'--archive/--no-archive', is_flag=True, default=True,
help=("Collect *.xml from the tests and put them into test_results/."))
archive_path_option = click.option(
'--archive-path', envvar='BODHI_CI_ARCHIVE_PATH',
default=os.path.join(PROJECT_PATH, 'test_results'),
help='Define where test results should be placed if -a is used.')
concurrency_option = click.option(
'--concurrency', '-j', default=multiprocessing.cpu_count(), callback=_set_concurrency, type=int,
help=('Number of concurrent processes to run. Integration test runs are separately limited due '
'to resource contention. Defaults to the number of cores detected'))
container_runtime_option = click.option(
'--container-runtime', '-c', default='docker', type=click.Choice(['docker', 'podman']),
help='Select the container runtime to use. Defaults to docker.',
callback=_set_container_runtime)
failfast_option = click.option('--failfast', '-x', is_flag=True,
help='Exit immediately upon error.', callback=_set_global)
init_option = click.option(
'--init/--no-init', default=True, callback=_set_global,
help=("Use docker's --init flag. This option only has an effect when docker is "
"the container runtime being used."))
no_build_option = click.option(
'--no-build', is_flag=True, callback=_set_global,
help='Do not run docker build if the image already exists.')
pyver_option = click.option(
'--pyver', '-p', default=[2, 3], multiple=True, type=int,
help=("Limit to a particular Python major version. May be specified multiple times. "
"Acceptable values: 2, 3"))
releases_option = click.option(
'--release', '-r', default=list(RELEASES), multiple=True,
help=("Limit to a particular release. May be specified multiple times. "
"Acceptable values: {}".format(', '.join(RELEASES))))
tty_option = click.option('--tty/--no-tty', default=True, help='Allocate a pseudo-TTY.',
callback=_set_global)
container_runtime = 'docker'
failfast = False
init = True
# If True, we will try to skip running any builds if suitable builds already exist. Set by
# _set_global().
no_build = False
tty = False
@click.group()
def cli():
"""
Bodhi's Continuous Integration helper script.
"""
@cli.command()
@archive_option
@archive_path_option
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@pyver_option
@releases_option
@tty_option
def all(archive, archive_path, concurrency, container_runtime, no_build, failfast, init, pyver,
release, tty):
"""Run all the types of tests in parallel."""
_run_jobs(_build_jobs_list('all', concurrency, release, pyvers=pyver, archive=archive,
archive_path=archive_path))
@cli.command()
@concurrency_option
@container_runtime_option
@failfast_option
@releases_option
@tty_option
def build(concurrency, container_runtime, failfast, release, tty):
"""Build the containers for testing."""
_run_jobs(_build_jobs_list('build', concurrency, release))
@cli.command()
@concurrency_option
@container_runtime_option
@init_option
@releases_option
@tty_option
def clean(concurrency, container_runtime, init, release, tty):
"""Remove all builds pertaining to Bodhi CI."""
buffer_output = concurrency != 1 or len(release) != 1
clean_jobs = [
CleanJob(r, buffer_output=buffer_output) for r in release
] + [
IntegrationCleanJob(app_name="bodhi", release=r, buffer_output=buffer_output)
for r in release
] + [
IntegrationCleanJob(app_name=app_name, release="prod", buffer_output=buffer_output)
for app_name in ("resultsdb", "waiverdb", "greenwave")
]
_run_jobs(clean_jobs)
@cli.command()
@archive_option
@archive_path_option
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@pyver_option
@releases_option
@tty_option
def diff_cover(archive, archive_path, concurrency, container_runtime, no_build, failfast, init,
pyver, release, tty):
"""Run the diff cover test."""
_run_jobs(_build_jobs_list('diff_cover', concurrency, release, pyvers=pyver, archive=archive,
archive_path=archive_path))
@cli.command()
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@releases_option
@tty_option
def docs(concurrency, container_runtime, failfast, init, no_build, release, tty):
"""Build the docs."""
_run_jobs(_build_jobs_list('docs', concurrency, release))
@cli.command()
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@releases_option
@tty_option
def flake8(concurrency, container_runtime, failfast, init, no_build, release, tty):
"""Run flake8 tests."""
_run_jobs(_build_jobs_list('flake8', concurrency, release))
@cli.command()
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@releases_option
@tty_option
def mypy(concurrency, container_runtime, failfast, init, no_build, release, tty):
"""Run mypy tests."""
_run_jobs(_build_jobs_list('mypy', concurrency, release))
@cli.command()
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@releases_option
@tty_option
def pydocstyle(concurrency, container_runtime, failfast, init, no_build, release, tty):
"""Run pydocstyle tests."""
_run_jobs(_build_jobs_list('pydocstyle', concurrency, release))
@cli.command()
@archive_option
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@pyver_option
@releases_option
@archive_path_option
@tty_option
def unit(archive, concurrency, container_runtime, no_build, failfast, init, pyver, release,
archive_path, tty):
"""Run the unit tests."""
_run_jobs(_build_jobs_list('unit', concurrency, release, pyvers=pyver, archive=archive,
archive_path=archive_path))
@cli.command("integration-build")
@concurrency_option
@container_runtime_option
@failfast_option
@pyver_option
@releases_option
@tty_option
def integration_build(concurrency, container_runtime, failfast, pyver, release, tty):
"""Build the containers for integration testing."""
_run_jobs(_build_jobs_list('integration-build', concurrency, release, pyvers=pyver))
@cli.command("integration-download")
@concurrency_option
@failfast_option
@tty_option
def integration_download(concurrency, failfast, tty):
"""Download the database dumps for integration testing."""
_run_jobs(_build_jobs_list('integration-download', concurrency, releases=[], pyvers=[3]))
@cli.command()
@archive_option
@concurrency_option
@container_runtime_option
@failfast_option
@init_option
@no_build_option
@pyver_option
@releases_option
@archive_path_option
@tty_option
def integration(archive, concurrency, container_runtime, no_build, failfast, init, pyver, release,
archive_path, tty):
"""Run the integration tests."""
_run_jobs(_build_jobs_list('integration', concurrency, release, pyvers=pyver, archive=archive,
archive_path=archive_path))
class ProgressReporter(object):
"""Report progress on the provided jobs.
Attributes:
jobs: A list of the Jobs to report on.
"""
def __init__(self, jobs: typing.List['Job']):
"""
Args:
jobs: A list of the Jobs to report on.
"""
self._jobs = [] # type: typing.List[Job]
for job in jobs:
self.register_job(job)
def register_job(self, job):
"""Register a job to be reported.
Args:
job (Job): A job to report the status of.
"""
self._jobs.append(job)
loop = asyncio.get_event_loop()
loop.create_task(self._print_on_complete(job))
async def _print_on_complete(self, job):
"""Print the status of a job when it's complete."""
await job.complete.wait()
self.print_status()
def print_status(self):
"""Print a status report on all the jobs."""
for job in self._jobs:
click.echo(job.summary_line)
click.echo('\n')
class EmptySemaphore(object):
"""
Implements a no-op context manager.
This is used as the initial value on Job for the concurrency_semaphore and
extra_concurrency_semaphore attributes so that they can be assumed to work with the async with
statement if they are used without being turned into Semaphores.
"""
async def __aenter__(self, *args, **kwargs):
"""Calls pass."""
pass
async def __aexit__(self, *args, **kwargs):
"""Calls pass."""
pass
class Job(object):
"""
Represent a CI job.
This is intended to be a superclass for specific CI jobs, such as building container images, or
running tests.
Attributes:
archive_dir (str): A path where this job's test results is on the host. Only set if an
archive is used (not used on all jobs).
release (str): The release this job is testing.
depends_on (list of Job): This Job will wait for all the jobs' complete Event in the list
to start.
cancelled (bool): True if this Job has been cancelled, False otherwise.
complete (asyncio.Event): An Event that allows other Jobs to wait for this Job to complete.
complete.set() is called at the end of run().
returncode (int or None): If the Job's process has finished, this will be set to its
returncode. Otherwise it will be None.
skipped (bool): If True, this Job was skipped.
"""
# A template to name the image that is built. CONTAINER_NAME and the release gets substituted
# into the {}'s.
_container_image_template = '{}/{}'
# Limit how many Jobs can run at once. This is set by _set_concurrency().
concurrency_semaphore = EmptySemaphore()
# Jobs can set this if they want to have some additional limitations on how many of them can
# run at once. The IntegrationJob uses this, for example, so that we don't run too many of
# them no matter what the -j flag is set to. This is set by _set_concurrency().
extra_concurrency_semaphore = EmptySemaphore()
def __init__(self, release: str,
depends_on: typing.Union[typing.List['Job'], 'Job'] = None,
buffer_output: bool = False):
"""
Initialize the new Job.
Args:
release: The release this Job pertains to.
depends_on: A list of Job that this Job should wait to complete before
starting. It is also possible to provide a Job instance if there is a single
dependency.
buffer_output: If True, this Job will buffer its stdout and stderr into its
output property. If False, child processes will send their output straign to stdout
and stderr.
"""
self.release = release
self.depends_on = depends_on or []
if not isinstance(self.depends_on, list):
self.depends_on = [self.depends_on]
self.cancelled = False
# Used to block dependent processes until this Job is done.
self.complete = asyncio.Event()
self.started = False
self.returncode = None
self.skipped = False
self._container_image = self._container_image_template.format(CONTAINER_NAME, self.release)
self._popen_kwargs = {'shell': False} # type: typing.Mapping[str, typing.Union[bool, int]]
if buffer_output:
# Let's buffer the output so the user doesn't see a jumbled mess.
self._popen_kwargs['stdout'] = subprocess.PIPE
self._popen_kwargs['stderr'] = subprocess.STDOUT
self._stdout = b''
def __repr__(self):
return "<{} release={!r}>".format(self.__class__.__name__, self.release)
@property
def label(self):
"""
Return a label that represents this job.
This label is used for the status line at the end of the bodhi-ci script, and is also
prepended to each line of output.
Returns:
str: A label to represent this Job.
"""
return LABEL_TEMPLATE.format(self.release, self._label)
@property
def output(self):
"""
Run decode on the output, and then prepend label in front of each line.
Returns:
str: The output from the process.
"""
if not self._stdout:
return ''
output = self._stdout.decode()
return '\n'.join(['{}\t{}'.format(self.label, line) for line in output.split('\n')])
async def run(self):
"""
Run the job, returning itself.
Returns:
Job: Returns self.
"""
try:
for job in self.depends_on:
await job.complete.wait()
if job.returncode != 0 and not job.skipped:
# If the Job we depend on failed, we should cancel.
raise asyncio.CancelledError()
async with self.extra_concurrency_semaphore, self.concurrency_semaphore:
# It's possible that we got cancelled while we were waiting on the Semaphore.
if not self.cancelled:
self._pre_start_hook()
self._start_time = datetime.datetime.utcnow()
self.started = True
if not self._popen_kwargs["shell"]:
process = await asyncio.create_subprocess_exec(
*self._command, **self._popen_kwargs)
else:
process = await asyncio.create_subprocess_shell(
*self._command, **self._popen_kwargs)
try:
self._stdout, stderr = await process.communicate()
if process.returncode < 0:
# A negative exit code means that our child process was sent a signal,
# so let's mark this task as cancelled.
raise asyncio.CancelledError()
except asyncio.CancelledError:
try:
process.terminate()
except ProcessLookupError:
# The process is already stopped, nothing to see here.
pass
cancelled_stdout, stderr = await process.communicate()
if self._stdout:
self._stdout = self._stdout + cancelled_stdout
else:
self._stdout = cancelled_stdout
raise
finally:
self.returncode = process.returncode
if self.returncode:
# If there was a failure, we need to raise an Exception in case the failfast flag
# was set, so that _run_jobs() can cancel the remaining tasks.
error = RuntimeError()
error.result = self
raise error
except asyncio.CancelledError:
self.cancelled = True
finally:
# If the job's been cancelled or successful, let's go ahead and print its output now.
# Failed jobs will have their output printed at the end.
if self.output and (self.returncode == 0 or self.cancelled):
click.echo(self.output)
self._finish_time = datetime.datetime.utcnow()
# Kick off any tasks that were waiting for us to finish.
self.complete.set()
return self
@property
def summary_line(self):
"""
Create a summary line for the Job.
If the exit_code indicates failure, it is printed to the console immediately. Failed jobs'
stdout is not printed until the end of the job, so this gives the user a way to know that a
job failed before its output is printed, and they can ctrl-c to see its output.
Returns:
str: A summary line suitable to print at the end of the process.
"""
blue_start = '\033[0;34m' if tty else ''
yellow_start = '\033[0;33m' if tty else ''
color_end = '\033[0m' if tty else ''
if not self.complete.is_set():
if not self.started:
return '{}: {}WAITING{}'.format(self.label, blue_start, color_end)
else:
return '{}: {}RUNNING{}'.format(self.label, yellow_start, color_end)
if self.cancelled:
if hasattr(self, '_start_time'):
return '{}: {}CANCELED{} [{}]'.format(self.label, yellow_start, color_end,
self._finish_time - self._start_time)
return '{}: {}CANCELED{}'.format(self.label, yellow_start, color_end)
if self.skipped:
return '{}: {}SKIPPED{}'.format(self.label, blue_start, color_end)
if self.returncode == 0:
color_start = '\033[0;32m' if tty else ''
return '{}: {}SUCCESS!{} [{}]'.format(self.label, color_start, color_end,
self._finish_time - self._start_time)
else:
color_start = '\033[0;31m' if tty else ''
color_end = '\033[0m' if tty else ''
return '{}: {}FAILED{} [{}] (exited with code: {})'.format(
self.label, color_start, color_end, self._finish_time - self._start_time,
self.returncode)
def _convert_command_for_container(self, archive=False, archive_path=''):
"""
Use this to convert self._command to run in a container.
This method is a convenience method that allows Jobs to define their self._command
attribute in a simple fashion, without having to redefine all the machinery to run the
command in a container. This method replaces self._command with a command that will run it
in a container.
Args:
archive_path (str): A path on the host to share as a volume into the container for
its /results path.
archive (bool): Whether to mount a shared volume from the host into the container for
archival purposes.
"""
args = [container_runtime, 'run', '--network', 'none', '--rm',
'--label', CONTAINER_LABEL]
# podman does not support --init: https://github.com/containers/libpod/issues/1670
if init and container_runtime == 'docker':
args.append('--init')
if tty:
args.append('-t')
if archive:
self.archive_dir = '{}/{}'.format(archive_path,
'{}-{}'.format(self.release, self._label))
args.extend(['-v', '{}:/results:z'.format(self.archive_dir)])
args.append(self._container_image)
args.extend(self._command)
self._command = args
def _pre_start_hook(self):
"""Print a message announcing that we are running now."""
click.echo('Running {}'.format(' '.join(self._command)))
class BuildJob(Job):
"""
Define a Job for building container images.
See the Job superclass's docblock for details about its attributes.
"""
_label = 'build'
# A template for finding the name of the Dockerfile to be used for this BuildJob. The release
# gets substituted into the {}'s.
_dockerfile_template = 'Dockerfile-{}'
# Extra arguments to be passed to the container build command.
_build_args = ['--force-rm', '--pull']
def __init__(self, *args, **kwargs):
"""
Initialize the BuildJob.
See the superclass's docblock for details about accepted parameters.
"""
super(BuildJob, self).__init__(*args, **kwargs)
dockerfile = os.path.join(PROJECT_PATH, 'devel', 'ci',
self._dockerfile_template.format(self.release))
self._command = [container_runtime, 'build', '-t', self._container_image,
'-f', dockerfile, '.']
if self._build_args:
for arg in reversed(self._build_args):
self._command.insert(2, arg)
async def run(self):
"""
Run the BuildJob, unless --no-build has been requested and the needed build already exists.
Returns:
BuildJob: Returns self.
"""
if no_build and self._build_exists:
self.complete.set()
self.skipped = True
else:
await super(BuildJob, self).run()
return self
@property
def _build_exists(self):
"""
Determine whether a container image exists for this build job.
Returns:
bool: True if a build exists, False otherwise.
"""
args = [container_runtime, 'images', self._container_image]
images = subprocess.check_output(args).decode()
if self._container_image in images:
return True
return False
class CleanJob(Job):
"""
Define a Job for removing all container images built by bodhi-ci.
See the Job superclass's docblock for details about its attributes.
"""
_label = 'clean'
def __init__(self, *args, **kwargs):
"""
Initialize the CleanJob.
See the superclass's docblock for details about accepted parameters.
"""
super(CleanJob, self).__init__(*args, **kwargs)
self._command = [container_runtime, 'rmi', self._container_image]
class DiffCoverJob(Job):
"""
Define a Job for running diff-cover on the test results.
See the Job superclass's docblock for details about its attributes.
"""
_label = 'diff-cover'
def __init__(self, archive, archive_path, pyver, *args, **kwargs):
"""
Initialize the DiffCoverJob.
See the superclass's docblock for details about additional accepted parameters.
Args:
archive (bool): If True, set up the volume mount so we can retrieve the test results
from the container.
archive_path (str): A path on the host to share as a volume into the container for
its /results path.
pyver (int): Which major version of Python we are testing with.
"""
super(DiffCoverJob, self).__init__(*args, **kwargs)
self.archive_path = archive_path
self._command = ['/usr/bin/diff-cover', '/results/coverage.xml',
'--compare-branch=origin/develop', '--fail-under=100']
self._label = '{}-py{}'.format(self._label, pyver)
self._convert_command_for_container(archive=archive, archive_path=archive_path)
def _pre_start_hook(self):
"""
Copy the coverage.xml from the unit test job to the diff_cover container, then announce.
"""
if not os.path.exists(self.archive_dir):
os.makedirs(self.archive_dir)
shutil.copy(os.path.join(self.depends_on[0].archive_dir, 'coverage.xml'),
os.path.join(self.archive_dir, 'coverage.xml'))
super(DiffCoverJob, self)._pre_start_hook()
class DocsJob(Job):
"""
Define a Job for building docs.
See the Job superclass's docblock for details about its attributes.
"""
_command = [
'/usr/bin/bash', '-c',
('/usr/bin/python3 setup.py develop && make -C docs clean && make -C docs html && make '
'-C docs man')]
_label = 'docs'
def __init__(self, *args, **kwargs):
"""
Initialize the DocsJob.
See the superclass's docblock for details about accepted parameters.
"""
super(DocsJob, self).__init__(*args, **kwargs)
self._convert_command_for_container()
class Flake8Job(Job):
"""
Define a Job for running flake8.
See the Job superclass's docblock for details about its attributes.
"""
_label = 'flake8'
def __init__(self, *args, **kwargs):
"""
Initialize the Flake8Job.
See the superclass's docblock for details about accepted parameters.
"""
super(Flake8Job, self).__init__(*args, **kwargs)
if self.release == 'pip':
self._command = ['/usr/local/bin/flake8']
else:
self._command = ['/usr/bin/flake8']
self._convert_command_for_container()
class MyPyBuildJob(BuildJob):
"""
Define a Job for building mypy container images.
See the Job superclass's docblock for details about its attributes.
"""
# We don't want the --pull build arg because we are based on a container built by bodhi-ci.
_build_args = [] # type: typing.List[str]
_container_image_template = '{}/{}/mypy'
_dockerfile_template = 'Dockerfile-mypy-{}'
_label = 'mypy-build'
class MyPyJob(Job):
"""
Define a Job for running mypy.
See the Job superclass's docblock for details about its attributes.
"""
_container_image_template = '{}/{}/mypy'
_label = 'mypy'
def __init__(self, *args, **kwargs):
"""
Initialize the MyPyJob.
See the superclass's docblock for details about accepted parameters.
"""
super(MyPyJob, self).__init__(*args, **kwargs)
self._command = ['/usr/local/bin/mypy', '--follow-imports', 'silent',
'bodhi/server/bugs.py', 'devel/ci/bodhi-ci']
self._convert_command_for_container()
class PydocstyleJob(Job):
"""
Define a Job for running pydocstyle.
See the Job superclass's docblock for details about its attributes.
"""
_label = 'pydocstyle'
def __init__(self, *args, **kwargs):
"""
Initialize the PydocstyleJob.
See the superclass's docblock for details about accepted parameters.
"""
super(PydocstyleJob, self).__init__(*args, **kwargs)
if self.release == 'pip':
self._command = ['/usr/local/bin/pydocstyle', 'bodhi']
else:
self._command = ['/usr/bin/pydocstyle', 'bodhi']
self._convert_command_for_container()
class StopJob(Job):
"""
Define a Job for stopping all containers started by this process.
See the Job superclass's docblock for details about its attributes.
"""
_label = 'stop'
def __init__(self, *args, **kwargs):
"""
Initialize the StopJob.
See the superclass's docblock for details about accepted parameters.
"""
super(StopJob, self).__init__(*args, **kwargs)
self._command = [container_runtime, 'stop', self.release]
self._popen_kwargs['stdout'] = subprocess.DEVNULL
def _pre_start_hook(self):
"""Do not announce this Job; it is noisy."""
pass
class UnitJob(Job):
"""
Define a Job for running the unit tests.
See the Job superclass's docblock for details about its attributes.
Attributes:
pyver (int): The Python version we are testing.
"""
_label = 'unit'
def __init__(self, archive, archive_path, pyver, *args, **kwargs):
"""
Initialize the UnitJob.
See the superclass's docblock for details about additional accepted parameters.
Args:
archive (bool): If True, set up the volume mount so we can retrieve the test results
from the container.
archive_path (str): A path on the host to share as a volume into the container for
its /results path.
pyver (int): Which major version of Python we are testing with.
"""
super(UnitJob, self).__init__(*args, **kwargs)
self.pyver = pyver
self._label = '{}-py{}'.format(self._label, pyver)
pytest_flags = '--junit-xml=nosetests.xml bodhi/tests'
if failfast:
pytest_flags += ' -x'
test_command = ('({} setup.py develop && {} {} || (cp *.xml /results && exit 1)) '
'&& cp *.xml /results')
if pyver == 2:
# We do not support the server on Python 2:
test_command = (
'rm requirements.txt && touch requirements.txt && rm .coveragerc && '
'rm -rf bodhi/tests/server && {}'.format(test_command))
if self.release == 'pip':
# pip installs some of the executables in different places than Fedora does.
if pyver == 2:
self._command = [
'/usr/bin/bash', '-c',
test_command.format('/usr/bin/python2', '/usr/bin/py.test', pytest_flags)]
else:
self._command = [
'/usr/bin/bash', '-c',
test_command.format('/usr/bin/python3', '/usr/local/bin/py.test', pytest_flags)]
else:
if pyver == 2:
self._command = [
'/usr/bin/bash', '-c',
test_command.format('/usr/bin/python2', '/usr/bin/py.test-2', pytest_flags)]
else:
self._command = [
'/usr/bin/bash', '-c',
test_command.format('/usr/bin/python3', '/usr/bin/py.test-3', pytest_flags)]
self._convert_command_for_container(archive=archive, archive_path=archive_path)
async def run(self):
"""
Run the UnitJob, unless --no-build has been requested and the needed coverage data exists.
Returns:
UnitJob: Returns self.
"""
if no_build and os.path.exists(os.path.join(self.archive_dir, 'coverage.xml')):
self.complete.set()
self.skipped = True
else:
await super(UnitJob, self).run()
return self
class IntegrationBuildJob(BuildJob):
"""
Define a Job for building container images for integration testing.
See the Job superclass's docblock for details about its attributes.
"""
def __init__(self, app_name, *args, **kwargs):
"""
Initialize the IntegrationBuildJob.
See the superclass's docblock for details about accepted parameters.
"""
super(IntegrationBuildJob, self).__init__(*args, **kwargs)
self._app_name = app_name
self._label = 'integration-build-{}'.format(app_name)
self._container_image = '{}-integration-{}'.format(CONTAINER_NAME, app_name)
dockerfile = os.path.join(
PROJECT_PATH, 'devel', 'ci', 'integration', app_name, 'Dockerfile'
)
self._command = [container_runtime, 'build', '--force-rm', '--pull',
'-t', self._container_image, '-f', dockerfile, '.']
def __repr__(self):
return "<{} app={!r}>".format(self.__class__.__name__, self._app_name)
class IntegrationBodhiBuildJob(IntegrationBuildJob):
"""
Build Bodhi in a container image for integration testing.
See the Job superclass's docblock for details about its attributes.
"""
def __init__(self, *args, **kwargs):
"""
Initialize the IntegrationBodhiBuildJob.
See the superclass's docblock for details about accepted parameters.
"""