2013-12-12 23:09:02 -05:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2014-02-21 18:10:09 -05:00
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-12-12 23:09:02 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepCreateSSHKey_impl(t *testing.T) {
|
|
|
|
var _ multistep.Step = new(StepCreateSSHKey)
|
|
|
|
}
|
|
|
|
|
2016-11-03 12:45:52 -04:00
|
|
|
func TestStepCreateSSHKey_privateKey(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCreateSSHKey)
|
2018-08-30 06:04:21 -04:00
|
|
|
cfg := state.Get("config").(*Config)
|
|
|
|
cfg.Comm.SSHPrivateKeyFile = "test-fixtures/fake-key"
|
2016-11-03 12:45:52 -04:00
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
// run the step
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2016-11-03 12:45:52 -04:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that we have a public/private key
|
2018-08-24 02:43:26 -04:00
|
|
|
if len(cfg.Comm.SSHPrivateKey) == 0 {
|
2016-11-03 12:45:52 -04:00
|
|
|
t.Fatal("should have key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:09:02 -05:00
|
|
|
func TestStepCreateSSHKey(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCreateSSHKey)
|
2018-08-30 06:04:21 -04:00
|
|
|
cfg := state.Get("config").(*Config)
|
2013-12-12 23:09:02 -05:00
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
// run the step
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-12 23:09:02 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that we have a public/private key
|
2018-08-24 02:43:26 -04:00
|
|
|
if len(cfg.Comm.SSHPrivateKey) == 0 {
|
2013-12-12 23:09:02 -05:00
|
|
|
t.Fatal("should have key")
|
|
|
|
}
|
2018-08-24 02:43:26 -04:00
|
|
|
if len(cfg.Comm.SSHPublicKey) == 0 {
|
2013-12-12 23:09:02 -05:00
|
|
|
t.Fatal("should have key")
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 18:10:09 -05:00
|
|
|
|
|
|
|
func TestStepCreateSSHKey_debug(t *testing.T) {
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-04-30 15:36:48 -04:00
|
|
|
defer os.Remove(tf.Name())
|
2014-02-21 18:10:09 -05:00
|
|
|
tf.Close()
|
|
|
|
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCreateSSHKey)
|
2018-08-30 06:04:21 -04:00
|
|
|
cfg := state.Get("config").(*Config)
|
2014-02-21 18:10:09 -05:00
|
|
|
step.Debug = true
|
|
|
|
step.DebugKeyPath = tf.Name()
|
|
|
|
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
// run the step
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2014-02-21 18:10:09 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that we have a public/private key
|
2018-08-24 02:43:26 -04:00
|
|
|
if len(cfg.Comm.SSHPrivateKey) == 0 {
|
2014-02-21 18:10:09 -05:00
|
|
|
t.Fatal("should have key")
|
|
|
|
}
|
2018-08-24 02:43:26 -04:00
|
|
|
if len(cfg.Comm.SSHPublicKey) == 0 {
|
2014-02-21 18:10:09 -05:00
|
|
|
t.Fatal("should have key")
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(tf.Name()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|