Add more checks for InfluxDB/Grafana

This change adds a few checks after the deployment of the
InfluxDB/Grafana plugin:

  - Check that authorized users can access the API (InfluxDB and Grafana)
  - Check that unauthorized users can't access the API (InfluxDB and
    Grafana)
  - Check that the root user can login and has admin rights (InfluxDB)
  - Check that the login page is accessible (Grafana)

Change-Id: I1fa49469f71b2d1cd3e99bac76de81f3f7a68c2b
This commit is contained in:
Simon Pasquier
2016-05-12 16:30:36 +02:00
parent a0611eb83e
commit 744cf3aff1
3 changed files with 67 additions and 37 deletions

View File

@@ -17,6 +17,18 @@ import requests
def check_http_get_response(url, expected_code=200, msg=None, **kwargs): def check_http_get_response(url, expected_code=200, msg=None, **kwargs):
"""Perform a HTTP GET request and assert that the HTTP server replies with
the expected code.
:param url: the request URL
:type url: str
:param expected_code: the expected HTTP response code. Defaults to 200
:type expected_code: int
:param msg: the assertion message. Defaults to None
:type msg: str
:returns: HTTP response object
:rtype: requests.Response
"""
msg = msg or "%s responded with {0}, expected {1}" % url msg = msg or "%s responded with {0}, expected {1}" % url
r = requests.get(url, **kwargs) r = requests.get(url, **kwargs)
asserts.assert_equal( asserts.assert_equal(

View File

@@ -35,52 +35,72 @@ class InfluxdbPluginApi(base_test.PluginApi):
def get_plugin_vip(self): def get_plugin_vip(self):
return self.helpers.get_plugin_vip(self.settings.vip_name) return self.helpers.get_plugin_vip(self.settings.vip_name)
def make_request_to_influx(self, def get_grafana_url(self, resource=''):
db=plugin_settings.influxdb_db_name, return "http://{0}:8000/{1}".format(self.get_plugin_vip(), resource)
user=plugin_settings.influxdb_rootuser,
password=plugin_settings.influxdb_rootpass,
query="",
expected_code=200):
influxdb_vip = self.get_plugin_vip()
params = { def get_influxdb_url(self, resource=''):
"db": db, return "http://{0}:8086/{1}".format(self.get_plugin_vip(), resource)
"u": user,
"p": password,
"q": query,
}
msg = "InfluxDB responded with {0}, expected {1}" def do_influxdb_query(self,
r = self.checkers.check_http_get_response( query,
self.settings.influxdb_url.format(influxdb_vip), db=plugin_settings.influxdb_db_name,
expected_code=expected_code, msg=msg, params=params) user=plugin_settings.influxdb_user,
return r password=plugin_settings.influxdb_pass,
expected_code=200):
return self.checkers.check_http_get_response(
url=self.get_influxdb_url('query'),
expected_code=expected_code,
params={"db": db, "u": user, "p": password, "q": query})
def check_plugin_online(self): def check_plugin_online(self):
self.make_request_to_influx(query="show measurements") logger.debug("Check that the InfluxDB server replies to ping requests")
logger.debug("Check that the Grafana server is running")
msg = "Grafana server responded with {0}, expected {1}"
self.checkers.check_http_get_response( self.checkers.check_http_get_response(
self.settings.grafana_url.format( url=self.get_influxdb_url('ping'),
self.settings.grafana_user, self.settings.grafana_pass, expected_code=204)
self.get_plugin_vip()),
msg=msg
)
def check_influxdb_nodes_count(self, nodes_count=1): logger.debug("Check that the InfluxDB API requires authentication")
response = self.make_request_to_influx( self.do_influxdb_query("show measurements",
user=plugin_settings.influxdb_user,
password='rogue', expected_code=401)
logger.debug("Check that the InfluxDB user is authorized")
self.do_influxdb_query("show measurements")
logger.debug("Check that the InfluxDB user doesn't have admin rights")
self.do_influxdb_query("show servers", expected_code=401)
logger.debug("Check that the InfluxDB root user has admin rights")
self.do_influxdb_query("show servers",
user=plugin_settings.influxdb_rootuser,
password=plugin_settings.influxdb_rootpass)
logger.debug("Check that the Grafana UI server is running")
self.checkers.check_http_get_response(
self.get_grafana_url('login'))
logger.debug("Check that the Grafana user is authorized")
self.checkers.check_http_get_response(
self.get_grafana_url('api/org'),
auth=(plugin_settings.grafana_user, plugin_settings.grafana_pass))
logger.debug("Check that the Grafana API requires authentication")
self.checkers.check_http_get_response(
self.get_grafana_url('api/org'),
auth=(plugin_settings.grafana_user, 'rogue'), expected_code=401)
def check_influxdb_nodes_count(self, count=1):
logger.debug('Check the number of InfluxDB servers')
response = self.do_influxdb_query(
"show servers",
user=self.settings.influxdb_rootuser, user=self.settings.influxdb_rootuser,
password=self.settings.influxdb_rootpass, password=self.settings.influxdb_rootpass)
query="show servers")
nodes_count_responsed = len( nodes_count_responsed = len(
response.json()["results"][0]["series"][0]["values"]) response.json()["results"][0]["series"][0]["values"])
msg = "InfluxDB nodes count expected, received instead: {}".format( msg = "Expected {0} InfluxDB nodes, got {}".format(
nodes_count_responsed) count, nodes_count_responsed)
asserts.assert_equal(nodes_count, nodes_count_responsed, msg) asserts.assert_equal(count, nodes_count_responsed, msg)
def get_influxdb_master_node(self, excluded_nodes_fqdns=()): def get_influxdb_master_node(self, excluded_nodes_fqdns=()):
influx_master_node = self.helpers.get_master_node_by_role( influx_master_node = self.helpers.get_master_node_by_role(

View File

@@ -26,11 +26,9 @@ influxdb_user = 'influxdb'
influxdb_pass = 'influxdbpass' influxdb_pass = 'influxdbpass'
influxdb_rootuser = 'root' influxdb_rootuser = 'root'
influxdb_rootpass = 'r00tme' influxdb_rootpass = 'r00tme'
influxdb_url = "http://{0}:8086/query"
grafana_user = 'grafana' grafana_user = 'grafana'
grafana_pass = 'grafanapass' grafana_pass = 'grafanapass'
grafana_url = "http://{0}:{1}@{2}:8000/api/org"
mysql_mode = 'local' mysql_mode = 'local'
mysql_dbname = 'grafanalma' mysql_dbname = 'grafanalma'