Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
2f7e95cc
Commit
2f7e95cc
authored
May 21, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template: Validate
parent
2e4dd639
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
102 additions
and
0 deletions
+102
-0
template/template.go
template/template.go
+35
-0
template/template_test.go
template/template_test.go
+42
-0
template/test-fixtures/validate-bad-override.json
template/test-fixtures/validate-bad-override.json
+12
-0
template/test-fixtures/validate-good-override.json
template/test-fixtures/validate-good-override.json
+12
-0
template/test-fixtures/validate-no-builders.json
template/test-fixtures/validate-no-builders.json
+1
-0
No files found.
template/template.go
View file @
2f7e95cc
package
template
import
(
"errors"
"fmt"
"time"
"github.com/hashicorp/go-multierror"
)
// Template represents the parsed template that is used to configure
...
...
@@ -68,6 +71,38 @@ type OnlyExcept struct {
Except
[]
string
}
//-------------------------------------------------------------------
// Functions
//-------------------------------------------------------------------
// Validate does some basic validation of the template on top of the
// validation that occurs while parsing. If possible, we try to defer
// validation to here. The validation errors that occur during parsing
// are the minimal necessary to make sure parsing builds a reasonable
// Template structure.
func
(
t
*
Template
)
Validate
()
error
{
var
err
error
// At least one builder must be defined
if
len
(
t
.
Builders
)
==
0
{
err
=
multierror
.
Append
(
err
,
errors
.
New
(
"at least one builder must be defined"
))
}
// Verify that the provisioner overrides target builders that exist
for
i
,
p
:=
range
t
.
Provisioners
{
for
name
,
_
:=
range
p
.
Override
{
if
_
,
ok
:=
t
.
Builders
[
name
];
!
ok
{
err
=
multierror
.
Append
(
err
,
fmt
.
Errorf
(
"provisioner %d: override '%s' doesn't exist"
,
i
+
1
,
name
))
}
}
}
return
err
}
//-------------------------------------------------------------------
// GoStringer
//-------------------------------------------------------------------
...
...
template/template_test.go
View file @
2f7e95cc
package
template
import
(
"os"
"path/filepath"
"testing"
)
const
FixturesDir
=
"./test-fixtures"
...
...
@@ -10,3 +12,43 @@ const FixturesDir = "./test-fixtures"
func
fixtureDir
(
n
string
)
string
{
return
filepath
.
Join
(
FixturesDir
,
n
)
}
func
TestTemplateValidate
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
File
string
Err
bool
}{
{
"validate-no-builders.json"
,
true
,
},
{
"validate-bad-override.json"
,
true
,
},
{
"validate-good-override.json"
,
false
,
},
}
for
_
,
tc
:=
range
cases
{
f
,
err
:=
os
.
Open
(
fixtureDir
(
tc
.
File
))
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
tpl
,
err
:=
Parse
(
f
)
f
.
Close
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s
\n\n
%s"
,
tc
.
File
,
err
)
}
err
=
tpl
.
Validate
()
if
(
err
!=
nil
)
!=
tc
.
Err
{
t
.
Fatalf
(
"err: %s
\n\n
%s"
,
tc
.
File
,
err
)
}
}
}
template/test-fixtures/validate-bad-override.json
0 → 100644
View file @
2f7e95cc
{
"builders"
:
[{
"type"
:
"foo"
}],
"provisioners"
:
[{
"type"
:
"bar"
,
"override"
:
{
"bar"
:
{}
}
}]
}
template/test-fixtures/validate-good-override.json
0 → 100644
View file @
2f7e95cc
{
"builders"
:
[{
"type"
:
"foo"
}],
"provisioners"
:
[{
"type"
:
"bar"
,
"override"
:
{
"foo"
:
{}
}
}]
}
template/test-fixtures/validate-no-builders.json
0 → 100644
View file @
2f7e95cc
{}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment