-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8: Covered influxdb-query with tests
- Loading branch information
1 parent
1301b21
commit 1ee824c
Showing
4 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import inspect | ||
import unittest | ||
import influxdb | ||
from tests.utils.influxdb_test import InfluxDBContainerRequirement, USER, PASSWORD, DB | ||
|
||
|
||
# import a script with "-" in filename | ||
path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + '/../' | ||
with open(path + '/infracheck/checks/influxdb-query', 'r') as script: | ||
exec(script.read()) | ||
|
||
|
||
class InfluxDBQueryCheckTest(InfluxDBContainerRequirement, unittest.TestCase): | ||
def test_finds_occurrence_in_json_results(self): | ||
client = influxdb.InfluxDBClient(host='localhost', port=8086) | ||
client.switch_database(DB) | ||
client.switch_user(USER, PASSWORD) | ||
|
||
for i in range(1, 5): | ||
client.write_points([ | ||
{ | ||
"measurement": "cpu_load_short", | ||
"tags": { | ||
"host": "server01", | ||
"region": "us-west" | ||
}, | ||
"time": "2009-11-10T23:00:{}Z".format(i * 10), | ||
"fields": { | ||
"value": 0.64 + (i * 10) | ||
} | ||
} | ||
]) | ||
|
||
check = InfluxDBQueryCheck( | ||
host='localhost', port=8086, | ||
user=USER, password=PASSWORD, | ||
database=DB | ||
) | ||
|
||
result, message = check.main('select value from cpu_load_short;', '"value": 10.64') | ||
|
||
self.assertEqual('Query results are matching', message) | ||
self.assertTrue(result) | ||
|
||
def test_will_not_find_occurrence(self): | ||
client = influxdb.InfluxDBClient(host='localhost', port=8086) | ||
client.switch_database(DB) | ||
client.switch_user(USER, PASSWORD) | ||
|
||
check = InfluxDBQueryCheck( | ||
host='localhost', port=8086, | ||
user=USER, password=PASSWORD, | ||
database=DB | ||
) | ||
|
||
result, message = check.main('select value from this_metrics_does_not_exist;', '"value": 10.64') | ||
|
||
self.assertEqual('Expected \'[]\' to contain \'"value": 10.64\'', message) | ||
self.assertFalse(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
import time | ||
from .base_docker_container_test import BaseDockerContainerRequirement | ||
|
||
DB = 'test' | ||
USER = 'infracheck' | ||
PASSWORD = 'solidarity_forever' | ||
|
||
|
||
class InfluxDBContainerRequirement(BaseDockerContainerRequirement): | ||
@staticmethod | ||
def _get_environment() -> dict: | ||
return { | ||
'INFLUXDB_DB': DB, | ||
'INFLUXDB_ADMIN_USER': USER, | ||
'INFLUXDB_ADMIN_PASSWORD': PASSWORD | ||
} | ||
|
||
@classmethod | ||
def _wait_for_container_to_be_ready(cls): | ||
time.sleep(1) | ||
cls._wait_for_log_message('Listening on HTTP') | ||
time.sleep(1) | ||
|
||
@staticmethod | ||
def _get_container_name() -> str: | ||
return 'influxdb' | ||
|
||
@staticmethod | ||
def _get_ports() -> dict: | ||
return {'8086/tcp': '8086'} | ||
|
||
@staticmethod | ||
def _get_image_name() -> str: | ||
return 'influxdb:1.8-alpine' |