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
639e63fd
Commit
639e63fd
authored
May 29, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: test for template path
parent
31d6dcb6
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
84 additions
and
31 deletions
+84
-31
helper/config/decode.go
helper/config/decode.go
+3
-1
packer/build.go
packer/build.go
+5
-0
packer/core.go
packer/core.go
+25
-18
packer/core_test.go
packer/core_test.go
+30
-0
template/interpolate/funcs.go
template/interpolate/funcs.go
+14
-9
template/interpolate/funcs_test.go
template/interpolate/funcs_test.go
+7
-3
No files found.
helper/config/decode.go
View file @
639e63fd
...
...
@@ -104,7 +104,8 @@ func Decode(target interface{}, config *DecodeOpts, raws ...interface{}) error {
// detecting things like user variables from the raw configuration params.
func
DetectContext
(
raws
...
interface
{})
(
*
interpolate
.
Context
,
error
)
{
var
s
struct
{
Vars
map
[
string
]
string
`mapstructure:"packer_user_variables"`
TemplatePath
string
`mapstructure:"packer_template_path"`
Vars
map
[
string
]
string
`mapstructure:"packer_user_variables"`
}
for
_
,
r
:=
range
raws
{
...
...
@@ -114,6 +115,7 @@ func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
}
return
&
interpolate
.
Context
{
TemplatePath
:
s
.
TemplatePath
,
UserVariables
:
s
.
Vars
,
},
nil
}
...
...
packer/build.go
View file @
639e63fd
...
...
@@ -24,6 +24,9 @@ const (
// force build is enabled.
ForceConfigKey
=
"packer_force"
// TemplatePathKey is the path to the template that configured this build
TemplatePathKey
=
"packer_template_path"
// This key contains a map[string]string of the user variables for
// template processing.
UserVariablesConfigKey
=
"packer_user_variables"
...
...
@@ -78,6 +81,7 @@ type coreBuild struct {
hooks
map
[
string
][]
Hook
postProcessors
[][]
coreBuildPostProcessor
provisioners
[]
coreBuildProvisioner
templatePath
string
variables
map
[
string
]
string
debug
bool
...
...
@@ -125,6 +129,7 @@ func (b *coreBuild) Prepare() (warn []string, err error) {
BuilderTypeConfigKey
:
b
.
builderType
,
DebugConfigKey
:
b
.
debug
,
ForceConfigKey
:
b
.
force
,
TemplatePathKey
:
b
.
templatePath
,
UserVariablesConfigKey
:
b
.
variables
,
}
...
...
packer/core.go
View file @
639e63fd
...
...
@@ -50,27 +50,10 @@ type ComponentFinder struct {
// NewCore creates a new Core.
func
NewCore
(
c
*
CoreConfig
)
(
*
Core
,
error
)
{
// Go through and interpolate all the build names. We shuld be able
// to do this at this point with the variables.
builds
:=
make
(
map
[
string
]
*
template
.
Builder
)
for
_
,
b
:=
range
c
.
Template
.
Builders
{
v
,
err
:=
interpolate
.
Render
(
b
.
Name
,
&
interpolate
.
Context
{
UserVariables
:
c
.
Variables
,
})
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Error interpolating builder '%s': %s"
,
b
.
Name
,
err
)
}
builds
[
v
]
=
b
}
result
:=
&
Core
{
components
:
c
.
Components
,
template
:
c
.
Template
,
variables
:
c
.
Variables
,
builds
:
builds
,
}
if
err
:=
result
.
validate
();
err
!=
nil
{
return
nil
,
err
...
...
@@ -79,6 +62,20 @@ func NewCore(c *CoreConfig) (*Core, error) {
return
nil
,
err
}
// Go through and interpolate all the build names. We shuld be able
// to do this at this point with the variables.
result
.
builds
=
make
(
map
[
string
]
*
template
.
Builder
)
for
_
,
b
:=
range
c
.
Template
.
Builders
{
v
,
err
:=
interpolate
.
Render
(
b
.
Name
,
result
.
context
())
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Error interpolating builder '%s': %s"
,
b
.
Name
,
err
)
}
result
.
builds
[
v
]
=
b
}
return
result
,
nil
}
...
...
@@ -204,6 +201,7 @@ func (c *Core) Build(n string) (Build, error) {
builderType
:
configBuilder
.
Type
,
postProcessors
:
postProcessors
,
provisioners
:
provisioners
,
templatePath
:
c
.
template
.
Path
,
variables
:
c
.
variables
,
},
nil
}
...
...
@@ -243,7 +241,9 @@ func (c *Core) init() error {
}
// Go through the variables and interpolate the environment variables
ctx
:=
&
interpolate
.
Context
{
EnableEnv
:
true
}
ctx
:=
c
.
context
()
ctx
.
EnableEnv
=
true
ctx
.
UserVariables
=
nil
for
k
,
v
:=
range
c
.
template
.
Variables
{
// Ignore variables that are required
if
v
.
Required
{
...
...
@@ -268,3 +268,10 @@ func (c *Core) init() error {
return
nil
}
func
(
c
*
Core
)
context
()
*
interpolate
.
Context
{
return
&
interpolate
.
Context
{
TemplatePath
:
c
.
template
.
Path
,
UserVariables
:
c
.
variables
,
}
}
packer/core_test.go
View file @
639e63fd
...
...
@@ -2,6 +2,7 @@ package packer
import
(
"os"
"path/filepath"
"reflect"
"testing"
...
...
@@ -338,6 +339,35 @@ func TestCoreBuild_postProcess(t *testing.T) {
}
}
func
TestCoreBuild_templatePath
(
t
*
testing
.
T
)
{
config
:=
TestCoreConfig
(
t
)
testCoreTemplate
(
t
,
config
,
fixtureDir
(
"build-template-path.json"
))
b
:=
TestBuilder
(
t
,
config
,
"test"
)
core
:=
TestCore
(
t
,
config
)
expected
,
_
:=
filepath
.
Abs
(
"./test-fixtures"
)
build
,
err
:=
core
.
Build
(
"test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
_
,
err
:=
build
.
Prepare
();
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
// Interpolate the config
var
result
map
[
string
]
interface
{}
err
=
configHelper
.
Decode
(
&
result
,
nil
,
b
.
PrepareConfig
...
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
result
[
"value"
]
!=
expected
{
t
.
Fatalf
(
"bad: %#v"
,
result
)
}
}
func
TestCoreValidate
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
File
string
...
...
template/interpolate/funcs.go
View file @
639e63fd
...
...
@@ -24,13 +24,13 @@ func init() {
// Funcs are the interpolation funcs that are available within interpolations.
var
FuncGens
=
map
[
string
]
FuncGenerator
{
"env"
:
funcGenEnv
,
"isotime"
:
funcGenIsotime
,
"pwd"
:
funcGenPwd
,
"template_
path"
:
funcGenTemplatePath
,
"timestamp"
:
funcGenTimestamp
,
"uuid"
:
funcGenUuid
,
"user"
:
funcGenUser
,
"env"
:
funcGenEnv
,
"isotime"
:
funcGenIsotime
,
"pwd"
:
funcGenPwd
,
"template_
dir"
:
funcGenTemplateDir
,
"timestamp"
:
funcGenTimestamp
,
"uuid"
:
funcGenUuid
,
"user"
:
funcGenUser
,
"upper"
:
funcGenPrimitive
(
strings
.
ToUpper
),
"lower"
:
funcGenPrimitive
(
strings
.
ToLower
),
...
...
@@ -94,13 +94,18 @@ func funcGenPwd(ctx *Context) interface{} {
}
}
func
funcGenTemplate
Path
(
ctx
*
Context
)
interface
{}
{
func
funcGenTemplate
Dir
(
ctx
*
Context
)
interface
{}
{
return
func
()
(
string
,
error
)
{
if
ctx
==
nil
||
ctx
.
TemplatePath
==
""
{
return
""
,
errors
.
New
(
"template path not available"
)
}
return
filepath
.
Dir
(
ctx
.
TemplatePath
),
nil
path
,
err
:=
filepath
.
Abs
(
filepath
.
Dir
(
ctx
.
TemplatePath
))
if
err
!=
nil
{
return
""
,
err
}
return
path
,
nil
}
}
...
...
template/interpolate/funcs_test.go
View file @
639e63fd
...
...
@@ -2,6 +2,7 @@ package interpolate
import
(
"os"
"path/filepath"
"strconv"
"testing"
"time"
...
...
@@ -117,18 +118,21 @@ func TestFuncPwd(t *testing.T) {
}
func
TestFuncTemplatePath
(
t
*
testing
.
T
)
{
path
:=
"foo/bar"
expected
,
_
:=
filepath
.
Abs
(
filepath
.
Dir
(
path
))
cases
:=
[]
struct
{
Input
string
Output
string
}{
{
`{{template_
path
}}`
,
`foo`
,
`{{template_
dir
}}`
,
expected
,
},
}
ctx
:=
&
Context
{
TemplatePath
:
"foo/bar"
,
TemplatePath
:
path
,
}
for
_
,
tc
:=
range
cases
{
i
:=
&
I
{
Value
:
tc
.
Input
}
...
...
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