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
47cd6df8
Commit
47cd6df8
authored
Aug 30, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Add Cancel method to Provisioner
parent
6312f680
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
84 additions
and
73 deletions
+84
-73
packer/build_test.go
packer/build_test.go
+9
-9
packer/environment_test.go
packer/environment_test.go
+2
-2
packer/plugin/plugin_test.go
packer/plugin/plugin_test.go
+1
-1
packer/plugin/provisioner.go
packer/plugin/provisioner.go
+9
-0
packer/plugin/provisioner_test.go
packer/plugin/provisioner_test.go
+0
-11
packer/provisioner.go
packer/provisioner.go
+5
-0
packer/provisioner_mock.go
packer/provisioner_mock.go
+27
-0
packer/provisioner_test.go
packer/provisioner_test.go
+4
-21
packer/rpc/provisioner.go
packer/rpc/provisioner.go
+13
-0
packer/rpc/provisioner_test.go
packer/rpc/provisioner_test.go
+12
-27
packer/template_test.go
packer/template_test.go
+2
-2
No files found.
packer/build_test.go
View file @
47cd6df8
...
...
@@ -16,7 +16,7 @@ func testBuild() *coreBuild {
"foo"
:
[]
Hook
{
&
MockHook
{}},
},
provisioners
:
[]
coreBuildProvisioner
{
coreBuildProvisioner
{
&
Test
Provisioner
{},
[]
interface
{}{
42
}},
coreBuildProvisioner
{
&
Mock
Provisioner
{},
[]
interface
{}{
42
}},
},
postProcessors
:
[][]
coreBuildPostProcessor
{
[]
coreBuildPostProcessor
{
...
...
@@ -59,9 +59,9 @@ func TestBuild_Prepare(t *testing.T) {
assert
.
Equal
(
builder
.
prepareConfig
,
[]
interface
{}{
42
,
packerConfig
},
"prepare config should be 42"
)
coreProv
:=
build
.
provisioners
[
0
]
prov
:=
coreProv
.
provisioner
.
(
*
Test
Provisioner
)
assert
.
True
(
prov
.
p
repCalled
,
"prepare should be called"
)
assert
.
Equal
(
prov
.
p
repConfigs
,
[]
interface
{}{
42
,
packerConfig
},
"prepare should be called with proper config"
)
prov
:=
coreProv
.
provisioner
.
(
*
Mock
Provisioner
)
assert
.
True
(
prov
.
P
repCalled
,
"prepare should be called"
)
assert
.
Equal
(
prov
.
P
repConfigs
,
[]
interface
{}{
42
,
packerConfig
},
"prepare should be called with proper config"
)
corePP
:=
build
.
postProcessors
[
0
][
0
]
pp
:=
corePP
.
processor
.
(
*
TestPostProcessor
)
...
...
@@ -104,9 +104,9 @@ func TestBuild_Prepare_Debug(t *testing.T) {
assert
.
Equal
(
builder
.
prepareConfig
,
[]
interface
{}{
42
,
packerConfig
},
"prepare config should be 42"
)
coreProv
:=
build
.
provisioners
[
0
]
prov
:=
coreProv
.
provisioner
.
(
*
Test
Provisioner
)
assert
.
True
(
prov
.
p
repCalled
,
"prepare should be called"
)
assert
.
Equal
(
prov
.
p
repConfigs
,
[]
interface
{}{
42
,
packerConfig
},
"prepare should be called with proper config"
)
prov
:=
coreProv
.
provisioner
.
(
*
Mock
Provisioner
)
assert
.
True
(
prov
.
P
repCalled
,
"prepare should be called"
)
assert
.
Equal
(
prov
.
P
repConfigs
,
[]
interface
{}{
42
,
packerConfig
},
"prepare should be called with proper config"
)
}
func
TestBuildPrepare_variables_default
(
t
*
testing
.
T
)
{
...
...
@@ -193,8 +193,8 @@ func TestBuild_Run(t *testing.T) {
// Verify provisioners run
dispatchHook
.
Run
(
HookProvision
,
nil
,
nil
,
42
)
prov
:=
build
.
provisioners
[
0
]
.
provisioner
.
(
*
Test
Provisioner
)
assert
.
True
(
prov
.
p
rovCalled
,
"provision should be called"
)
prov
:=
build
.
provisioners
[
0
]
.
provisioner
.
(
*
Mock
Provisioner
)
assert
.
True
(
prov
.
P
rovCalled
,
"provision should be called"
)
// Verify post-processor was run
pp
:=
build
.
postProcessors
[
0
][
0
]
.
processor
.
(
*
TestPostProcessor
)
...
...
packer/environment_test.go
View file @
47cd6df8
...
...
@@ -20,7 +20,7 @@ func init() {
func
testComponentFinder
()
*
ComponentFinder
{
builderFactory
:=
func
(
n
string
)
(
Builder
,
error
)
{
return
testBuilder
(),
nil
}
ppFactory
:=
func
(
n
string
)
(
PostProcessor
,
error
)
{
return
new
(
TestPostProcessor
),
nil
}
provFactory
:=
func
(
n
string
)
(
Provisioner
,
error
)
{
return
new
(
Test
Provisioner
),
nil
}
provFactory
:=
func
(
n
string
)
(
Provisioner
,
error
)
{
return
new
(
Mock
Provisioner
),
nil
}
return
&
ComponentFinder
{
Builder
:
builderFactory
,
PostProcessor
:
ppFactory
,
...
...
@@ -309,7 +309,7 @@ func TestEnvironment_PostProcessor_Error(t *testing.T) {
func
TestEnvironmentProvisioner
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
p
:=
&
Test
Provisioner
{}
p
:=
&
Mock
Provisioner
{}
ps
:=
make
(
map
[
string
]
Provisioner
)
ps
[
"foo"
]
=
p
...
...
packer/plugin/plugin_test.go
View file @
47cd6df8
...
...
@@ -64,7 +64,7 @@ func TestHelperProcess(*testing.T) {
case
"post-processor"
:
ServePostProcessor
(
new
(
helperPostProcessor
))
case
"provisioner"
:
ServeProvisioner
(
new
(
helper
Provisioner
))
ServeProvisioner
(
new
(
packer
.
Mock
Provisioner
))
case
"start-timeout"
:
time
.
Sleep
(
1
*
time
.
Minute
)
os
.
Exit
(
1
)
...
...
packer/plugin/provisioner.go
View file @
47cd6df8
...
...
@@ -28,6 +28,15 @@ func (c *cmdProvisioner) Provision(ui packer.Ui, comm packer.Communicator) error
return
c
.
p
.
Provision
(
ui
,
comm
)
}
func
(
c
*
cmdProvisioner
)
Cancel
()
{
defer
func
()
{
r
:=
recover
()
c
.
checkExit
(
r
,
nil
)
}()
c
.
p
.
Cancel
()
}
func
(
c
*
cmdProvisioner
)
checkExit
(
p
interface
{},
cb
func
())
{
if
c
.
client
.
Exited
()
&&
cb
!=
nil
{
cb
()
...
...
packer/plugin/provisioner_test.go
View file @
47cd6df8
package
plugin
import
(
"github.com/mitchellh/packer/packer"
"os/exec"
"testing"
)
type
helperProvisioner
byte
func
(
helperProvisioner
)
Prepare
(
...
interface
{})
error
{
return
nil
}
func
(
helperProvisioner
)
Provision
(
packer
.
Ui
,
packer
.
Communicator
)
error
{
return
nil
}
func
TestProvisioner_NoExist
(
t
*
testing
.
T
)
{
c
:=
NewClient
(
&
ClientConfig
{
Cmd
:
exec
.
Command
(
"i-should-not-exist"
)})
defer
c
.
Kill
()
...
...
packer/provisioner.go
View file @
47cd6df8
...
...
@@ -13,6 +13,11 @@ type Provisioner interface {
// is guaranteed to be connected to some machine so that provisioning
// can be done.
Provision
(
Ui
,
Communicator
)
error
// Cancel is called to cancel the provisioning. This is usually called
// while Provision is still being called. The Provisioner should act
// to stop its execution as quickly as possible in a race-free way.
Cancel
()
}
// A Hook implementation that runs the given provisioners.
...
...
packer/provisioner_mock.go
0 → 100644
View file @
47cd6df8
package
packer
// MockProvisioner is an implementation of Provisioner that can be
// used for tests.
type
MockProvisioner
struct
{
PrepCalled
bool
PrepConfigs
[]
interface
{}
ProvCalled
bool
ProvUi
Ui
CancelCalled
bool
}
func
(
t
*
MockProvisioner
)
Prepare
(
configs
...
interface
{})
error
{
t
.
PrepCalled
=
true
t
.
PrepConfigs
=
configs
return
nil
}
func
(
t
*
MockProvisioner
)
Provision
(
ui
Ui
,
comm
Communicator
)
error
{
t
.
ProvCalled
=
true
t
.
ProvUi
=
ui
return
nil
}
func
(
t
*
MockProvisioner
)
Cancel
()
{
t
.
CancelCalled
=
true
}
packer/provisioner_test.go
View file @
47cd6df8
...
...
@@ -2,23 +2,6 @@ package packer
import
"testing"
type
TestProvisioner
struct
{
prepCalled
bool
prepConfigs
[]
interface
{}
provCalled
bool
}
func
(
t
*
TestProvisioner
)
Prepare
(
configs
...
interface
{})
error
{
t
.
prepCalled
=
true
t
.
prepConfigs
=
configs
return
nil
}
func
(
t
*
TestProvisioner
)
Provision
(
Ui
,
Communicator
)
error
{
t
.
provCalled
=
true
return
nil
}
func
TestProvisionHook_Impl
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
&
ProvisionHook
{}
...
...
@@ -28,8 +11,8 @@ func TestProvisionHook_Impl(t *testing.T) {
}
func
TestProvisionHook
(
t
*
testing
.
T
)
{
pA
:=
&
Test
Provisioner
{}
pB
:=
&
Test
Provisioner
{}
pA
:=
&
Mock
Provisioner
{}
pB
:=
&
Mock
Provisioner
{}
ui
:=
testUi
()
var
comm
Communicator
=
nil
...
...
@@ -38,11 +21,11 @@ func TestProvisionHook(t *testing.T) {
hook
:=
&
ProvisionHook
{[]
Provisioner
{
pA
,
pB
}}
hook
.
Run
(
"foo"
,
ui
,
comm
,
data
)
if
!
pA
.
p
rovCalled
{
if
!
pA
.
P
rovCalled
{
t
.
Error
(
"provision should be called on pA"
)
}
if
!
pB
.
p
rovCalled
{
if
!
pB
.
P
rovCalled
{
t
.
Error
(
"provision should be called on pB"
)
}
}
...
...
packer/rpc/provisioner.go
View file @
47cd6df8
...
...
@@ -2,6 +2,7 @@ package rpc
import
(
"github.com/mitchellh/packer/packer"
"log"
"net/rpc"
)
...
...
@@ -47,6 +48,13 @@ func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return
p
.
client
.
Call
(
"Provisioner.Provision"
,
args
,
new
(
interface
{}))
}
func
(
p
*
provisioner
)
Cancel
()
{
err
:=
p
.
client
.
Call
(
"Provisioner.Cancel"
,
new
(
interface
{}),
new
(
interface
{}))
if
err
!=
nil
{
log
.
Printf
(
"Provisioner.Cancel err: %s"
,
err
)
}
}
func
(
p
*
ProvisionerServer
)
Prepare
(
args
*
ProvisionerPrepareArgs
,
reply
*
error
)
error
{
*
reply
=
p
.
p
.
Prepare
(
args
.
Configs
...
)
if
*
reply
!=
nil
{
...
...
@@ -71,3 +79,8 @@ func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *int
return
nil
}
func
(
p
*
ProvisionerServer
)
Cancel
(
args
*
interface
{},
reply
*
interface
{})
error
{
p
.
p
.
Cancel
()
return
nil
}
packer/rpc/provisioner_test.go
View file @
47cd6df8
...
...
@@ -7,32 +7,11 @@ import (
"testing"
)
type
testProvisioner
struct
{
prepareCalled
bool
prepareConfigs
[]
interface
{}
provCalled
bool
provComm
packer
.
Communicator
provUi
packer
.
Ui
}
func
(
p
*
testProvisioner
)
Prepare
(
configs
...
interface
{})
error
{
p
.
prepareCalled
=
true
p
.
prepareConfigs
=
configs
return
nil
}
func
(
p
*
testProvisioner
)
Provision
(
ui
packer
.
Ui
,
comm
packer
.
Communicator
)
error
{
p
.
provCalled
=
true
p
.
provComm
=
comm
p
.
provUi
=
ui
return
nil
}
func
TestProvisionerRPC
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
// Create the interface to test
p
:=
new
(
test
Provisioner
)
p
:=
new
(
packer
.
Mock
Provisioner
)
// Start the server
server
:=
rpc
.
NewServer
()
...
...
@@ -47,17 +26,23 @@ func TestProvisionerRPC(t *testing.T) {
config
:=
42
pClient
:=
Provisioner
(
client
)
pClient
.
Prepare
(
config
)
assert
.
True
(
p
.
prepare
Called
,
"prepare should be called"
)
assert
.
Equal
(
p
.
prepare
Configs
,
[]
interface
{}{
42
},
"prepare should be called with right arg"
)
assert
.
True
(
p
.
Prep
Called
,
"prepare should be called"
)
assert
.
Equal
(
p
.
Prep
Configs
,
[]
interface
{}{
42
},
"prepare should be called with right arg"
)
// Test Provision
ui
:=
&
testUi
{}
comm
:=
new
(
packer
.
MockCommunicator
)
comm
:=
&
packer
.
MockCommunicator
{}
pClient
.
Provision
(
ui
,
comm
)
assert
.
True
(
p
.
p
rovCalled
,
"provision should be called"
)
assert
.
True
(
p
.
P
rovCalled
,
"provision should be called"
)
p
.
p
rovUi
.
Say
(
"foo"
)
p
.
P
rovUi
.
Say
(
"foo"
)
assert
.
True
(
ui
.
sayCalled
,
"say should be called"
)
// Test Cancel
pClient
.
Cancel
()
if
!
p
.
CancelCalled
{
t
.
Fatal
(
"cancel should be called"
)
}
}
func
TestProvisioner_Implements
(
t
*
testing
.
T
)
{
...
...
packer/template_test.go
View file @
47cd6df8
...
...
@@ -589,7 +589,7 @@ func TestTemplate_Build(t *testing.T) {
"test-builder"
:
builder
,
}
provisioner
:=
&
Test
Provisioner
{}
provisioner
:=
&
Mock
Provisioner
{}
provisionerMap
:=
map
[
string
]
Provisioner
{
"test-prov"
:
provisioner
,
}
...
...
@@ -677,7 +677,7 @@ func TestTemplate_Build_ProvisionerOverride(t *testing.T) {
"test-builder"
:
builder
,
}
provisioner
:=
&
Test
Provisioner
{}
provisioner
:=
&
Mock
Provisioner
{}
provisionerMap
:=
map
[
string
]
Provisioner
{
"test-prov"
:
provisioner
,
}
...
...
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