def run_init_system_image( base_image, with_socat=True, path=DOCKERFILES_DIR, dockerfile=None, ingest_host="ingest.us0.signalfx.com", # Whatever value is used here needs a self-signed cert in ./images/certs/ api_host="api.us0.signalfx.com", # Whatever value is used here needs a self-signed cert in ./images/certs/ command=None, buildargs=None, ): # pylint: disable=too-many-arguments image_id = retry( lambda: build_base_image(base_image, path, dockerfile, buildargs), docker.errors.BuildError) print("Image ID: %s" % image_id) if with_socat: backend_ip = "127.0.0.1" else: backend_ip = get_host_ip() with fake_backend.start(ip_addr=backend_ip) as backend: container_options = { # Init systems running in the container want permissions "privileged": True, "volumes": { "/sys/fs/cgroup": { "bind": "/sys/fs/cgroup", "mode": "ro" }, "/tmp/scratch": { "bind": "/tmp/scratch", "mode": "rw" }, }, "extra_hosts": { # Socat will be running on localhost to forward requests to # these hosts to the fake backend ingest_host: backend.ingest_host, api_host: backend.api_host, }, } if command: container_options["command"] = command with run_container(image_id, wait_for_ip=True, **container_options) as cont: if with_socat: # Proxy the backend calls through a fake HTTPS endpoint so that we # don't have to change the default configuration default by the # package. The base_image used should trust the self-signed certs # default in the images dir so that the agent doesn't throw TLS # verification errors. with socat_https_proxy(cont, backend.ingest_host, backend.ingest_port, ingest_host, "127.0.0.1"), socat_https_proxy( cont, backend.api_host, backend.api_port, api_host, "127.0.0.2"): yield [cont, backend] else: yield [cont, backend]
def run_kong(kong_version): pg_env = dict(POSTGRES_USER="******", POSTGRES_PASSWORD="******", POSTGRES_DB="kong") kong_env = dict( KONG_ADMIN_LISTEN="0.0.0.0:8001", KONG_LOG_LEVEL="warn", KONG_DATABASE="postgres", KONG_PG_DATABASE=pg_env["POSTGRES_DB"], KONG_PG_PASSWORD=pg_env["POSTGRES_PASSWORD"], ) with run_container("postgres:9.5", environment=pg_env) as db: db_ip = container_ip(db) kong_env["KONG_PG_HOST"] = db_ip assert wait_for(p(tcp_socket_open, db_ip, 5432)) with run_service("kong", buildargs={"KONG_VERSION": kong_version}, environment=kong_env, command="sleep inf") as migrations: if kong_version in ["0.15-centos", "1.0.0-centos"]: assert container_cmd_exit_0(migrations, "kong migrations bootstrap") else: assert container_cmd_exit_0(migrations, "kong migrations up") with run_service("kong", buildargs={"KONG_VERSION": kong_version}, environment=kong_env) as kong, run_container( "openresty/openresty:1.15.8.1-4-centos", files=[(SCRIPT_DIR / "echo.conf", "/etc/nginx/conf.d/echo.conf")]) as echo: kong_ip = container_ip(kong) kong_admin = f"http://{kong_ip}:8001" assert wait_for( p(http_status, url=f"{kong_admin}/signalfx", status=[200])) paths, _ = configure_kong(kong_admin, kong_version, container_ip(echo)) # Needs time to settle after creating routes. retry(lambda: run_traffic(paths, f"http://{kong_ip}:8000"), AssertionError, interval_seconds=2) yield kong_ip
def start_registry(self): self.get_client() print("\nStarting registry container localhost:%d in minikube ..." % self.registry_port) retry( p( self.client.containers.run, image="registry:2.7", name="registry", detach=True, environment={"REGISTRY_HTTP_ADDR": "0.0.0.0:%d" % self.registry_port}, ports={"%d/tcp" % self.registry_port: self.registry_port}, ), docker.errors.DockerException, ) assert wait_for( p(tcp_socket_open, self.container_ip, self.registry_port), timeout_seconds=30, interval_seconds=2 ), "timed out waiting for registry to start!"
def check_k8s_version(k8s_version): assert k8s_version, "K8S version not defined" k8s_latest_version = retry(get_latest_k8s_version, urllib.error.URLError) if k8s_version.lower() == "latest": k8s_version = k8s_latest_version k8s_version = k8s_version.lstrip("v") assert re.match(r"^\d+\.\d+\.\d+$", k8s_version), "Invalid K8S version '%s'" % k8s_version assert semver.match(k8s_version, ">=" + K8S_MIN_VERSION), "K8S version %s not supported" % k8s_version assert semver.match(k8s_version, "<=" + k8s_latest_version), "K8S version %s not supported" % k8s_version return "v" + k8s_version
def build_image(self, dockerfile_dir, build_opts=None, docker_url=None): """ Use low-level api client to build images in order to get build logs. Returns the image id. """ def _build(): client = docker.APIClient(base_url=docker_url, version="auto") build_log = [] has_error = False image_id = None for line in client.build(path=dockerfile_dir, rm=True, forcerm=True, **build_opts): json_line = json.loads(line) keys = json_line.keys() if "stream" in keys: build_log.append(json_line.get("stream").strip()) else: build_log.append(str(json_line)) if "error" in keys: has_error = True elif "aux" in keys: image_id = json_line.get("aux").get("ID") assert not has_error, "build failed for %s:\n%s" % ( dockerfile_dir, "\n".join(build_log)) assert image_id, "failed to get id from output for built image:\n%s" % "\n".join( build_log) return image_id if os.path.isdir(os.path.join(TEST_SERVICES_DIR, dockerfile_dir)): dockerfile_dir = os.path.join(TEST_SERVICES_DIR, dockerfile_dir) else: assert os.path.isdir( dockerfile_dir ), "Dockerfile directory %s not found!" % dockerfile_dir if build_opts is None: build_opts = {} if not docker_url: docker_url = "tcp://%s:2375" % self.container_ip print("\nBuilding image from %s ..." % dockerfile_dir) return retry(_build, AssertionError)