builder/googlecompute: StepCreateSSHKey tests

This commit is contained in:
Mitchell Hashimoto 2013-12-12 20:09:02 -08:00
parent 2091dffe40
commit 3657f33a4d
5 changed files with 73 additions and 13 deletions

View File

@ -50,7 +50,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps. // Build the steps.
steps := []multistep.Step{ steps := []multistep.Step{
new(stepCreateSSHKey), new(StepCreateSSHKey),
new(stepCreateInstance), new(stepCreateInstance),
new(stepInstanceInfo), new(stepInstanceInfo),
&common.StepConnectSSH{ &common.StepConnectSSH{

View File

@ -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) { func testConfigErr(t *testing.T, warns []string, err error, extra string) {
if len(warns) > 0 { if len(warns) > 0 {
t.Fatalf("bad: %#v", warns) t.Fatalf("bad: %#v", warns)

View File

@ -12,28 +12,28 @@ import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
// stepCreateSSHKey represents a Packer build step that generates SSH key pairs. // StepCreateSSHKey represents a Packer build step that generates SSH key pairs.
type stepCreateSSHKey int type StepCreateSSHKey int
// Run executes the Packer build step that generates SSH key pairs. // Run executes the Packer build step that generates SSH key pairs.
func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
var ( ui := state.Get("ui").(packer.Ui)
ui = state.Get("ui").(packer.Ui)
) ui.Say("Creating temporary SSH key for instance...")
ui.Say("Creating temporary ssh key for instance...") priv, err := rsa.GenerateKey(rand.Reader, 2048)
priv, err := rsa.GenerateKey(rand.Reader, 2014)
if err != nil { if err != nil {
err := fmt.Errorf("Error creating temporary ssh key: %s", err) err := fmt.Errorf("Error creating temporary ssh key: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
priv_der := x509.MarshalPKCS1PrivateKey(priv)
priv_blk := pem.Block{ priv_blk := pem.Block{
Type: "RSA PRIVATE KEY", Type: "RSA PRIVATE KEY",
Headers: nil, Headers: nil,
Bytes: priv_der, Bytes: x509.MarshalPKCS1PrivateKey(priv),
} }
pub, err := ssh.NewPublicKey(&priv.PublicKey) pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil { if err != nil {
err := fmt.Errorf("Error creating temporary ssh key: %s", err) 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()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk))) state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk)))
state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub))) state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub)))
return multistep.ActionContinue return multistep.ActionContinue
} }
// Cleanup.
// Nothing to clean up. SSH keys are associated with a single GCE instance. // Nothing to clean up. SSH keys are associated with a single GCE instance.
func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {} func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag) {}

View File

@ -0,0 +1,29 @@
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")
}
}

View File

@ -0,0 +1,19 @@
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
}