| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Define the test module for an influxdb instance.""" |
| 3 |
|
| 4 |
from __future__ import absolute_import |
| 5 |
from __future__ import division |
| 6 |
from __future__ import print_function |
| 7 |
from __future__ import unicode_literals |
| 8 |
|
| 9 |
import datetime |
| 10 |
import distutils.spawn |
| 11 |
import os |
| 12 |
import tempfile |
| 13 |
import shutil |
| 14 |
import subprocess |
| 15 |
import sys |
| 16 |
import time |
| 17 |
import unittest |
| 18 |
|
| 19 |
from influxdb.tests.misc import is_port_open, get_free_ports |
| 20 |
|
| 21 |
# hack in check_output if it's not defined, like for python 2.6 |
| 22 |
if "check_output" not in dir(subprocess): |
| 23 |
def f(*popenargs, **kwargs): |
| 24 |
"""Check for output.""" |
| 25 |
if 'stdout' in kwargs: |
| 26 |
raise ValueError( |
| 27 |
'stdout argument not allowed, it will be overridden.' |
| 28 |
) |
| 29 |
process = subprocess.Popen(stdout=subprocess.PIPE, |
| 30 |
*popenargs, |
| 31 |
**kwargs) |
| 32 |
output, unused_err = process.communicate() |
| 33 |
retcode = process.poll() |
| 34 |
if retcode: |
| 35 |
cmd = kwargs.get("args") |
| 36 |
if cmd is None: |
| 37 |
cmd = popenargs[0] |
| 38 |
raise subprocess.CalledProcessError(retcode, cmd) |
| 39 |
return output |
| 40 |
subprocess.check_output = f |
| 41 |
|
| 42 |
|
| 43 |
class InfluxDbInstance(object): |
| 44 |
"""Define an instance of InfluxDB. |
| 45 |
|
| 46 |
A class to launch of fresh influxdb server instance |
| 47 |
in a temporary place, using a config file template. |
| 48 |
""" |
| 49 |
|
| 50 |
def __init__(self, conf_template, udp_enabled=False): |
| 51 |
"""Initialize an instance of InfluxDbInstance.""" |
| 52 |
if os.environ.get("INFLUXDB_PYTHON_SKIP_SERVER_TESTS", None) == 'True': |
| 53 |
raise unittest.SkipTest( |
| 54 |
"Skipping server test (INFLUXDB_PYTHON_SKIP_SERVER_TESTS)" |
| 55 |
) |
| 56 |
|
| 57 |
self.influxd_path = self.find_influxd_path() |
| 58 |
|
| 59 |
errors = 0 |
| 60 |
while True: |
| 61 |
try: |
| 62 |
self._start_server(conf_template, udp_enabled) |
| 63 |
break |
| 64 |
# Happens when the ports are already in use. |
| 65 |
except RuntimeError as e: |
| 66 |
errors += 1 |
| 67 |
if errors > 2: |
| 68 |
raise e |
| 69 |
|
| 70 |
def _start_server(self, conf_template, udp_enabled): |
| 71 |
# create a temporary dir to store all needed files |
| 72 |
# for the influxdb server instance : |
| 73 |
self.temp_dir_base = tempfile.mkdtemp() |
| 74 |
|
| 75 |
# "temp_dir_base" will be used for conf file and logs, |
| 76 |
# while "temp_dir_influxdb" is for the databases files/dirs : |
| 77 |
tempdir = self.temp_dir_influxdb = tempfile.mkdtemp( |
| 78 |
dir=self.temp_dir_base) |
| 79 |
|
| 80 |
# find a couple free ports : |
| 81 |
free_ports = get_free_ports(4) |
| 82 |
ports = {} |
| 83 |
for service in 'http', 'global', 'meta', 'udp': |
| 84 |
ports[service + '_port'] = free_ports.pop() |
| 85 |
if not udp_enabled: |
| 86 |
ports['udp_port'] = -1 |
| 87 |
|
| 88 |
conf_data = dict( |
| 89 |
meta_dir=os.path.join(tempdir, 'meta'), |
| 90 |
data_dir=os.path.join(tempdir, 'data'), |
| 91 |
wal_dir=os.path.join(tempdir, 'wal'), |
| 92 |
cluster_dir=os.path.join(tempdir, 'state'), |
| 93 |
handoff_dir=os.path.join(tempdir, 'handoff'), |
| 94 |
logs_file=os.path.join(self.temp_dir_base, 'logs.txt'), |
| 95 |
udp_enabled='true' if udp_enabled else 'false', |
| 96 |
) |
| 97 |
conf_data.update(ports) |
| 98 |
self.__dict__.update(conf_data) |
| 99 |
|
| 100 |
conf_file = os.path.join(self.temp_dir_base, 'influxdb.conf') |
| 101 |
with open(conf_file, "w") as fh: |
| 102 |
with open(conf_template) as fh_template: |
| 103 |
fh.write(fh_template.read().format(**conf_data)) |
| 104 |
|
| 105 |
# now start the server instance: |
| 106 |
self.proc = subprocess.Popen( |
| 107 |
[self.influxd_path, '-config', conf_file], |
| 108 |
stdout=subprocess.PIPE, |
| 109 |
stderr=subprocess.PIPE |
| 110 |
) |
| 111 |
|
| 112 |
print( |
| 113 |
"%s > Started influxdb bin in %r with ports %s and %s.." % ( |
| 114 |
datetime.datetime.now(), |
| 115 |
self.temp_dir_base, |
| 116 |
self.global_port, |
| 117 |
self.http_port |
| 118 |
) |
| 119 |
) |
| 120 |
|
| 121 |
# wait for it to listen on the broker and admin ports: |
| 122 |
# usually a fresh instance is ready in less than 1 sec .. |
| 123 |
timeout = time.time() + 10 # so 10 secs should be enough, |
| 124 |
# otherwise either your system load is high, |
| 125 |
# or you run a 286 @ 1Mhz ? |
| 126 |
try: |
| 127 |
while time.time() < timeout: |
| 128 |
if (is_port_open(self.http_port) and |
| 129 |
is_port_open(self.global_port)): |
| 130 |
# it's hard to check if a UDP port is open.. |
| 131 |
if udp_enabled: |
| 132 |
# so let's just sleep 0.5 sec in this case |
| 133 |
# to be sure that the server has open the port |
| 134 |
time.sleep(0.5) |
| 135 |
break |
| 136 |
time.sleep(0.5) |
| 137 |
if self.proc.poll() is not None: |
| 138 |
raise RuntimeError('influxdb prematurely exited') |
| 139 |
else: |
| 140 |
self.proc.terminate() |
| 141 |
self.proc.wait() |
| 142 |
raise RuntimeError('Timeout waiting for influxdb to listen' |
| 143 |
' on its ports (%s)' % ports) |
| 144 |
except RuntimeError as err: |
| 145 |
data = self.get_logs_and_output() |
| 146 |
data['reason'] = str(err) |
| 147 |
data['now'] = datetime.datetime.now() |
| 148 |
raise RuntimeError("%(now)s > %(reason)s. RC=%(rc)s\n" |
| 149 |
"stdout=%(out)s\nstderr=%(err)s\nlogs=%(logs)r" |
| 150 |
% data) |
| 151 |
|
| 152 |
def find_influxd_path(self): |
| 153 |
"""Find the path for InfluxDB.""" |
| 154 |
influxdb_bin_path = os.environ.get( |
| 155 |
'INFLUXDB_PYTHON_INFLUXD_PATH', |
| 156 |
None |
| 157 |
) |
| 158 |
|
| 159 |
if influxdb_bin_path is None: |
| 160 |
influxdb_bin_path = distutils.spawn.find_executable('influxd') |
| 161 |
if not influxdb_bin_path: |
| 162 |
try: |
| 163 |
influxdb_bin_path = subprocess.check_output( |
| 164 |
['which', 'influxd'] |
| 165 |
).strip() |
| 166 |
except subprocess.CalledProcessError: |
| 167 |
# fallback on : |
| 168 |
influxdb_bin_path = '/opt/influxdb/influxd' |
| 169 |
|
| 170 |
if not os.path.isfile(influxdb_bin_path): |
| 171 |
raise unittest.SkipTest("Could not find influxd binary") |
| 172 |
|
| 173 |
version = subprocess.check_output([influxdb_bin_path, 'version']) |
| 174 |
print("InfluxDB version: %s" % version, file=sys.stderr) |
| 175 |
|
| 176 |
return influxdb_bin_path |
| 177 |
|
| 178 |
def get_logs_and_output(self): |
| 179 |
"""Query for logs and output.""" |
| 180 |
proc = self.proc |
| 181 |
try: |
| 182 |
with open(self.logs_file) as fh: |
| 183 |
logs = fh.read() |
| 184 |
except IOError as err: |
| 185 |
logs = "Couldn't read logs: %s" % err |
| 186 |
return { |
| 187 |
'rc': proc.returncode, |
| 188 |
'out': proc.stdout.read(), |
| 189 |
'err': proc.stderr.read(), |
| 190 |
'logs': logs |
| 191 |
} |
| 192 |
|
| 193 |
def close(self, remove_tree=True): |
| 194 |
"""Close an instance of InfluxDB.""" |
| 195 |
self.proc.terminate() |
| 196 |
self.proc.wait() |
| 197 |
if remove_tree: |
| 198 |
shutil.rmtree(self.temp_dir_base) |