github.com/rsyabuta/packer@v1.1.4-0.20180119234903-5ef0c2280f0b/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  }