-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest_config.py
176 lines (133 loc) · 7.89 KB
/
test_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import unittest
import os
from prometheus_pgbouncer_exporter.config import *
CURR_DIR=os.path.dirname(os.path.realpath(__file__))
class TestConfig(unittest.TestCase):
#
# read()
#
def testReadShouldRaiseErrorOnUnexistingFile(self):
config = Config()
with self.assertRaises(Exception):
config.read("/path/to/none.yml")
def testReadShouldSupportAnEmptyConfigFile(self):
config = Config()
config.read(CURR_DIR + "/fixtures/config-empty.yml")
self.assertEqual(config.getExporterHost(), "127.0.0.1")
self.assertEqual(config.getExporterPort(), 9100)
self.assertEqual(config.getPgbouncers(), [])
def testReadShouldParseConfigFileWithOnePgbouncer(self):
config = Config()
config.read(CURR_DIR + "/fixtures/config-with-one-pgbouncer.yml")
self.assertEqual(config.getExporterHost(), "0.0.0.0")
self.assertEqual(config.getExporterPort(), 1234)
self.assertEqual(len(config.getPgbouncers()), 1)
self.assertEqual(config.getPgbouncers()[0].getDsn(), "postgresql://user:password@host:6431/pgbouncer")
self.assertEqual(config.getPgbouncers()[0].getDsnWithMaskedPassword(), "postgresql://user:***@host:6431/pgbouncer")
self.assertEqual(config.getPgbouncers()[0].getConnectTimeout(), 2)
self.assertEqual(config.getPgbouncers()[0].getIncludeDatabases(), ["one", "two"])
self.assertEqual(config.getPgbouncers()[0].getExcludeDatabases(), ["three"])
self.assertEqual(config.getPgbouncers()[0].getExtraLabels(), {"first": "1", "second": "2"})
def testReadShouldParseConfigFileWithTwoPgbouncer(self):
config = Config()
config.read(CURR_DIR + "/fixtures/config-with-two-pgbouncer.yml")
self.assertEqual(config.getExporterHost(), "0.0.0.0")
self.assertEqual(config.getExporterPort(), 1234)
self.assertEqual(len(config.getPgbouncers()), 2)
self.assertEqual(config.getPgbouncers()[0].getDsn(), "postgresql://user:password@host:6431/pgbouncer")
self.assertEqual(config.getPgbouncers()[0].getDsnWithMaskedPassword(), "postgresql://user:***@host:6431/pgbouncer")
self.assertEqual(config.getPgbouncers()[0].getConnectTimeout(), 2)
self.assertEqual(config.getPgbouncers()[0].getIncludeDatabases(), ["one", "two"])
self.assertEqual(config.getPgbouncers()[0].getExcludeDatabases(), ["three"])
self.assertEqual(config.getPgbouncers()[0].getExtraLabels(), {"first": "1", "second": "2"})
self.assertEqual(config.getPgbouncers()[1].getDsn(), "postgresql://user:password@host:6432/pgbouncer")
self.assertEqual(config.getPgbouncers()[1].getDsnWithMaskedPassword(), "postgresql://user:***@host:6432/pgbouncer")
self.assertEqual(config.getPgbouncers()[1].getConnectTimeout(), 5)
self.assertEqual(config.getPgbouncers()[1].getIncludeDatabases(), [])
self.assertEqual(config.getPgbouncers()[1].getExcludeDatabases(), [])
self.assertEqual(config.getPgbouncers()[1].getExtraLabels(), {})
def testReadShouldInjectEnvironmentVariablesOnParsing(self):
os.environ["TEST_USERNAME"] = "marco"
os.environ["TEST_PASSWORD"] = "secret"
os.environ["TEST_INCLUDE_DATABASE"] = "production"
os.environ["TEST_EXTRA_LABEL_NAME"] = "cluster"
os.environ["TEST_EXTRA_LABEL_VALUE"] = "users-1-1000"
config = Config()
config.read(CURR_DIR + "/fixtures/config-with-env-vars.yml")
self.assertEqual(len(config.getPgbouncers()), 1)
self.assertEqual(config.getPgbouncers()[0].getDsn(), "postgresql://marco:secret@host:6431/pgbouncer")
self.assertEqual(config.getPgbouncers()[0].getIncludeDatabases(), ["production"])
self.assertEqual(config.getPgbouncers()[0].getExtraLabels(), {"cluster": "users-1-1000"})
def testReadShouldInjectEnvironmentVariablesOnParsingEvenIfEmpty(self):
os.environ["TEST_USERNAME"] = "marco"
os.environ["TEST_PASSWORD"] = ""
os.environ["TEST_INCLUDE_DATABASE"] = "production"
os.environ["TEST_EXTRA_LABEL_NAME"] = "cluster"
os.environ["TEST_EXTRA_LABEL_VALUE"] = "users-1-1000"
config = Config()
config.read(CURR_DIR + "/fixtures/config-with-env-vars.yml")
self.assertEqual(len(config.getPgbouncers()), 1)
self.assertEqual(config.getPgbouncers()[0].getDsn(), "postgresql://marco:@host:6431/pgbouncer")
def testReadShouldKeepOriginalConfigOnMissingEnvironmentVariables(self):
del os.environ["TEST_USERNAME"]
os.environ["TEST_PASSWORD"] = "secret"
del os.environ["TEST_INCLUDE_DATABASE"]
os.environ["TEST_EXTRA_LABEL_NAME"] = "cluster"
os.environ["TEST_EXTRA_LABEL_VALUE"] = "users-1-1000"
config = Config()
config.read(CURR_DIR + "/fixtures/config-with-env-vars.yml")
self.assertEqual(len(config.getPgbouncers()), 1)
self.assertEqual(config.getPgbouncers()[0].getDsn(), "postgresql://$(TEST_USERNAME):secret@host:6431/pgbouncer")
self.assertEqual(config.getPgbouncers()[0].getIncludeDatabases(), ["$(TEST_INCLUDE_DATABASE)"])
self.assertEqual(config.getPgbouncers()[0].getExtraLabels(), {"cluster": "users-1-1000"})
#
# validate()
#
def testValidateShouldPassOnConfigContainingOnePgbouncer(self):
config = Config()
config.read(CURR_DIR + "/fixtures/config-with-one-pgbouncer.yml")
config.validate()
def testValidateShouldPassOnConfigContainingTwoPgbouncer(self):
config = Config()
config.read(CURR_DIR + "/fixtures/config-with-two-pgbouncer.yml")
config.validate()
def testValidateShouldRaiseExceptionOnNoPgbouncerConfigured(self):
config = Config({})
with self.assertRaisesRegex(Exception, "no pgbouncer instance configured"):
config.validate()
def testValidateShouldRaiseExceptionOnTwoPgbouncersWithNoLabels(self):
config = Config({"pgbouncers": [
{"dsn": "postgresql://"},
{"dsn": "postgresql://"}
]})
with self.assertRaisesRegex(Exception, "extra_labels configured for each pgbouncer must be unique"):
config.validate()
def testValidateShouldRaiseExceptionOnTwoPgbouncersWithSameExtraLabels(self):
config = Config({"pgbouncers": [
{"dsn": "postgresql://", "extra_labels": {"pool_id": 1, "another_id": 5}},
{"dsn": "postgresql://", "extra_labels": {"pool_id": 1, "another_id": 5}}
]})
with self.assertRaisesRegex(Exception, "extra_labels configured for each pgbouncer must be unique"):
config.validate()
def testValidateShouldPassOnTwoPgbouncersWithDifferentExtraLabels(self):
config = Config({"pgbouncers": [
{"dsn": "postgresql://", "extra_labels": {"pool_id": 1, "another_id": 5}},
{"dsn": "postgresql://", "extra_labels": {"pool_id": 2, "another_id": 5}}
]})
config.validate()
class TestPgbouncerConfig(unittest.TestCase):
def testGetDsnWithMaskedPasswordShouldReturnDsnWithThreeAsterisksInsteadOfThePassword(self):
config = PgbouncerConfig({"dsn": "postgresql://pgbouncer:secret@localhost:6431/pgbouncer"})
self.assertEqual(config.getDsnWithMaskedPassword(), "postgresql://pgbouncer:***@localhost:6431/pgbouncer")
def testGetDsnWithMaskedPasswordShouldWorkEvenIfThePasswordIsEmpty(self):
config = PgbouncerConfig({"dsn": "postgresql://pgbouncer:@localhost:6431/pgbouncer"})
self.assertEqual(config.getDsnWithMaskedPassword(), "postgresql://pgbouncer:***@localhost:6431/pgbouncer")
def testValidateShouldPassOnConfigContainingOnlyDsn(self):
config = PgbouncerConfig({"dsn": "postgresql://"})
config.validate()
def testValidateShouldRaiseExceptionOnEmptyDsn(self):
config = PgbouncerConfig({"dsn": ""})
with self.assertRaisesRegex(Exception, "The DSN is required"):
config.validate()
if __name__ == '__main__':
unittest.main()