1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
"""Test helpers.utils"""
import os
import random
import string
import binascii
from unittest import TestCase
from unittest.mock import Mock
import pytest
from es_client.exceptions import ConfigurationError
from es_client.helpers import utils as u
from . import FileTestObj
# pylint: disable=R0903,W0718
DEFAULT = {
"elasticsearch": {
"other_settings": {
"master_only": False,
"skip_version_test": False,
"username": None,
"password": None,
},
"client": {
"hosts": "http://127.0.0.1:9200",
"request_timeout": 30,
},
}
}
# The leading spaces are important here to create a proper yaml file.
YAML = "\n".join(["---", "elasticsearch:", " client:", " hosts: {0}"])
def random_envvar(size):
"""Generate a random environment variable"""
return "".join(
random.SystemRandom().choice(string.ascii_uppercase + string.digits)
for _ in range(size)
)
class TestEnsureList(TestCase):
"""Test the u.ensure_list function"""
def test_utils_ensure_list_returns_lists(self):
"""
Test several examples of lists: existing lists, strings, mixed lists/numbers
"""
verify = ["a", "b", "c", "d"]
source = ["a", "b", "c", "d"]
assert verify == u.ensure_list(source)
verify = ["abcd"]
source = "abcd"
assert verify == u.ensure_list(source)
verify = [["abcd", "defg"], 1, 2, 3]
source = [["abcd", "defg"], 1, 2, 3]
assert verify == u.ensure_list(source)
verify = [{"a": "b", "c": "d"}]
source = {"a": "b", "c": "d"}
assert verify == u.ensure_list(source)
class TestPruneNones(TestCase):
"""Test the u.prune_nones function"""
def test_utils_prune_nones_with(self):
"""Ensure that a dict with a single None value comes back as an empty dict"""
assert not u.prune_nones({"a": None})
def test_utils_prune_nones_without(self):
"""Ensure that a dict with no None values comes back unchanged"""
testval = {"foo": "bar"}
assert testval == u.prune_nones(testval)
class TestReadFile:
"""Test the u.read_file function"""
def test_utils_read_file_present(self):
"""Ensure that the written value is what was in the filename"""
obj = FileTestObj()
assert obj.written_value == u.read_file(obj.args["filename"])
obj.teardown()
def test_raise_when_no_file(self):
"""Raise an exception when there is no file"""
obj = FileTestObj()
with pytest.raises(ConfigurationError):
u.read_file(obj.args["no_file_here"])
obj.teardown()
class TestReadCerts:
"""Test the u.verify_ssl_paths function"""
def test_all_as_one(self):
"""Test all 3 possible cert files at once from the same file"""
obj = FileTestObj()
config = {
"ca_certs": obj.args["filename"],
"client_cert": obj.args["filename"],
"client_key": obj.args["filename"],
}
try:
u.verify_ssl_paths(config)
except Exception:
pytest.fail("Unexpected Exception...")
class TestEnvVars:
"""Test the ability to read environment variables"""
def test_present(self):
"""Test an existing (present) envvar"""
obj = FileTestObj()
evar = random_envvar(8)
os.environ[evar] = "1234"
dollar = "${" + evar + "}"
obj.write_config(obj.args["configfile"], YAML.format(dollar))
cfg = u.get_yaml(obj.args["configfile"])
assert cfg["elasticsearch"]["client"]["hosts"] == os.environ.get(evar)
del os.environ[evar]
obj.teardown()
def test_not_present(self):
"""Test a non-existent (not-present) envvar. It should set None here"""
obj = FileTestObj()
evar = random_envvar(8)
dollar = "${" + evar + "}"
obj.write_config(obj.args["configfile"], YAML.format(dollar))
cfg = u.get_yaml(obj.args["configfile"])
assert cfg["elasticsearch"]["client"]["hosts"] is None
obj.teardown()
def test_not_present_with_default(self):
"""
Test a non-existent (not-present) envvar. It should set a default value here
"""
obj = FileTestObj()
evar = random_envvar(8)
default = random_envvar(8)
dollar = "${" + evar + ":" + default + "}"
obj.write_config(obj.args["configfile"], YAML.format(dollar))
cfg = u.get_yaml(obj.args["configfile"])
assert cfg["elasticsearch"]["client"]["hosts"] == default
obj.teardown()
def test_raises_exception(self):
"""Ensure that improper formatting raises a ConfigurationError exception"""
obj = FileTestObj()
obj.write_config(
obj.args["configfile"],
"""
[weird brackets go here]
I'm not a yaml file!!!=I have no keys
I have lots of spaces
""",
)
with pytest.raises(ConfigurationError):
u.get_yaml(obj.args["configfile"])
obj.teardown()
class TestVerifyURLSchema:
"""Test the u.verify_url_schema function"""
def test_full_schema(self):
"""Verify that a proper schema comes back unchanged"""
url = "https://127.0.0.1:9200"
assert u.verify_url_schema(url) == url
def test_http_schema_no_port(self):
"""
Verify that port 80 is tacked on when no port is specified as a port is required
"""
http_port = "80"
url = "http://127.0.0.1"
assert u.verify_url_schema(url) == "http://127.0.0.1" + ":" + http_port
def test_https_schema_no_port(self):
"""
Verify that 443 is tacked on when no port is specified but https is the schema
"""
https_port = "443"
url = "https://127.0.0.1"
assert u.verify_url_schema(url) == "https://127.0.0.1" + ":" + https_port
def test_bad_schema_no_port(self):
"""A URL starting with other than http or https raises an exception w/o port"""
url = "abcd://127.0.0.1"
with pytest.raises(ConfigurationError):
u.verify_url_schema(url)
def test_bad_schema_with_port(self):
"""A URL starting with other than http or https raises an exception w/port"""
url = "abcd://127.0.0.1:1234"
with pytest.raises(ConfigurationError):
u.verify_url_schema(url)
def test_bad_schema_too_many_colons(self):
"""An invalid URL with too many colons raises an exception"""
url = "http://127.0.0.1:1234:5678"
with pytest.raises(ConfigurationError):
u.verify_url_schema(url)
class TestGetVersion:
"""Test the u.get_version function"""
def test_positive(self):
"""Ensure that what goes in comes back out unchanged"""
client = Mock()
client.info.return_value = {"version": {"number": "9.9.9"}}
version = u.get_version(client)
assert version == (9, 9, 9)
def test_negative(self):
"""Ensure that mismatches are caught"""
client = Mock()
client.info.return_value = {"version": {"number": "9.9.9"}}
version = u.get_version(client)
assert version != (8, 8, 8)
def test_dev_version_4_dots(self):
"""Test that anything after a third value and a period is truncated"""
client = Mock()
client.info.return_value = {"version": {"number": "9.9.9.dev"}}
version = u.get_version(client)
assert version == (9, 9, 9)
def test_dev_version_with_dash(self):
"""Test that anything after a third value and a dash is truncated"""
client = Mock()
client.info.return_value = {"version": {"number": "9.9.9-dev"}}
version = u.get_version(client)
assert version == (9, 9, 9)
class TestFileExists:
"""Test the u.file_exists function"""
def test_positive(self):
"""Ensure that an existing file returns True"""
obj = FileTestObj()
obj.write_config(
obj.args["configfile"],
"""
[weird brackets go here]
I'm not a yaml file!!!=I have no keys
I have lots of spaces
""",
)
assert u.file_exists(obj.args["configfile"])
obj.teardown()
def test_negative(self):
"""Ensure that a non-existing file returns False"""
obj = FileTestObj()
obj.write_config(
obj.args["configfile"],
"""
This file will be deleted
""",
)
obj.teardown()
assert not u.file_exists(obj.args["configfile"])
class TestParseAPIKeyToken:
"""Test the u.parse_apikey_token function"""
def success(self):
"""Successfully parse a token"""
token = "X1VoN0VZY0JJV0lrUTlrdS1QZ2k6QjNZN1VJMlVRd0NHM1VTdHhuNnRKdw=="
expected = ("_Uh7EYcBIWIkQ9ku-Pgi", "B3Y7UI2UQwCG3UStxn6tJw")
assert expected == u.parse_apikey_token(token)
def raises_exception1(self):
"""Raise a binascii.Error when unable to base64 decode a token"""
token = "Not a valid token"
with pytest.raises(binascii.Error):
u.parse_apikey_token(token)
def raises_exception2(self):
"""Raise an IndexError when able to base64 decode a token, not split by colon"""
token = "VGhpcyB0ZXh0IGhhcyBubyBjb2xvbg=="
with pytest.raises(IndexError):
u.parse_apikey_token(token)
|