From patchwork Tue Feb 16 12:39:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 8324741 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id EBEDD9F6E7 for ; Tue, 16 Feb 2016 12:42:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 16D512026C for ; Tue, 16 Feb 2016 12:42:43 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0593D2022A for ; Tue, 16 Feb 2016 12:42:42 +0000 (UTC) Received: from localhost ([::1]:45253 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVexl-00040s-D1 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 16 Feb 2016 07:42:41 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58400) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVevU-0000kn-JY for qemu-devel@nongnu.org; Tue, 16 Feb 2016 07:40:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aVevP-0002zU-J2 for qemu-devel@nongnu.org; Tue, 16 Feb 2016 07:40:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45943) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVevP-0002zL-9Z for qemu-devel@nongnu.org; Tue, 16 Feb 2016 07:40:15 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id DE506C077541; Tue, 16 Feb 2016 12:40:14 +0000 (UTC) Received: from fam-t430.redhat.com (vpn1-7-134.pek2.redhat.com [10.72.7.134]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u1GCdxbC024209; Tue, 16 Feb 2016 07:40:07 -0500 From: Fam Zheng To: qemu-devel@nongnu.org Date: Tue, 16 Feb 2016 20:39:45 +0800 Message-Id: <1455626399-7111-2-git-send-email-famz@redhat.com> In-Reply-To: <1455626399-7111-1-git-send-email-famz@redhat.com> References: <1455626399-7111-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, peter.maydell@linaro.org, jsnow@redhat.com, stefanha@redhat.com, sw@weilnetz.de, Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , david@gibson.dropbear.id.au Subject: [Qemu-devel] [PATCH v2 01/15] tests: Add utilities for docker testing X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP docker_run: A wrapper for "docker run" (or "sudo -n docker run" if necessary), which takes care of killing and removing the running container at SIGINT. docker_clean: A tool to tear down all the containers including inactive ones that are started by docker_run. docker_build: A tool to compare an image from given dockerfile and rebuild it if they're different. Signed-off-by: Fam Zheng --- tests/docker/docker.py | 113 ++++++++++++++++++++++++++++++++++++++++++++++ tests/docker/docker_build | 42 +++++++++++++++++ tests/docker/docker_clean | 22 +++++++++ tests/docker/docker_run | 29 ++++++++++++ 4 files changed, 206 insertions(+) create mode 100755 tests/docker/docker.py create mode 100755 tests/docker/docker_build create mode 100755 tests/docker/docker_clean create mode 100755 tests/docker/docker_run diff --git a/tests/docker/docker.py b/tests/docker/docker.py new file mode 100755 index 0000000..d175a86 --- /dev/null +++ b/tests/docker/docker.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python2 -B +# +# Docker controlling module +# +# Copyright (c) 2016 Red Hat Inc. +# +# Authors: +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2 +# or (at your option) any later version. See the COPYING file in +# the top-level directory. + +import os +import subprocess +import json +import hashlib +import atexit +import uuid + +class ContainerTerminated(Exception): + """ Raised if the container has already existed """ + pass + +class Docker(object): + """ Running Docker commands """ + def __init__(self): + self._command = self._guess_command() + self._instances = [] + atexit.register(self._kill_instances) + + def _do(self, cmd, quiet=True, **kwargs): + if quiet: + kwargs["stdout"] = subprocess.PIPE + return subprocess.call(self._command + cmd, **kwargs) + + def _do_kill_instances(self, only_known, only_active=True): + cmd = ["ps", "-q"] + if not only_active: + cmd.append("-a") + for i in self._output(cmd).split(): + resp = self._output(["inspect", i]) + labels = json.loads(resp)[0]["Config"]["Labels"] + active = json.loads(resp)[0]["State"]["Running"] + if not labels: + continue + instance_uuid = labels.get("com.qemu.instance.uuid", None) + if not instance_uuid: + continue + if only_known and instance_uuid not in self._instances: + continue + print "Terminating", i + if active: + self._do(["kill", i]) + self._do(["rm", i]) + + def clean(self): + self._do_kill_instances(False, False) + return 0 + + def _kill_instances(self): + return self._do_kill_instances(True) + + def _output(self, cmd, **kwargs): + return subprocess.check_output(self._command + cmd, + stderr=subprocess.STDOUT, + **kwargs) + + def _guess_command(self): + commands = [["docker"], ["sudo", "-n", "docker"]] + for cmd in commands: + if subprocess.call(cmd + ["images"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) == 0: + return cmd + commands_txt = "\n".join([" " + " ".join(x) for x in commands]) + raise Exception("Cannot find working docker command. Tried:\n%s" % commands_txt) + + def get_image_dockerfile_checksum(self, tag): + resp = self._output(["inspect", tag]) + labels = json.loads(resp)[0]["Config"].get("Labels", {}) + return labels.get("com.qemu.dockerfile-checksum", "") + + def checksum(self, text): + return hashlib.sha1(text).hexdigest() + + def build_image(self, tag, dockerfile, df, quiet=True): + tmp = dockerfile + "\n" + \ + "LABEL com.qemu.dockerfile-checksum=%s" % self.checksum(dockerfile) + tmp_df = df + ".tmp" + tmp_file = open(tmp_df, "wb") + tmp_file.write(tmp) + tmp_file.close() + self._do(["build", "-t", tag, "-f", tmp_df, os.path.dirname(df)], + quiet=quiet) + os.unlink(tmp_df) + + def image_matches_dockerfile(self, tag, dockerfile): + try: + checksum = self.get_image_dockerfile_checksum(tag) + except: + return False + return checksum == self.checksum(dockerfile) + + def run(self, cmd, keep, quiet): + label = uuid.uuid1().hex + if not keep: + self._instances.append(label) + ret = self._do(["run", "--label", "com.qemu.instance.uuid=" + label] + cmd, quiet=quiet) + if not keep: + self._instances.remove(label) + return ret + diff --git a/tests/docker/docker_build b/tests/docker/docker_build new file mode 100755 index 0000000..6948e2c --- /dev/null +++ b/tests/docker/docker_build @@ -0,0 +1,42 @@ +#!/usr/bin/env python2 +# +# Compare to Dockerfile and rebuild a docker image if necessary. +# +# Copyright (c) 2016 Red Hat Inc. +# +# Authors: +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2 +# or (at your option) any later version. See the COPYING file in +# the top-level directory. + +import sys +import docker +import argparse + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("tag", + help="Image Tag") + parser.add_argument("dockerfile", + help="Dockerfile name") + parser.add_argument("--verbose", "-v", action="store_true", + help="Print verbose information") + args = parser.parse_args() + + dockerfile = open(args.dockerfile, "rb").read() + tag = args.tag + + dkr = docker.Docker() + if dkr.image_matches_dockerfile(tag, dockerfile): + if args.verbose: + print "Image is up to date." + return 0 + + quiet = not args.verbose + dkr.build_image(tag, dockerfile, args.dockerfile, quiet=quiet) + return 0 + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/docker/docker_clean b/tests/docker/docker_clean new file mode 100755 index 0000000..88cdba6 --- /dev/null +++ b/tests/docker/docker_clean @@ -0,0 +1,22 @@ +#!/usr/bin/env python2 +# +# Clean up uselsee containers. +# +# Copyright (c) 2016 Red Hat Inc. +# +# Authors: +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2 +# or (at your option) any later version. See the COPYING file in +# the top-level directory. + +import sys +import docker + +def main(): + docker.Docker().clean() + return 0 + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/docker/docker_run b/tests/docker/docker_run new file mode 100755 index 0000000..4c46d90 --- /dev/null +++ b/tests/docker/docker_run @@ -0,0 +1,29 @@ +#!/usr/bin/env python2 +# +# Wrapper for "docker run" with automatical clean up if the execution is +# iterrupted. +# +# Copyright (c) 2016 Red Hat Inc. +# +# Authors: +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2 +# or (at your option) any later version. See the COPYING file in +# the top-level directory. + +import sys +import argparse +import docker + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--keep", action="store_true", + help="Don't remove image when the command completes") + parser.add_argument("--quiet", action="store_true", + help="Run quietly unless an error occured") + args, argv = parser.parse_known_args() + return docker.Docker().run(argv, args.keep, quiet=args.quiet) + +if __name__ == "__main__": + sys.exit(main())