Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to pytest asserts #1276

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 67 additions & 69 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import elasticsearch
import urllib3.exceptions
import pytest

from esrally import client, exceptions, doc_link
from esrally.utils import console
Expand All @@ -42,12 +43,12 @@ def test_create_http_connection(self):

f = client.EsClientFactory(hosts, client_options)

self.assertEqual(hosts, f.hosts)
self.assertIsNone(f.ssl_context)
self.assertEqual("http", f.client_options["scheme"])
self.assertFalse("http_auth" in f.client_options)
assert f.hosts == hosts
assert f.ssl_context is None
assert f.client_options["scheme"] == "http"
assert "http_auth" not in f.client_options

self.assertDictEqual(original_client_options, client_options)
assert client_options == original_client_options

@mock.patch.object(ssl.SSLContext, "load_cert_chain")
def test_create_https_connection_verify_server(self, mocked_load_cert_chain):
Expand All @@ -72,17 +73,17 @@ def test_create_https_connection_verify_server(self, mocked_load_cert_chain):
assert not mocked_load_cert_chain.called, "ssl_context.load_cert_chain should not have been called as we have not supplied " \
"client certs"

self.assertEqual(hosts, f.hosts)
self.assertTrue(f.ssl_context.check_hostname)
self.assertEqual(ssl.CERT_REQUIRED, f.ssl_context.verify_mode)
assert f.hosts == hosts
assert f.ssl_context.check_hostname
assert f.ssl_context.verify_mode == ssl.CERT_REQUIRED

self.assertEqual("https", f.client_options["scheme"])
self.assertEqual(("user", "password"), f.client_options["http_auth"])
self.assertNotIn("use_ssl", f.client_options)
self.assertNotIn("verify_certs", f.client_options)
self.assertNotIn("ca_certs", f.client_options)
assert f.client_options["scheme"] == "https"
assert f.client_options["http_auth"] == ("user", "password")
assert "use_ssl" not in f.client_options
assert "verify_certs" not in f.client_options
assert "ca_certs" not in f.client_options

self.assertDictEqual(original_client_options, client_options)
assert client_options == original_client_options

@mock.patch.object(ssl.SSLContext, "load_cert_chain")
def test_create_https_connection_verify_self_signed_server_and_client_certificate(self, mocked_load_cert_chain):
Expand Down Expand Up @@ -112,19 +113,19 @@ def test_create_https_connection_verify_self_signed_server_and_client_certificat
keyfile=client_options["client_key"]
)

self.assertEqual(hosts, f.hosts)
self.assertTrue(f.ssl_context.check_hostname)
self.assertEqual(ssl.CERT_REQUIRED, f.ssl_context.verify_mode)
assert f.hosts == hosts
assert f.ssl_context.check_hostname
assert f.ssl_context.verify_mode == ssl.CERT_REQUIRED

self.assertEqual("https", f.client_options["scheme"])
self.assertEqual(("user", "password"), f.client_options["http_auth"])
self.assertNotIn("use_ssl", f.client_options)
self.assertNotIn("verify_certs", f.client_options)
self.assertNotIn("ca_certs", f.client_options)
self.assertNotIn("client_cert", f.client_options)
self.assertNotIn("client_key", f.client_options)
assert f.client_options["scheme"] == "https"
assert f.client_options["http_auth"] == ("user", "password")
assert "use_ssl" not in f.client_options
assert "verify_certs" not in f.client_options
assert "ca_certs" not in f.client_options
assert "client_cert" not in f.client_options
assert "client_key" not in f.client_options

self.assertDictEqual(original_client_options, client_options)
assert client_options == original_client_options

@mock.patch.object(ssl.SSLContext, "load_cert_chain")
def test_create_https_connection_only_verify_self_signed_server_certificate(self, mocked_load_cert_chain):
Expand All @@ -149,17 +150,17 @@ def test_create_https_connection_only_verify_self_signed_server_certificate(self

assert not mocked_load_cert_chain.called, "ssl_context.load_cert_chain should not have been called as we have not supplied " \
"client certs"
self.assertEqual(hosts, f.hosts)
self.assertTrue(f.ssl_context.check_hostname)
self.assertEqual(ssl.CERT_REQUIRED, f.ssl_context.verify_mode)
assert f.hosts == hosts
assert f.ssl_context.check_hostname
assert f.ssl_context.verify_mode == ssl.CERT_REQUIRED

self.assertEqual("https", f.client_options["scheme"])
self.assertEqual(("user", "password"), f.client_options["http_auth"])
self.assertNotIn("use_ssl", f.client_options)
self.assertNotIn("verify_certs", f.client_options)
self.assertNotIn("ca_certs", f.client_options)
assert f.client_options["scheme"] == "https"
assert f.client_options["http_auth"] == ("user", "password")
assert "use_ssl" not in f.client_options
assert "verify_certs" not in f.client_options
assert "ca_certs" not in f.client_options

self.assertDictEqual(original_client_options, client_options)
assert client_options == original_client_options

def test_raises_error_when_only_one_of_client_cert_and_client_key_defined(self):
hosts = [{"host": "127.0.0.1", "port": 9200}]
Expand All @@ -181,7 +182,7 @@ def test_raises_error_when_only_one_of_client_cert_and_client_key_defined(self):
{random_client_ssl_option: client_ssl_options[random_client_ssl_option]}
)

with self.assertRaises(exceptions.SystemSetupError) as ctx:
with pytest.raises(exceptions.SystemSetupError) as ctx:
with mock.patch.object(console, "println") as mocked_console_println:
client.EsClientFactory(hosts, client_options)
mocked_console_println.assert_called_once_with(
Expand All @@ -193,13 +194,11 @@ def test_raises_error_when_only_one_of_client_cert_and_client_key_defined(self):
console.format.link(doc_link("command_line_reference.html#client-options"))
)
)
self.assertEqual(
assert ctx.value.args[0] == \
"Cannot specify '{}' without also specifying '{}' in client-options.".format(
random_client_ssl_option,
missing_client_ssl_option
),
ctx.exception.args[0]
)
)

@mock.patch.object(ssl.SSLContext, "load_cert_chain")
def test_create_https_connection_unverified_certificate(self, mocked_load_cert_chain):
Expand All @@ -225,18 +224,18 @@ def test_create_https_connection_unverified_certificate(self, mocked_load_cert_c
assert not mocked_load_cert_chain.called, "ssl_context.load_cert_chain should not have been called as we have not supplied " \
"client certs"

self.assertEqual(hosts, f.hosts)
self.assertFalse(f.ssl_context.check_hostname)
self.assertEqual(ssl.CERT_NONE, f.ssl_context.verify_mode)
assert f.hosts == hosts
assert not f.ssl_context.check_hostname
assert f.ssl_context.verify_mode == ssl.CERT_NONE

self.assertEqual("https", f.client_options["scheme"])
self.assertEqual(("user", "password"), f.client_options["http_auth"])
self.assertNotIn("use_ssl", f.client_options)
self.assertNotIn("verify_certs", f.client_options)
self.assertNotIn("basic_auth_user", f.client_options)
self.assertNotIn("basic_auth_password", f.client_options)
assert f.client_options["scheme"] == "https"
assert f.client_options["http_auth"] == ("user", "password")
assert "use_ssl" not in f.client_options
assert "verify_certs" not in f.client_options
assert "basic_auth_user" not in f.client_options
assert "basic_auth_password" not in f.client_options

self.assertDictEqual(original_client_options, client_options)
assert client_options == original_client_options

@mock.patch.object(ssl.SSLContext, "load_cert_chain")
def test_create_https_connection_unverified_certificate_present_client_certificates(self, mocked_load_cert_chain):
Expand Down Expand Up @@ -264,21 +263,21 @@ def test_create_https_connection_unverified_certificate_present_client_certifica
keyfile=client_options["client_key"]
)

self.assertEqual(hosts, f.hosts)
self.assertFalse(f.ssl_context.check_hostname)
self.assertEqual(ssl.CERT_NONE, f.ssl_context.verify_mode)
assert f.hosts == hosts
assert not f.ssl_context.check_hostname
assert f.ssl_context.verify_mode == ssl.CERT_NONE

self.assertEqual("https", f.client_options["scheme"])
self.assertEqual(("user", "password"), f.client_options["http_auth"])
self.assertNotIn("use_ssl", f.client_options)
self.assertNotIn("verify_certs", f.client_options)
self.assertNotIn("basic_auth_user", f.client_options)
self.assertNotIn("basic_auth_password", f.client_options)
self.assertNotIn("ca_certs", f.client_options)
self.assertNotIn("client_cert", f.client_options)
self.assertNotIn("client_key", f.client_options)
assert f.client_options["scheme"] == "https"
assert f.client_options["http_auth"] == ("user", "password")
assert "use_ssl" not in f.client_options
assert "verify_certs" not in f.client_options
assert "basic_auth_user" not in f.client_options
assert "basic_auth_password" not in f.client_options
assert "ca_certs" not in f.client_options
assert "client_cert" not in f.client_options
assert "client_key" not in f.client_options

self.assertDictEqual(original_client_options, client_options)
assert client_options == original_client_options


class RequestContextManagerTests(TestCase):
Expand All @@ -297,9 +296,9 @@ async def test_propagates_nested_context(self):
top_level_duration = top_level_ctx.request_end - top_level_ctx.request_start

# top level request should cover total duration
self.assertAlmostEqual(top_level_duration, 0.2, delta=0.05)
assert abs(top_level_duration-0.2) < 0.05
# nested request should only cover nested duration
self.assertAlmostEqual(nested_duration, 0.1, delta=0.05)
assert abs(nested_duration-0.1) < 0.05


class RestLayerTests(TestCase):
Expand All @@ -310,7 +309,7 @@ def test_successfully_waits_for_rest_layer(self, es):
{"host": "node-b.example.org", "port": 9200}
]

self.assertTrue(client.wait_for_rest_layer(es, max_attempts=3))
assert client.wait_for_rest_layer(es, max_attempts=3)

es.cluster.health.assert_has_calls([
mock.call(wait_for_nodes=">=2"),
Expand All @@ -332,21 +331,20 @@ def test_retries_on_transport_errors(self, es, sleep):
}
}
]
self.assertTrue(client.wait_for_rest_layer(es, max_attempts=5))
assert client.wait_for_rest_layer(es, max_attempts=5)

# don't sleep in realtime
@mock.patch("time.sleep")
@mock.patch("elasticsearch.Elasticsearch")
def test_dont_retry_eternally_on_transport_errors(self, es, sleep):
es.cluster.health.side_effect = elasticsearch.TransportError(401, "Unauthorized")
self.assertFalse(client.wait_for_rest_layer(es, max_attempts=3))
assert not client.wait_for_rest_layer(es, max_attempts=3)

@mock.patch("elasticsearch.Elasticsearch")
def test_ssl_error(self, es):
es.cluster.health.side_effect = elasticsearch.ConnectionError("N/A",
"[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:719)",
urllib3.exceptions.SSLError(
"[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:719)"))
with self.assertRaisesRegex(expected_exception=exceptions.SystemSetupError,
expected_regex="Could not connect to cluster via https. Is this an https endpoint?"):
with pytest.raises(exceptions.SystemSetupError, match="Could not connect to cluster via https. Is this an https endpoint?"):
client.wait_for_rest_layer(es, max_attempts=3)
55 changes: 28 additions & 27 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import configparser
from unittest import TestCase

import pytest

from esrally import config, exceptions


Expand Down Expand Up @@ -84,13 +86,13 @@ def load(self, interpolation=None):
class ConfigTests(TestCase):
def test_load_non_existing_config(self):
cfg = config.Config(config_file_class=InMemoryConfigStore)
self.assertFalse(cfg.config_present())
assert not cfg.config_present()
# standard properties are still available
self.assertEqual("rally-node", cfg.opts("provisioning", "node.name.prefix"))
assert cfg.opts("provisioning", "node.name.prefix") == "rally-node"

def test_load_existing_config(self):
cfg = config.Config(config_file_class=InMemoryConfigStore)
self.assertFalse(cfg.config_present())
assert not cfg.config_present()

sample_config = {
"tests": {
Expand All @@ -102,18 +104,18 @@ def test_load_existing_config(self):
}
cfg.config_file.store(sample_config)

self.assertTrue(cfg.config_present())
assert cfg.config_present()
cfg.load_config()
# standard properties are still available
self.assertEqual("rally-node", cfg.opts("provisioning", "node.name.prefix"))
self.assertEqual("value", cfg.opts("tests", "sample.key"))
assert cfg.opts("provisioning", "node.name.prefix") == "rally-node"
assert cfg.opts("tests", "sample.key") == "value"
# we can also override values
cfg.add(config.Scope.applicationOverride, "tests", "sample.key", "override")
self.assertEqual("override", cfg.opts("tests", "sample.key"))
assert cfg.opts("tests", "sample.key") == "override"

def test_load_all_opts_in_section(self):
cfg = config.Config(config_file_class=InMemoryConfigStore)
self.assertFalse(cfg.config_present())
assert not cfg.config_present()

sample_config = {
"distributions": {
Expand All @@ -131,18 +133,18 @@ def test_load_all_opts_in_section(self):
}
cfg.config_file.store(sample_config)

self.assertTrue(cfg.config_present())
assert cfg.config_present()
cfg.load_config()
# override a value so we can see that the scoping logic still works. Default is scope "application"
cfg.add(config.Scope.applicationOverride, "distributions", "snapshot.cache", "true")

self.assertEqual({
assert cfg.all_opts("distributions") == {
"release.url": "https://acme.com/releases",
"release.cache": "true",
"snapshot.url": "https://acme.com/snapshots",
# overridden!
"snapshot.cache": "true"
}, cfg.all_opts("distributions"))
}

def test_add_all_in_section(self):
source_cfg = config.Config(config_file_class=InMemoryConfigStore)
Expand All @@ -163,11 +165,11 @@ def test_add_all_in_section(self):

target_cfg = config.Config(config_file_class=InMemoryConfigStore)

self.assertIsNone(target_cfg.opts("tests", "sample.key", mandatory=False))
assert target_cfg.opts("tests", "sample.key", mandatory=False) is None

target_cfg.add_all(source=source_cfg, section="tests")
self.assertEqual("value", target_cfg.opts("tests", "sample.key"))
self.assertIsNone(target_cfg.opts("no_copy", "other.key", mandatory=False))
assert target_cfg.opts("tests", "sample.key") == "value"
assert target_cfg.opts("no_copy", "other.key", mandatory=False) is None

# nonexisting key will not throw an error
target_cfg.add_all(source=source_cfg, section="this section does not exist")
Expand All @@ -185,9 +187,9 @@ def test_can_create_non_existing_config(self):
base_cfg.add(config.Scope.application, "defaults", "preserve_benchmark_candidate", True)

cfg = config.auto_load_local_config(base_cfg, config_file_class=InMemoryConfigStore)
self.assertTrue(cfg.config_file.present)
assert cfg.config_file.present
# did not just copy base config
self.assertNotEqual(base_cfg.opts("benchmarks", "local.dataset.cache"), cfg.opts("benchmarks", "local.dataset.cache"))
assert base_cfg.opts("benchmarks", "local.dataset.cache") != cfg.opts("benchmarks", "local.dataset.cache")
# copied sections from base config
self.assert_equals_base_config(base_cfg, cfg, "reporting", "datastore.type")
self.assert_equals_base_config(base_cfg, cfg, "tracks", "metrics.url")
Expand Down Expand Up @@ -217,11 +219,11 @@ def test_can_load_and_amend_existing_config(self):
"local.dataset.cache": "/tmp/rally/data"
}
})
self.assertTrue(cfg.config_file.present)
assert cfg.config_file.present
# did not just copy base config
self.assertNotEqual(base_cfg.opts("benchmarks", "local.dataset.cache"), cfg.opts("benchmarks", "local.dataset.cache"))
assert base_cfg.opts("benchmarks", "local.dataset.cache") != cfg.opts("benchmarks", "local.dataset.cache")
# keeps config properties
self.assertEqual("existing-unit-test-config", cfg.opts("system", "env.name"))
assert cfg.opts("system", "env.name") == "existing-unit-test-config"
# copies additional properties
self.assert_equals_base_config(base_cfg, cfg, "unit-test", "sample.property")

Expand Down Expand Up @@ -252,14 +254,14 @@ def test_can_migrate_outdated_config(self):
"java8.home": "/opt/jdk8"
}
})
self.assertTrue(cfg.config_file.present)
assert cfg.config_file.present
# did not just copy base config
self.assertNotEqual(base_cfg.opts("benchmarks", "local.dataset.cache"), cfg.opts("benchmarks", "local.dataset.cache"))
assert base_cfg.opts("benchmarks", "local.dataset.cache") != cfg.opts("benchmarks", "local.dataset.cache")
# migrated existing config
self.assertEqual(config.Config.CURRENT_CONFIG_VERSION, int(cfg.opts("meta", "config.version")))
assert int(cfg.opts("meta", "config.version")) == config.Config.CURRENT_CONFIG_VERSION

def assert_equals_base_config(self, base_config, local_config, section, key):
self.assertEqual(base_config.opts(section, key), local_config.opts(section, key))
assert local_config.opts(section, key) == base_config.opts(section, key)


class ConfigMigrationTests(TestCase):
Expand Down Expand Up @@ -288,8 +290,7 @@ def test_does_not_migrate_outdated_config(self):
}

config_file.store(sample_config)
with self.assertRaisesRegex(exceptions.ConfigError,
"The config file.*is too old. Please delete it and reconfigure Rally from scratch"):
with pytest.raises(exceptions.ConfigError, match="The config file.*is too old. Please delete it and reconfigure Rally from scratch"):
config.migrate(config_file, config.Config.EARLIEST_SUPPORTED_VERSION - 1, config.Config.CURRENT_CONFIG_VERSION, out=null_output)

# catch all test, migrations are checked in more detail in the other tests
Expand Down Expand Up @@ -327,5 +328,5 @@ def test_migrate_from_earliest_supported_to_latest(self):
config.migrate(config_file, config.Config.EARLIEST_SUPPORTED_VERSION, config.Config.CURRENT_CONFIG_VERSION, out=null_output)

if config.Config.EARLIEST_SUPPORTED_VERSION < config.Config.CURRENT_CONFIG_VERSION:
self.assertTrue(config_file.backup_created)
self.assertEqual(str(config.Config.CURRENT_CONFIG_VERSION), config_file.config["meta"]["config.version"])
assert config_file.backup_created
assert config_file.config["meta"]["config.version"] == str(config.Config.CURRENT_CONFIG_VERSION)
Loading