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
e0a9215e
Commit
e0a9215e
authored
May 28, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: test for environment variables interpolation
parent
913d6f69
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
2 deletions
+82
-2
packer/core.go
packer/core.go
+39
-2
packer/core_test.go
packer/core_test.go
+33
-0
packer/test-fixtures/build-env.json
packer/test-fixtures/build-env.json
+10
-0
No files found.
packer/core.go
View file @
e0a9215e
...
...
@@ -66,12 +66,17 @@ func NewCore(c *CoreConfig) (*Core, error) {
builds
[
v
]
=
b
}
re
turn
&
Core
{
re
sult
:=
&
Core
{
components
:
c
.
Components
,
template
:
c
.
Template
,
variables
:
c
.
Variables
,
builds
:
builds
,
},
nil
}
if
err
:=
result
.
init
();
err
!=
nil
{
return
nil
,
err
}
return
result
,
nil
}
// BuildNames returns the builds that are available in this configured core.
...
...
@@ -228,3 +233,35 @@ func (c *Core) Validate() error {
return
err
}
func
(
c
*
Core
)
init
()
error
{
if
c
.
variables
==
nil
{
c
.
variables
=
make
(
map
[
string
]
string
)
}
// Go through the variables and interpolate the environment variables
ctx
:=
&
interpolate
.
Context
{
EnableEnv
:
true
}
for
k
,
v
:=
range
c
.
template
.
Variables
{
// Ignore variables that are required
if
v
.
Required
{
continue
}
// Ignore variables that have a value
if
_
,
ok
:=
c
.
variables
[
k
];
ok
{
continue
}
// Interpolate the default
def
,
err
:=
interpolate
.
Render
(
v
.
Default
,
ctx
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"error interpolating default value for '%s': %s"
,
k
,
err
)
}
c
.
variables
[
k
]
=
def
}
return
nil
}
packer/core_test.go
View file @
e0a9215e
...
...
@@ -5,6 +5,7 @@ import (
"reflect"
"testing"
configHelper
"github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/template"
)
...
...
@@ -108,6 +109,38 @@ func TestCoreBuild_basicInterpolated(t *testing.T) {
}
}
func
TestCoreBuild_env
(
t
*
testing
.
T
)
{
os
.
Setenv
(
"PACKER_TEST_ENV"
,
"test"
)
defer
os
.
Setenv
(
"PACKER_TEST_ENV"
,
""
)
config
:=
TestCoreConfig
(
t
)
testCoreTemplate
(
t
,
config
,
fixtureDir
(
"build-env.json"
))
b
:=
TestBuilder
(
t
,
config
,
"test"
)
core
:=
TestCore
(
t
,
config
)
b
.
ArtifactId
=
"hello"
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"
]
!=
"test"
{
t
.
Fatalf
(
"bad: %#v"
,
result
)
}
}
func
TestCoreBuild_nonExist
(
t
*
testing
.
T
)
{
config
:=
TestCoreConfig
(
t
)
testCoreTemplate
(
t
,
config
,
fixtureDir
(
"build-basic.json"
))
...
...
packer/test-fixtures/build-env.json
0 → 100644
View file @
e0a9215e
{
"variables"
:
{
"var"
:
"{{env `PACKER_TEST_ENV`}}"
},
"builders"
:
[{
"type"
:
"test"
,
"value"
:
"{{user `var`}}"
}]
}
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