Application version support

Set the vault app version in juju status using the version
returned from the Vault health data (rather than the installed
snap).

This review also puts in place the atexit handling needed for
general status assessment.

Change-Id: I1f8f0991534f4e130e924ffac733fa13141f0970
This commit is contained in:
James Page 2018-04-16 10:51:21 +01:00
parent 7850184802
commit 026e801475
3 changed files with 61 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import base64 import base64
import hvac import hvac
import psycopg2 import psycopg2
import requests
import subprocess import subprocess
@ -18,6 +19,8 @@ from charmhelpers.core.hookenv import (
open_port, open_port,
status_set, status_set,
unit_private_ip, unit_private_ip,
application_version_set,
atexit,
) )
from charmhelpers.core.host import ( from charmhelpers.core.host import (
@ -60,11 +63,18 @@ VAULT_INDEX_DDL = """
CREATE INDEX IF NOT EXISTS parent_path_idx ON vault_kv_store (parent_path); CREATE INDEX IF NOT EXISTS parent_path_idx ON vault_kv_store (parent_path);
""" """
VAULT_HEALTH_URL = '{vault_addr}/v1/sys/health'
def get_client(): def get_client():
return hvac.Client(url=get_api_url()) return hvac.Client(url=get_api_url())
def get_vault_health():
response = requests.get(VAULT_HEALTH_URL.format(vault_addr=get_api_url()))
return response.json()
def can_restart(): def can_restart():
safe_restart = False safe_restart = False
if not service_running('vault'): if not service_running('vault'):
@ -317,3 +327,14 @@ def nagios_context_changed():
@when('config.changed.nagios_servicegroups') @when('config.changed.nagios_servicegroups')
def nagios_servicegroups_changed(): def nagios_servicegroups_changed():
remove_state('vault.nrpe.configured') remove_state('vault.nrpe.configured')
@when('snap.installed.vault')
def prime_assess_status():
atexit(_assess_status)
def _assess_status():
if service_running('vault'):
health = get_vault_health()
application_version_set(health.get('version'))

View File

@ -48,7 +48,7 @@ deps = -r{toxinidir}/test-requirements.txt
commands = ostestr {posargs} commands = ostestr {posargs}
[testenv:pep8] [testenv:pep8]
basepython = python3.5 basepython = python3
deps = -r{toxinidir}/test-requirements.txt deps = -r{toxinidir}/test-requirements.txt
commands = flake8 {posargs} src unit_tests commands = flake8 {posargs} src unit_tests

View File

@ -16,6 +16,16 @@ import reactive.vault as handlers # noqa: E402
class TestHandlers(unittest.TestCase): class TestHandlers(unittest.TestCase):
_health_response = {
"initialized": True,
"sealed": False,
"standby": False,
"server_time_utc": 1523952750,
"version": "0.9.0",
"cluster_name": "vault-cluster-9dd8dd12",
"cluster_id": "1ea3d74c-3819-fbaf-f780-bae0babc998f"
}
def setUp(self): def setUp(self):
super(TestHandlers, self).setUp() super(TestHandlers, self).setUp()
self.patches = [ self.patches = [
@ -32,6 +42,7 @@ class TestHandlers(unittest.TestCase):
'remove_state', 'remove_state',
'render', 'render',
'unit_private_ip', 'unit_private_ip',
'application_version_set',
] ]
self.patch_all() self.patch_all()
@ -240,3 +251,31 @@ class TestHandlers(unittest.TestCase):
self.is_state.return_value = False self.is_state.return_value = False
self.unit_private_ip.return_value = '1.2.3.4' self.unit_private_ip.return_value = '1.2.3.4'
self.assertEqual(handlers.get_api_url(), 'http://1.2.3.4:8200') self.assertEqual(handlers.get_api_url(), 'http://1.2.3.4:8200')
@patch.object(handlers, 'get_api_url')
@patch.object(handlers, 'requests')
def test_get_vault_health(self, requests, get_api_url):
get_api_url.return_value = "https://vault.demo.com:8200"
mock_response = mock.MagicMock()
mock_response.json.return_value = self._health_response
requests.get.return_value = mock_response
self.assertEqual(handlers.get_vault_health(),
self._health_response)
requests.get.assert_called_with(
"https://vault.demo.com:8200/v1/sys/health")
mock_response.json.assert_called_once()
@patch.object(handlers, 'get_vault_health')
def test_assess_status(self, get_vault_health):
get_vault_health.return_value = self._health_response
self.service_running.return_value = True
handlers._assess_status()
self.application_version_set.assert_called_with(
self._health_response['version'])
@patch.object(handlers, 'get_vault_health')
def test_assess_status_not_running(self, get_vault_health):
get_vault_health.return_value = self._health_response
self.service_running.return_value = False
handlers._assess_status()
self.application_version_set.assert_not_called()