github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/googlecompute/step_create_ssh_key_test.go (about)

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