~ubuntu-core-dev/unattended-upgrades/ubuntu

« back to all changes in this revision

Viewing changes to test/test_mail.py

  • Committer: Barry Warsaw
  • Date: 2012-06-19 20:19:36 UTC
  • mfrom: (277.1.5 unattended-upgrades)
  • mto: This revision was merged to the branch mainline in revision 288.
  • Revision ID: barry@python.org-20120619201936-vrpi8yxbbxzdr6qg
Merge Thomas's branch: Port to Python 3
- Additional fixes by Barry

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
 
1
#!/usr/bin/python3
2
2
 
3
3
import apt
4
4
import apt_pkg
7
7
import unittest
8
8
import sys
9
9
 
10
 
from StringIO import StringIO
 
10
try:
 
11
    from StringIO import StringIO    # Python 2
 
12
except ImportError:
 
13
    from io import StringIO          # Python 3
11
14
 
12
15
import unattended_upgrade
13
16
import unattended_upgrade
36
39
        pkgs_kept_back = []
37
40
        mem_log = StringIO("mem_log text")
38
41
        logfile_dpkg = "./apt-term.log"
39
 
        open("./apt-term.log", "w").write("logfile_dpkg text")
 
42
        with open("./apt-term.log", "w") as fp:
 
43
            fp.write("logfile_dpkg text")
40
44
        return (pkgs, res, pkgs_kept_back, mem_log, logfile_dpkg)
41
45
 
42
46
    def _verify_common_mail_content(self, mail_txt):
45
49
        self.assertTrue("Packages that are upgraded:\n 2vcard" in mail_txt)
46
50
 
47
51
    def test_summary_mail_reboot(self):
48
 
        open("./reboot-required","w").write("")
 
52
        with open("./reboot-required","w") as fp:
 
53
            fp.write("")
49
54
        send_summary_mail(*self._return_mock_data())
50
55
        os.unlink("./reboot-required")
51
 
        mail_txt = open("mail.txt").read()
 
56
        with open("mail.txt") as fp:
 
57
            mail_txt = fp.read()
52
58
        self.assertTrue("[reboot required]" in mail_txt)
53
59
        self._verify_common_mail_content(mail_txt)
54
60
        
55
61
    def test_summary_mail_no_reboot(self):
56
62
        send_summary_mail(*self._return_mock_data())
57
 
        mail_txt = open("mail.txt").read()
 
63
        with open("mail.txt") as fp:
 
64
            mail_txt = fp.read()
58
65
        self.assertFalse("[reboot required]" in mail_txt)
59
66
        self._verify_common_mail_content(mail_txt)
60
67
    
63
70
        # for both success and failure
64
71
        apt_pkg.config.set("Unattended-Upgrade::MailOnlyOnError", "false")
65
72
        send_summary_mail(*self._return_mock_data(successful=True))
66
 
        self._verify_common_mail_content(open("mail.txt").read())
 
73
        with open("mail.txt") as fp:
 
74
            self._verify_common_mail_content(fp.read())
67
75
        os.remove("mail.txt")
68
76
        # now with a simulated failure
69
77
        send_summary_mail(*self._return_mock_data(successful=False))
70
 
        self._verify_common_mail_content(open("mail.txt").read())
 
78
        with open("mail.txt") as fp:
 
79
            self._verify_common_mail_content(fp.read())
71
80
        os.remove("mail.txt")
72
81
        # now test with "MailOnlyOnError"
73
82
        apt_pkg.config.set("Unattended-Upgrade::MailOnlyOnError", "true")
74
83
        send_summary_mail(*self._return_mock_data(successful=True))
75
84
        self.assertFalse(os.path.exists("mail.txt"))
76
85
        send_summary_mail(*self._return_mock_data(successful=False))
77
 
        mail_txt = open("mail.txt").read()
 
86
        with open("mail.txt") as fp:
 
87
            mail_txt = fp.read()
78
88
        self._verify_common_mail_content(mail_txt)
79
89
        self.assertTrue("Unattended upgrade returned: False" in mail_txt)
80
90
        self.assertTrue(os.path.exists("mail.txt"))