github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/step_create_ssh_key_test.go (about) 1 package googlecompute 2 3 import ( 4 "context" 5 6 "github.com/hashicorp/packer/helper/multistep" 7 8 "io/ioutil" 9 "os" 10 "testing" 11 ) 12 13 func TestStepCreateSSHKey_impl(t *testing.T) { 14 var _ multistep.Step = new(StepCreateSSHKey) 15 } 16 17 func TestStepCreateSSHKey_privateKey(t *testing.T) { 18 state := testState(t) 19 step := new(StepCreateSSHKey) 20 cfg := state.Get("config").(*Config) 21 cfg.Comm.SSHPrivateKeyFile = "test-fixtures/fake-key" 22 defer step.Cleanup(state) 23 24 // run the step 25 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 26 t.Fatalf("bad action: %#v", action) 27 } 28 29 // Verify that we have a public/private key 30 if len(cfg.Comm.SSHPrivateKey) == 0 { 31 t.Fatal("should have key") 32 } 33 } 34 35 func TestStepCreateSSHKey(t *testing.T) { 36 state := testState(t) 37 step := new(StepCreateSSHKey) 38 cfg := state.Get("config").(*Config) 39 defer step.Cleanup(state) 40 41 // run the step 42 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 43 t.Fatalf("bad action: %#v", action) 44 } 45 46 // Verify that we have a public/private key 47 if len(cfg.Comm.SSHPrivateKey) == 0 { 48 t.Fatal("should have key") 49 } 50 if len(cfg.Comm.SSHPublicKey) == 0 { 51 t.Fatal("should have key") 52 } 53 } 54 55 func TestStepCreateSSHKey_debug(t *testing.T) { 56 tf, err := ioutil.TempFile("", "packer") 57 if err != nil { 58 t.Fatalf("err: %s", err) 59 } 60 defer os.Remove(tf.Name()) 61 tf.Close() 62 63 state := testState(t) 64 step := new(StepCreateSSHKey) 65 cfg := state.Get("config").(*Config) 66 step.Debug = true 67 step.DebugKeyPath = tf.Name() 68 69 defer step.Cleanup(state) 70 71 // run the step 72 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 73 t.Fatalf("bad action: %#v", action) 74 } 75 76 // Verify that we have a public/private key 77 if len(cfg.Comm.SSHPrivateKey) == 0 { 78 t.Fatal("should have key") 79 } 80 if len(cfg.Comm.SSHPublicKey) == 0 { 81 t.Fatal("should have key") 82 } 83 if _, err := os.Stat(tf.Name()); err != nil { 84 t.Fatalf("err: %s", err) 85 } 86 }