Commit 8028a6ba authored by Majavah's avatar Majavah
Browse files

New upstream version 0.1.5

parent ccd20603
[flake8]
exclude = .venv,src/vendor
max-complexity = 10
max-line-length = 120
......@@ -10,3 +10,5 @@ debian/files
parts
prime
stage
.idea
.tox
......@@ -29,11 +29,13 @@ install-build-depends:
debhelper \
git-buildpackage
lint: lint-python
lint:
@echo "Running lint checks"
@tox -e lint
# See .flake8 for config options.
lint-python: $(VENV)
$(FLAKE8) $(PROJECTPATH) $(EXTRA_PY)
black:
@echo "Reformat files with black"
@tox -e black
test: test-python
......
......@@ -9,6 +9,7 @@ OS_INTERFACE=internal
OS_IDENTITY_API_VERSION=3
OS_REGION_NAME=mycloud
#logLevel=INFO
#listenPort=9183
#cacheRefreshInterval=300
#vcpuRatio=1.0
......
This diff is collapsed.
......@@ -9,6 +9,7 @@ cloud: VAR_CLOUD # cloud=${OS_REGION_NAME:-mycloud}
openstack_allocation_ratio_vcpu: VAR_VCPU_RATIO # vcpuRatio=1.0
openstack_allocation_ratio_ram: VAR_RAM_RATIO # ramRatio=1.0
openstack_allocation_ratio_disk: VAR_DISK_RATIO # diskRatio=1.0
log_level: VAR_LOG_LEVEL # logLevel=INFO
# Configure the enabled collectors here. Note that the Swift account
# collector in particular has special requirements.
......
"""This module install prometheus-openstack-exporter."""
import os
from setuptools import setup
def read(fname):
"""Read a file and return the content."""
return open(os.path.join(os.path.dirname(__file__), fname)).read()
......@@ -24,11 +27,10 @@ setup(
"netaddr",
"swift",
],
long_description=read('README.md'),
long_description=read("README.md"),
classifiers=[
"Development Status :: 4 - Beta",
"Topic :: System :: Networking :: Monitoring",
"License :: OSI Approved :: "
"GNU General Public License v3 or later (GPLv3+)",
"License :: OSI Approved :: " "GNU General Public License v3 or later (GPLv3+)",
],
)
......@@ -17,6 +17,8 @@ parts:
plugin: python
python-version: python2
source: .
build-packages:
- build-essential
stage-packages:
- libdb5.3
- liberasurecode-dev
......
"""Unit Test for Swift Account metrics collector."""
import unittest
from mock import Mock, call, patch
from requests.structures import CaseInsensitiveDict
import prometheus_openstack_exporter as poe
from requests.structures import CaseInsensitiveDict
class TestSwiftAccountUsage(unittest.TestCase):
@patch('prometheus_openstack_exporter.SwiftAccountUsage._get_account_ring')
@patch('prometheus_openstack_exporter.requests.head')
@patch('prometheus_openstack_exporter.config')
def test__get_account_usage(self, _config, _requests_head, _SwiftAccountUsage__get_account_ring):
class TestSwiftAccountUsage(unittest.TestCase): # noqa: D101
@patch("prometheus_openstack_exporter.SwiftAccountUsage._get_account_ring")
@patch("prometheus_openstack_exporter.requests.head")
@patch("prometheus_openstack_exporter.config")
def test__get_account_usage(self, _config, _requests_head, _get_account_ring):
"""Test Swift Account Usage metrics collection."""
s = poe.SwiftAccountUsage()
s.account_ring.get_nodes.return_value = (26701, [
{'device': 'sdb', 'id': 0, 'ip': '10.24.0.18', 'meta': u'', 'port': 6002,
'region': 1, 'replication_ip': '10.24.0.18', 'replication_port': 6002, 'weight': 100.0, 'zone': 1},
{'device': 'sdd', 'id': 50, 'ip': '10.24.0.71', 'meta': u'', 'port': 6002,
'region': 1, 'replication_ip': '10.24.0.71', 'replication_port': 6002, 'weight': 180.0, 'zone': 3},
{'device': 'sdi', 'id': 59, 'ip': '10.24.0.72', 'meta': u'', 'port': 6002,
'region': 1, 'replication_ip': '10.24.0.72', 'replication_port': 6002, 'weight': 360.0, 'zone': 2}
])
s.account_ring.get_nodes.return_value = (
26701,
[
{
"device": "sdb",
"id": 0,
"ip": "10.24.0.18",
"meta": u"",
"port": 6002,
"region": 1,
"replication_ip": "10.24.0.18",
"replication_port": 6002,
"weight": 100.0,
"zone": 1,
},
{
"device": "sdd",
"id": 50,
"ip": "10.24.0.71",
"meta": u"",
"port": 6002,
"region": 1,
"replication_ip": "10.24.0.71",
"replication_port": 6002,
"weight": 180.0,
"zone": 3,
},
{
"device": "sdi",
"id": 59,
"ip": "10.24.0.72",
"meta": u"",
"port": 6002,
"region": 1,
"replication_ip": "10.24.0.72",
"replication_port": 6002,
"weight": 360.0,
"zone": 2,
},
],
)
response_mock = Mock()
response_mock.configure_mock(
status_code=204,
headers=CaseInsensitiveDict({'x-account-bytes-used': '368259416'}),
)
headers=CaseInsensitiveDict({"x-account-bytes-used": "368259416"}),
)
_requests_head.return_value = response_mock
# Assert that _get_account_ring does what we expect.
self.assertEqual(s._get_account_usage('AUTH_12bb569bf909441b90791482ae6f9ca9'), 368259416)
self.assertEqual(
s._get_account_usage("AUTH_12bb569bf909441b90791482ae6f9ca9"), 368259416
)
# Assert that _get_account_ring did it in the manner we expected.
s.account_ring.get_nodes.assert_called_once_with(account='AUTH_12bb569bf909441b90791482ae6f9ca9')
s.account_ring.get_nodes.assert_called_once_with(
account="AUTH_12bb569bf909441b90791482ae6f9ca9"
)
poe.requests.head.assert_called_once()
self.assertTrue(poe.requests.head.call_args in [
call('http://10.24.0.18:6002/sdb/26701/AUTH_12bb569bf909441b90791482ae6f9ca9'),
call('http://10.24.0.71:6002/sdd/26701/AUTH_12bb569bf909441b90791482ae6f9ca9'),
call('http://10.24.0.72:6002/sdi/26701/AUTH_12bb569bf909441b90791482ae6f9ca9'),
])
self.assertTrue(
poe.requests.head.call_args
in [
call(
"http://10.24.0.18:6002/sdb/26701/AUTH_12bb569bf909441b90791482ae6f9ca9" # noqa: E501
),
call(
"http://10.24.0.71:6002/sdd/26701/AUTH_12bb569bf909441b90791482ae6f9ca9" # noqa: E501
),
call(
"http://10.24.0.72:6002/sdi/26701/AUTH_12bb569bf909441b90791482ae6f9ca9" # noqa: E501
),
]
)
"""Unit Test for Prometheus OpenStack exporter."""
import unittest
import mock
......@@ -5,39 +6,49 @@ import mock
import prometheus_openstack_exporter as poe
class TestPrometheusOpenstackExporter(unittest.TestCase):
def test_data_gatherer_needed(self):
class TestPrometheusOpenstackExporter(unittest.TestCase): # noqa: D101
def test_data_gatherer_needed(self): # noqa: D102
self.assertTrue(
poe.data_gatherer_needed(
{'enabled_collectors':
['cinder', 'neutron', 'nova', 'swift']}))
{"enabled_collectors": ["cinder", "neutron", "nova", "swift"]}
)
)
self.assertTrue(
poe.data_gatherer_needed(
{'enabled_collectors':
['cinder', 'neutron', 'nova', 'swift', 'swift-account-usage']}))
{
"enabled_collectors": [
"cinder",
"neutron",
"nova",
"swift",
"swift-account-usage",
]
}
)
)
self.assertFalse(
poe.data_gatherer_needed(
{'enabled_collectors':
['swift-account-usage']}))
poe.data_gatherer_needed({"enabled_collectors": ["swift-account-usage"]})
)
self.assertFalse(
poe.data_gatherer_needed(
{'enabled_collectors':
['swift', 'swift-account-usage']}))
{"enabled_collectors": ["swift", "swift-account-usage"]}
)
)
self.assertEqual(
poe.data_gatherer_needed(
{'enabled_collectors':
['cinder', 'neutron', 'nova', 'swift']}),
set(['cinder', 'nova', 'neutron']),
{"enabled_collectors": ["cinder", "neutron", "nova", "swift"]}
),
set(["cinder", "nova", "neutron"]),
)
self.assertEqual(
poe.data_gatherer_needed({}),
set(['cinder', 'nova', 'neutron']),
set(["cinder", "nova", "neutron"]),
)
@mock.patch('prometheus_openstack_exporter.config')
def test_get_nova_info(self, config):
@mock.patch("prometheus_openstack_exporter.config")
def test_get_nova_info(self, config): # noqa: D102
config.return_value = {}
prodstack = {'tenants': []}
prodstack = {"tenants": []}
nova = mock.Mock()
nova.aggregates = mock.MagicMock()
nova.flavors = mock.MagicMock()
......@@ -50,14 +61,63 @@ class TestPrometheusOpenstackExporter(unittest.TestCase):
data_gatherer._get_nova_info(nova, None, prodstack)
expected = [
mock.call(search_opts={'marker': '', 'limit': '100', 'status': 'ACTIVE', 'all_tenants': '1'}),
mock.call(search_opts={'marker': '', 'limit': '100', 'status': 'ERROR', 'all_tenants': '1'}),
mock.call(search_opts={'marker': '', 'limit': '100', 'status': 'SHELVED_OFFLOADED', 'all_tenants': '1'}),
mock.call(search_opts={'marker': '', 'limit': '100', 'status': 'SHUTOFF', 'all_tenants': '1'}),
mock.call(search_opts={'marker': '', 'limit': '100', 'status': 'SUSPENDED', 'all_tenants': '1'}),
mock.call(search_opts={'marker': '', 'limit': '100', 'status': 'VERIFY_RESIZE', 'all_tenants': '1'}),
mock.call(
search_opts={
"marker": "",
"limit": "100",
"status": "ACTIVE",
"all_tenants": "1",
}
),
mock.call(
search_opts={
"marker": "",
"limit": "100",
"status": "ERROR",
"all_tenants": "1",
}
),
mock.call(
search_opts={
"marker": "",
"limit": "100",
"status": "SHELVED_OFFLOADED",
"all_tenants": "1",
}
),
mock.call(
search_opts={
"marker": "",
"limit": "100",
"status": "SHUTOFF",
"all_tenants": "1",
}
),
mock.call(
search_opts={
"marker": "",
"limit": "100",
"status": "SUSPENDED",
"all_tenants": "1",
}
),
mock.call(
search_opts={
"marker": "",
"limit": "100",
"status": "VERIFY_RESIZE",
"all_tenants": "1",
}
),
]
nova.servers.list.assert_has_calls(expected, any_order=True)
not_expected = mock.call(search_opts={'marker': '', 'limit': '100', 'status': 'BUILD', 'all_tenants': '1'})
not_expected = mock.call(
search_opts={
"marker": "",
"limit": "100",
"status": "BUILD",
"all_tenants": "1",
}
)
self.assertTrue(not_expected not in nova.servers.list.call_args_list)
[tox]
skipsdist=True
skip_missing_interpreters = True
envlist = lint
[testenv]
basepython = python3
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/lib/:{toxinidir}/hooks/
passenv =
HOME
PATH
CHARM_BUILD_DIR
PYTEST_KEEP_MODEL
PYTEST_CLOUD_NAME
PYTEST_CLOUD_REGION
PYTEST_MODEL
MODEL_SETTINGS
HTTP_PROXY
HTTPS_PROXY
NO_PROXY
SNAP_HTTP_PROXY
SNAP_HTTPS_PROXY
[testenv:lint]
commands =
flake8
black --check prometheus-openstack-exporter
black --check tests
black --check setup.py
deps =
black
flake8
flake8-docstrings
flake8-import-order
pep8-naming
flake8-colors
[flake8]
exclude =
.git,
__pycache__,
.tox,
charmhelpers,
mod,
.build,
.venv
max-line-length = 88
max-complexity = 10
[testenv:black]
commands =
black prometheus-openstack-exporter
black tests
black setup.py
deps =
black
\ No newline at end of file
......@@ -2,6 +2,7 @@
prometheusDir='/etc/prometheus'
configFile=${configFile:-"${prometheusDir}/prometheus-openstack-exporter.yaml"}
logLevel=${logLevel:-INFO}
listenPort=${listenPort:-9183}
cacheRefreshInterval=${cacheRefreshInterval:-300}
cacheFileName=${cacheFileName:-"$(mktemp -p /dev/shm/)"}
......@@ -26,6 +27,7 @@ if [ ! -e "${configFile}" ]; then
cp prometheus-openstack-exporter.sample.yaml ${configFile}
sed -i "s|VAR_LISTEN_PORT|${listenPort}|g" ${configFile}
sed -i "s|VAR_LOG_LEVEL|${logLevel}|g" ${configFile}
sed -i "s|VAR_CACHE_REFRESH_INTERVAL|${cacheRefreshInterval}|g" ${configFile}
sed -i "s|VAR_CACHE_FILE|${cacheFileName}|g" ${configFile}
sed -i "s|VAR_CLOUD|${cloud}|g" ${configFile}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment