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
26c7ac2d
Commit
26c7ac2d
authored
May 26, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: post-processors
parent
85e615bb
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
167 additions
and
57 deletions
+167
-57
packer/build_test.go
packer/build_test.go
+15
-15
packer/core.go
packer/core.go
+47
-6
packer/core_test.go
packer/core_test.go
+37
-1
packer/post_processor_mock.go
packer/post_processor_mock.go
+33
-0
packer/post_processor_test.go
packer/post_processor_test.go
+0
-24
packer/template_test.go
packer/template_test.go
+2
-2
packer/test-fixtures/build-pp.json
packer/test-fixtures/build-pp.json
+7
-0
packer/testing.go
packer/testing.go
+26
-9
No files found.
packer/build_test.go
View file @
26c7ac2d
...
...
@@ -19,7 +19,7 @@ func testBuild() *coreBuild {
},
postProcessors
:
[][]
coreBuildPostProcessor
{
[]
coreBuildPostProcessor
{
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp"
},
"testPP"
,
make
(
map
[
string
]
interface
{}),
true
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp"
},
"testPP"
,
make
(
map
[
string
]
interface
{}),
true
},
},
},
variables
:
make
(
map
[
string
]
string
),
...
...
@@ -66,12 +66,12 @@ func TestBuild_Prepare(t *testing.T) {
}
corePP
:=
build
.
postProcessors
[
0
][
0
]
pp
:=
corePP
.
processor
.
(
*
Test
PostProcessor
)
if
!
pp
.
config
Called
{
pp
:=
corePP
.
processor
.
(
*
Mock
PostProcessor
)
if
!
pp
.
Configure
Called
{
t
.
Fatal
(
"should be called"
)
}
if
!
reflect
.
DeepEqual
(
pp
.
configVal
,
[]
interface
{}{
make
(
map
[
string
]
interface
{}),
packerConfig
})
{
t
.
Fatalf
(
"bad: %#v"
,
pp
.
configVal
)
if
!
reflect
.
DeepEqual
(
pp
.
ConfigureConfigs
,
[]
interface
{}{
make
(
map
[
string
]
interface
{}),
packerConfig
})
{
t
.
Fatalf
(
"bad: %#v"
,
pp
.
ConfigureConfigs
)
}
}
...
...
@@ -208,8 +208,8 @@ func TestBuild_Run(t *testing.T) {
}
// Verify post-processor was run
pp
:=
build
.
postProcessors
[
0
][
0
]
.
processor
.
(
*
Test
PostProcessor
)
if
!
pp
.
pp
Called
{
pp
:=
build
.
postProcessors
[
0
][
0
]
.
processor
.
(
*
Mock
PostProcessor
)
if
!
pp
.
PostProcess
Called
{
t
.
Fatal
(
"should be called"
)
}
}
...
...
@@ -244,7 +244,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build
=
testBuild
()
build
.
postProcessors
=
[][]
coreBuildPostProcessor
{
[]
coreBuildPostProcessor
{
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
},
}
...
...
@@ -269,10 +269,10 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build
=
testBuild
()
build
.
postProcessors
=
[][]
coreBuildPostProcessor
{
[]
coreBuildPostProcessor
{
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp1"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp1"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
},
[]
coreBuildPostProcessor
{
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp2"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
true
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp2"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
true
},
},
}
...
...
@@ -297,12 +297,12 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build
=
testBuild
()
build
.
postProcessors
=
[][]
coreBuildPostProcessor
{
[]
coreBuildPostProcessor
{
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp1a"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp1b"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
true
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp1a"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp1b"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
true
},
},
[]
coreBuildPostProcessor
{
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp2a"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
coreBuildPostProcessor
{
&
TestPostProcessor
{
a
rtifactId
:
"pp2b"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp2a"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
coreBuildPostProcessor
{
&
MockPostProcessor
{
A
rtifactId
:
"pp2b"
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
},
},
}
...
...
@@ -328,7 +328,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build
.
postProcessors
=
[][]
coreBuildPostProcessor
{
[]
coreBuildPostProcessor
{
coreBuildPostProcessor
{
&
TestPostProcessor
{
artifactId
:
"pp"
,
k
eep
:
true
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
,
&
MockPostProcessor
{
ArtifactId
:
"pp"
,
K
eep
:
true
},
"pp"
,
make
(
map
[
string
]
interface
{}),
false
,
},
},
}
...
...
packer/core.go
View file @
26c7ac2d
...
...
@@ -161,13 +161,54 @@ func (c *Core) Build(n string) (Build, error) {
})
}
// Setup the post-processors
postProcessors
:=
make
([][]
coreBuildPostProcessor
,
0
,
len
(
c
.
template
.
PostProcessors
))
for
_
,
rawPs
:=
range
c
.
template
.
PostProcessors
{
current
:=
make
([]
coreBuildPostProcessor
,
0
,
len
(
rawPs
))
for
_
,
rawP
:=
range
rawPs
{
// If we skip, ignore
if
rawP
.
Skip
(
rawName
)
{
continue
}
// Get the post-processor
postProcessor
,
err
:=
c
.
components
.
PostProcessor
(
rawP
.
Type
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error initializing post-processor '%s': %s"
,
rawP
.
Type
,
err
)
}
if
postProcessor
==
nil
{
return
nil
,
fmt
.
Errorf
(
"post-processor type not found: %s"
,
rawP
.
Type
)
}
current
=
append
(
current
,
coreBuildPostProcessor
{
processor
:
postProcessor
,
processorType
:
rawP
.
Type
,
config
:
rawP
.
Config
,
keepInputArtifact
:
rawP
.
KeepInputArtifact
,
})
}
// If we have no post-processors in this chain, just continue.
if
len
(
current
)
==
0
{
continue
}
postProcessors
=
append
(
postProcessors
,
current
)
}
// TODO hooks one day
return
&
coreBuild
{
name
:
n
,
builder
:
builder
,
builderConfig
:
configBuilder
.
Config
,
builderType
:
configBuilder
.
Type
,
provisioners
:
provisioners
,
variables
:
c
.
variables
,
name
:
n
,
builder
:
builder
,
builderConfig
:
configBuilder
.
Config
,
builderType
:
configBuilder
.
Type
,
postProcessors
:
postProcessors
,
provisioners
:
provisioners
,
variables
:
c
.
variables
,
},
nil
}
...
...
packer/core_test.go
View file @
26c7ac2d
...
...
@@ -222,6 +222,42 @@ func TestCoreBuild_provSkipInclude(t *testing.T) {
}
}
func
TestCoreBuild_postProcess
(
t
*
testing
.
T
)
{
config
:=
TestCoreConfig
(
t
)
testCoreTemplate
(
t
,
config
,
fixtureDir
(
"build-pp.json"
))
b
:=
TestBuilder
(
t
,
config
,
"test"
)
p
:=
TestPostProcessor
(
t
,
config
,
"test"
)
core
:=
TestCore
(
t
,
config
)
ui
:=
TestUi
(
t
)
b
.
ArtifactId
=
"hello"
p
.
ArtifactId
=
"goodbye"
build
,
err
:=
core
.
Build
(
"test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
_
,
err
:=
build
.
Prepare
();
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
artifact
,
err
:=
build
.
Run
(
ui
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
len
(
artifact
)
!=
1
{
t
.
Fatalf
(
"bad: %#v"
,
artifact
)
}
if
artifact
[
0
]
.
Id
()
!=
p
.
ArtifactId
{
t
.
Fatalf
(
"bad: %s"
,
artifact
[
0
]
.
Id
())
}
if
p
.
PostProcessArtifact
.
Id
()
!=
b
.
ArtifactId
{
t
.
Fatalf
(
"bad: %s"
,
p
.
PostProcessArtifact
.
Id
())
}
}
func
TestCoreValidate
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
File
string
...
...
@@ -276,7 +312,7 @@ func TestCoreValidate(t *testing.T) {
func
testComponentFinder
()
*
ComponentFinder
{
builderFactory
:=
func
(
n
string
)
(
Builder
,
error
)
{
return
new
(
MockBuilder
),
nil
}
ppFactory
:=
func
(
n
string
)
(
PostProcessor
,
error
)
{
return
new
(
Test
PostProcessor
),
nil
}
ppFactory
:=
func
(
n
string
)
(
PostProcessor
,
error
)
{
return
new
(
Mock
PostProcessor
),
nil
}
provFactory
:=
func
(
n
string
)
(
Provisioner
,
error
)
{
return
new
(
MockProvisioner
),
nil
}
return
&
ComponentFinder
{
Builder
:
builderFactory
,
...
...
packer/post_processor_mock.go
0 → 100644
View file @
26c7ac2d
package
packer
// MockPostProcessor is an implementation of PostProcessor that can be
// used for tests.
type
MockPostProcessor
struct
{
ArtifactId
string
Keep
bool
Error
error
ConfigureCalled
bool
ConfigureConfigs
[]
interface
{}
ConfigureError
error
PostProcessCalled
bool
PostProcessArtifact
Artifact
PostProcessUi
Ui
}
func
(
t
*
MockPostProcessor
)
Configure
(
configs
...
interface
{})
error
{
t
.
ConfigureCalled
=
true
t
.
ConfigureConfigs
=
configs
return
t
.
ConfigureError
}
func
(
t
*
MockPostProcessor
)
PostProcess
(
ui
Ui
,
a
Artifact
)
(
Artifact
,
bool
,
error
)
{
t
.
PostProcessCalled
=
true
t
.
PostProcessArtifact
=
a
t
.
PostProcessUi
=
ui
return
&
MockArtifact
{
IdValue
:
t
.
ArtifactId
,
},
t
.
Keep
,
t
.
Error
}
packer/post_processor_test.go
deleted
100644 → 0
View file @
85e615bb
package
packer
type
TestPostProcessor
struct
{
artifactId
string
keep
bool
configCalled
bool
configVal
[]
interface
{}
ppCalled
bool
ppArtifact
Artifact
ppUi
Ui
}
func
(
pp
*
TestPostProcessor
)
Configure
(
v
...
interface
{})
error
{
pp
.
configCalled
=
true
pp
.
configVal
=
v
return
nil
}
func
(
pp
*
TestPostProcessor
)
PostProcess
(
ui
Ui
,
a
Artifact
)
(
Artifact
,
bool
,
error
)
{
pp
.
ppCalled
=
true
pp
.
ppArtifact
=
a
pp
.
ppUi
=
ui
return
&
TestArtifact
{
id
:
pp
.
artifactId
},
pp
.
keep
,
nil
}
packer/template_test.go
View file @
26c7ac2d
...
...
@@ -11,7 +11,7 @@ import (
func
testTemplateComponentFinder
()
*
ComponentFinder
{
builder
:=
new
(
MockBuilder
)
pp
:=
new
(
Test
PostProcessor
)
pp
:=
new
(
Mock
PostProcessor
)
provisioner
:=
&
MockProvisioner
{}
builderMap
:=
map
[
string
]
Builder
{
...
...
@@ -1018,7 +1018,7 @@ func TestTemplate_Build(t *testing.T) {
"test-prov"
:
provisioner
,
}
pp
:=
new
(
Test
PostProcessor
)
pp
:=
new
(
Mock
PostProcessor
)
ppMap
:=
map
[
string
]
PostProcessor
{
"simple"
:
pp
,
}
...
...
packer/test-fixtures/build-pp.json
0 → 100644
View file @
26c7ac2d
{
"builders"
:
[{
"type"
:
"test"
}],
"post-processors"
:
[
"test"
]
}
packer/testing.go
View file @
26c7ac2d
...
...
@@ -8,14 +8,6 @@ import (
)
func
TestCoreConfig
(
t
*
testing
.
T
)
*
CoreConfig
{
// Create a UI that is effectively /dev/null everywhere
var
buf
bytes
.
Buffer
ui
:=
&
BasicUi
{
Reader
:
&
buf
,
Writer
:
ioutil
.
Discard
,
ErrorWriter
:
ioutil
.
Discard
,
}
// Create some test components
components
:=
ComponentFinder
{
Builder
:
func
(
n
string
)
(
Builder
,
error
)
{
...
...
@@ -30,7 +22,7 @@ func TestCoreConfig(t *testing.T) *CoreConfig {
return
&
CoreConfig
{
Cache
:
&
FileCache
{
CacheDir
:
os
.
TempDir
()},
Components
:
components
,
Ui
:
ui
,
Ui
:
TestUi
(
t
)
,
}
}
...
...
@@ -43,6 +35,15 @@ func TestCore(t *testing.T, c *CoreConfig) *Core {
return
core
}
func
TestUi
(
t
*
testing
.
T
)
Ui
{
var
buf
bytes
.
Buffer
return
&
BasicUi
{
Reader
:
&
buf
,
Writer
:
ioutil
.
Discard
,
ErrorWriter
:
ioutil
.
Discard
,
}
}
// TestBuilder sets the builder with the name n to the component finder
// and returns the mock.
func
TestBuilder
(
t
*
testing
.
T
,
c
*
CoreConfig
,
n
string
)
*
MockBuilder
{
...
...
@@ -74,3 +75,19 @@ func TestProvisioner(t *testing.T, c *CoreConfig, n string) *MockProvisioner {
return
&
b
}
// TestPostProcessor sets the prov. with the name n to the component finder
// and returns the mock.
func
TestPostProcessor
(
t
*
testing
.
T
,
c
*
CoreConfig
,
n
string
)
*
MockPostProcessor
{
var
b
MockPostProcessor
c
.
Components
.
PostProcessor
=
func
(
actual
string
)
(
PostProcessor
,
error
)
{
if
actual
!=
n
{
return
nil
,
nil
}
return
&
b
,
nil
}
return
&
b
}
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