Skip to content

Commit 156a5e8

Browse files
committed
add test cases for deprecation warning on variable capitalization
Signed-off-by: Marc-Aurèle Brothier <m@brothier.org>
1 parent 2d2fc94 commit 156a5e8

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

tests/test_multiprocess.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import sys
77
import tempfile
8+
import warnings
89

910
from prometheus_client import mmap_dict, values
1011
from prometheus_client.core import (
@@ -13,7 +14,7 @@
1314
from prometheus_client.multiprocess import (
1415
mark_process_dead, MultiProcessCollector,
1516
)
16-
from prometheus_client.values import MultiProcessValue, MutexValue
17+
from prometheus_client.values import MultiProcessValue, MutexValue, get_value_class
1718

1819
if sys.version_info < (2, 7):
1920
# We need the skip decorators from unittest2 on Python 2.6.
@@ -22,10 +23,36 @@
2223
import unittest
2324

2425

25-
class TestMultiProcess(unittest.TestCase):
26+
class TestMultiProcessDeprecation(unittest.TestCase):
2627
def setUp(self):
2728
self.tempdir = tempfile.mkdtemp()
29+
30+
def tearDown(self):
31+
del os.environ['prometheus_multiproc_dir']
32+
del os.environ['PROMETHEUS_MULTIPROC_DIR']
33+
shutil.rmtree(self.tempdir)
34+
35+
def test_deprecation_warning(self):
2836
os.environ['prometheus_multiproc_dir'] = self.tempdir
37+
with warnings.catch_warnings(record=True) as w:
38+
get_value_class()
39+
assert os.environ['PROMETHEUS_MULTIPROC_DIR'] == self.tempdir
40+
assert len(w) == 1
41+
assert issubclass(w[-1].category, DeprecationWarning)
42+
assert "PROMETHEUS_MULTIPROC_DIR" in str(w[-1].message)
43+
44+
def test_both_variables_priority(self):
45+
os.environ['prometheus_multiproc_dir'] = 'should not be picked'
46+
os.environ['PROMETHEUS_MULTIPROC_DIR'] = self.tempdir
47+
with warnings.catch_warnings(record=True) as w:
48+
get_value_class()
49+
assert len(w) == 0
50+
51+
52+
class TestMultiProcess(unittest.TestCase):
53+
def setUp(self):
54+
self.tempdir = tempfile.mkdtemp()
55+
os.environ['PROMETHEUS_MULTIPROC_DIR'] = self.tempdir
2956
values.ValueClass = MultiProcessValue(lambda: 123)
3057
self.registry = CollectorRegistry()
3158
self.collector = MultiProcessCollector(self.registry, self.tempdir)
@@ -35,7 +62,7 @@ def _value_class(self):
3562
return
3663

3764
def tearDown(self):
38-
del os.environ['prometheus_multiproc_dir']
65+
del os.environ['PROMETHEUS_MULTIPROC_DIR']
3966
shutil.rmtree(self.tempdir)
4067
values.ValueClass = MutexValue
4168

@@ -80,7 +107,7 @@ def test_gauge_all(self):
80107
self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '456'}))
81108
g1.set(1)
82109
g2.set(2)
83-
mark_process_dead(123, os.environ['prometheus_multiproc_dir'])
110+
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])
84111
self.assertEqual(1, self.registry.get_sample_value('g', {'pid': '123'}))
85112
self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
86113

@@ -94,7 +121,7 @@ def test_gauge_liveall(self):
94121
g2.set(2)
95122
self.assertEqual(1, self.registry.get_sample_value('g', {'pid': '123'}))
96123
self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
97-
mark_process_dead(123, os.environ['prometheus_multiproc_dir'])
124+
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])
98125
self.assertEqual(None, self.registry.get_sample_value('g', {'pid': '123'}))
99126
self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
100127

@@ -124,7 +151,7 @@ def test_gauge_livesum(self):
124151
g1.set(1)
125152
g2.set(2)
126153
self.assertEqual(3, self.registry.get_sample_value('g'))
127-
mark_process_dead(123, os.environ['prometheus_multiproc_dir'])
154+
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])
128155
self.assertEqual(2, self.registry.get_sample_value('g'))
129156

130157
def test_namespace_subsystem(self):
@@ -151,7 +178,7 @@ def test_initialization_detects_pid_change(self):
151178
# can not inspect the files cache directly, as it's a closure, so we
152179
# check for the actual files themselves
153180
def files():
154-
fs = os.listdir(os.environ['prometheus_multiproc_dir'])
181+
fs = os.listdir(os.environ['PROMETHEUS_MULTIPROC_DIR'])
155182
fs.sort()
156183
return fs
157184

@@ -240,7 +267,7 @@ def add_label(key, value):
240267
pid = 1
241268
h.labels(**labels).observe(5)
242269

243-
path = os.path.join(os.environ['prometheus_multiproc_dir'], '*.db')
270+
path = os.path.join(os.environ['PROMETHEUS_MULTIPROC_DIR'], '*.db')
244271
files = glob.glob(path)
245272
metrics = dict(
246273
(m.name, m) for m in self.collector.merge(files, accumulate=False)

0 commit comments

Comments
 (0)