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
diff --git a/src/lib/charm/postfix/postfix_relay.py b/src/lib/charm/postfix/postfix_relay.py
index 2ca0ac6..ca916a2 100644
--- a/src/lib/charm/postfix/postfix_relay.py
+++ b/src/lib/charm/postfix/postfix_relay.py
@@ -13,6 +13,7 @@
13# See the License for the specific language governing permissions and13# See the License for the specific language governing permissions and
14# limitations under the License14# limitations under the License
15import os15import os
16import socket
16import subprocess17import subprocess
17from base64 import b64decode18from base64 import b64decode
1819
@@ -36,6 +37,7 @@ def write_configs():
36 cfg_template = Template(t.read())37 cfg_template = Template(t.read())
37 with open(MAIN_CFG, "w+") as f:38 with open(MAIN_CFG, "w+") as f:
38 f.write(cfg_template.render(PostfixContext()()))39 f.write(cfg_template.render(PostfixContext()()))
40 ensure_mailname()
39 if config("smtp_auth_username") and config("smtp_auth_password"):41 if config("smtp_auth_username") and config("smtp_auth_password"):
40 write_auth_file()42 write_auth_file()
41 if config("domain_rewrite_map"):43 if config("domain_rewrite_map"):
@@ -47,6 +49,13 @@ def write_configs():
47 restart_postfix()49 restart_postfix()
4850
4951
52def ensure_mailname():
53 """Ensure that /etc/mailname exists."""
54 if not os.path.exists("/etc/mailname"):
55 with open("/etc/mailname", "w") as mailname:
56 mailname.write(socket.getfqdn() + "\n")
57
58
50def write_auth_file():59def write_auth_file():
51 """Render sasl_passwd file."""60 """Render sasl_passwd file."""
52 auth_template_path = os.path.join(TEMPLATES, "sasl_passwd")61 auth_template_path = os.path.join(TEMPLATES, "sasl_passwd")
diff --git a/src/tests/unit/test_postfix_relay.py b/src/tests/unit/test_postfix_relay.py
index 74cf14c..46685f8 100644
--- a/src/tests/unit/test_postfix_relay.py
+++ b/src/tests/unit/test_postfix_relay.py
@@ -1,6 +1,7 @@
1"""Unit tests for charm.postfix.postfix_relay."""1"""Unit tests for charm.postfix.postfix_relay."""
22
3import os3import os
4import socket
4import subprocess5import subprocess
5from unittest import TestCase, mock6from unittest import TestCase, mock
67
@@ -9,6 +10,26 @@ from systemfixtures import FakeFilesystem, FakeProcesses
9from charm.postfix import postfix_relay10from charm.postfix import postfix_relay
1011
1112
13class EnsureMailnameTests(TestCase):
14 def test_already_exists(self):
15 with FakeFilesystem() as fs:
16 fs.add("/etc")
17 os.mkdir("/etc")
18 with open("/etc/mailname", "w") as mailname:
19 mailname.write("unit.example.com\n")
20 postfix_relay.ensure_mailname()
21 with open("/etc/mailname") as mailname:
22 self.assertEqual("unit.example.com\n", mailname.read())
23
24 def test_creates(self):
25 with FakeFilesystem() as fs:
26 fs.add("/etc")
27 os.mkdir("/etc")
28 postfix_relay.ensure_mailname()
29 with open("/etc/mailname") as mailname:
30 self.assertEqual(socket.getfqdn() + "\n", mailname.read())
31
32
12class WriteRelayhostMapTests(TestCase):33class WriteRelayhostMapTests(TestCase):
13 @mock.patch("charm.postfix.postfix_relay.config")34 @mock.patch("charm.postfix.postfix_relay.config")
14 def test_single_sender(self, mock_config):35 def test_single_sender(self, mock_config):

Subscribers

People subscribed via source and target branches

to all changes: