util.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from __future__ import print_function
  2. import itertools as it
  3. import os
  4. import subprocess
  5. import time
  6. import pytest
  7. ros_test = 'ros-test'
  8. arch = os.environ.get('ARCH', 'amd64')
  9. suffix = ''
  10. if arch != 'amd64':
  11. suffix = '_' + arch
  12. def iter_lines(s):
  13. return it.imap(str.rstrip, iter(s.readline, ''))
  14. def strip_comment(prefix):
  15. return lambda s: s.partition(prefix)[0].strip()
  16. def non_empty(s):
  17. return s != ''
  18. def parse_value(var):
  19. def get_value(s):
  20. (k, _, v) = s.partition('=')
  21. (k, v) = (k.strip(), v.strip())
  22. if k == var:
  23. return v
  24. return ''
  25. return get_value
  26. def with_effect(p):
  27. def effect(s):
  28. p(s)
  29. return s
  30. return effect
  31. def rancheros_version(build_conf):
  32. with open(build_conf) as f:
  33. for v in it.ifilter(non_empty,
  34. it.imap(parse_value('VERSION'),
  35. it.ifilter(non_empty,
  36. it.imap(strip_comment('#'), iter_lines(f))))):
  37. return v
  38. raise RuntimeError("Could not parse RancherOS version")
  39. def run_qemu(request, run_args=[]):
  40. print('\nStarting QEMU')
  41. p = subprocess.Popen(['./scripts/run', '--qemu', '--no-rebuild', '--no-rm-usr', '--fresh'] + run_args,
  42. stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
  43. assert p.returncode is None
  44. def fin():
  45. print('\nTerminating QEMU')
  46. p.terminate()
  47. p.wait()
  48. request.addfinalizer(fin)
  49. return p
  50. def has_substr(token):
  51. return lambda s: str.find(s, token) > -1
  52. def flush_out(stdout, substr='RancherOS '):
  53. for _ in it.ifilter(has_substr(substr),
  54. it.imap(with_effect(print), iter_lines(stdout))):
  55. return
  56. @pytest.mark.timeout(10)
  57. def wait_for_ssh(qemu, ssh_command=['./scripts/ssh', '--qemu'], command=['docker version >/dev/null 2>&1']):
  58. i = 0
  59. assert qemu.returncode is None
  60. print('\nWaiting for ssh and docker... ' + str(i))
  61. while subprocess.call(ssh_command + command) != 0:
  62. i += 1
  63. print('\nWaiting for ssh and docker... ' + str(i))
  64. time.sleep(1)
  65. if i > 150:
  66. raise AssertionError('Failed to connect to SSH')
  67. assert qemu.returncode is None
  68. class SSH:
  69. def __init__(self, qemu, ssh_command=['./scripts/ssh', '--qemu']):
  70. self._qemu = qemu
  71. self._ssh_command = ssh_command
  72. self._waited = False
  73. def wait(self):
  74. if not self._waited:
  75. wait_for_ssh(self._qemu, ssh_command=self._ssh_command)
  76. self._waited = True
  77. def check_call(self, *args, **kw):
  78. self.wait()
  79. kw['stderr'] = subprocess.STDOUT
  80. kw['universal_newlines'] = True
  81. return subprocess.check_call(self._ssh_command + list(args), **kw)
  82. def check_output(self, *args, **kw):
  83. self.wait()
  84. kw['stderr'] = subprocess.STDOUT
  85. kw['universal_newlines'] = True
  86. return subprocess.check_output(self._ssh_command + list(args), **kw)