Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: third_party/grpc/tools/run_tests/dockerjob.py

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2015, Google Inc.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Helpers to run docker instances as jobs."""
31
32 import jobset
33 import tempfile
34 import time
35 import uuid
36 import os
37 import subprocess
38
39 _DEVNULL = open(os.devnull, 'w')
40
41
42 def random_name(base_name):
43 """Randomizes given base name."""
44 return '%s_%s' % (base_name, uuid.uuid4())
45
46
47 def docker_kill(cid):
48 """Kills a docker container. Returns True if successful."""
49 return subprocess.call(['docker','kill', str(cid)],
50 stdin=subprocess.PIPE,
51 stdout=_DEVNULL,
52 stderr=subprocess.STDOUT) == 0
53
54
55 def docker_mapped_port(cid, port, timeout_seconds=15):
56 """Get port mapped to internal given internal port for given container."""
57 started = time.time()
58 while time.time() - started < timeout_seconds:
59 try:
60 output = subprocess.check_output('docker port %s %s' % (cid, port),
61 stderr=_DEVNULL,
62 shell=True)
63 return int(output.split(':', 2)[1])
64 except subprocess.CalledProcessError as e:
65 pass
66 raise Exception('Failed to get exposed port %s for container %s.' %
67 (port, cid))
68
69
70 def finish_jobs(jobs):
71 """Kills given docker containers and waits for corresponding jobs to finish"""
72 for job in jobs:
73 job.kill(suppress_failure=True)
74
75 while any(job.is_running() for job in jobs):
76 time.sleep(1)
77
78
79 def image_exists(image):
80 """Returns True if given docker image exists."""
81 return subprocess.call(['docker','inspect', image],
82 stdin=subprocess.PIPE,
83 stdout=_DEVNULL,
84 stderr=subprocess.STDOUT) == 0
85
86
87 def remove_image(image, skip_nonexistent=False, max_retries=10):
88 """Attempts to remove docker image with retries."""
89 if skip_nonexistent and not image_exists(image):
90 return True
91 for attempt in range(0, max_retries):
92 if subprocess.call(['docker','rmi', '-f', image],
93 stdin=subprocess.PIPE,
94 stdout=_DEVNULL,
95 stderr=subprocess.STDOUT) == 0:
96 return True
97 time.sleep(2)
98 print 'Failed to remove docker image %s' % image
99 return False
100
101
102 class DockerJob:
103 """Encapsulates a job"""
104
105 def __init__(self, spec):
106 self._spec = spec
107 self._job = jobset.Job(spec, bin_hash=None, newline_on_success=True, travis= True, add_env={})
108 self._container_name = spec.container_name
109
110 def mapped_port(self, port):
111 return docker_mapped_port(self._container_name, port)
112
113 def kill(self, suppress_failure=False):
114 """Sends kill signal to the container."""
115 if suppress_failure:
116 self._job.suppress_failure_message()
117 return docker_kill(self._container_name)
118
119 def is_running(self):
120 """Polls a job and returns True if given job is still running."""
121 return self._job.state(jobset.NoCache()) == jobset._RUNNING
OLDNEW
« no previous file with comments | « third_party/grpc/tools/run_tests/distribtest_targets.py ('k') | third_party/grpc/tools/run_tests/interop_html_report.template » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698