Merge ~cjwatson/charm-postfix-relay:ensure-mailname into charm-postfix-relay:master

Proposed by Colin Watson
Status: Merged
Approved by: Tianqi Xiao
Approved revision: 7c23020114f7e355998ef6fb789aefad193f0598
Merged at revision: 9a44eb5794433800cf04674b4c5195de53326388
Proposed branch: ~cjwatson/charm-postfix-relay:ensure-mailname
Merge into: charm-postfix-relay:master
Diff against target: 73 lines (+30/-0)
2 files modified
src/lib/charm/postfix/postfix_relay.py (+9/-0)
src/tests/unit/test_postfix_relay.py (+21/-0)
Reviewer Review Type Date Requested Status
Tianqi Xiao (community) Approve
Gabriel Cocenza Approve
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
Nick Moffitt (community) Approve
Review via email: mp+444992@code.launchpad.net

Commit message

Ensure that /etc/mailname exists

Description of the change

While Ubuntu LXD containers come up with `/etc/mailname` by default, not all image providers do the same - for example OpenStack VMs using Ubuntu cloud images don't. Ensure that it exists with some minimally-reasonable contents, since the generated `/etc/postfix/main.cf` requires it.

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
Nick Moffitt (nick-moffitt) wrote :

This mirrors a workaround we've done by hand in the past. We used `uname -n` instead of `socket.getfqdn()`, but I've verified on those systems that the result is the same.

review: Approve
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

LGTM

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

Change successfully merged at revision 9a44eb5794433800cf04674b4c5195de53326388

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/charm/postfix/postfix_relay.py b/src/lib/charm/postfix/postfix_relay.py
2index 2ca0ac6..ca916a2 100644
3--- a/src/lib/charm/postfix/postfix_relay.py
4+++ b/src/lib/charm/postfix/postfix_relay.py
5@@ -13,6 +13,7 @@
6 # See the License for the specific language governing permissions and
7 # limitations under the License
8 import os
9+import socket
10 import subprocess
11 from base64 import b64decode
12
13@@ -36,6 +37,7 @@ def write_configs():
14 cfg_template = Template(t.read())
15 with open(MAIN_CFG, "w+") as f:
16 f.write(cfg_template.render(PostfixContext()()))
17+ ensure_mailname()
18 if config("smtp_auth_username") and config("smtp_auth_password"):
19 write_auth_file()
20 if config("domain_rewrite_map"):
21@@ -47,6 +49,13 @@ def write_configs():
22 restart_postfix()
23
24
25+def ensure_mailname():
26+ """Ensure that /etc/mailname exists."""
27+ if not os.path.exists("/etc/mailname"):
28+ with open("/etc/mailname", "w") as mailname:
29+ mailname.write(socket.getfqdn() + "\n")
30+
31+
32 def write_auth_file():
33 """Render sasl_passwd file."""
34 auth_template_path = os.path.join(TEMPLATES, "sasl_passwd")
35diff --git a/src/tests/unit/test_postfix_relay.py b/src/tests/unit/test_postfix_relay.py
36index 74cf14c..46685f8 100644
37--- a/src/tests/unit/test_postfix_relay.py
38+++ b/src/tests/unit/test_postfix_relay.py
39@@ -1,6 +1,7 @@
40 """Unit tests for charm.postfix.postfix_relay."""
41
42 import os
43+import socket
44 import subprocess
45 from unittest import TestCase, mock
46
47@@ -9,6 +10,26 @@ from systemfixtures import FakeFilesystem, FakeProcesses
48 from charm.postfix import postfix_relay
49
50
51+class EnsureMailnameTests(TestCase):
52+ def test_already_exists(self):
53+ with FakeFilesystem() as fs:
54+ fs.add("/etc")
55+ os.mkdir("/etc")
56+ with open("/etc/mailname", "w") as mailname:
57+ mailname.write("unit.example.com\n")
58+ postfix_relay.ensure_mailname()
59+ with open("/etc/mailname") as mailname:
60+ self.assertEqual("unit.example.com\n", mailname.read())
61+
62+ def test_creates(self):
63+ with FakeFilesystem() as fs:
64+ fs.add("/etc")
65+ os.mkdir("/etc")
66+ postfix_relay.ensure_mailname()
67+ with open("/etc/mailname") as mailname:
68+ self.assertEqual(socket.getfqdn() + "\n", mailname.read())
69+
70+
71 class WriteRelayhostMapTests(TestCase):
72 @mock.patch("charm.postfix.postfix_relay.config")
73 def test_single_sender(self, mock_config):

Subscribers

People subscribed via source and target branches

to all changes: