From: Fam Zheng <famz@redhat.com>
To: John Snow <jsnow@redhat.com>
Cc: kwolf@redhat.com, peter.maydell@linaro.org, sw@weilnetz.de,
qemu-devel@nongnu.org, stefanha@redhat.com,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
david@gibson.dropbear.id.au
Subject: Re: [Qemu-devel] [PATCH 01/12] tests: Add utilities for docker testing
Date: Tue, 9 Feb 2016 10:01:40 +0800 [thread overview]
Message-ID: <20160209020140.GA7103@ad.usersys.redhat.com> (raw)
In-Reply-To: <56B90D7C.70301@redhat.com>
On Mon, 02/08 16:49, John Snow wrote:
> > + def _guess_command(self):
> > + for c in [["docker"], ["sudo", "-n", "docker"]]:
>
> If the sudo version fails (Say, because a password prompt shows up) we
> get the unhelpful error "Cannot find working docker command."
If you have previously "sudo $something" and typed in the password, this will
work.
You can also specify passworless for docker only:
fam ALL=(ALL) NOPASSWD: /usr/bin/docker
Fam
>
> > + if subprocess.call(c + ["images"],
> > + stdout=subprocess.PIPE,
> > + stderr=subprocess.PIPE) == 0:
> > + return c
> > + raise Exception("Cannot find working docker command")
> > +
> > + def get_image_dockerfile_checksum(self, tag):
> > + resp = self._output(["inspect", tag])
> > + t = json.loads(resp)[0]
> > + return t["Config"].get("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"
> > + f = open(tmp_df, "wb")
> > + f.write(tmp)
> > + f.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:
> > + a = self.get_image_dockerfile_checksum(tag)
> > + except:
> > + return False
> > + return a == self.checksum(dockerfile)
> > +
> > + def run(self, cmd, quiet, **kwargs):
> > + label = uuid.uuid1().hex
> > + self._instances.append(label)
> > + r = self._do(["run", "--label", "com.qemu.instance.uuid=" + label] + cmd, quiet=quiet)
> > + self._instances.remove(label)
> > + return r
> > +
> > diff --git a/tests/docker/docker_build b/tests/docker/docker_build
> > new file mode 100755
> > index 0000000..b4f0dec
> > --- /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 <famz@redhat.com>
> > +#
> > +# 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
> > +
> > + d = docker.Docker()
> > + if d.image_matches_dockerfile(tag, dockerfile):
> > + if args.verbose:
> > + print "Image is up to date."
> > + return 0
> > +
> > + quiet = not args.verbose
> > + d.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 <famz@redhat.com>
> > +#
> > +# 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..5cf9d04
> > --- /dev/null
> > +++ b/tests/docker/docker_run
> > @@ -0,0 +1,28 @@
> > +#!/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 <famz@redhat.com>
> > +#
> > +# 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 sys
> > +import argparse
> > +import docker
> > +
> > +def main():
> > + parser = argparse.ArgumentParser()
> > + 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, quiet=args.quiet)
> > +
> > +if __name__ == "__main__":
> > + sys.exit(main())
> >
>
> --
> —js
next prev parent reply other threads:[~2016-02-09 2:01 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-05 9:24 [Qemu-devel] [PATCH 00/12] tests: Introducing docker tests Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 01/12] tests: Add utilities for docker testing Fam Zheng
2016-02-08 21:49 ` John Snow
2016-02-09 2:01 ` Fam Zheng [this message]
2016-02-09 23:16 ` John Snow
2016-02-14 5:10 ` Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 02/12] Makefile: Rules " Fam Zheng
2016-02-15 10:06 ` Alex Bennée
2016-02-15 13:52 ` Fam Zheng
2016-02-15 14:13 ` Alex Bennée
2016-02-05 9:24 ` [Qemu-devel] [PATCH 03/12] docker: Add images Fam Zheng
2016-02-15 10:15 ` Alex Bennée
2016-02-15 13:44 ` Fam Zheng
2016-02-15 14:12 ` Alex Bennée
2016-02-05 9:24 ` [Qemu-devel] [PATCH 04/12] docker: Add test runner Fam Zheng
2016-02-15 10:55 ` Alex Bennée
2016-02-15 12:45 ` Alex Bennée
2016-02-15 13:29 ` Fam Zheng
2016-02-15 14:10 ` Alex Bennée
2016-02-16 2:52 ` Fam Zheng
2016-02-16 6:00 ` Fam Zheng
2016-02-16 8:20 ` Alex Bennée
2016-02-05 9:24 ` [Qemu-devel] [PATCH 05/12] docker: Add common.rc Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 06/12] docker: Add basic test Fam Zheng
2016-02-15 14:34 ` Alex Bennée
2016-02-15 14:42 ` Peter Maydell
2016-02-15 14:52 ` Alex Bennée
2016-02-16 1:15 ` Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 07/12] docker: Add clang test Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 08/12] docker: Add mingw test Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 09/12] docker: Add travis tool Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 10/12] docs: Add text for tests/docker in build-system.txt Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 11/12] .gitignore: Ignore temporary dockerfile Fam Zheng
2016-02-15 14:42 ` Alex Bennée
2016-02-16 2:43 ` Fam Zheng
2016-02-05 9:24 ` [Qemu-devel] [PATCH 12/12] MAINTAINERS: Add tests/docker Fam Zheng
2016-02-15 14:36 ` Alex Bennée
2016-02-10 11:23 ` [Qemu-devel] [PATCH 00/12] tests: Introducing docker tests Alex Bennée
2016-02-14 5:22 ` Fam Zheng
2016-02-15 17:59 ` Alex Bennée
2016-02-16 2:42 ` Fam Zheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160209020140.GA7103@ad.usersys.redhat.com \
--to=famz@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=david@gibson.dropbear.id.au \
--cc=jsnow@redhat.com \
--cc=kwolf@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
--cc=sw@weilnetz.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).