test_runtime.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python.runtime}.
  5. """
  6. from __future__ import division, absolute_import
  7. import sys
  8. from twisted.python.reflect import namedModule
  9. from twisted.trial.util import suppress as SUPRESS
  10. from twisted.trial.unittest import SynchronousTestCase
  11. from twisted.python.runtime import Platform, shortPythonVersion
  12. class PythonVersionTests(SynchronousTestCase):
  13. """
  14. Tests the shortPythonVersion method.
  15. """
  16. def test_shortPythonVersion(self):
  17. """
  18. Verify if the Python version is returned correctly.
  19. """
  20. ver = shortPythonVersion().split('.')
  21. for i in range(3):
  22. self.assertEqual(int(ver[i]), sys.version_info[i])
  23. class PlatformTests(SynchronousTestCase):
  24. """
  25. Tests for the default L{Platform} initializer.
  26. """
  27. isWinNTDeprecationMessage = ('twisted.python.runtime.Platform.isWinNT was '
  28. 'deprecated in Twisted 13.0. Use Platform.isWindows instead.')
  29. def test_isKnown(self):
  30. """
  31. L{Platform.isKnown} returns a boolean indicating whether this is one of
  32. the L{runtime.knownPlatforms}.
  33. """
  34. platform = Platform()
  35. self.assertTrue(platform.isKnown())
  36. def test_isVistaConsistency(self):
  37. """
  38. Verify consistency of L{Platform.isVista}: it can only be C{True} if
  39. L{Platform.isWinNT} and L{Platform.isWindows} are C{True}.
  40. """
  41. platform = Platform()
  42. if platform.isVista():
  43. self.assertTrue(platform.isWinNT())
  44. self.assertTrue(platform.isWindows())
  45. self.assertFalse(platform.isMacOSX())
  46. def test_isMacOSXConsistency(self):
  47. """
  48. L{Platform.isMacOSX} can only return C{True} if L{Platform.getType}
  49. returns C{'posix'}.
  50. """
  51. platform = Platform()
  52. if platform.isMacOSX():
  53. self.assertEqual(platform.getType(), 'posix')
  54. def test_isLinuxConsistency(self):
  55. """
  56. L{Platform.isLinux} can only return C{True} if L{Platform.getType}
  57. returns C{'posix'} and L{sys.platform} starts with C{"linux"}.
  58. """
  59. platform = Platform()
  60. if platform.isLinux():
  61. self.assertTrue(sys.platform.startswith("linux"))
  62. def test_isWinNT(self):
  63. """
  64. L{Platform.isWinNT} can return only C{False} or C{True} and can not
  65. return C{True} if L{Platform.getType} is not C{"win32"}.
  66. """
  67. platform = Platform()
  68. isWinNT = platform.isWinNT()
  69. self.assertIn(isWinNT, (False, True))
  70. if platform.getType() != "win32":
  71. self.assertFalse(isWinNT)
  72. test_isWinNT.suppress = [SUPRESS(category=DeprecationWarning,
  73. message=isWinNTDeprecationMessage)]
  74. def test_isWinNTDeprecated(self):
  75. """
  76. L{Platform.isWinNT} is deprecated in favor of L{platform.isWindows}.
  77. """
  78. platform = Platform()
  79. platform.isWinNT()
  80. warnings = self.flushWarnings([self.test_isWinNTDeprecated])
  81. self.assertEqual(len(warnings), 1)
  82. self.assertEqual(
  83. warnings[0]['message'], self.isWinNTDeprecationMessage)
  84. def test_supportsThreads(self):
  85. """
  86. L{Platform.supportsThreads} returns C{True} if threads can be created in
  87. this runtime, C{False} otherwise.
  88. """
  89. # It's difficult to test both cases of this without faking the threading
  90. # module. Perhaps an adequate test is to just test the behavior with
  91. # the current runtime, whatever that happens to be.
  92. try:
  93. namedModule('threading')
  94. except ImportError:
  95. self.assertFalse(Platform().supportsThreads())
  96. else:
  97. self.assertTrue(Platform().supportsThreads())
  98. class ForeignPlatformTests(SynchronousTestCase):
  99. """
  100. Tests for L{Platform} based overridden initializer values.
  101. """
  102. def test_getType(self):
  103. """
  104. If an operating system name is supplied to L{Platform}'s initializer,
  105. L{Platform.getType} returns the platform type which corresponds to that
  106. name.
  107. """
  108. self.assertEqual(Platform('nt').getType(), 'win32')
  109. self.assertEqual(Platform('ce').getType(), 'win32')
  110. self.assertEqual(Platform('posix').getType(), 'posix')
  111. self.assertEqual(Platform('java').getType(), 'java')
  112. def test_isMacOSX(self):
  113. """
  114. If a system platform name is supplied to L{Platform}'s initializer, it
  115. is used to determine the result of L{Platform.isMacOSX}, which returns
  116. C{True} for C{"darwin"}, C{False} otherwise.
  117. """
  118. self.assertTrue(Platform(None, 'darwin').isMacOSX())
  119. self.assertFalse(Platform(None, 'linux2').isMacOSX())
  120. self.assertFalse(Platform(None, 'win32').isMacOSX())
  121. def test_isLinux(self):
  122. """
  123. If a system platform name is supplied to L{Platform}'s initializer, it
  124. is used to determine the result of L{Platform.isLinux}, which returns
  125. C{True} for values beginning with C{"linux"}, C{False} otherwise.
  126. """
  127. self.assertFalse(Platform(None, 'darwin').isLinux())
  128. self.assertTrue(Platform(None, 'linux').isLinux())
  129. self.assertTrue(Platform(None, 'linux2').isLinux())
  130. self.assertTrue(Platform(None, 'linux3').isLinux())
  131. self.assertFalse(Platform(None, 'win32').isLinux())
  132. class DockerPlatformTests(SynchronousTestCase):
  133. """
  134. Tests for L{twisted.python.runtime.Platform.isDocker}.
  135. """
  136. def test_noChecksOnLinux(self):
  137. """
  138. If the platform is not Linux, C{isDocker()} always returns L{False}.
  139. """
  140. platform = Platform(None, 'win32')
  141. self.assertFalse(platform.isDocker())
  142. def test_noCGroups(self):
  143. """
  144. If the platform is Linux, and the cgroups file in C{/proc} does not
  145. exist, C{isDocker()} returns L{False}
  146. """
  147. platform = Platform(None, 'linux')
  148. self.assertFalse(platform.isDocker(_initCGroupLocation="fakepath"))
  149. def test_cgroupsSuggestsDocker(self):
  150. """
  151. If the platform is Linux, and the cgroups file (faked out here) exists,
  152. and one of the paths starts with C{/docker/}, C{isDocker()} returns
  153. C{True}.
  154. """
  155. cgroupsFile = self.mktemp()
  156. with open(cgroupsFile, 'wb') as f:
  157. # real cgroups file from inside a Debian 7 docker container
  158. f.write(b"""10:debug:/
  159. 9:net_prio:/
  160. 8:perf_event:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
  161. 7:net_cls:/
  162. 6:freezer:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
  163. 5:devices:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
  164. 4:blkio:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
  165. 3:cpuacct:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
  166. 2:cpu:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f
  167. 1:cpuset:/docker/104155a6453cb67590027e397dc90fc25a06a7508403c797bc89ea43adf8d35f""")
  168. platform = Platform(None, 'linux')
  169. self.assertTrue(platform.isDocker(_initCGroupLocation=cgroupsFile))
  170. def test_cgroupsSuggestsRealSystem(self):
  171. """
  172. If the platform is Linux, and the cgroups file (faked out here) exists,
  173. and none of the paths starts with C{/docker/}, C{isDocker()} returns
  174. C{False}.
  175. """
  176. cgroupsFile = self.mktemp()
  177. with open(cgroupsFile, 'wb') as f:
  178. # real cgroups file from a Fedora 17 system
  179. f.write(b"""9:perf_event:/
  180. 8:blkio:/
  181. 7:net_cls:/
  182. 6:freezer:/
  183. 5:devices:/
  184. 4:memory:/
  185. 3:cpuacct,cpu:/
  186. 2:cpuset:/
  187. 1:name=systemd:/system""")
  188. platform = Platform(None, 'linux')
  189. self.assertFalse(platform.isDocker(_initCGroupLocation=cgroupsFile))