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

« back to all changes in this revision

Viewing changes to test/test_mail.py

  • Committer: Michael Vogt
  • Date: 2010-08-02 09:04:20 UTC
  • mfrom: (136 ubuntu)
  • mto: This revision was merged to the branch mainline in revision 139.
  • Revision ID: michael.vogt@ubuntu.com-20100802090420-b1vaha9f359q9vto
mergedĀ fromĀ ubuntu

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import apt
 
4
import apt_pkg
 
5
import os
 
6
import logging
 
7
import unittest
 
8
import sys
 
9
 
 
10
from StringIO import StringIO
 
11
 
 
12
import unattended_upgrade
 
13
from unattended_upgrade import send_summary_mail
 
14
 
 
15
class TestSendSummaryMail(unittest.TestCase):
 
16
 
 
17
    def setUp(self):
 
18
        # monkey patch to make it testable
 
19
        unattended_upgrade.REBOOT_REQUIRED_FILE = "./reboot-required"
 
20
        # mock-mail binary that creates a mail.txt file
 
21
        unattended_upgrade.MAIL_BINARY = "./mock-mail"
 
22
        # setup mail
 
23
        apt_pkg.config.set("Unattended-Upgrade::Mail", "root")
 
24
 
 
25
    def tearDown(self):
 
26
        for f in ["mail.txt", "reboot-required", "apt-term.log"]:
 
27
            if os.path.exists(f):
 
28
                os.unlink(f)
 
29
 
 
30
    def _return_mock_data(self):
 
31
        """ return input tuple for send_summary_mail """
 
32
        pkgs = "\n".join(["2vcard"])
 
33
        res = True
 
34
        pkgs_kept_back = []
 
35
        mem_log = StringIO("mem_log text")
 
36
        logfile_dpkg = "./apt-term.log"
 
37
        open("./apt-term.log", "w").write("logfile_dpkg text")
 
38
        return (pkgs, res, pkgs_kept_back, mem_log, logfile_dpkg)
 
39
 
 
40
    def _verify_common_mail_content(self, mail_txt):
 
41
        self.assertTrue("logfile_dpkg text" in mail_txt)
 
42
        self.assertTrue("mem_log text" in mail_txt)
 
43
        self.assertTrue("Packages that are upgraded:\n 2vcard" in mail_txt)
 
44
 
 
45
    def testSummaryMailReboot(self):
 
46
        open("./reboot-required","w").write("")
 
47
        send_summary_mail(*self._return_mock_data())
 
48
        os.unlink("./reboot-required")
 
49
        mail_txt = open("mail.txt").read()
 
50
        self.assertTrue("[reboot required]" in mail_txt)
 
51
        self._verify_common_mail_content(mail_txt)
 
52
        
 
53
    def testSummaryMailNoReboot(self):
 
54
        send_summary_mail(*self._return_mock_data())
 
55
        mail_txt = open("mail.txt").read()
 
56
        self.assertFalse("[reboot required]" in mail_txt)
 
57
        self._verify_common_mail_content(mail_txt)
 
58
 
 
59
if __name__ == "__main__":
 
60
    logging.basicConfig(level=logging.DEBUG)
 
61
    unittest.main()
 
62