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
80e8e09e
Commit
80e8e09e
authored
Aug 30, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Add Cancel() method to hook
parent
d5c6b9fa
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
66 additions
and
49 deletions
+66
-49
packer/build_test.go
packer/build_test.go
+4
-4
packer/environment_test.go
packer/environment_test.go
+1
-1
packer/hook.go
packer/hook.go
+8
-0
packer/hook_mock.go
packer/hook_mock.go
+24
-0
packer/hook_test.go
packer/hook_test.go
+4
-21
packer/plugin/hook.go
packer/plugin/hook.go
+10
-1
packer/plugin/hook_test.go
packer/plugin/hook_test.go
+0
-7
packer/plugin/plugin_test.go
packer/plugin/plugin_test.go
+2
-1
packer/provisioner.go
packer/provisioner.go
+5
-0
packer/rpc/builder_test.go
packer/rpc/builder_test.go
+2
-2
packer/rpc/hook.go
packer/rpc/hook.go
+4
-0
packer/rpc/hook_test.go
packer/rpc/hook_test.go
+2
-12
No files found.
packer/build_test.go
View file @
80e8e09e
...
...
@@ -13,7 +13,7 @@ func testBuild() *coreBuild {
builderConfig
:
42
,
builderType
:
"foo"
,
hooks
:
map
[
string
][]
Hook
{
"foo"
:
[]
Hook
{
&
Test
Hook
{}},
"foo"
:
[]
Hook
{
&
Mock
Hook
{}},
},
provisioners
:
[]
coreBuildProvisioner
{
coreBuildProvisioner
{
&
TestProvisioner
{},
[]
interface
{}{
42
}},
...
...
@@ -187,9 +187,9 @@ func TestBuild_Run(t *testing.T) {
dispatchHook
:=
builder
.
runHook
dispatchHook
.
Run
(
"foo"
,
nil
,
nil
,
42
)
hook
:=
build
.
hooks
[
"foo"
][
0
]
.
(
*
Test
Hook
)
assert
.
True
(
hook
.
r
unCalled
,
"run should be called"
)
assert
.
Equal
(
hook
.
r
unData
,
42
,
"should have correct data"
)
hook
:=
build
.
hooks
[
"foo"
][
0
]
.
(
*
Mock
Hook
)
assert
.
True
(
hook
.
R
unCalled
,
"run should be called"
)
assert
.
Equal
(
hook
.
R
unData
,
42
,
"should have correct data"
)
// Verify provisioners run
dispatchHook
.
Run
(
HookProvision
,
nil
,
nil
,
42
)
...
...
packer/environment_test.go
View file @
80e8e09e
...
...
@@ -227,7 +227,7 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) {
func
TestEnvironment_Hook
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
hook
:=
&
Test
Hook
{}
hook
:=
&
Mock
Hook
{}
hooks
:=
make
(
map
[
string
]
Hook
)
hooks
[
"foo"
]
=
hook
...
...
packer/hook.go
View file @
80e8e09e
...
...
@@ -11,8 +11,14 @@ const HookProvision = "packer_provision"
// you must reference the documentation for the specific hook you're interested
// in. In addition to that, the Hook is given access to a UI so that it can
// output things to the user.
//
// Cancel is called when the hook needs to be cancelled. This will usually
// be called when Run is still in progress so the mechanism that handles this
// must be race-free. Cancel should attempt to cancel the hook in the
// quickest, safest way possible.
type
Hook
interface
{
Run
(
string
,
Ui
,
Communicator
,
interface
{})
error
Cancel
()
}
// A Hook implementation that dispatches based on an internal mapping.
...
...
@@ -38,3 +44,5 @@ func (h *DispatchHook) Run(name string, ui Ui, comm Communicator, data interface
return
nil
}
func
(
h
*
DispatchHook
)
Cancel
()
{}
packer/hook_mock.go
0 → 100644
View file @
80e8e09e
package
packer
// MockHook is an implementation of Hook that can be used for tests.
type
MockHook
struct
{
RunCalled
bool
RunComm
Communicator
RunData
interface
{}
RunName
string
RunUi
Ui
CancelCalled
bool
}
func
(
t
*
MockHook
)
Run
(
name
string
,
ui
Ui
,
comm
Communicator
,
data
interface
{})
error
{
t
.
RunCalled
=
true
t
.
RunComm
=
comm
t
.
RunData
=
data
t
.
RunName
=
name
t
.
RunUi
=
ui
return
nil
}
func
(
t
*
MockHook
)
Cancel
()
{
t
.
CancelCalled
=
true
}
packer/hook_test.go
View file @
80e8e09e
...
...
@@ -5,23 +5,6 @@ import (
"testing"
)
type
TestHook
struct
{
runCalled
bool
runComm
Communicator
runData
interface
{}
runName
string
runUi
Ui
}
func
(
t
*
TestHook
)
Run
(
name
string
,
ui
Ui
,
comm
Communicator
,
data
interface
{})
error
{
t
.
runCalled
=
true
t
.
runComm
=
comm
t
.
runData
=
data
t
.
runName
=
name
t
.
runUi
=
ui
return
nil
}
func
TestDispatchHook_Implements
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
...
...
@@ -40,14 +23,14 @@ func TestDispatchHook_Run_NoHooks(t *testing.T) {
func
TestDispatchHook_Run
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
hook
:=
&
Test
Hook
{}
hook
:=
&
Mock
Hook
{}
mapping
:=
make
(
map
[
string
][]
Hook
)
mapping
[
"foo"
]
=
[]
Hook
{
hook
}
dh
:=
&
DispatchHook
{
mapping
}
dh
.
Run
(
"foo"
,
nil
,
nil
,
42
)
assert
.
True
(
hook
.
r
unCalled
,
"run should be called"
)
assert
.
Equal
(
hook
.
r
unName
,
"foo"
,
"should be proper event"
)
assert
.
Equal
(
hook
.
r
unData
,
42
,
"should be correct data"
)
assert
.
True
(
hook
.
R
unCalled
,
"run should be called"
)
assert
.
Equal
(
hook
.
R
unName
,
"foo"
,
"should be proper event"
)
assert
.
Equal
(
hook
.
R
unData
,
42
,
"should be correct data"
)
}
packer/plugin/hook.go
View file @
80e8e09e
...
...
@@ -19,8 +19,17 @@ func (c *cmdHook) Run(name string, ui packer.Ui, comm packer.Communicator, data
return
c
.
hook
.
Run
(
name
,
ui
,
comm
,
data
)
}
func
(
c
*
cmdHook
)
Cancel
()
{
defer
func
()
{
r
:=
recover
()
c
.
checkExit
(
r
,
nil
)
}()
c
.
hook
.
Cancel
()
}
func
(
c
*
cmdHook
)
checkExit
(
p
interface
{},
cb
func
())
{
if
c
.
client
.
Exited
()
{
if
c
.
client
.
Exited
()
&&
cb
!=
nil
{
cb
()
}
else
if
p
!=
nil
&&
!
Killed
{
log
.
Panic
(
p
)
...
...
packer/plugin/hook_test.go
View file @
80e8e09e
package
plugin
import
(
"github.com/mitchellh/packer/packer"
"os/exec"
"testing"
)
type
helperHook
byte
func
(
helperHook
)
Run
(
string
,
packer
.
Ui
,
packer
.
Communicator
,
interface
{})
error
{
return
nil
}
func
TestHook_NoExist
(
t
*
testing
.
T
)
{
c
:=
NewClient
(
&
ClientConfig
{
Cmd
:
exec
.
Command
(
"i-should-not-exist"
)})
defer
c
.
Kill
()
...
...
packer/plugin/plugin_test.go
View file @
80e8e09e
...
...
@@ -2,6 +2,7 @@ package plugin
import
(
"fmt"
"github.com/mitchellh/packer/packer"
"log"
"os"
"os/exec"
...
...
@@ -54,7 +55,7 @@ func TestHelperProcess(*testing.T) {
case
"command"
:
ServeCommand
(
new
(
helperCommand
))
case
"hook"
:
ServeHook
(
new
(
helper
Hook
))
ServeHook
(
new
(
packer
.
Mock
Hook
))
case
"invalid-rpc-address"
:
fmt
.
Println
(
"lolinvalid"
)
case
"mock"
:
...
...
packer/provisioner.go
View file @
80e8e09e
...
...
@@ -32,3 +32,8 @@ func (h *ProvisionHook) Run(name string, ui Ui, comm Communicator, data interfac
return
nil
}
// Cancels the privisioners that are still running.
func
(
h
*
ProvisionHook
)
Cancel
()
{
// TODO(mitchellh): implement
}
packer/rpc/builder_test.go
View file @
80e8e09e
...
...
@@ -72,7 +72,7 @@ func TestBuilderRPC(t *testing.T) {
// Test Run
cache
:=
new
(
testCache
)
hook
:=
&
test
Hook
{}
hook
:=
&
packer
.
Mock
Hook
{}
ui
:=
&
testUi
{}
artifact
,
err
:=
bClient
.
Run
(
ui
,
hook
,
cache
)
assert
.
Nil
(
err
,
"should have no error"
)
...
...
@@ -83,7 +83,7 @@ func TestBuilderRPC(t *testing.T) {
assert
.
True
(
cache
.
lockCalled
,
"lock should be called"
)
b
.
runHook
.
Run
(
"foo"
,
nil
,
nil
,
nil
)
assert
.
True
(
hook
.
r
unCalled
,
"run should be called"
)
assert
.
True
(
hook
.
R
unCalled
,
"run should be called"
)
b
.
runUi
.
Say
(
"format"
)
assert
.
True
(
ui
.
sayCalled
,
"say should be called"
)
...
...
packer/rpc/hook.go
View file @
80e8e09e
...
...
@@ -37,6 +37,10 @@ func (h *hook) Run(name string, ui packer.Ui, comm packer.Communicator, data int
return
h
.
client
.
Call
(
"Hook.Run"
,
args
,
new
(
interface
{}))
}
func
(
h
*
hook
)
Cancel
()
{
// TODO(mitchellh): implement
}
func
(
h
*
HookServer
)
Run
(
args
*
HookRunArgs
,
reply
*
interface
{})
error
{
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
args
.
RPCAddress
)
if
err
!=
nil
{
...
...
packer/rpc/hook_test.go
View file @
80e8e09e
...
...
@@ -7,21 +7,11 @@ import (
"testing"
)
type
testHook
struct
{
runCalled
bool
runUi
packer
.
Ui
}
func
(
h
*
testHook
)
Run
(
name
string
,
ui
packer
.
Ui
,
comm
packer
.
Communicator
,
data
interface
{})
error
{
h
.
runCalled
=
true
return
nil
}
func
TestHookRPC
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
// Create the UI to test
h
:=
new
(
test
Hook
)
h
:=
new
(
packer
.
Mock
Hook
)
// Serve
server
:=
rpc
.
NewServer
()
...
...
@@ -37,7 +27,7 @@ func TestHookRPC(t *testing.T) {
// Test Run
ui
:=
&
testUi
{}
hClient
.
Run
(
"foo"
,
ui
,
nil
,
42
)
assert
.
True
(
h
.
r
unCalled
,
"run should be called"
)
assert
.
True
(
h
.
R
unCalled
,
"run should be called"
)
}
func
TestHook_Implements
(
t
*
testing
.
T
)
{
...
...
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