[lxc-devel] [distrobuilder/master] definition: Validate generators

monstermunchkin on Github lxc-bot at linuxcontainers.org
Fri Mar 9 14:07:11 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 363 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180309/9b37b16d/attachment.bin>
-------------- next part --------------
From 65bfe8960488e4cd71b6b1725be789a9d3d9fbec Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 9 Mar 2018 15:06:01 +0100
Subject: [PATCH] definition: Validate generators

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/definition.go      | 13 +++++++++++++
 shared/definition_test.go | 29 +++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/shared/definition.go b/shared/definition.go
index 7e60657..97d2ee3 100644
--- a/shared/definition.go
+++ b/shared/definition.go
@@ -141,5 +141,18 @@ func ValidateDefinition(def Definition) error {
 		return fmt.Errorf("packages.manager must be one of %v", validManagers)
 	}
 
+	validGenerators := []string{
+		"hostname",
+		"hosts",
+		"remove",
+		"dump",
+	}
+
+	for _, file := range def.Files {
+		if !shared.StringInSlice(strings.TrimSpace(file.Generator), validGenerators) {
+			return fmt.Errorf("files.*.generator must be one of %v", validGenerators)
+		}
+	}
+
 	return nil
 }
diff --git a/shared/definition_test.go b/shared/definition_test.go
index ded182f..c60e76d 100644
--- a/shared/definition_test.go
+++ b/shared/definition_test.go
@@ -43,6 +43,11 @@ func TestValidateDefinition(t *testing.T) {
 				Packages: DefinitionPackages{
 					Manager: "apt",
 				},
+				Files: []DefinitionFile{
+					{
+						Generator: "dump",
+					},
+				},
 			},
 			"",
 			false,
@@ -82,6 +87,30 @@ func TestValidateDefinition(t *testing.T) {
 			"",
 			false,
 		},
+		{
+			"invalid generator",
+			Definition{
+				Image: DefinitionImage{
+					Distribution: "ubuntu",
+					Release:      "artful",
+				},
+				Source: DefinitionSource{
+					Downloader: "debootstrap",
+					URL:        "https://ubuntu.com",
+					Keys:       []string{"0xCODE"},
+				},
+				Packages: DefinitionPackages{
+					Manager: "apt",
+				},
+				Files: []DefinitionFile{
+					{
+						Generator: "foo",
+					},
+				},
+			},
+			"files\\.\\*\\.generator must be one of .+",
+			true,
+		},
 		{
 			"empty image.distribution",
 			Definition{},


More information about the lxc-devel mailing list