Merge ~hloeung/content-cache-charm:haproxy-config into content-cache-charm:master

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 006445fc0ebead5c9333c09563e1aadbef0f0961
Merged at revision: 4eba9f57939da382d43929801d51d9143f4c12f1
Proposed branch: ~hloeung/content-cache-charm:haproxy-config
Merge into: content-cache-charm:master
Diff against target: 309 lines (+119/-3)
17 files modified
lib/haproxy.py (+9/-2)
lib/utils.py (+13/-0)
templates/haproxy_cfg.tmpl (+12/-0)
tests/unit/files/content_cache_rendered_haproxy_test_output.txt (+7/-0)
tests/unit/files/content_cache_rendered_haproxy_test_output2.txt (+7/-0)
tests/unit/files/content_cache_rendered_haproxy_test_output3.txt (+7/-0)
tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt (+7/-0)
tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt (+7/-0)
tests/unit/files/haproxy_config_rendered_backends_stanzas_test_output2.txt (+6/-0)
tests/unit/files/haproxy_config_rendered_test_output.txt (+7/-0)
tests/unit/files/haproxy_config_rendered_test_output2.txt (+7/-0)
tests/unit/files/resolv.conf (+3/-0)
tests/unit/files/resolv.conf-none (+1/-0)
tests/unit/files/resolv.conf2 (+2/-0)
tests/unit/test_content_cache.py (+5/-0)
tests/unit/test_haproxy.py (+11/-1)
tests/unit/test_utils.py (+8/-0)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Content Cache Charmers Pending
Review via email: mp+382891@code.launchpad.net

Commit message

Use resolvers when hostname rather than IP provided

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Stuart Bishop (stub) wrote :

All looks good.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Failed to merge change (unable to merge source repository due to conflicts), setting status to needs review.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 4eba9f57939da382d43929801d51d9143f4c12f1

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/haproxy.py b/lib/haproxy.py
index 6171d28..33c0c3b 100644
--- a/lib/haproxy.py
+++ b/lib/haproxy.py
@@ -207,7 +207,7 @@ listen {name}
207207
208 return rendered_output208 return rendered_output
209209
210 def render_stanza_backend(self, config):210 def render_stanza_backend(self, config): # NOQA: C901
211 backend_stanza = """211 backend_stanza = """
212backend backend-{name}212backend backend-{name}
213{options}{indent}{httpchk}213{options}{indent}{httpchk}
@@ -252,11 +252,17 @@ backend backend-{name}
252 count += 1252 count += 1
253253
254 name = 'server_{}'.format(count)254 name = 'server_{}'.format(count)
255 use_resolvers = ''
256 try:
257 utils.ip_addr_port_split(backend)
258 except utils.InvalidAddressPortError:
259 use_resolvers = ' resolvers dns init-addr none'
255 backend_confs.append(260 backend_confs.append(
256 '{indent}server {name} {backend} check inter {inter_time} rise 2 fall 5 '261 '{indent}server {name} {backend}{use_resolvers} check inter {inter_time} rise 2 fall 5 '
257 'maxconn {maxconn}{tls}'.format(262 'maxconn {maxconn}{tls}'.format(
258 name=name,263 name=name,
259 backend=backend,264 backend=backend,
265 use_resolvers=use_resolvers,
260 inter_time=inter_time,266 inter_time=inter_time,
261 maxconn=maxconn,267 maxconn=maxconn,
262 tls=tls_config,268 tls=tls_config,
@@ -335,6 +341,7 @@ backend backend-{name}
335 return template.render(341 return template.render(
336 {342 {
337 'backend': self.render_stanza_backend(config),343 'backend': self.render_stanza_backend(config),
344 'dns_servers': utils.dns_servers(),
338 'global_max_connections': global_max_connections,345 'global_max_connections': global_max_connections,
339 'hard_stop_after': self.hard_stop_after,346 'hard_stop_after': self.hard_stop_after,
340 'listen': listen_stanzas,347 'listen': listen_stanzas,
diff --git a/lib/utils.py b/lib/utils.py
index 9bbb42f..f06effc 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -196,6 +196,19 @@ def process_rlimits(pid, res, limits_file=None):
196 return None196 return None
197197
198198
199def dns_servers(resolvconf_file='/etc/resolv.conf'):
200 servers = []
201 with open(resolvconf_file, 'r', encoding='utf-8') as f:
202 resolvconf = f.read()
203 for line in resolvconf.split('\n'):
204 t = line.split()
205 if not t:
206 continue
207 if t[0] == 'nameserver':
208 servers.append(t[1])
209 return servers
210
211
199def package_version(package):212def package_version(package):
200 cmd = ['dpkg-query', '--show', r'--showformat=${Version}\n', package]213 cmd = ['dpkg-query', '--show', r'--showformat=${Version}\n', package]
201 try:214 try:
diff --git a/templates/haproxy_cfg.tmpl b/templates/haproxy_cfg.tmpl
index 6d7f0b8..26242b2 100644
--- a/templates/haproxy_cfg.tmpl
+++ b/templates/haproxy_cfg.tmpl
@@ -59,6 +59,18 @@ defaults
59 errorfile 503 /etc/haproxy/errors/503.http59 errorfile 503 /etc/haproxy/errors/503.http
60 errorfile 504 /etc/haproxy/errors/504.http60 errorfile 504 /etc/haproxy/errors/504.http
6161
62{%- if dns_servers %}
63
64resolvers dns
65{%- for resolver in dns_servers %}
66 nameserver dns{{loop.index}} {{resolver}}:53
67{%- endfor %}
68 resolve_retries 3
69 timeout resolve 3s
70 timeout retry 3s
71 accepted_payload_size 8192
72{%- endif %}
73
62listen stats74listen stats
63 bind 127.0.0.1:1000075 bind 127.0.0.1:10000
64 acl allowed_cidr src 127.0.0.0/876 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
index 60c0b9e..775bc5f 100644
--- a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
@@ -54,6 +54,13 @@ defaults
54 errorfile 503 /etc/haproxy/errors/503.http54 errorfile 503 /etc/haproxy/errors/503.http
55 errorfile 504 /etc/haproxy/errors/504.http55 errorfile 504 /etc/haproxy/errors/504.http
5656
57resolvers dns
58 nameserver dns1 127.0.0.53:53
59 resolve_retries 3
60 timeout resolve 3s
61 timeout retry 3s
62 accepted_payload_size 8192
63
57listen stats64listen stats
58 bind 127.0.0.1:1000065 bind 127.0.0.1:10000
59 acl allowed_cidr src 127.0.0.0/866 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output2.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output2.txt
index 89506ab..0ef9fd0 100644
--- a/tests/unit/files/content_cache_rendered_haproxy_test_output2.txt
+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output2.txt
@@ -54,6 +54,13 @@ defaults
54 errorfile 503 /etc/haproxy/errors/503.http54 errorfile 503 /etc/haproxy/errors/503.http
55 errorfile 504 /etc/haproxy/errors/504.http55 errorfile 504 /etc/haproxy/errors/504.http
5656
57resolvers dns
58 nameserver dns1 127.0.0.53:53
59 resolve_retries 3
60 timeout resolve 3s
61 timeout retry 3s
62 accepted_payload_size 8192
63
57listen stats64listen stats
58 bind 127.0.0.1:1000065 bind 127.0.0.1:10000
59 acl allowed_cidr src 127.0.0.0/866 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output3.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output3.txt
index c82bf5c..43c80e7 100644
--- a/tests/unit/files/content_cache_rendered_haproxy_test_output3.txt
+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output3.txt
@@ -54,6 +54,13 @@ defaults
54 errorfile 503 /etc/haproxy/errors/503.http54 errorfile 503 /etc/haproxy/errors/503.http
55 errorfile 504 /etc/haproxy/errors/504.http55 errorfile 504 /etc/haproxy/errors/504.http
5656
57resolvers dns
58 nameserver dns1 127.0.0.53:53
59 resolve_retries 3
60 timeout resolve 3s
61 timeout retry 3s
62 accepted_payload_size 8192
63
57listen stats64listen stats
58 bind 127.0.0.1:1000065 bind 127.0.0.1:10000
59 acl allowed_cidr src 127.0.0.0/866 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt
index 79359af..222ccc7 100644
--- a/tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt
+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output_auto_maxconns.txt
@@ -54,6 +54,13 @@ defaults
54 errorfile 503 /etc/haproxy/errors/503.http54 errorfile 503 /etc/haproxy/errors/503.http
55 errorfile 504 /etc/haproxy/errors/504.http55 errorfile 504 /etc/haproxy/errors/504.http
5656
57resolvers dns
58 nameserver dns1 127.0.0.53:53
59 resolve_retries 3
60 timeout resolve 3s
61 timeout retry 3s
62 accepted_payload_size 8192
63
57listen stats64listen stats
58 bind 127.0.0.1:1000065 bind 127.0.0.1:10000
59 acl allowed_cidr src 127.0.0.0/866 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt
index dbf58b0..91c8ca1 100644
--- a/tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt
+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output_processes_and_threads.txt
@@ -55,6 +55,13 @@ defaults
55 errorfile 503 /etc/haproxy/errors/503.http55 errorfile 503 /etc/haproxy/errors/503.http
56 errorfile 504 /etc/haproxy/errors/504.http56 errorfile 504 /etc/haproxy/errors/504.http
5757
58resolvers dns
59 nameserver dns1 127.0.0.53:53
60 resolve_retries 3
61 timeout resolve 3s
62 timeout retry 3s
63 accepted_payload_size 8192
64
58listen stats65listen stats
59 bind 127.0.0.1:1000066 bind 127.0.0.1:10000
60 acl allowed_cidr src 127.0.0.0/867 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/haproxy_config_rendered_backends_stanzas_test_output2.txt b/tests/unit/files/haproxy_config_rendered_backends_stanzas_test_output2.txt
61new file mode 10064468new file mode 100644
index 0000000..44e1159
--- /dev/null
+++ b/tests/unit/files/haproxy_config_rendered_backends_stanzas_test_output2.txt
@@ -0,0 +1,6 @@
1
2backend backend-site1-local
3 option httpchk HEAD / HTTP/1.0\r\nHost:\ site1.local\r\nUser-Agent:\ haproxy/httpchk\r\nCache-Control:\ no-cache
4 http-request set-header Host site1.local
5 balance leastconn
6 server server_1 archive.ubuntu.com:80 resolvers dns init-addr none check inter 5s rise 2 fall 5 maxconn 2048
diff --git a/tests/unit/files/haproxy_config_rendered_test_output.txt b/tests/unit/files/haproxy_config_rendered_test_output.txt
index 7b07b8f..9545d62 100644
--- a/tests/unit/files/haproxy_config_rendered_test_output.txt
+++ b/tests/unit/files/haproxy_config_rendered_test_output.txt
@@ -55,6 +55,13 @@ defaults
55 errorfile 503 /etc/haproxy/errors/503.http55 errorfile 503 /etc/haproxy/errors/503.http
56 errorfile 504 /etc/haproxy/errors/504.http56 errorfile 504 /etc/haproxy/errors/504.http
5757
58resolvers dns
59 nameserver dns1 127.0.0.53:53
60 resolve_retries 3
61 timeout resolve 3s
62 timeout retry 3s
63 accepted_payload_size 8192
64
58listen stats65listen stats
59 bind 127.0.0.1:1000066 bind 127.0.0.1:10000
60 acl allowed_cidr src 127.0.0.0/867 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/haproxy_config_rendered_test_output2.txt b/tests/unit/files/haproxy_config_rendered_test_output2.txt
index 196b6c3..3e44b41 100644
--- a/tests/unit/files/haproxy_config_rendered_test_output2.txt
+++ b/tests/unit/files/haproxy_config_rendered_test_output2.txt
@@ -55,6 +55,13 @@ defaults
55 errorfile 503 /etc/haproxy/errors/503.http55 errorfile 503 /etc/haproxy/errors/503.http
56 errorfile 504 /etc/haproxy/errors/504.http56 errorfile 504 /etc/haproxy/errors/504.http
5757
58resolvers dns
59 nameserver dns1 127.0.0.53:53
60 resolve_retries 3
61 timeout resolve 3s
62 timeout retry 3s
63 accepted_payload_size 8192
64
58listen stats65listen stats
59 bind 127.0.0.1:1000066 bind 127.0.0.1:10000
60 acl allowed_cidr src 127.0.0.0/867 acl allowed_cidr src 127.0.0.0/8
diff --git a/tests/unit/files/resolv.conf b/tests/unit/files/resolv.conf
61new file mode 10064468new file mode 100644
index 0000000..e459d59
--- /dev/null
+++ b/tests/unit/files/resolv.conf
@@ -0,0 +1,3 @@
1nameserver 127.0.0.53
2domain local
3# comment
diff --git a/tests/unit/files/resolv.conf-none b/tests/unit/files/resolv.conf-none
0new file mode 1006444new file mode 100644
index 0000000..7fc2489
--- /dev/null
+++ b/tests/unit/files/resolv.conf-none
@@ -0,0 +1 @@
1domain local
diff --git a/tests/unit/files/resolv.conf2 b/tests/unit/files/resolv.conf2
0new file mode 1006442new file mode 100644
index 0000000..205859b
--- /dev/null
+++ b/tests/unit/files/resolv.conf2
@@ -0,0 +1,2 @@
1nameserver 127.0.0.53
2nameserver 127.0.0.153
diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
index 5b4e386..3b62cbb 100644
--- a/tests/unit/test_content_cache.py
+++ b/tests/unit/test_content_cache.py
@@ -70,6 +70,11 @@ class TestCharm(unittest.TestCase):
70 self.mock_close_port = patcher.start()70 self.mock_close_port = patcher.start()
71 self.addCleanup(patcher.stop)71 self.addCleanup(patcher.stop)
7272
73 patcher = mock.patch('lib.utils.dns_servers')
74 self.mock_dns_servers = patcher.start()
75 self.addCleanup(patcher.stop)
76 self.mock_dns_servers.return_value = ['127.0.0.53']
77
73 patcher = mock.patch('multiprocessing.cpu_count')78 patcher = mock.patch('multiprocessing.cpu_count')
74 self.mock_cpu_count = patcher.start()79 self.mock_cpu_count = patcher.start()
75 self.addCleanup(patcher.stop)80 self.addCleanup(patcher.stop)
diff --git a/tests/unit/test_haproxy.py b/tests/unit/test_haproxy.py
index 60cf0d0..198879d 100644
--- a/tests/unit/test_haproxy.py
+++ b/tests/unit/test_haproxy.py
@@ -146,10 +146,20 @@ class TestLibHAProxy(unittest.TestCase):
146 want = f.read()146 want = f.read()
147 self.assertEqual(''.join(haproxy.render_stanza_backend(config)), want)147 self.assertEqual(''.join(haproxy.render_stanza_backend(config)), want)
148148
149 def test_haproxy_config_rendered_backend_stanzas_use_dns(self):
150 haproxy = HAProxy.HAProxyConf(self.tmpdir)
151 config = {'site1.local': {'locations': {'/': {'backends': ['archive.ubuntu.com:80']}}}}
152 output = 'tests/unit/files/haproxy_config_rendered_backends_stanzas_test_output2.txt'
153 with open(output, 'r', encoding='utf-8') as f:
154 want = f.read()
155 self.assertEqual(''.join(haproxy.render_stanza_backend(config)), want)
156
149 @freezegun.freeze_time("2019-03-22", tz_offset=0)157 @freezegun.freeze_time("2019-03-22", tz_offset=0)
158 @mock.patch('lib.utils.dns_servers')
150 @mock.patch('lib.utils.package_version')159 @mock.patch('lib.utils.package_version')
151 @mock.patch('lib.utils.process_rlimits')160 @mock.patch('lib.utils.process_rlimits')
152 def test_haproxy_config_rendered_full_config(self, process_rlimits, package_version):161 def test_haproxy_config_rendered_full_config(self, process_rlimits, package_version, dns_servers):
162 dns_servers.return_value = ['127.0.0.53']
153 package_version.return_value = '1.8.8-1ubuntu0.10'163 package_version.return_value = '1.8.8-1ubuntu0.10'
154 haproxy = HAProxy.HAProxyConf(self.tmpdir, max_connections=5000)164 haproxy = HAProxy.HAProxyConf(self.tmpdir, max_connections=5000)
155 config = self.site_config165 config = self.site_config
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 60da5f6..b2d7348 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -186,6 +186,14 @@ class TestLibUtils(unittest.TestCase):
186186
187 self.assertEqual(None, utils.process_rlimits(1, 'NOMATCH'))187 self.assertEqual(None, utils.process_rlimits(1, 'NOMATCH'))
188188
189 def test_dns_servers(self):
190 want = ['127.0.0.53']
191 self.assertEqual(want, utils.dns_servers('tests/unit/files/resolv.conf'))
192 want = ['127.0.0.53', '127.0.0.153']
193 self.assertEqual(want, utils.dns_servers('tests/unit/files/resolv.conf2'))
194 want = []
195 self.assertEqual(want, utils.dns_servers('tests/unit/files/resolv.conf-none'))
196
189 def test_package_version(self):197 def test_package_version(self):
190 self.assertTrue(int(utils.package_version('apt').split('.')[0]) > 1)198 self.assertTrue(int(utils.package_version('apt').split('.')[0]) > 1)
191199

Subscribers

People subscribed via source and target branches