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
62b81dc4
Commit
62b81dc4
authored
Nov 09, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/docker: StepPull test, driver abstraction for tests
parent
2da92336
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
99 additions
and
3 deletions
+99
-3
builder/docker/builder.go
builder/docker/builder.go
+5
-0
builder/docker/driver.go
builder/docker/driver.go
+9
-0
builder/docker/driver_docker.go
builder/docker/driver_docker.go
+15
-0
builder/docker/driver_mock.go
builder/docker/driver_mock.go
+15
-0
builder/docker/step_pull.go
builder/docker/step_pull.go
+2
-3
builder/docker/step_pull_test.go
builder/docker/step_pull_test.go
+52
-0
builder/docker/step_test.go
builder/docker/step_test.go
+1
-0
No files found.
builder/docker/builder.go
View file @
62b81dc4
...
...
@@ -56,6 +56,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state
.
Put
(
"hook"
,
hook
)
state
.
Put
(
"ui"
,
ui
)
// Setup the driver that will talk to Docker
state
.
Put
(
"driver"
,
&
DockerDriver
{
Ui
:
ui
,
})
// Run!
if
b
.
config
.
PackerDebug
{
b
.
runner
=
&
multistep
.
DebugRunner
{
...
...
builder/docker/driver.go
0 → 100644
View file @
62b81dc4
package
docker
// Driver is the interface that has to be implemented to communicate with
// Docker. The Driver interface also allows the steps to be tested since
// a mock driver can be shimmed in.
type
Driver
interface
{
// Pull should pull down the given image.
Pull
(
image
string
)
error
}
builder/docker/driver_docker.go
0 → 100644
View file @
62b81dc4
package
docker
import
(
"github.com/mitchellh/packer/packer"
"os/exec"
)
type
DockerDriver
struct
{
Ui
packer
.
Ui
}
func
(
d
*
DockerDriver
)
Pull
(
image
string
)
error
{
cmd
:=
exec
.
Command
(
"docker"
,
"pull"
,
image
)
return
runAndStream
(
cmd
,
d
.
Ui
)
}
builder/docker/driver_mock.go
0 → 100644
View file @
62b81dc4
package
docker
// MockDriver is a driver implementation that can be used for tests.
type
MockDriver
struct
{
PullError
error
PullCalled
bool
PullImage
string
}
func
(
d
*
MockDriver
)
Pull
(
image
string
)
error
{
d
.
PullCalled
=
true
d
.
PullImage
=
image
return
d
.
PullError
}
builder/docker/step_pull.go
View file @
62b81dc4
...
...
@@ -4,18 +4,17 @@ import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"os/exec"
)
type
StepPull
struct
{}
func
(
s
*
StepPull
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
driver
:=
state
.
Get
(
"driver"
)
.
(
Driver
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
ui
.
Say
(
fmt
.
Sprintf
(
"Pulling Docker image: %s"
,
config
.
Image
))
cmd
:=
exec
.
Command
(
"docker"
,
"pull"
,
config
.
Image
)
if
err
:=
runAndStream
(
cmd
,
ui
);
err
!=
nil
{
if
err
:=
driver
.
Pull
(
config
.
Image
);
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error pulling Docker image: %s"
,
err
)
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
...
...
builder/docker/step_pull_test.go
0 → 100644
View file @
62b81dc4
package
docker
import
(
"errors"
"github.com/mitchellh/multistep"
"testing"
)
func
TestStepPull_impl
(
t
*
testing
.
T
)
{
var
_
multistep
.
Step
=
new
(
StepPull
)
}
func
TestStepPull
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepPull
)
defer
step
.
Cleanup
(
state
)
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
driver
:=
state
.
Get
(
"driver"
)
.
(
*
MockDriver
)
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// verify we did the right thing
if
!
driver
.
PullCalled
{
t
.
Fatal
(
"should've pulled"
)
}
if
driver
.
PullImage
!=
config
.
Image
{
t
.
Fatalf
(
"bad: %#v"
,
driver
.
PullImage
)
}
}
func
TestStepPull_error
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepPull
)
defer
step
.
Cleanup
(
state
)
driver
:=
state
.
Get
(
"driver"
)
.
(
*
MockDriver
)
driver
.
PullError
=
errors
.
New
(
"foo"
)
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionHalt
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// verify we have an error
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
!
ok
{
t
.
Fatal
(
"should have error"
)
}
}
builder/docker/step_test.go
View file @
62b81dc4
...
...
@@ -10,6 +10,7 @@ import (
func
testState
(
t
*
testing
.
T
)
multistep
.
StateBag
{
state
:=
new
(
multistep
.
BasicStateBag
)
state
.
Put
(
"config"
,
testConfigStruct
(
t
))
state
.
Put
(
"driver"
,
&
MockDriver
{})
state
.
Put
(
"hook"
,
&
packer
.
MockHook
{})
state
.
Put
(
"ui"
,
&
packer
.
BasicUi
{
Reader
:
new
(
bytes
.
Buffer
),
...
...
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