Debian Codegraph @

tests/trickstest.py

raw
1 """
2 Tests for the various utils.*tricks modules.
3 """
4
5 #c Copyright 2008-2025, the GAVO project <[email protected]>
6 #c
7 #c This program is free software, covered by the GNU GPL. See the
8 #c COPYING file in the source distribution.
9
10
11 import glob
12 import os
13 import time
14
15 from gavo.helpers import testhelpers
16
17 from gavo import base
18 from gavo import utils
19
20
21 class RemoteURLTest(testhelpers.VerboseTest):
22 """tests for urlopenRemote rejecting unwanted URLs.
23 """
24 def testNoFile(self):
25 self.assertRaises(IOError,
26 utils.urlopenRemote, "file:///etc/passwd")
27
28 def testHTTPConnect(self):
29 # this assumes nothing actually listens on 57388
30 self.assertRaisesWithMsg(IOError,
31 "Could not open URL http://localhost:57388: Connection refused",
32 utils.urlopenRemote, ("http://localhost:57388",))
33
34 def testMalformedURL(self):
35 self.assertRaisesWithMsg(IOError,
36 "Could not open URL /etc/passwd: unknown url type: '/etc/passwd'",
37 utils.urlopenRemote, ("/etc/passwd",))
38
39
40 class SafeReplacedTest(testhelpers.VerboseTest):
41 testName = os.path.join(base.getConfig("tempDir"), "someFile")
42
43 def tearDown(self):
44 try:
45 os.unlink(self.testName)
46 except os.error:
47 pass
48
49 def testDelayedOverwrite(self):
50 with open(self.testName, "w") as f:
51 f.write("\n".join(["line%03d"%i for i in range(50)]))
52 with utils.safeReplaced(self.testName, binary=False) as destF:
53 with open(self.testName, "r") as f:
54 for ln in f:
55 destF.write("proc"+ln)
56 with open(self.testName) as f:
57 self.assertEqual(f.read().split("\n")[48], "procline048")
58
59 def testNoCrapOnError(self):
60 with open(self.testName, "wb") as f:
61 f.write(utils.bytify("\n".join(["line%03d"%i for i in range(50)])))
62 try:
63 with utils.safeReplaced(self.testName) as destF:
64 with open(self.testName, "rb") as f:
65 for ln in f:
66 destF.write(b"proc"+ln)
67 raise ValueError()
68 except ValueError:
69 # it's expected, I'm raising it myself
70 pass
71
72 with open(self.testName, "rb") as f:
73 self.assertEqual(f.read().split(b"\n")[48], b"line048")
74 self.assertFalse(
75 glob.glob(os.path.join(base.getConfig("tempDir"), "*.temp")),
76 "There's still a *.temp file left in tempDir; this could be"
77 " because of earlier failed tests. Just remove all the stuff"
78 " in %s"%(base.getConfig("tempDir")))
79
80
81 class RateLimiterTest(testhelpers.VerboseTest):
82 def testBasic(self):
83 limiter = utils.RateLimiter(0.01)
84 self.assertEqual(limiter.inDeadtime("test1"), False)
85 self.assertEqual(limiter.inDeadtime("test2"), False)
86 self.assertEqual(limiter.inDeadtime("test1"), True)
87 self.assertEqual(limiter.inDeadtime("test2"), True)
88 time.sleep(0.01)
89 self.assertEqual(limiter.inDeadtime("test1"), False)
90 self.assertEqual(limiter.inDeadtime("test2"), False)
91 self.assertEqual(limiter.inDeadtime("test1"), True)
92 self.assertEqual(limiter.inDeadtime("test2"), True)
93
94
95 if __name__=="__main__":
96 testhelpers.main(SafeReplacedTest)