2015-12-17 09:59:52 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-09-09 06:33:54 +00:00
|
|
|
import itertools as it
|
2016-03-31 00:48:31 +00:00
|
|
|
import os
|
2015-05-08 09:10:17 +00:00
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
|
2015-12-17 09:59:52 +00:00
|
|
|
import pytest
|
2015-05-08 09:10:17 +00:00
|
|
|
|
2015-09-18 15:58:42 +00:00
|
|
|
ros_test = 'ros-test'
|
2016-05-23 06:11:26 +00:00
|
|
|
arch = os.environ.get('ARCH', 'amd64')
|
2016-03-31 00:48:31 +00:00
|
|
|
|
|
|
|
suffix = ''
|
|
|
|
if arch != 'amd64':
|
|
|
|
suffix = '_' + arch
|
2015-09-18 15:58:42 +00:00
|
|
|
|
|
|
|
|
2015-09-01 14:11:02 +00:00
|
|
|
def iter_lines(s):
|
2015-09-09 06:33:54 +00:00
|
|
|
return it.imap(str.rstrip, iter(s.readline, ''))
|
|
|
|
|
|
|
|
|
|
|
|
def strip_comment(prefix):
|
|
|
|
return lambda s: s.partition(prefix)[0].strip()
|
|
|
|
|
|
|
|
|
|
|
|
def non_empty(s):
|
|
|
|
return s != ''
|
|
|
|
|
|
|
|
|
|
|
|
def parse_value(var):
|
|
|
|
def get_value(s):
|
|
|
|
(k, _, v) = s.partition('=')
|
|
|
|
(k, v) = (k.strip(), v.strip())
|
|
|
|
if k == var:
|
|
|
|
return v
|
|
|
|
return ''
|
2015-12-17 09:59:52 +00:00
|
|
|
|
2015-09-09 06:33:54 +00:00
|
|
|
return get_value
|
|
|
|
|
|
|
|
|
|
|
|
def with_effect(p):
|
|
|
|
def effect(s):
|
|
|
|
p(s)
|
|
|
|
return s
|
2015-12-17 09:59:52 +00:00
|
|
|
|
2015-09-09 06:33:54 +00:00
|
|
|
return effect
|
2015-09-01 14:11:02 +00:00
|
|
|
|
2015-09-03 15:07:57 +00:00
|
|
|
|
2015-09-09 13:53:04 +00:00
|
|
|
def rancheros_version(build_conf):
|
|
|
|
with open(build_conf) as f:
|
|
|
|
for v in it.ifilter(non_empty,
|
|
|
|
it.imap(parse_value('VERSION'),
|
|
|
|
it.ifilter(non_empty,
|
|
|
|
it.imap(strip_comment('#'), iter_lines(f))))):
|
|
|
|
return v
|
|
|
|
raise RuntimeError("Could not parse RancherOS version")
|
|
|
|
|
|
|
|
|
2015-05-08 09:10:17 +00:00
|
|
|
def run_qemu(request, run_args=[]):
|
|
|
|
print('\nStarting QEMU')
|
2015-10-15 11:56:28 +00:00
|
|
|
p = subprocess.Popen(['./scripts/run', '--qemu', '--no-rebuild', '--no-rm-usr', '--fresh'] + run_args,
|
2015-05-08 09:10:17 +00:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
|
2015-12-17 09:59:52 +00:00
|
|
|
assert p.returncode is None
|
2015-05-08 09:10:17 +00:00
|
|
|
|
|
|
|
def fin():
|
|
|
|
print('\nTerminating QEMU')
|
|
|
|
p.terminate()
|
2015-12-17 09:59:52 +00:00
|
|
|
p.wait()
|
2015-05-08 09:10:17 +00:00
|
|
|
|
|
|
|
request.addfinalizer(fin)
|
|
|
|
return p
|
|
|
|
|
|
|
|
|
2015-12-17 09:59:52 +00:00
|
|
|
def has_substr(token):
|
|
|
|
return lambda s: str.find(s, token) > -1
|
|
|
|
|
|
|
|
|
|
|
|
def flush_out(stdout, substr='RancherOS '):
|
|
|
|
for _ in it.ifilter(has_substr(substr),
|
|
|
|
it.imap(with_effect(print), iter_lines(stdout))):
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2015-05-08 09:10:17 +00:00
|
|
|
@pytest.mark.timeout(10)
|
2015-12-17 13:30:16 +00:00
|
|
|
def wait_for_ssh(qemu, ssh_command=['./scripts/ssh', '--qemu'], command=['docker version >/dev/null 2>&1']):
|
2015-12-02 12:24:35 +00:00
|
|
|
i = 0
|
2015-12-17 09:59:52 +00:00
|
|
|
assert qemu.returncode is None
|
2015-12-02 12:24:35 +00:00
|
|
|
print('\nWaiting for ssh and docker... ' + str(i))
|
2015-12-17 13:30:16 +00:00
|
|
|
while subprocess.call(ssh_command + command) != 0:
|
2015-12-02 12:24:35 +00:00
|
|
|
i += 1
|
|
|
|
print('\nWaiting for ssh and docker... ' + str(i))
|
2015-05-08 09:10:17 +00:00
|
|
|
time.sleep(1)
|
2016-04-01 18:00:42 +00:00
|
|
|
if i > 150:
|
|
|
|
raise AssertionError('Failed to connect to SSH')
|
2015-12-17 09:59:52 +00:00
|
|
|
assert qemu.returncode is None
|
2015-12-20 05:34:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SSH:
|
|
|
|
def __init__(self, qemu, ssh_command=['./scripts/ssh', '--qemu']):
|
|
|
|
self._qemu = qemu
|
|
|
|
self._ssh_command = ssh_command
|
|
|
|
self._waited = False
|
|
|
|
|
2016-04-01 18:00:42 +00:00
|
|
|
def wait(self):
|
2015-12-20 05:34:07 +00:00
|
|
|
if not self._waited:
|
|
|
|
wait_for_ssh(self._qemu, ssh_command=self._ssh_command)
|
|
|
|
self._waited = True
|
|
|
|
|
2016-04-01 18:00:42 +00:00
|
|
|
def check_call(self, *args, **kw):
|
|
|
|
self.wait()
|
2015-12-20 05:34:07 +00:00
|
|
|
kw['stderr'] = subprocess.STDOUT
|
|
|
|
kw['universal_newlines'] = True
|
|
|
|
return subprocess.check_call(self._ssh_command + list(args), **kw)
|
2016-04-01 18:00:42 +00:00
|
|
|
|
|
|
|
def check_output(self, *args, **kw):
|
|
|
|
self.wait()
|
|
|
|
kw['stderr'] = subprocess.STDOUT
|
|
|
|
kw['universal_newlines'] = True
|
|
|
|
return subprocess.check_output(self._ssh_command + list(args), **kw)
|