Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Packer
Commits
8490bbc4
Commit
8490bbc4
authored
5 years ago
by
Megan Marsh
Browse files
Options
Download
Email Patches
Plain Diff
add tests for info sharing
parent
cd7abf1f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
packer/build.go
+1
-1
packer/build.go
packer/build_test.go
+30
-0
packer/build_test.go
packer/builder_mock.go
+3
-1
packer/builder_mock.go
template/interpolate/funcs_test.go
+85
-0
template/interpolate/funcs_test.go
with
119 additions
and
2 deletions
+119
-2
packer/build.go
+
1
-
1
View file @
8490bbc4
...
...
@@ -163,7 +163,7 @@ func (b *CoreBuild) Prepare() (warn []string, err error) {
generatedPlaceholderMap
:=
BasicPlaceholderData
()
if
generatedVars
!=
nil
{
for
_
,
k
:=
range
generatedVars
{
generatedPlaceholderMap
[
k
]
=
fmt
.
Sprintf
(
"
Generate
d_%s. "
+
generatedPlaceholderMap
[
k
]
=
fmt
.
Sprintf
(
"
Buil
d_%s. "
+
common
.
PlaceholderMsg
,
k
)
}
}
...
...
This diff is collapsed.
Click to expand it.
packer/build_test.go
+
30
-
0
View file @
8490bbc4
...
...
@@ -4,6 +4,8 @@ import (
"context"
"reflect"
"testing"
"github.com/hashicorp/packer/helper/common"
)
func
boolPointer
(
tf
bool
)
*
bool
{
...
...
@@ -176,6 +178,34 @@ func TestBuildPrepare_variables_default(t *testing.T) {
}
}
func
TestBuildPrepare_ProvisionerGetsGeneratedMap
(
t
*
testing
.
T
)
{
packerConfig
:=
testDefaultPackerConfig
()
build
:=
testBuild
()
builder
:=
build
.
Builder
.
(
*
MockBuilder
)
builder
.
GeneratedVars
=
[]
string
{
"PartyVar"
}
build
.
Prepare
()
if
!
builder
.
PrepareCalled
{
t
.
Fatalf
(
"should be called"
)
}
if
!
reflect
.
DeepEqual
(
builder
.
PrepareConfig
,
[]
interface
{}{
42
,
packerConfig
})
{
t
.
Fatalf
(
"bad: %#v"
,
builder
.
PrepareConfig
)
}
coreProv
:=
build
.
Provisioners
[
0
]
prov
:=
coreProv
.
Provisioner
.
(
*
MockProvisioner
)
if
!
prov
.
PrepCalled
{
t
.
Fatal
(
"prepare should be called"
)
}
generated
:=
BasicPlaceholderData
()
generated
[
"PartyVar"
]
=
"Build_PartyVar. "
+
common
.
PlaceholderMsg
if
!
reflect
.
DeepEqual
(
prov
.
PrepConfigs
,
[]
interface
{}{
42
,
packerConfig
,
generated
})
{
t
.
Fatalf
(
"bad: %#v"
,
prov
.
PrepConfigs
)
}
}
func
TestBuild_Run
(
t
*
testing
.
T
)
{
ui
:=
testUi
()
...
...
This diff is collapsed.
Click to expand it.
packer/builder_mock.go
+
3
-
1
View file @
8490bbc4
...
...
@@ -25,6 +25,8 @@ type MockBuilder struct {
RunUi
Ui
CancelCalled
bool
RunFn
func
(
ctx
context
.
Context
)
GeneratedVars
[]
string
}
func
(
tb
*
MockBuilder
)
ConfigSpec
()
hcldec
.
ObjectSpec
{
return
tb
.
FlatMapstructure
()
.
HCL2Spec
()
}
...
...
@@ -34,7 +36,7 @@ func (tb *MockBuilder) FlatConfig() interface{} { return tb.FlatMapstructure() }
func
(
tb
*
MockBuilder
)
Prepare
(
config
...
interface
{})
([]
string
,
[]
string
,
error
)
{
tb
.
PrepareCalled
=
true
tb
.
PrepareConfig
=
config
return
nil
,
tb
.
PrepareWarnings
,
nil
return
tb
.
GeneratedVars
,
tb
.
PrepareWarnings
,
nil
}
func
(
tb
*
MockBuilder
)
Run
(
ctx
context
.
Context
,
ui
Ui
,
h
Hook
)
(
Artifact
,
error
)
{
...
...
This diff is collapsed.
Click to expand it.
template/interpolate/funcs_test.go
+
85
-
0
View file @
8490bbc4
...
...
@@ -9,6 +9,7 @@ import (
"time"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/version"
)
...
...
@@ -318,6 +319,90 @@ func TestFuncUser(t *testing.T) {
}
}
func
TestFuncPackerBuild
(
t
*
testing
.
T
)
{
type
cases
struct
{
DataMap
interface
{}
ErrExpected
bool
Template
string
OutVal
string
}
testCases
:=
[]
cases
{
// Data map is empty; there should be an error.
{
DataMap
:
nil
,
ErrExpected
:
true
,
Template
:
"{{ build `PartyVar` }}"
,
OutVal
:
""
,
},
// Data map is a map[string]string and contains value
{
DataMap
:
map
[
string
]
string
{
"PartyVar"
:
"PartyVal"
},
ErrExpected
:
false
,
Template
:
"{{ build `PartyVar` }}"
,
OutVal
:
"PartyVal"
,
},
// Data map is a map[string]string and contains value
{
DataMap
:
map
[
string
]
string
{
"PartyVar"
:
"PartyVal"
},
ErrExpected
:
false
,
Template
:
"{{ build `PartyVar` }}"
,
OutVal
:
"PartyVal"
,
},
// Data map is a map[string]string and contains value with placeholder.
{
DataMap
:
map
[
string
]
string
{
"PartyVar"
:
"PartyVal"
+
common
.
PlaceholderMsg
},
ErrExpected
:
false
,
Template
:
"{{ build `PartyVar` }}"
,
OutVal
:
"{{.PartyVar}}"
,
},
// Data map is a map[interface{}]interface{} and contains value
{
DataMap
:
map
[
interface
{}]
interface
{}{
"PartyVar"
:
"PartyVal"
},
ErrExpected
:
false
,
Template
:
"{{ build `PartyVar` }}"
,
OutVal
:
"PartyVal"
,
},
// Data map is a map[interface{}]interface{} and contains value
{
DataMap
:
map
[
interface
{}]
interface
{}{
"PartyVar"
:
"PartyVal"
},
ErrExpected
:
false
,
Template
:
"{{ build `PartyVar` }}"
,
OutVal
:
"PartyVal"
,
},
// Data map is a map[interface{}]interface{} and contains value with placeholder.
{
DataMap
:
map
[
interface
{}]
interface
{}{
"PartyVar"
:
"PartyVal"
+
common
.
PlaceholderMsg
},
ErrExpected
:
false
,
Template
:
"{{ build `PartyVar` }}"
,
OutVal
:
"{{.PartyVar}}"
,
},
// Data map is a map[interface{}]interface{} and doesn't have value.
{
DataMap
:
map
[
interface
{}]
interface
{}{
"BadVar"
:
"PartyVal"
+
common
.
PlaceholderMsg
},
ErrExpected
:
true
,
Template
:
"{{ build `MissingVar` }}"
,
OutVal
:
""
,
},
}
for
_
,
tc
:=
range
testCases
{
ctx
:=
&
Context
{}
ctx
.
Data
=
tc
.
DataMap
i
:=
&
I
{
Value
:
tc
.
Template
}
result
,
err
:=
i
.
Render
(
ctx
)
if
(
err
!=
nil
)
!=
tc
.
ErrExpected
{
t
.
Fatalf
(
"Input: %s
\n\n
err: %s"
,
tc
.
Template
,
err
)
}
if
ok
:=
strings
.
Compare
(
result
,
tc
.
OutVal
);
ok
!=
0
{
t
.
Fatalf
(
"Expected input to include: %s
\n\n
Got: %s"
,
tc
.
OutVal
,
result
)
}
}
}
func
TestFuncPackerVersion
(
t
*
testing
.
T
)
{
template
:=
`{{packer_version}}`
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
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
Menu
Projects
Groups
Snippets
Help