2019-01-25 18:49:19 +00:00
|
|
|
import os
|
2018-09-24 19:32:09 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
from termcolor import colored
|
|
|
|
|
2019-01-25 22:28:35 +00:00
|
|
|
from test.clients.client import DockerClient, Command, FileCopy
|
|
|
|
|
2018-09-24 19:32:09 +00:00
|
|
|
def remove_control_characters(s):
|
|
|
|
return "".join(ch for ch in unicode(s) if unicodedata.category(ch)[0]!="C")
|
|
|
|
|
2019-01-25 22:28:35 +00:00
|
|
|
|
|
|
|
# These tuples are the box&version and the client to use.
|
2018-09-24 19:32:09 +00:00
|
|
|
BOXES = [
|
2019-01-25 22:28:35 +00:00
|
|
|
("kleesc/coreos --box-version=1911.4.0", DockerClient()), # docker 18.06.1
|
|
|
|
("kleesc/coreos --box-version=1800.7.0", DockerClient()), # docker 18.03.1
|
|
|
|
("kleesc/coreos --box-version=1688.5.3", DockerClient()), # docker 17.12.1
|
|
|
|
("kleesc/coreos --box-version=1632.3.0", DockerClient()), # docker 17.09.1
|
|
|
|
("kleesc/coreos --box-version=1576.5.0", DockerClient()), # docker 17.09.0
|
|
|
|
("kleesc/coreos --box-version=1520.9.0", DockerClient()), # docker 1.12.6
|
|
|
|
("kleesc/coreos --box-version=1235.6.0", DockerClient()), # docker 1.12.3
|
|
|
|
("kleesc/coreos --box-version=1185.5.0", DockerClient()), # docker 1.11.2
|
|
|
|
|
|
|
|
("kleesc/coreos --box-version=1122.3.0", DockerClient()), # docker 1.10.3
|
|
|
|
("kleesc/coreos --box-version=899.17.0", DockerClient()), # docker 1.9.1
|
|
|
|
("kleesc/coreos --box-version=835.13.0", DockerClient()), # docker 1.8.3
|
|
|
|
("kleesc/coreos --box-version=766.5.0", DockerClient()), # docker 1.7.1
|
|
|
|
("kleesc/coreos --box-version=717.3.0", DockerClient()), # docker 1.6.2
|
|
|
|
("kleesc/coreos --box-version=647.2.0", DockerClient()), # docker 1.5.0
|
|
|
|
("kleesc/coreos --box-version=557.2.0", DockerClient()), # docker 1.4.1
|
|
|
|
("kleesc/coreos --box-version=522.6.0", DockerClient()), # docker 1.3.3
|
|
|
|
|
|
|
|
("yungsang/coreos --box-version=1.3.7", DockerClient()), # docker 1.3.2
|
|
|
|
("yungsang/coreos --box-version=1.2.9", DockerClient()), # docker 1.2.0
|
|
|
|
("yungsang/coreos --box-version=1.1.5", DockerClient()), # docker 1.1.2
|
|
|
|
("yungsang/coreos --box-version=1.0.0", DockerClient()), # docker 1.0.1
|
|
|
|
("yungsang/coreos --box-version=0.9.10", DockerClient()), # docker 1.0.0
|
|
|
|
("yungsang/coreos --box-version=0.9.6", DockerClient()), # docker 0.11.1
|
|
|
|
|
|
|
|
("yungsang/coreos --box-version=0.9.1", DockerClient(True)), # docker 0.10.0
|
|
|
|
("yungsang/coreos --box-version=0.3.1", DockerClient(True)),
|
2018-09-24 19:32:09 +00:00
|
|
|
]
|
|
|
|
|
2019-01-25 18:49:19 +00:00
|
|
|
def _check_vagrant():
|
|
|
|
vagrant_command = 'vagrant'
|
2019-01-25 22:28:35 +00:00
|
|
|
vagrant = any(os.access(os.path.join(path, vagrant_command), os.X_OK)
|
|
|
|
for path in os.environ.get('PATH').split(':'))
|
2019-01-25 18:49:19 +00:00
|
|
|
vagrant_plugins = subprocess.check_output([vagrant_command, 'plugin', 'list'])
|
|
|
|
return (vagrant, 'vagrant-scp' in vagrant_plugins)
|
|
|
|
|
|
|
|
|
2018-09-24 19:32:09 +00:00
|
|
|
class CommandFailedException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SpinOutputter(Thread):
|
|
|
|
def __init__(self, initial_message):
|
|
|
|
super(SpinOutputter, self).__init__()
|
|
|
|
self.previous_line = ''
|
|
|
|
self.next_line = initial_message
|
|
|
|
self.running = True
|
|
|
|
self.daemon = True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def spinning_cursor():
|
|
|
|
while 1:
|
|
|
|
for cursor in '|/-\\':
|
|
|
|
yield cursor
|
|
|
|
|
|
|
|
def set_next(self, text):
|
|
|
|
first_line = text.split('\n')[0].strip()
|
|
|
|
first_line = remove_control_characters(first_line)
|
|
|
|
self.next_line = first_line[:80]
|
|
|
|
|
|
|
|
def _clear_line(self):
|
|
|
|
sys.stdout.write('\r')
|
|
|
|
sys.stdout.write(' ' * (len(self.previous_line) + 2))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
sys.stdout.write('\r')
|
|
|
|
sys.stdout.flush()
|
|
|
|
self.previous_line = ''
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._clear_line()
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
spinner = SpinOutputter.spinning_cursor()
|
|
|
|
while self.running:
|
|
|
|
self._clear_line()
|
|
|
|
sys.stdout.write('\r')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
sys.stdout.write(next(spinner))
|
|
|
|
sys.stdout.write(" ")
|
|
|
|
sys.stdout.write(colored(self.next_line, attrs=['dark']))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
self.previous_line = self.next_line
|
|
|
|
time.sleep(0.25)
|
|
|
|
|
|
|
|
|
|
|
|
def _run_and_wait(command, error_allowed=False):
|
|
|
|
# Run the command itself.
|
|
|
|
outputter = SpinOutputter('Running command %s' % command)
|
|
|
|
outputter.start()
|
|
|
|
|
|
|
|
output = ''
|
|
|
|
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
|
|
for line in iter(process.stdout.readline, ''):
|
|
|
|
output += line
|
|
|
|
outputter.set_next(line)
|
|
|
|
|
|
|
|
result = process.wait()
|
|
|
|
outputter.stop()
|
|
|
|
|
|
|
|
if result != 0 and not error_allowed:
|
|
|
|
print colored('>>> Command `%s` Failed:' % command, 'red')
|
|
|
|
print output
|
|
|
|
raise CommandFailedException()
|
|
|
|
|
2018-09-25 22:21:39 +00:00
|
|
|
return output
|
2018-09-24 19:32:09 +00:00
|
|
|
|
2018-09-25 22:21:39 +00:00
|
|
|
|
|
|
|
def _indent(text, amount):
|
|
|
|
return ''.join((' ' * amount) + line for line in text.splitlines(True))
|
|
|
|
|
2019-01-25 22:28:35 +00:00
|
|
|
|
|
|
|
def _run_commands(commands):
|
|
|
|
last_result = None
|
|
|
|
for command in commands:
|
|
|
|
if isinstance(command, Command):
|
|
|
|
last_result = _run_and_wait(['vagrant', 'ssh', '-c', command.command])
|
|
|
|
else:
|
|
|
|
last_result = _run_and_wait(['vagrant', 'scp', command.source, command.destination])
|
|
|
|
|
|
|
|
return last_result
|
|
|
|
|
|
|
|
def _run_box(box, client, registry):
|
2019-01-25 18:49:19 +00:00
|
|
|
vagrant, vagrant_scp = _check_vagrant()
|
|
|
|
if not vagrant:
|
|
|
|
print("vagrant command not found")
|
|
|
|
return
|
|
|
|
|
|
|
|
if not vagrant_scp:
|
|
|
|
print("vagrant-scp plugin not installed")
|
|
|
|
return
|
|
|
|
|
2019-01-25 22:28:35 +00:00
|
|
|
namespace = 'devtable'
|
|
|
|
repo_name = 'testrepo%s' % int(time.time())
|
|
|
|
username = 'devtable'
|
|
|
|
password = 'password'
|
|
|
|
|
2018-09-25 22:21:39 +00:00
|
|
|
print colored('>>> Box: %s' % box, attrs=['bold'])
|
2018-09-24 19:32:09 +00:00
|
|
|
print colored('>>> Starting box', 'yellow')
|
|
|
|
_run_and_wait(['vagrant', 'destroy', '-f'], error_allowed=True)
|
|
|
|
_run_and_wait(['rm', 'Vagrantfile'], error_allowed=True)
|
|
|
|
_run_and_wait(['vagrant', 'init'] + box.split(' '))
|
|
|
|
_run_and_wait(['vagrant', 'up', '--provider', 'virtualbox'])
|
|
|
|
|
2019-01-25 22:28:35 +00:00
|
|
|
print colored('>>> Setting up runtime', 'yellow')
|
|
|
|
_run_commands(client.setup_client(registry))
|
2018-09-24 19:32:09 +00:00
|
|
|
|
2019-01-25 22:28:35 +00:00
|
|
|
print colored('>>> Client version', 'cyan')
|
|
|
|
docker_version = _run_commands(client.print_version())
|
2018-09-25 22:21:39 +00:00
|
|
|
print _indent(docker_version, 4)
|
|
|
|
|
2019-01-25 22:28:35 +00:00
|
|
|
print colored('>>> Populating test image', 'yellow')
|
|
|
|
_run_commands(client.populate_test_image(registry, namespace, repo_name))
|
2018-09-24 19:32:09 +00:00
|
|
|
|
|
|
|
print colored('>>> Testing login', 'cyan')
|
2019-01-25 22:28:35 +00:00
|
|
|
_run_commands(client.login(registry, username, password))
|
2018-09-24 19:32:09 +00:00
|
|
|
|
|
|
|
print colored('>>> Testing push', 'cyan')
|
2019-01-25 22:28:35 +00:00
|
|
|
_run_commands(client.push(registry, namespace, repo_name))
|
2018-09-24 19:32:09 +00:00
|
|
|
|
|
|
|
print colored('>>> Removing all images', 'yellow')
|
2019-01-25 22:28:35 +00:00
|
|
|
_run_commands(client.pre_pull_cleanup(registry, namespace, repo_name))
|
2018-09-24 19:32:09 +00:00
|
|
|
|
|
|
|
print colored('>>> Testing pull', 'cyan')
|
2019-01-25 22:28:35 +00:00
|
|
|
_run_commands(client.pull(registry, namespace, repo_name))
|
|
|
|
|
|
|
|
print colored('>>> Verifying', 'cyan')
|
|
|
|
_run_commands(client.verify(registry, namespace, repo_name))
|
2018-09-24 19:32:09 +00:00
|
|
|
|
|
|
|
print colored('>>> Tearing down box', 'magenta')
|
|
|
|
_run_and_wait(['vagrant', 'destroy', '-f'], error_allowed=True)
|
|
|
|
|
|
|
|
print colored('>>> Successfully tested box %s' % box, 'green')
|
|
|
|
print ""
|
|
|
|
|
|
|
|
def test_clients(registry='10.0.2.2:5000'):
|
|
|
|
print colored('>>> Running against registry ', attrs=['bold']) + colored(registry, 'cyan')
|
2019-01-25 22:28:35 +00:00
|
|
|
for box, client in BOXES:
|
2018-09-24 19:32:09 +00:00
|
|
|
try:
|
2019-01-25 22:28:35 +00:00
|
|
|
_run_box(box, client, registry)
|
2018-09-24 19:32:09 +00:00
|
|
|
except CommandFailedException:
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_clients(sys.argv[1] if len(sys.argv) > 1 else '10.0.2.2:5000')
|