github.com/uber/kraken@v0.1.4/test/python/utils.py (about) 1 # Copyright (c) 2016-2019 Uber Technologies, Inc. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 from __future__ import absolute_import 15 16 import os 17 from socket import socket 18 from threading import Thread 19 20 21 def find_free_port(): 22 s = socket() 23 s.bind(('', 0)) 24 port = s.getsockname()[1] 25 s.close() 26 return port 27 28 29 class PortReservation(object): 30 """ 31 PortReservation is a utility for finding and reserving an open port. Normally, 32 find_free_port is sufficient for components which find an available port and 33 immediately start their container. However, the more consecutive find_free_port 34 calls are made without actually binding to the "free" ports, the higher chance 35 that successive find_free_port calls will return a port twice. 36 37 PortReservation solves this problem by binding a socket to the port until 38 the port is ready to be used, at which point the client can call release 39 to close the socket and assume ownership of the port. 40 41 Obviously this is not bullet-proof and there is a race between releasing 42 the PortReservation and the client binding to the port. 43 """ 44 45 def __init__(self): 46 self._sock = socket() 47 self._sock.bind(('', 0)) 48 self._open = True 49 self._port = self._sock.getsockname()[1] 50 51 def get(self): 52 return self._port 53 54 def release(self): 55 if self._open: 56 self._sock.close() 57 self._open = False 58 59 60 def concurrently_apply(f, inputs): 61 62 errors = [None] * len(inputs) 63 64 def worker(i): 65 try: 66 f(inputs[i]) 67 except Exception as e: 68 errors[i] = e 69 raise 70 71 threads = [Thread(target=worker, args=(i,)) for i in range(len(inputs))] 72 for t in threads: 73 t.start() 74 for t in threads: 75 t.join() 76 77 # If the exception is raised in a thread, it won't fail the test. 78 for e in errors: 79 assert e is None 80 81 82 def format_insecure_curl(url): 83 return ' '.join([ 84 'curl', 85 ## Use --insecure flag to disable server cert verification for test only. 86 '--insecure', 87 url, 88 ]) 89 90 91 def tls_opts(): 92 return { 93 'verify': False, ## Set verify=False to disable server cert verification for test only. 94 } 95 96 97 def tls_opts_with_client_certs(): 98 return { 99 'cert': ('test/tls/client/client.crt', 'test/tls/client/client_decrypted.key'), 100 'verify': False, ## Set verify=False to disable server cert verification for test only. 101 } 102 103 104 def dev_tag(image_name): 105 tag = os.getenv("PACKAGE_VERSION", "latest") 106 return "{}:{}".format(image_name, tag)