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
230cc973
Commit
230cc973
authored
Nov 02, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/rpc: use packer.MockBuilder for tests
parent
0b61e506
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
81 deletions
+76
-81
packer/builder_mock.go
packer/builder_mock.go
+15
-0
packer/rpc/builder_test.go
packer/rpc/builder_test.go
+59
-79
packer/rpc/environment_test.go
packer/rpc/environment_test.go
+2
-2
No files found.
packer/builder_mock.go
View file @
230cc973
package
packer
package
packer
import
(
"errors"
)
// MockBuilder is an implementation of Builder that can be used for tests.
// MockBuilder is an implementation of Builder that can be used for tests.
// You can set some fake return values and you can keep track of what
// You can set some fake return values and you can keep track of what
// methods were called on the builder. It is fairly basic.
// methods were called on the builder. It is fairly basic.
type
MockBuilder
struct
{
type
MockBuilder
struct
{
ArtifactId
string
ArtifactId
string
PrepareWarnings
[]
string
PrepareWarnings
[]
string
RunErrResult
bool
RunNilResult
bool
PrepareCalled
bool
PrepareCalled
bool
PrepareConfig
[]
interface
{}
PrepareConfig
[]
interface
{}
...
@@ -27,6 +33,15 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) {
...
@@ -27,6 +33,15 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) {
tb
.
RunHook
=
h
tb
.
RunHook
=
h
tb
.
RunUi
=
ui
tb
.
RunUi
=
ui
tb
.
RunCache
=
c
tb
.
RunCache
=
c
if
tb
.
RunErrResult
{
return
nil
,
errors
.
New
(
"foo"
)
}
if
tb
.
RunNilResult
{
return
nil
,
nil
}
return
&
MockArtifact
{
return
&
MockArtifact
{
IdValue
:
tb
.
ArtifactId
,
IdValue
:
tb
.
ArtifactId
,
},
nil
},
nil
...
...
packer/rpc/builder_test.go
View file @
230cc973
package
rpc
package
rpc
import
(
import
(
"errors"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer"
"net/rpc"
"net/rpc"
"reflect"
"reflect"
...
@@ -10,47 +9,8 @@ import (
...
@@ -10,47 +9,8 @@ import (
var
testBuilderArtifact
=
&
testArtifact
{}
var
testBuilderArtifact
=
&
testArtifact
{}
type
testBuilder
struct
{
func
builderRPCClient
(
t
*
testing
.
T
)
(
*
packer
.
MockBuilder
,
packer
.
Builder
)
{
prepareCalled
bool
b
:=
new
(
packer
.
MockBuilder
)
prepareConfig
[]
interface
{}
runCalled
bool
runCache
packer
.
Cache
runHook
packer
.
Hook
runUi
packer
.
Ui
cancelCalled
bool
errRunResult
bool
nilRunResult
bool
}
func
(
b
*
testBuilder
)
Prepare
(
config
...
interface
{})
([]
string
,
error
)
{
b
.
prepareCalled
=
true
b
.
prepareConfig
=
config
return
nil
,
nil
}
func
(
b
*
testBuilder
)
Run
(
ui
packer
.
Ui
,
hook
packer
.
Hook
,
cache
packer
.
Cache
)
(
packer
.
Artifact
,
error
)
{
b
.
runCache
=
cache
b
.
runCalled
=
true
b
.
runHook
=
hook
b
.
runUi
=
ui
if
b
.
errRunResult
{
return
nil
,
errors
.
New
(
"foo"
)
}
else
if
b
.
nilRunResult
{
return
nil
,
nil
}
else
{
return
testBuilderArtifact
,
nil
}
}
func
(
b
*
testBuilder
)
Cancel
()
{
b
.
cancelCalled
=
true
}
func
TestBuilderRPC
(
t
*
testing
.
T
)
{
// Create the interface to test
b
:=
new
(
testBuilder
)
// Start the server
// Start the server
server
:=
rpc
.
NewServer
()
server
:=
rpc
.
NewServer
()
...
@@ -62,18 +22,26 @@ func TestBuilderRPC(t *testing.T) {
...
@@ -62,18 +22,26 @@ func TestBuilderRPC(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
t
.
Fatalf
(
"err: %s"
,
err
)
}
}
return
b
,
Builder
(
client
)
}
func
TestBuilderPrepare
(
t
*
testing
.
T
)
{
b
,
bClient
:=
builderRPCClient
(
t
)
// Test Prepare
// Test Prepare
config
:=
42
config
:=
42
bClient
:=
Builder
(
client
)
bClient
.
Prepare
(
config
)
bClient
.
Prepare
(
config
)
if
!
b
.
p
repareCalled
{
if
!
b
.
P
repareCalled
{
t
.
Fatal
(
"should be called"
)
t
.
Fatal
(
"should be called"
)
}
}
if
!
reflect
.
DeepEqual
(
b
.
p
repareConfig
,
[]
interface
{}{
42
})
{
if
!
reflect
.
DeepEqual
(
b
.
P
repareConfig
,
[]
interface
{}{
42
})
{
t
.
Fatalf
(
"bad: %#v"
,
b
.
p
repareConfig
)
t
.
Fatalf
(
"bad: %#v"
,
b
.
P
repareConfig
)
}
}
}
func
TestBuilderRun
(
t
*
testing
.
T
)
{
b
,
bClient
:=
builderRPCClient
(
t
)
// Test Run
// Test Run
cache
:=
new
(
testCache
)
cache
:=
new
(
testCache
)
...
@@ -84,22 +52,21 @@ func TestBuilderRPC(t *testing.T) {
...
@@ -84,22 +52,21 @@ func TestBuilderRPC(t *testing.T) {
t
.
Fatalf
(
"err: %s"
,
err
)
t
.
Fatalf
(
"err: %s"
,
err
)
}
}
if
!
b
.
r
unCalled
{
if
!
b
.
R
unCalled
{
t
.
Fatal
(
"run should be called"
)
t
.
Fatal
(
"run should be called"
)
}
}
if
b
.
runCalled
{
b
.
RunCache
.
Lock
(
"foo"
)
b
.
runCache
.
Lock
(
"foo"
)
if
!
cache
.
lockCalled
{
if
!
cache
.
lockCalled
{
t
.
Fatal
(
"should be called"
)
t
.
Fatal
(
"should be called"
)
}
}
b
.
r
unHook
.
Run
(
"foo"
,
nil
,
nil
,
nil
)
b
.
R
unHook
.
Run
(
"foo"
,
nil
,
nil
,
nil
)
if
!
hook
.
RunCalled
{
if
!
hook
.
RunCalled
{
t
.
Fatal
(
"should be called"
)
t
.
Fatal
(
"should be called"
)
}
}
b
.
r
unUi
.
Say
(
"format"
)
b
.
R
unUi
.
Say
(
"format"
)
if
!
ui
.
sayCalled
{
if
!
ui
.
sayCalled
{
t
.
Fatal
(
"say should be called"
)
t
.
Fatal
(
"say should be called"
)
}
}
...
@@ -111,32 +78,45 @@ func TestBuilderRPC(t *testing.T) {
...
@@ -111,32 +78,45 @@ func TestBuilderRPC(t *testing.T) {
if
artifact
.
Id
()
!=
testBuilderArtifact
.
Id
()
{
if
artifact
.
Id
()
!=
testBuilderArtifact
.
Id
()
{
t
.
Fatalf
(
"bad: %s"
,
artifact
.
Id
())
t
.
Fatalf
(
"bad: %s"
,
artifact
.
Id
())
}
}
}
}
func
TestBuilderRun_nilResult
(
t
*
testing
.
T
)
{
b
,
bClient
:=
builderRPCClient
(
t
)
b
.
RunNilResult
=
true
// Test run with nil result
cache
:=
new
(
testCache
)
b
.
nilRunResult
=
true
hook
:=
&
packer
.
MockHook
{}
artifact
,
err
=
bClient
.
Run
(
ui
,
hook
,
cache
)
ui
:=
&
testUi
{}
artifact
,
err
:=
bClient
.
Run
(
ui
,
hook
,
cache
)
if
artifact
!=
nil
{
if
artifact
!=
nil
{
t
.
Fatalf
(
"bad: %#v"
,
artifact
)
t
.
Fatalf
(
"bad: %#v"
,
artifact
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"bad: %#v"
,
err
)
t
.
Fatalf
(
"bad: %#v"
,
err
)
}
}
}
// Test with an error
func
TestBuilderRun_ErrResult
(
t
*
testing
.
T
)
{
b
.
errRunResult
=
true
b
,
bClient
:=
builderRPCClient
(
t
)
b
.
nilRunResult
=
false
b
.
RunErrResult
=
true
artifact
,
err
=
bClient
.
Run
(
ui
,
hook
,
cache
)
cache
:=
new
(
testCache
)
hook
:=
&
packer
.
MockHook
{}
ui
:=
&
testUi
{}
artifact
,
err
:=
bClient
.
Run
(
ui
,
hook
,
cache
)
if
artifact
!=
nil
{
if
artifact
!=
nil
{
t
.
Fatalf
(
"bad: %#v"
,
artifact
)
t
.
Fatalf
(
"bad: %#v"
,
artifact
)
}
}
if
err
==
nil
{
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
t
.
Fatal
(
"should have error"
)
}
}
}
func
TestBuilderCancel
(
t
*
testing
.
T
)
{
b
,
bClient
:=
builderRPCClient
(
t
)
// Test Cancel
bClient
.
Cancel
()
bClient
.
Cancel
()
if
!
b
.
c
ancelCalled
{
if
!
b
.
C
ancelCalled
{
t
.
Fatal
(
"cancel should be called"
)
t
.
Fatal
(
"cancel should be called"
)
}
}
}
}
...
...
packer/rpc/environment_test.go
View file @
230cc973
...
@@ -7,7 +7,7 @@ import (
...
@@ -7,7 +7,7 @@ import (
"testing"
"testing"
)
)
var
testEnvBuilder
=
&
test
Builder
{}
var
testEnvBuilder
=
&
packer
.
Mock
Builder
{}
var
testEnvCache
=
&
testCache
{}
var
testEnvCache
=
&
testCache
{}
var
testEnvUi
=
&
testUi
{}
var
testEnvUi
=
&
testUi
{}
...
@@ -90,7 +90,7 @@ func TestEnvironmentRPC(t *testing.T) {
...
@@ -90,7 +90,7 @@ func TestEnvironmentRPC(t *testing.T) {
}
}
builder
.
Prepare
(
nil
)
builder
.
Prepare
(
nil
)
if
!
testEnvBuilder
.
p
repareCalled
{
if
!
testEnvBuilder
.
P
repareCalled
{
t
.
Fatal
(
"should be called"
)
t
.
Fatal
(
"should be called"
)
}
}
...
...
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