github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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  	step.PrivateKeyFile = "test-fixtures/fake-key"
    21  	defer step.Cleanup(state)
    22  
    23  	// run the step
    24  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    25  		t.Fatalf("bad action: %#v", action)
    26  	}
    27  
    28  	// Verify that we have a public/private key
    29  	if _, ok := state.GetOk("ssh_private_key"); !ok {
    30  		t.Fatal("should have key")
    31  	}
    32  }
    33  
    34  func TestStepCreateSSHKey(t *testing.T) {
    35  	state := testState(t)
    36  	step := new(StepCreateSSHKey)
    37  	defer step.Cleanup(state)
    38  
    39  	// run the step
    40  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    41  		t.Fatalf("bad action: %#v", action)
    42  	}
    43  
    44  	// Verify that we have a public/private key
    45  	if _, ok := state.GetOk("ssh_private_key"); !ok {
    46  		t.Fatal("should have key")
    47  	}
    48  	if _, ok := state.GetOk("ssh_public_key"); !ok {
    49  		t.Fatal("should have key")
    50  	}
    51  }
    52  
    53  func TestStepCreateSSHKey_debug(t *testing.T) {
    54  	tf, err := ioutil.TempFile("", "packer")
    55  	if err != nil {
    56  		t.Fatalf("err: %s", err)
    57  	}
    58  	defer os.Remove(tf.Name())
    59  	tf.Close()
    60  
    61  	state := testState(t)
    62  	step := new(StepCreateSSHKey)
    63  	step.Debug = true
    64  	step.DebugKeyPath = tf.Name()
    65  
    66  	defer step.Cleanup(state)
    67  
    68  	// run the step
    69  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    70  		t.Fatalf("bad action: %#v", action)
    71  	}
    72  
    73  	// Verify that we have a public/private key
    74  	if _, ok := state.GetOk("ssh_private_key"); !ok {
    75  		t.Fatal("should have key")
    76  	}
    77  	if _, ok := state.GetOk("ssh_public_key"); !ok {
    78  		t.Fatal("should have key")
    79  	}
    80  	if _, err := os.Stat(tf.Name()); err != nil {
    81  		t.Fatalf("err: %s", err)
    82  	}
    83  }