Commit 5f01415f authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/googlecompute: tests for #867

parent 05ecbd54
...@@ -10,6 +10,8 @@ FEATURES: ...@@ -10,6 +10,8 @@ FEATURES:
IMPROVEMENTS: IMPROVEMENTS:
* core: Most downloads made by Packer now use a custom user agent. [GH-803] * 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". * builder/virtualbox-ovf: Can specify import options such as "keepallmacs".
[GH-883] [GH-883]
......
...@@ -12,8 +12,9 @@ import ( ...@@ -12,8 +12,9 @@ import (
// StepCreateInstance represents a Packer build step that creates GCE instances. // StepCreateInstance represents a Packer build step that creates GCE instances.
type StepCreateInstance struct { type StepCreateInstance struct {
Debug bool
instanceName string instanceName string
Debug bool
} }
// Run executes the Packer build step that creates a GCE instance. // Run executes the Packer build step that creates a GCE instance.
......
...@@ -14,7 +14,6 @@ import ( ...@@ -14,7 +14,6 @@ import (
// StepCreateSSHKey represents a Packer build step that generates SSH key pairs. // StepCreateSSHKey represents a Packer build step that generates SSH key pairs.
type StepCreateSSHKey struct { type StepCreateSSHKey struct {
key int
Debug bool Debug bool
DebugKeyPath string DebugKeyPath string
} }
...@@ -50,14 +49,19 @@ func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { ...@@ -50,14 +49,19 @@ func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
if s.Debug { if s.Debug {
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) 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 { if err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
// Write out the key // Write out the key
pem.Encode(f, &priv_blk) err = pem.Encode(f, &priv_blk)
f.Close() f.Close()
if err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt
}
} }
return multistep.ActionContinue return multistep.ActionContinue
} }
......
...@@ -2,6 +2,9 @@ package googlecompute ...@@ -2,6 +2,9 @@ package googlecompute
import ( import (
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"io/ioutil"
"os"
"testing" "testing"
) )
...@@ -27,3 +30,34 @@ func TestStepCreateSSHKey(t *testing.T) { ...@@ -27,3 +30,34 @@ func TestStepCreateSSHKey(t *testing.T) {
t.Fatal("should have key") 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)
}
}
...@@ -11,8 +11,9 @@ import ( ...@@ -11,8 +11,9 @@ import (
// stepInstanceInfo represents a Packer build step that gathers GCE instance info. // stepInstanceInfo represents a Packer build step that gathers GCE instance info.
type StepInstanceInfo struct { type StepInstanceInfo struct {
info int
Debug bool Debug bool
info int
} }
// Run executes the Packer build step that gathers GCE instance info. // Run executes the Packer build step that gathers GCE instance info.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment