[lxc-devel] [lxd/master] Template fixes and tests
stgraber on Github
lxc-bot at linuxcontainers.org
Thu Nov 24 22:29:38 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161124/ed3fd351/attachment.bin>
-------------- next part --------------
From 5a8881baf5e699f8e76c223907e3b216358cc01d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 24 Nov 2016 13:08:57 -0500
Subject: [PATCH 1/2] Don't double apply templates
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
lxd/container_lxc.go | 2 ++
1 file changed, 2 insertions(+)
diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index e3bafaf..118af75 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -3870,6 +3870,8 @@ func (c *containerLXC) TemplateApply(trigger string) error {
if err != nil {
return err
}
+
+ return nil
}
return c.templateApplyNow(trigger)
From 599cb97a5eda5d008881325057bdde342cfa742b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 24 Nov 2016 17:28:23 -0500
Subject: [PATCH 2/2] tests: Add tests for file templating
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
test/deps/import-busybox | 36 ++++++++++++++++++--
test/main.sh | 4 +++
test/suites/template.sh | 89 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 126 insertions(+), 3 deletions(-)
create mode 100644 test/suites/template.sh
diff --git a/test/deps/import-busybox b/test/deps/import-busybox
index d78d777..9ee5c8b 100755
--- a/test/deps/import-busybox
+++ b/test/deps/import-busybox
@@ -175,7 +175,7 @@ class Busybox(object):
if self.workdir:
shutil.rmtree(self.workdir)
- def create_tarball(self, split=False):
+ def create_tarball(self, split=False, template=[]):
xz = "pxz" if find_on_path("pxz") else "xz"
destination_tar = os.path.join(self.workdir, "busybox.tar")
@@ -239,6 +239,32 @@ class Busybox(object):
directory_file.name = "rootfs/%s" % path
target_tarball.addfile(directory_file)
+ # Deal with templating
+ if template:
+ metadata["templates"] = {"/template": {
+ "when": template,
+ "template": "template.tpl"}}
+
+ directory_file = tarfile.TarInfo()
+ directory_file.type = tarfile.DIRTYPE
+ directory_file.name = "templates"
+ target_tarball.addfile(directory_file)
+
+ template = """name: {{ container.name }}
+architecture: {{ container.architecture }}
+privileged: {{ container.privileged }}
+ephemeral: {{ container.ephemeral }}
+trigger: {{ trigger }}
+path: {{ path }}
+user.foo: {{ config_get("user.foo", "_unset_") }}
+"""
+
+ template_file = tarfile.TarInfo()
+ template_file.size = len(template)
+ template_file.name = "templates/template.tpl"
+ target_tarball.addfile(template_file,
+ io.BytesIO(template.encode()))
+
# Add the metadata file
metadata_yaml = json.dumps(metadata, sort_keys=True,
indent=4, separators=(',', ': '),
@@ -304,7 +330,9 @@ if __name__ == "__main__":
busybox = Busybox()
if args.split:
- meta_path, rootfs_path = busybox.create_tarball(split=True)
+ meta_path, rootfs_path = busybox.create_tarball(
+ split=True,
+ template=args.template.split(","))
with open(meta_path, "rb") as meta_fd:
with open(rootfs_path, "rb") as rootfs_fd:
@@ -321,7 +349,7 @@ if __name__ == "__main__":
r = lxd.images_upload((meta_path, rootfs_path), args.public)
print("Image imported as: %s" % r['fingerprint'])
else:
- path = busybox.create_tarball()
+ path = busybox.create_tarball(template=args.template.split(","))
with open(path, "rb") as fd:
fingerprint = hashlib.sha256(fd.read()).hexdigest()
@@ -343,6 +371,8 @@ if __name__ == "__main__":
default=False, help="Whether to create a split image")
parser.add_argument("--filename", action="store_true",
default=False, help="Set the split image's filename")
+ parser.add_argument("--template", type=str,
+ default="", help="Trigger test template")
parser.set_defaults(func=import_busybox)
# Call the function
diff --git a/test/main.sh b/test/main.sh
index 6d69479..44bd337 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -490,6 +490,10 @@ echo "==> TEST: idmap"
TEST_CURRENT=test_idmap
test_idmap
+echo "==> TEST: template"
+TEST_CURRENT=test_template
+test_template
+
echo "==> TEST: devlxd"
TEST_CURRENT=test_devlxd
test_devlxd
diff --git a/test/suites/template.sh b/test/suites/template.sh
new file mode 100644
index 0000000..90c452f
--- /dev/null
+++ b/test/suites/template.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+test_template() {
+ # Import a template which only triggers on create
+ deps/import-busybox --alias template-test --template create
+ lxc init template-test template
+
+ # Confirm that template application is delayed to first start
+ ! lxc file pull template/template -
+
+ # Validate that the template is applied
+ lxc start template
+ lxc file pull template/template - | grep "^name: template$"
+
+ # Confirm it's not applied on copies
+ lxc copy template template1
+ lxc file pull template1/template - | grep "^name: template$"
+
+ # Cleanup
+ lxc image delete template-test
+ lxc delete template template1 --force
+
+
+ # Import a template which only triggers on copy
+ deps/import-busybox --alias template-test --template copy
+ lxc launch template-test template
+
+ # Confirm that the template doesn't trigger on create
+ ! lxc file pull template/template -
+
+ # Copy the container
+ lxc copy template template1
+
+ # Confirm that template application is delayed to first start
+ ! lxc file pull template1/template -
+
+ # Validate that the template is applied
+ lxc start template1
+ lxc file pull template1/template - | grep "^name: template1$"
+
+ # Cleanup
+ lxc image delete template-test
+ lxc delete template template1 --force
+
+
+ # Import a template which only triggers on start
+ deps/import-busybox --alias template-test --template start
+ lxc launch template-test template
+
+ # Validate that the template is applied
+ lxc file pull template/template - | grep "^name: template$"
+ lxc file pull template/template - | grep "^user.foo: _unset_$"
+
+ # Confirm it's re-run at every start
+ lxc config set template user.foo bar
+ lxc restart template --force
+ lxc file pull template/template - | grep "^user.foo: bar$"
+
+ # Cleanup
+ lxc image delete template-test
+ lxc delete template --force
+
+
+ # Import a template which triggers on both create and copy
+ deps/import-busybox --alias template-test --template create,copy
+ lxc init template-test template
+
+ # Confirm that template application is delayed to first start
+ ! lxc file pull template/template -
+
+ # Validate that the template is applied
+ lxc start template
+ lxc file pull template/template - | grep "^name: template$"
+
+ # Confirm it's also applied on copies
+ lxc copy template template1
+ lxc start template1
+ lxc file pull template1/template - | grep "^name: template1$"
+ lxc file pull template1/template - | grep "^user.foo: _unset_$"
+
+ # But doesn't change on restart
+ lxc config set template1 user.foo bar
+ lxc restart template1 --force
+ lxc file pull template1/template - | grep "^user.foo: _unset_$"
+
+ # Cleanup
+ lxc image delete template-test
+ lxc delete template --force
+}
More information about the lxc-devel
mailing list