PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/testing/test_xspec.py

https://bitbucket.org/hpk42/execnet/
Python | 244 lines | 221 code | 23 blank | 0 comment | 2 complexity | 8042117cf76a8803b06fc7d758bd5d6e MD5 | raw file
  1. import os
  2. import py
  3. import sys
  4. import pytest
  5. import execnet
  6. import subprocess
  7. from execnet.gateway_io import ssh_args, popen_args, vagrant_ssh_args
  8. XSpec = execnet.XSpec
  9. class TestXSpec:
  10. def test_norm_attributes(self):
  11. spec = XSpec("socket=192.168.102.2:8888//python=c:/this/python2.5"
  12. "//chdir=d:\hello")
  13. assert spec.socket == "192.168.102.2:8888"
  14. assert spec.python == "c:/this/python2.5"
  15. assert spec.chdir == "d:\hello"
  16. assert spec.nice is None
  17. assert not hasattr(spec, '_xyz')
  18. pytest.raises(AttributeError, "spec._hello")
  19. spec = XSpec("socket=192.168.102.2:8888//python=python2.5//nice=3")
  20. assert spec.socket == "192.168.102.2:8888"
  21. assert spec.python == "python2.5"
  22. assert spec.chdir is None
  23. assert spec.nice == "3"
  24. spec = XSpec("ssh=user@host"
  25. "//chdir=/hello/this//python=/usr/bin/python2.5")
  26. assert spec.ssh == "user@host"
  27. assert spec.python == "/usr/bin/python2.5"
  28. assert spec.chdir == "/hello/this"
  29. spec = XSpec("popen")
  30. assert spec.popen is True
  31. def test_ssh_options(self):
  32. spec = XSpec("ssh=-p 22100 user@host//python=python3")
  33. assert spec.ssh == "-p 22100 user@host"
  34. assert spec.python == "python3"
  35. spec = XSpec(
  36. "ssh=-i ~/.ssh/id_rsa-passwordless_login -p 22100 user@host"
  37. "//python=python3")
  38. assert spec.ssh == \
  39. "-i ~/.ssh/id_rsa-passwordless_login -p 22100 user@host"
  40. assert spec.python == "python3"
  41. def test_execmodel(self):
  42. spec = XSpec("execmodel=thread")
  43. assert spec.execmodel == "thread"
  44. spec = XSpec("execmodel=eventlet")
  45. assert spec.execmodel == "eventlet"
  46. def test_ssh_options_and_config(self):
  47. spec = XSpec("ssh=-p 22100 user@host//python=python3")
  48. spec.ssh_config = "/home/user/ssh_config"
  49. assert ssh_args(spec)[:6] == [
  50. "ssh", "-C", "-F", spec.ssh_config, "-p", "22100"]
  51. def test_vagrant_options(self):
  52. spec = XSpec("vagrant_ssh=default//python=python3")
  53. assert vagrant_ssh_args(spec)[:-1] == [
  54. 'vagrant', 'ssh', 'default', '--', '-C']
  55. def test_popen_with_sudo_python(self):
  56. spec = XSpec("popen//python=sudo python3")
  57. assert popen_args(spec) == [
  58. 'sudo', 'python3', '-u', '-c',
  59. 'import sys;exec(eval(sys.stdin.readline()))'
  60. ]
  61. def test_env(self):
  62. xspec = XSpec("popen//env:NAME=value1")
  63. assert xspec.env['NAME'] == "value1"
  64. def test__samefilesystem(self):
  65. assert XSpec("popen")._samefilesystem()
  66. assert XSpec("popen//python=123")._samefilesystem()
  67. assert not XSpec("popen//chdir=hello")._samefilesystem()
  68. def test__spec_spec(self):
  69. for x in ("popen", "popen//python=this"):
  70. assert XSpec(x)._spec == x
  71. def test_samekeyword_twice_raises(self):
  72. pytest.raises(ValueError, XSpec, 'popen//popen')
  73. pytest.raises(ValueError, XSpec, 'popen//popen=123')
  74. def test_unknown_keys_allowed(self):
  75. xspec = XSpec("hello=3")
  76. assert xspec.hello == '3'
  77. def test_repr_and_string(self):
  78. for x in ("popen", "popen//python=this"):
  79. assert repr(XSpec(x)).find("popen") != -1
  80. assert str(XSpec(x)) == x
  81. def test_hash_equality(self):
  82. assert XSpec("popen") == XSpec("popen")
  83. assert hash(XSpec("popen")) == hash(XSpec("popen"))
  84. assert XSpec("popen//python=123") != XSpec("popen")
  85. assert hash(XSpec("socket=hello:8080")) != hash(XSpec("popen"))
  86. class TestMakegateway:
  87. def test_no_type(self, makegateway):
  88. pytest.raises(ValueError, lambda: makegateway('hello'))
  89. def test_popen_default(self, makegateway):
  90. gw = makegateway("")
  91. assert gw.spec.popen
  92. assert gw.spec.python is None
  93. rinfo = gw._rinfo()
  94. # assert rinfo.executable == sys.executable
  95. assert rinfo.cwd == os.getcwd()
  96. assert rinfo.version_info == sys.version_info
  97. @pytest.mark.skipif("not hasattr(os, 'nice')")
  98. def test_popen_nice(self, makegateway):
  99. gw = makegateway("popen")
  100. def getnice(channel):
  101. import os
  102. if hasattr(os, 'nice'):
  103. channel.send(os.nice(0))
  104. else:
  105. channel.send(None)
  106. remotenice = gw.remote_exec(getnice).receive()
  107. gw.exit()
  108. if remotenice is not None:
  109. gw = makegateway("popen//nice=5")
  110. remotenice2 = gw.remote_exec(getnice).receive()
  111. assert remotenice2 == remotenice + 5
  112. def test_popen_env(self, makegateway):
  113. gw = makegateway("popen//env:NAME123=123")
  114. ch = gw.remote_exec("""
  115. import os
  116. channel.send(os.environ['NAME123'])
  117. """)
  118. value = ch.receive()
  119. assert value == "123"
  120. def test_popen_explicit(self, makegateway):
  121. gw = makegateway("popen//python=%s" % sys.executable)
  122. assert gw.spec.python == sys.executable
  123. rinfo = gw._rinfo()
  124. assert rinfo.executable == sys.executable
  125. assert rinfo.cwd == os.getcwd()
  126. assert rinfo.version_info == sys.version_info
  127. def test_popen_cpython25(self, makegateway):
  128. for trypath in ('python2.5', r'C:\Python25\python.exe'):
  129. cpython25 = py.path.local.sysfind(trypath)
  130. if cpython25 is not None:
  131. cpython25 = cpython25.realpath()
  132. break
  133. else:
  134. pytest.skip("cpython2.5 not found")
  135. gw = makegateway("popen//python=%s" % cpython25)
  136. rinfo = gw._rinfo()
  137. if sys.platform != "darwin": # it's confusing there
  138. assert rinfo.executable == cpython25
  139. assert rinfo.cwd == os.getcwd()
  140. assert rinfo.version_info[:2] == (2, 5)
  141. def test_popen_cpython26(self, makegateway):
  142. for trypath in ('python2.6', r'C:\Python26\python.exe'):
  143. cpython26 = py.path.local.sysfind(trypath)
  144. if cpython26 is not None:
  145. break
  146. else:
  147. py.test.skip("cpython2.6 not found")
  148. gw = makegateway("popen//python=%s" % cpython26)
  149. rinfo = gw._rinfo()
  150. # assert rinfo.executable == cpython26
  151. assert rinfo.cwd == os.getcwd()
  152. assert rinfo.version_info[:2] == (2, 6)
  153. def test_popen_chdir_absolute(self, testdir, makegateway):
  154. gw = makegateway("popen//chdir=%s" % testdir.tmpdir)
  155. rinfo = gw._rinfo()
  156. assert rinfo.cwd == str(testdir.tmpdir.realpath())
  157. def test_popen_chdir_newsub(self, testdir, makegateway):
  158. testdir.chdir()
  159. gw = makegateway("popen//chdir=hello")
  160. rinfo = gw._rinfo()
  161. expected = str(testdir.tmpdir.join("hello").realpath()).lower()
  162. assert rinfo.cwd.lower() == expected
  163. def test_ssh(self, specssh, makegateway):
  164. sshhost = specssh.ssh
  165. gw = makegateway("ssh=%s//id=ssh1" % sshhost)
  166. rinfo = gw._rinfo()
  167. assert gw.id == 'ssh1'
  168. gw2 = execnet.SshGateway(sshhost)
  169. rinfo2 = gw2._rinfo()
  170. assert rinfo.executable == rinfo2.executable
  171. assert rinfo.cwd == rinfo2.cwd
  172. assert rinfo.version_info == rinfo2.version_info
  173. def test_vagrant(self, makegateway, tmpdir, monkeypatch):
  174. vagrant = py.path.local.sysfind('vagrant')
  175. if vagrant is None:
  176. pytest.skip('Vagrant binary not in PATH')
  177. monkeypatch.chdir(tmpdir)
  178. subprocess.check_call("vagrant init hashicorp/precise32", shell=True)
  179. subprocess.check_call("vagrant up --provider virtualbox", shell=True)
  180. gw = makegateway("vagrant_ssh=default")
  181. rinfo = gw._rinfo()
  182. rinfo.cwd == '/home/vagrant'
  183. rinfo.executable == '/usr/bin/python'
  184. subprocess.check_call("vagrant halt", shell=True)
  185. def test_socket(self, specsocket, makegateway):
  186. gw = makegateway("socket=%s//id=sock1" % specsocket.socket)
  187. rinfo = gw._rinfo()
  188. assert rinfo.executable
  189. assert rinfo.cwd
  190. assert rinfo.version_info
  191. assert gw.id == "sock1"
  192. # we cannot instantiate a second gateway
  193. @pytest.mark.xfail(reason='we can\'t instantiate a second gateway')
  194. def test_socket_second(self, specsocket, makegateway):
  195. gw = makegateway("socket=%s//id=sock1" % specsocket.socket)
  196. gw2 = makegateway("socket=%s//id=sock1" % specsocket.socket)
  197. rinfo = gw._rinfo()
  198. rinfo2 = gw2._rinfo()
  199. assert rinfo.executable == rinfo2.executable
  200. assert rinfo.cwd == rinfo2.cwd
  201. assert rinfo.version_info == rinfo2.version_info
  202. def test_socket_installvia(self):
  203. group = execnet.Group()
  204. group.makegateway("popen//id=p1")
  205. gw = group.makegateway("socket//installvia=p1//id=s1")
  206. assert gw.id == "s1"
  207. assert gw.remote_status()
  208. group.terminate()