github.com/ddnomad/packer@v1.3.2/provisioner/guest_commands_test.go (about)

     1  package provisioner
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestNewGuestCommands(t *testing.T) {
     8  	_, err := NewGuestCommands("Amiga", true)
     9  	if err == nil {
    10  		t.Fatalf("Should have returned an err for unsupported OS type")
    11  	}
    12  }
    13  
    14  func TestCreateDir(t *testing.T) {
    15  	// *nix OS
    16  	guestCmd, err := NewGuestCommands(UnixOSType, false)
    17  	if err != nil {
    18  		t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
    19  	}
    20  	cmd := guestCmd.CreateDir("/tmp/tempdir")
    21  	if cmd != "mkdir -p '/tmp/tempdir'" {
    22  		t.Fatalf("Unexpected Unix create dir cmd: %s", cmd)
    23  	}
    24  
    25  	// *nix OS w/sudo
    26  	guestCmd, err = NewGuestCommands(UnixOSType, true)
    27  	if err != nil {
    28  		t.Fatalf("Failed to create new sudo GuestCommands for OS: %s", UnixOSType)
    29  	}
    30  	cmd = guestCmd.CreateDir("/tmp/tempdir")
    31  	if cmd != "sudo mkdir -p '/tmp/tempdir'" {
    32  		t.Fatalf("Unexpected Unix sudo create dir cmd: %s", cmd)
    33  	}
    34  
    35  	// Windows OS
    36  	guestCmd, err = NewGuestCommands(WindowsOSType, false)
    37  	if err != nil {
    38  		t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
    39  	}
    40  	cmd = guestCmd.CreateDir("C:\\Windows\\Temp\\tempdir")
    41  	if cmd != "powershell.exe -Command \"New-Item -ItemType directory -Force -ErrorAction SilentlyContinue -Path C:\\Windows\\Temp\\tempdir\"" {
    42  		t.Fatalf("Unexpected Windows create dir cmd: %s", cmd)
    43  	}
    44  
    45  	// Windows OS w/ space in path
    46  	cmd = guestCmd.CreateDir("C:\\Windows\\Temp\\temp dir")
    47  	if cmd != "powershell.exe -Command \"New-Item -ItemType directory -Force -ErrorAction SilentlyContinue -Path C:\\Windows\\Temp\\temp` dir\"" {
    48  		t.Fatalf("Unexpected Windows create dir cmd: %s", cmd)
    49  	}
    50  }
    51  
    52  func TestChmod(t *testing.T) {
    53  	// *nix
    54  	guestCmd, err := NewGuestCommands(UnixOSType, false)
    55  	if err != nil {
    56  		t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
    57  	}
    58  	cmd := guestCmd.Chmod("/usr/local/bin/script.sh", "0666")
    59  	if cmd != "chmod 0666 '/usr/local/bin/script.sh'" {
    60  		t.Fatalf("Unexpected Unix chmod 0666 cmd: %s", cmd)
    61  	}
    62  
    63  	// sudo *nix
    64  	guestCmd, err = NewGuestCommands(UnixOSType, true)
    65  	if err != nil {
    66  		t.Fatalf("Failed to create new sudo GuestCommands for OS: %s", UnixOSType)
    67  	}
    68  	cmd = guestCmd.Chmod("/usr/local/bin/script.sh", "+x")
    69  	if cmd != "sudo chmod +x '/usr/local/bin/script.sh'" {
    70  		t.Fatalf("Unexpected Unix chmod +x cmd: %s", cmd)
    71  	}
    72  
    73  	// Windows
    74  	guestCmd, err = NewGuestCommands(WindowsOSType, false)
    75  	if err != nil {
    76  		t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
    77  	}
    78  	cmd = guestCmd.Chmod("C:\\Program Files\\SomeApp\\someapp.exe", "+x")
    79  	if cmd != "echo 'skipping chmod +x C:\\Program` Files\\SomeApp\\someapp.exe'" {
    80  		t.Fatalf("Unexpected Windows chmod +x cmd: %s", cmd)
    81  	}
    82  }
    83  
    84  func TestRemoveDir(t *testing.T) {
    85  	// *nix
    86  	guestCmd, err := NewGuestCommands(UnixOSType, false)
    87  	if err != nil {
    88  		t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
    89  	}
    90  	cmd := guestCmd.RemoveDir("/tmp/somedir")
    91  	if cmd != "rm -rf '/tmp/somedir'" {
    92  		t.Fatalf("Unexpected Unix remove dir cmd: %s", cmd)
    93  	}
    94  
    95  	// sudo *nix
    96  	guestCmd, err = NewGuestCommands(UnixOSType, true)
    97  	if err != nil {
    98  		t.Fatalf("Failed to create new sudo GuestCommands for OS: %s", UnixOSType)
    99  	}
   100  	cmd = guestCmd.RemoveDir("/tmp/somedir")
   101  	if cmd != "sudo rm -rf '/tmp/somedir'" {
   102  		t.Fatalf("Unexpected Unix sudo remove dir cmd: %s", cmd)
   103  	}
   104  
   105  	// Windows OS
   106  	guestCmd, err = NewGuestCommands(WindowsOSType, false)
   107  	if err != nil {
   108  		t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
   109  	}
   110  	cmd = guestCmd.RemoveDir("C:\\Temp\\SomeDir")
   111  	if cmd != "powershell.exe -Command \"rm C:\\Temp\\SomeDir -recurse -force\"" {
   112  		t.Fatalf("Unexpected Windows remove dir cmd: %s", cmd)
   113  	}
   114  
   115  	// Windows OS w/ space in path
   116  	cmd = guestCmd.RemoveDir("C:\\Temp\\Some Dir")
   117  	if cmd != "powershell.exe -Command \"rm C:\\Temp\\Some` Dir -recurse -force\"" {
   118  		t.Fatalf("Unexpected Windows remove dir cmd: %s", cmd)
   119  	}
   120  }
   121  
   122  func TestStatPath(t *testing.T) {
   123  	// *nix
   124  	guestCmd, err := NewGuestCommands(UnixOSType, false)
   125  	if err != nil {
   126  		t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
   127  	}
   128  	cmd := guestCmd.StatPath("/tmp/somedir")
   129  	if cmd != "stat '/tmp/somedir'" {
   130  		t.Fatalf("Unexpected Unix stat cmd: %s", cmd)
   131  	}
   132  
   133  	guestCmd, err = NewGuestCommands(UnixOSType, true)
   134  	if err != nil {
   135  		t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
   136  	}
   137  	cmd = guestCmd.StatPath("/tmp/somedir")
   138  	if cmd != "sudo stat '/tmp/somedir'" {
   139  		t.Fatalf("Unexpected Unix stat cmd: %s", cmd)
   140  	}
   141  
   142  	// Windows OS
   143  	guestCmd, err = NewGuestCommands(WindowsOSType, false)
   144  	if err != nil {
   145  		t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
   146  	}
   147  	cmd = guestCmd.StatPath("C:\\Temp\\SomeDir")
   148  	if cmd != "powershell.exe -Command { if (test-path C:\\Temp\\SomeDir) { exit 0 } else { exit 1 } }" {
   149  		t.Fatalf("Unexpected Windows stat cmd: %s", cmd)
   150  	}
   151  
   152  	// Windows OS w/ space in path
   153  	cmd = guestCmd.StatPath("C:\\Temp\\Some Dir")
   154  	if cmd != "powershell.exe -Command { if (test-path C:\\Temp\\Some` Dir) { exit 0 } else { exit 1 } }" {
   155  		t.Fatalf("Unexpected Windows stat cmd: %s", cmd)
   156  	}
   157  }
   158  
   159  func TestMovePath(t *testing.T) {
   160  	// *nix
   161  	guestCmd, err := NewGuestCommands(UnixOSType, false)
   162  	if err != nil {
   163  		t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
   164  	}
   165  	cmd := guestCmd.MovePath("/tmp/somedir", "/tmp/newdir")
   166  	if cmd != "mv '/tmp/somedir' '/tmp/newdir'" {
   167  		t.Fatalf("Unexpected Unix move cmd: %s", cmd)
   168  	}
   169  
   170  	// sudo *nix
   171  	guestCmd, err = NewGuestCommands(UnixOSType, true)
   172  	if err != nil {
   173  		t.Fatalf("Failed to create new sudo GuestCommands for OS: %s", UnixOSType)
   174  	}
   175  	cmd = guestCmd.MovePath("/tmp/somedir", "/tmp/newdir")
   176  	if cmd != "sudo mv '/tmp/somedir' '/tmp/newdir'" {
   177  		t.Fatalf("Unexpected Unix sudo mv cmd: %s", cmd)
   178  	}
   179  
   180  	// Windows OS
   181  	guestCmd, err = NewGuestCommands(WindowsOSType, false)
   182  	if err != nil {
   183  		t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
   184  	}
   185  	cmd = guestCmd.MovePath("C:\\Temp\\SomeDir", "C:\\Temp\\NewDir")
   186  	if cmd != "powershell.exe -Command \"mv C:\\Temp\\SomeDir C:\\Temp\\NewDir\"" {
   187  		t.Fatalf("Unexpected Windows remove dir cmd: %s", cmd)
   188  	}
   189  
   190  	// Windows OS w/ space in path
   191  	cmd = guestCmd.MovePath("C:\\Temp\\Some Dir", "C:\\Temp\\New Dir")
   192  	if cmd != "powershell.exe -Command \"mv C:\\Temp\\Some` Dir C:\\Temp\\New` Dir\"" {
   193  		t.Fatalf("Unexpected Windows remove dir cmd: %s", cmd)
   194  	}
   195  }