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 toaddr = "%s@%s" % (self.mailname, self.host)
67 msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, toaddr))
70 msg += "Test mail collected from %s.\n" % (self.id(),)
71 server.sendmail(fromaddr, toaddr, msg)
75 "Run a mail fetch on this site."
77 ofp = open(TestSite.temp, "w")
78 ofp.write('defaults mda "(echo; echo \'From torturetest\' `date`; cat) >>TEST.LOG"\n')
79 ofp.write(site.entryprint())
81 (self.status, self.output) = commands.getstatusoutput("fetchmail -d0 -v -f - <%s"%TestSite.temp)
83 os.remove(TestSite.temp)
86 "Did we have a test failure here?"
87 return os.WIFEXITED(self.status) and os.WEXITSTATUS(self.status) > 1
90 "Explain the status of the last test."
91 if not os.WIFEXITED(self.status):
92 return self.id() + ": abnormal termination\n"
93 elif os.WEXITSTATUS(self.status) > 1:
94 return self.id() + ": %d\n" % os.WEXITSTATUS(self.status) + self.output
96 return self.id() + ": succeeded\n"
98 if __name__ == "__main__":
99 # Start by reading in the sitelist
100 ifp = open("testsites")
105 line = ifp.readline()
108 elif line[0] in ("#", "\n"):
112 sitelist.append(TestSite(line))
114 print "Error on line %d" % linect
117 (options, arguments) = getopt.getopt(sys.argv[1:], "dfp:tigvs")
119 for (switch, value) in options:
121 # Prettprint the sitelist
122 map(lambda x: sys.stdout.write(x.prettyprint() + "%%\n"), sitelist)
125 # Dump the sitelist as a .fetchmailrc file
126 map(lambda x: sys.stdout.write(x.entryprint()), sitelist)
129 # Probe a single site
131 for site in sitelist:
132 if `site`.find(value) > -1:
133 selected.append(site)
137 # Dump the sitelist in HTML table form
138 map(lambda x: sys.stdout.write(x.tableprint()), sitelist)
141 # Dump the ids of the sitelist
142 map(lambda x: sys.stdout.write(x.id() + "\n"), sitelist)
146 for site in sitelist:
147 print "Sending test mail to " + site.id()
150 # Send test mail to each site
151 sys.stdout.write("Delaying to give the test mail time to land...")
153 sys.stdout.write("here we go:\n")
156 # Display the test output
159 # Dump version strings of all tested servers as a Python tuple
160 print "(" + ",\n".join(map(lambda x: repr(x.version), filter(lambda x: x.version, sitelist))) + ")"
163 # If no options, run the torture test
165 failures = successes = 0
166 os.system("fetchmail -q")
167 for site in sitelist:
168 print "Testing " + site.id()
177 # OK, summarize results
178 print "\n%d successes and %d failures out of %d tests" \
179 % (successes, failures, len(sitelist))
182 print "Bad status was returned on the following sites:"
183 for site in sitelist:
185 sys.stdout.write(site.explain() + "\n")
186 except KeyboardInterrupt: