|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# The OpenSearch Contributors require contributions made to |
| 4 | +# this file be licensed under the Apache-2.0 license or a |
| 5 | +# compatible open source license. |
| 6 | +# Modifications Copyright OpenSearch Contributors. See |
| 7 | +# GitHub history for details. |
| 8 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 9 | +# license agreements. See the NOTICE file distributed with |
| 10 | +# this work for additional information regarding copyright |
| 11 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 12 | +# the Apache License, Version 2.0 (the "License"); you may |
| 13 | +# not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, |
| 19 | +# software distributed under the License is distributed on an |
| 20 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 21 | +# KIND, either express or implied. See the License for the |
| 22 | +# specific language governing permissions and limitations |
| 23 | +# under the License. |
| 24 | + |
| 25 | +import collections |
| 26 | +import os |
| 27 | +import shutil |
| 28 | +import tempfile |
| 29 | + |
| 30 | +import pytest |
| 31 | + |
| 32 | +import it |
| 33 | +from osbenchmark.utils import process |
| 34 | + |
| 35 | +HttpProxy = collections.namedtuple("HttpProxy", ["authenticated_url", "anonymous_url"]) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="module") |
| 39 | +def http_proxy(): |
| 40 | + config_dir = os.path.join(os.path.dirname(__file__), "resources", "squid") |
| 41 | + |
| 42 | + lines = process.run_subprocess_with_output(f"docker run --rm --name squid -d " |
| 43 | + f"-v {config_dir}/squidpasswords:/etc/squid/squidpasswords " |
| 44 | + f"-v {config_dir}/squid.conf:/etc/squid/squid.conf " |
| 45 | + f"-p 3128:3128 ubuntu/squid") |
| 46 | + proxy_container_id = lines[0].strip() |
| 47 | + proxy = HttpProxy(authenticated_url="http://testuser:testuser@127.0.0.1:3128", |
| 48 | + anonymous_url="http://127.0.0.1:3128") |
| 49 | + yield proxy |
| 50 | + process.run_subprocess(f"docker stop {proxy_container_id}") |
| 51 | + |
| 52 | + |
| 53 | +# ensures that a fresh log file is available |
| 54 | +@pytest.fixture(scope="function") |
| 55 | +def fresh_log_file(): |
| 56 | + cfg = it.ConfigFile(config_name=None) |
| 57 | + log_file = os.path.join(cfg.benchmark_home, "logs", "benchmark.log") |
| 58 | + |
| 59 | + if os.path.exists(log_file): |
| 60 | + bak = os.path.join(tempfile.mkdtemp(), "benchmark.log") |
| 61 | + shutil.move(log_file, bak) |
| 62 | + yield log_file |
| 63 | + # append log lines to the original file and move it back to its original |
| 64 | + with open(log_file, "r") as src: |
| 65 | + with open(bak, "a") as dst: |
| 66 | + dst.write(src.read()) |
| 67 | + shutil.move(bak, log_file) |
| 68 | + else: |
| 69 | + yield log_file |
| 70 | + |
| 71 | + |
| 72 | +def assert_log_line_present(log_file, text): |
| 73 | + with open(log_file, "r") as f: |
| 74 | + assert any(text in line for line in f), f"Could not find [{text}] in [{log_file}]." |
| 75 | + |
| 76 | + |
| 77 | +@it.benchmark_in_mem |
| 78 | +def test_run_with_direct_internet_connection(cfg, http_proxy, fresh_log_file): |
| 79 | + assert it.osbenchmark(cfg, "list workloads") == 0 |
| 80 | + assert_log_line_present(fresh_log_file, "Connecting directly to the Internet") |
| 81 | + |
| 82 | + |
| 83 | +@it.benchmark_in_mem |
| 84 | +def test_anonymous_proxy_no_connection(cfg, http_proxy, fresh_log_file): |
| 85 | + env = dict(os.environ) |
| 86 | + env["http_proxy"] = http_proxy.anonymous_url |
| 87 | + assert process.run_subprocess_with_logging(it.osbenchmark_command_line_for(cfg, "list workloads"), env=env) == 0 |
| 88 | + assert_log_line_present(fresh_log_file, f"Connecting via proxy URL [{http_proxy.anonymous_url}] to the Internet") |
| 89 | + # unauthenticated proxy access is prevented |
| 90 | + assert_log_line_present(fresh_log_file, "No Internet connection detected") |
| 91 | + |
| 92 | + |
| 93 | +@it.benchmark_in_mem |
| 94 | +def test_authenticated_proxy_user_can_connect(cfg, http_proxy, fresh_log_file): |
| 95 | + env = dict(os.environ) |
| 96 | + env["http_proxy"] = http_proxy.authenticated_url |
| 97 | + assert process.run_subprocess_with_logging(it.osbenchmark_command_line_for(cfg, "list workloads"), env=env) == 0 |
| 98 | + assert_log_line_present(fresh_log_file, |
| 99 | + f"Connecting via proxy URL [{http_proxy.authenticated_url}] to the Internet") |
| 100 | + # authenticated proxy access is allowed |
| 101 | + assert_log_line_present(fresh_log_file, "Detected a working Internet connection") |
0 commit comments