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
3657f33a
Commit
3657f33a
authored
Dec 12, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/googlecompute: StepCreateSSHKey tests
parent
2091dffe
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
13 deletions
+73
-13
builder/googlecompute/builder.go
builder/googlecompute/builder.go
+1
-1
builder/googlecompute/config_test.go
builder/googlecompute/config_test.go
+12
-0
builder/googlecompute/step_create_ssh_key.go
builder/googlecompute/step_create_ssh_key.go
+12
-12
builder/googlecompute/step_create_ssh_key_test.go
builder/googlecompute/step_create_ssh_key_test.go
+29
-0
builder/googlecompute/step_test.go
builder/googlecompute/step_test.go
+19
-0
No files found.
builder/googlecompute/builder.go
View file @
3657f33a
...
...
@@ -50,7 +50,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps.
steps
:=
[]
multistep
.
Step
{
new
(
s
tepCreateSSHKey
),
new
(
S
tepCreateSSHKey
),
new
(
stepCreateInstance
),
new
(
stepInstanceInfo
),
&
common
.
StepConnectSSH
{
...
...
builder/googlecompute/config_test.go
View file @
3657f33a
...
...
@@ -15,6 +15,18 @@ func testConfig(t *testing.T) map[string]interface{} {
}
}
func
testConfigStruct
(
t
*
testing
.
T
)
*
Config
{
c
,
warns
,
errs
:=
NewConfig
(
testConfig
(
t
))
if
len
(
warns
)
>
0
{
t
.
Fatalf
(
"bad: %#v"
,
len
(
warns
))
}
if
errs
!=
nil
{
t
.
Fatalf
(
"bad: %#v"
,
errs
)
}
return
c
}
func
testConfigErr
(
t
*
testing
.
T
,
warns
[]
string
,
err
error
,
extra
string
)
{
if
len
(
warns
)
>
0
{
t
.
Fatalf
(
"bad: %#v"
,
warns
)
...
...
builder/googlecompute/step_create_ssh_key.go
View file @
3657f33a
...
...
@@ -12,28 +12,28 @@ import (
"github.com/mitchellh/packer/packer"
)
//
s
tepCreateSSHKey represents a Packer build step that generates SSH key pairs.
type
s
tepCreateSSHKey
int
//
S
tepCreateSSHKey represents a Packer build step that generates SSH key pairs.
type
S
tepCreateSSHKey
int
// Run executes the Packer build step that generates SSH key pairs.
func
(
s
*
stepCreateSSHKey
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
var
(
ui
=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
)
ui
.
Say
(
"Creating temporary ssh key for instance..."
)
priv
,
err
:=
rsa
.
GenerateKey
(
rand
.
Reader
,
2014
)
func
(
s
*
StepCreateSSHKey
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
ui
.
Say
(
"Creating temporary SSH key for instance..."
)
priv
,
err
:=
rsa
.
GenerateKey
(
rand
.
Reader
,
2048
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating temporary ssh key: %s"
,
err
)
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
priv_der
:=
x509
.
MarshalPKCS1PrivateKey
(
priv
)
priv_blk
:=
pem
.
Block
{
Type
:
"RSA PRIVATE KEY"
,
Headers
:
nil
,
Bytes
:
priv_der
,
Bytes
:
x509
.
MarshalPKCS1PrivateKey
(
priv
)
,
}
pub
,
err
:=
ssh
.
NewPublicKey
(
&
priv
.
PublicKey
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating temporary ssh key: %s"
,
err
)
...
...
@@ -41,11 +41,11 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
state
.
Put
(
"ssh_private_key"
,
string
(
pem
.
EncodeToMemory
(
&
priv_blk
)))
state
.
Put
(
"ssh_public_key"
,
string
(
ssh
.
MarshalAuthorizedKey
(
pub
)))
return
multistep
.
ActionContinue
}
// Cleanup.
// Nothing to clean up. SSH keys are associated with a single GCE instance.
func
(
s
*
s
tepCreateSSHKey
)
Cleanup
(
state
multistep
.
StateBag
)
{}
func
(
s
*
S
tepCreateSSHKey
)
Cleanup
(
state
multistep
.
StateBag
)
{}
builder/googlecompute/step_create_ssh_key_test.go
0 → 100644
View file @
3657f33a
package
googlecompute
import
(
"github.com/mitchellh/multistep"
"testing"
)
func
TestStepCreateSSHKey_impl
(
t
*
testing
.
T
)
{
var
_
multistep
.
Step
=
new
(
StepCreateSSHKey
)
}
func
TestStepCreateSSHKey
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCreateSSHKey
)
defer
step
.
Cleanup
(
state
)
// run the step
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
// Verify that we have a public/private key
if
_
,
ok
:=
state
.
GetOk
(
"ssh_private_key"
);
!
ok
{
t
.
Fatal
(
"should have key"
)
}
if
_
,
ok
:=
state
.
GetOk
(
"ssh_public_key"
);
!
ok
{
t
.
Fatal
(
"should have key"
)
}
}
builder/googlecompute/step_test.go
0 → 100644
View file @
3657f33a
package
googlecompute
import
(
"bytes"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"testing"
)
func
testState
(
t
*
testing
.
T
)
multistep
.
StateBag
{
state
:=
new
(
multistep
.
BasicStateBag
)
state
.
Put
(
"config"
,
testConfigStruct
(
t
))
state
.
Put
(
"hook"
,
&
packer
.
MockHook
{})
state
.
Put
(
"ui"
,
&
packer
.
BasicUi
{
Reader
:
new
(
bytes
.
Buffer
),
Writer
:
new
(
bytes
.
Buffer
),
})
return
state
}
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