builder/googlecompute: tests for #867

This commit is contained in:
Mitchell Hashimoto 2014-02-21 15:10:09 -08:00
parent 05ecbd546b
commit 5f01415fb7
5 changed files with 47 additions and 5 deletions

View File

@ -10,6 +10,8 @@ FEATURES:
IMPROVEMENTS:
* core: Most downloads made by Packer now use a custom user agent. [GH-803]
* builder/googlecompute: SSH private key will be saved to disk if `-debug`
is specified. [GH-867]
* builder/virtualbox-ovf: Can specify import options such as "keepallmacs".
[GH-883]

View File

@ -12,8 +12,9 @@ import (
// StepCreateInstance represents a Packer build step that creates GCE instances.
type StepCreateInstance struct {
instanceName string
Debug bool
instanceName string
}
// Run executes the Packer build step that creates a GCE instance.

View File

@ -14,7 +14,6 @@ import (
// StepCreateSSHKey represents a Packer build step that generates SSH key pairs.
type StepCreateSSHKey struct {
key int
Debug bool
DebugKeyPath string
}
@ -50,14 +49,19 @@ func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
if s.Debug {
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
f, err := os.OpenFile(s.DebugKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
f, err := os.Create(s.DebugKeyPath)
if err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt
}
// Write out the key
pem.Encode(f, &priv_blk)
err = pem.Encode(f, &priv_blk)
f.Close()
if err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}

View File

@ -2,6 +2,9 @@ package googlecompute
import (
"github.com/mitchellh/multistep"
"io/ioutil"
"os"
"testing"
)
@ -27,3 +30,34 @@ func TestStepCreateSSHKey(t *testing.T) {
t.Fatal("should have key")
}
}
func TestStepCreateSSHKey_debug(t *testing.T) {
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
tf.Close()
state := testState(t)
step := new(StepCreateSSHKey)
step.Debug = true
step.DebugKeyPath = tf.Name()
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")
}
if _, err := os.Stat(tf.Name()); err != nil {
t.Fatalf("err: %s", err)
}
}

View File

@ -11,8 +11,9 @@ import (
// stepInstanceInfo represents a Packer build step that gathers GCE instance info.
type StepInstanceInfo struct {
info int
Debug bool
info int
}
// Run executes the Packer build step that gathers GCE instance info.