3 import sys, getopt, os, smtplib, commands, time
6 temp = "/usr/tmp/torturestest-%d" % os.getpid()
8 def __init__(self, line):
9 "Initialize site data from the external representation."
10 (self.host, self.mailname, self.userid, self.password, \
11 self.proto, self.options, self.capabilities, self.version, self.comment) = \
12 line.strip().split(":")
14 self.mailname = self.userid
20 "Return a tuple consisting of alll this site's attributes."
21 return (self.host, self.mailname, self.userid, self.password, \
22 self.proto, self.options, self.capabilities, \
23 self.version, self.comment)
26 "Return the external representation of this site's data."
27 return ":".join(self.allattrs())
29 def prettyprint(self):
30 "Prettyprint a site entry in human-readable form."
37 "Capabilities: %s\n" \
43 "Print a .fetchmailrc entry corresponding to a site entry."
44 return "poll %s-%s via %s with proto %s %s\n" \
45 " user %s there with password '%s' is esr here\n\n" \
46 % (self.host,self.proto,self.host,self.proto,self.options,self.userid,self.password)
49 "Print an HTML server-type table entry."
50 return "<tr><td>%s: %s</td><td>%s</td>\n" \
51 % (self.proto, self.comment, self.capabilities)
55 rep = "%s %s at %s" % (self.proto, self.version, self.host)
57 rep += " (" + self.capabilities + ")"
59 rep += " using " + self.options
62 def testmail(self, n=None):
63 "Send test mail to the site."
64 server = smtplib.SMTP("localhost")
65 fromaddr = "esr@thyrsus.com"
66 if self.mailname.find("@") > -1:
67 toaddr = self.mailname
69 toaddr = "%s@%s" % (self.mailname, self.host)
70 msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, toaddr))
73 msg += "Test mail collected from %s.\n" % (self.id(),)
74 server.sendmail(fromaddr, toaddr, msg)
78 "Run a mail fetch on this site."
80 ofp = open(TestSite.temp, "w")
81 ofp.write('defaults mda "(echo; echo \'From torturetest\' `date`; cat) >>TEST.LOG"\n')
82 ofp.write(site.entryprint())
84 (self.status, self.output) = commands.getstatusoutput("fetchmail -d0 -v -f - <%s"%TestSite.temp)
86 os.remove(TestSite.temp)
89 "Did we have a test failure here?"
90 return os.WIFEXITED(self.status) and os.WEXITSTATUS(self.status) > 1
93 "Explain the status of the last test."
94 if not os.WIFEXITED(self.status):
95 return self.id() + ": abnormal termination\n"
96 elif os.WEXITSTATUS(self.status) > 1:
97 return self.id() + ": %d\n" % os.WEXITSTATUS(self.status) + self.output
99 return self.id() + ": succeeded\n"
101 if __name__ == "__main__":
102 # Start by reading in the sitelist
103 ifp = open("testsites")
108 line = ifp.readline()
111 elif line[0] in ("#", "\n"):
115 sitelist.append(TestSite(line))
117 print "Error on line %d" % linect
120 (options, arguments) = getopt.getopt(sys.argv[1:], "dfp:tigvs")
122 for (switch, value) in options:
124 # Prettprint the sitelist
125 map(lambda x: sys.stdout.write(x.prettyprint() + "%%\n"), sitelist)
128 # Dump the sitelist as a .fetchmailrc file
129 map(lambda x: sys.stdout.write(x.entryprint()), sitelist)
132 # Probe a single site
134 for site in sitelist:
135 if `site`.find(value) > -1:
136 selected.append(site)
140 # Dump the sitelist in HTML table form
141 map(lambda x: sys.stdout.write(x.tableprint()), sitelist)
144 # Dump the ids of the sitelist
145 map(lambda x: sys.stdout.write(x.id() + "\n"), sitelist)
149 for site in sitelist:
150 print "Sending test mail to " + site.id()
153 # Send test mail to each site
154 sys.stdout.write("Delaying to give the test mail time to land...")
156 sys.stdout.write("here we go:\n")
159 # Display the test output
162 # Dump version strings of all tested servers as a Python tuple
163 print "(" + ",\n".join(map(lambda x: repr(x.version), filter(lambda x: x.version, sitelist))) + ")"
166 # If no options, run the torture test
168 failures = successes = 0
169 os.system("fetchmail -q")
170 for site in sitelist:
171 print "Testing " + site.id()
180 # OK, summarize results
181 print "\n%d successes and %d failures out of %d tests" \
182 % (successes, failures, len(sitelist))
185 print "Bad status was returned on the following sites:"
186 for site in sitelist:
188 sys.stdout.write(site.explain() + "\n")
189 except KeyboardInterrupt: