github.com/uber/kraken@v0.1.4/test/python/conftest.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 subprocess 17 from collections import namedtuple 18 19 import pytest 20 21 from components import ( 22 Agent, 23 AgentFactory, 24 BuildIndex, 25 Cluster, 26 Origin, 27 OriginCluster, 28 Proxy, 29 Redis, 30 TestFS, 31 Tracker, 32 find_free_port, 33 get_docker_bridge, 34 ) 35 36 DEFAULT = 'default' 37 38 39 # It turns out that URL path escaping Docker tags is a common bug which is very 40 # annoying to debug in production. This function prefixes images with a "test/", 41 # such that if the "/" is not properly escaped, the tests will break. 42 def _setup_test_image(name): 43 new_name = 'test/' + name 44 for command in [ 45 ['docker', 'pull', name], 46 ['docker', 'tag', name, new_name], 47 ]: 48 subprocess.check_call(command) 49 return new_name 50 51 52 TEST_IMAGE = _setup_test_image('alpine:latest') 53 TEST_IMAGE_2 = _setup_test_image('redis:latest') 54 55 56 @pytest.fixture 57 def redis(): 58 redis = Redis(DEFAULT) 59 yield redis 60 redis.teardown() 61 62 63 @pytest.fixture 64 def tracker(redis, origin_cluster, testfs): 65 tracker = Tracker(DEFAULT, redis, origin_cluster) 66 yield tracker 67 tracker.teardown() 68 69 70 @pytest.fixture 71 def origin_cluster(testfs): 72 instances = { 73 name: Origin.Instance(name) 74 for name in ('kraken-origin-01', 'kraken-origin-02', 'kraken-origin-03') 75 } 76 origin_cluster = OriginCluster([ 77 Origin(DEFAULT, instances, name, testfs) 78 for name in instances 79 ]) 80 yield origin_cluster 81 for origin in origin_cluster: 82 origin.teardown() 83 84 85 @pytest.fixture 86 def agent_factory(tracker, build_index): 87 return AgentFactory(DEFAULT, tracker, [build_index]) 88 89 90 @pytest.fixture 91 def agent(agent_factory): 92 with agent_factory.create() as agent: 93 yield agent 94 95 96 @pytest.fixture 97 def proxy(origin_cluster, build_index): 98 proxy = Proxy(DEFAULT, origin_cluster, [build_index]) 99 yield proxy 100 proxy.teardown() 101 102 103 @pytest.fixture 104 def build_index(origin_cluster, testfs): 105 name = 'kraken-build-index-01' 106 instances = {name: BuildIndex.Instance(name)} 107 build_index = BuildIndex(DEFAULT, instances, name, origin_cluster, testfs, {}) 108 yield build_index 109 build_index.teardown() 110 111 112 @pytest.fixture 113 def testfs(): 114 testfs = TestFS(DEFAULT) 115 yield testfs 116 testfs.teardown() 117 118 119 def _create_build_index_instances(): 120 return { 121 name: BuildIndex.Instance(name) 122 for name in ('kraken-build-index-01', 'kraken-build-index-02', 'kraken-build-index-03') 123 } 124 125 126 @pytest.fixture 127 def one_way_replicas(): 128 Replicas = namedtuple('Replicas', ['src', 'dst']) 129 130 src_build_index_instances = _create_build_index_instances() 131 dst_build_index_instances = _create_build_index_instances() 132 133 replicas = Replicas( 134 src=Cluster('src', src_build_index_instances, [dst_build_index_instances.values()[0]]), 135 dst=Cluster('dst', dst_build_index_instances)) 136 137 yield replicas 138 139 replicas.src.teardown() 140 replicas.dst.teardown() 141 142 143 @pytest.fixture 144 def two_way_replicas(): 145 Replicas = namedtuple('Replicas', ['zone1', 'zone2']) 146 147 zone1_build_index_instances = _create_build_index_instances() 148 zone2_build_index_instances = _create_build_index_instances() 149 150 replicas = Replicas( 151 zone1=Cluster('zone1', zone1_build_index_instances, [zone2_build_index_instances.values()[0]]), 152 zone2=Cluster('zone2', zone2_build_index_instances, [zone1_build_index_instances.values()[0]])) 153 154 yield replicas 155 156 replicas.zone1.teardown() 157 replicas.zone2.teardown()