github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/step_create_windows_password_test.go (about)

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