github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/googlecompute/step_create_windows_password_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"github.com/mitchellh/multistep"
     9  
    10  	"testing"
    11  )
    12  
    13  func TestStepCreateOrResetWindowsPassword(t *testing.T) {
    14  	state := testState(t)
    15  
    16  	// Step is run after the instance is created so we will have an instance name set
    17  	state.Put("instance_name", "mock_instance")
    18  	state.Put("create_windows_password", true)
    19  
    20  	step := new(StepCreateWindowsPassword)
    21  	defer step.Cleanup(state)
    22  
    23  	// run the step
    24  	if action := step.Run(state); action != multistep.ActionContinue {
    25  		t.Fatalf("bad action: %#v", action)
    26  	}
    27  
    28  	if password, ok := state.GetOk("winrm_password"); !ok || password.(string) != "MOCK_PASSWORD" {
    29  		t.Fatal("should have a password", password, ok)
    30  	}
    31  }
    32  
    33  func TestStepCreateOrResetWindowsPassword_passwordSet(t *testing.T) {
    34  	state := testState(t)
    35  
    36  	// Step is run after the instance is created so we will have an instance name set
    37  	state.Put("instance_name", "mock_instance")
    38  
    39  	c := state.Get("config").(*Config)
    40  
    41  	c.Comm.WinRMPassword = "password"
    42  
    43  	step := new(StepCreateWindowsPassword)
    44  	defer step.Cleanup(state)
    45  
    46  	// run the step
    47  	if action := step.Run(state); action != multistep.ActionContinue {
    48  		t.Fatalf("bad action: %#v", action)
    49  	}
    50  
    51  	if password, ok := state.GetOk("winrm_password"); !ok || password.(string) != "password" {
    52  		t.Fatal("should have used existing password", password, ok)
    53  	}
    54  }
    55  
    56  func TestStepCreateOrResetWindowsPassword_dontNeedPassword(t *testing.T) {
    57  	state := testState(t)
    58  
    59  	// Step is run after the instance is created so we will have an instance name set
    60  	state.Put("instance_name", "mock_instance")
    61  
    62  	step := new(StepCreateWindowsPassword)
    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  }
    71  
    72  func TestStepCreateOrResetWindowsPassword_debug(t *testing.T) {
    73  	tf, err := ioutil.TempFile("", "packer")
    74  	if err != nil {
    75  		t.Fatalf("err: %s", err)
    76  	}
    77  	tf.Close()
    78  
    79  	state := testState(t)
    80  	// Step is run after the instance is created so we will have an instance name set
    81  	state.Put("instance_name", "mock_instance")
    82  	state.Put("create_windows_password", true)
    83  
    84  	step := new(StepCreateWindowsPassword)
    85  
    86  	step.Debug = true
    87  	step.DebugKeyPath = tf.Name()
    88  
    89  	defer step.Cleanup(state)
    90  
    91  	// run the step
    92  	if action := step.Run(state); action != multistep.ActionContinue {
    93  		t.Fatalf("bad action: %#v", action)
    94  	}
    95  
    96  	if password, ok := state.GetOk("winrm_password"); !ok || password.(string) != "MOCK_PASSWORD" {
    97  		t.Fatal("should have a password", password, ok)
    98  	}
    99  
   100  	if _, err := os.Stat(tf.Name()); err != nil {
   101  		t.Fatalf("err: %s", err)
   102  	}
   103  }
   104  
   105  func TestStepCreateOrResetWindowsPassword_error(t *testing.T) {
   106  	state := testState(t)
   107  
   108  	// Step is run after the instance is created so we will have an instance name set
   109  	state.Put("instance_name", "mock_instance")
   110  	state.Put("create_windows_password", true)
   111  
   112  	step := new(StepCreateWindowsPassword)
   113  	defer step.Cleanup(state)
   114  
   115  	driver := state.Get("driver").(*DriverMock)
   116  	driver.CreateOrResetWindowsPasswordErr = errors.New("error")
   117  
   118  	// run the step
   119  	if action := step.Run(state); action != multistep.ActionHalt {
   120  		t.Fatalf("bad action: %#v", action)
   121  	}
   122  
   123  	// Verify state
   124  	if _, ok := state.GetOk("error"); !ok {
   125  		t.Fatal("should have error")
   126  	}
   127  
   128  	if _, ok := state.GetOk("winrm_password"); ok {
   129  		t.Fatal("should NOT have instance name")
   130  	}
   131  }
   132  
   133  func TestStepCreateOrResetWindowsPassword_errorOnChannel(t *testing.T) {
   134  	state := testState(t)
   135  
   136  	// Step is run after the instance is created so we will have an instance name set
   137  	state.Put("instance_name", "mock_instance")
   138  	state.Put("create_windows_password", true)
   139  
   140  	step := new(StepCreateWindowsPassword)
   141  	defer step.Cleanup(state)
   142  
   143  	driver := state.Get("driver").(*DriverMock)
   144  
   145  	errCh := make(chan error, 1)
   146  	errCh <- errors.New("error")
   147  
   148  	driver.CreateOrResetWindowsPasswordErrCh = errCh
   149  
   150  	// run the step
   151  	if action := step.Run(state); action != multistep.ActionHalt {
   152  		t.Fatalf("bad action: %#v", action)
   153  	}
   154  
   155  	// Verify state
   156  	if _, ok := state.GetOk("error"); !ok {
   157  		t.Fatal("should have error")
   158  	}
   159  	if _, ok := state.GetOk("winrm_password"); ok {
   160  		t.Fatal("should NOT have instance name")
   161  	}
   162  }