forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguest_commands_test.go
120 lines (107 loc) · 3.78 KB
/
guest_commands_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package provisioner
import (
"testing"
)
func TestNewGuestCommands(t *testing.T) {
_, err := NewGuestCommands("Amiga", true)
if err == nil {
t.Fatalf("Should have returned an err for unsupported OS type")
}
}
func TestCreateDir(t *testing.T) {
// *nix OS
guestCmd, err := NewGuestCommands(UnixOSType, false)
if err != nil {
t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
}
cmd := guestCmd.CreateDir("/tmp/tempdir")
if cmd != "mkdir -p '/tmp/tempdir'" {
t.Fatalf("Unexpected Unix create dir cmd: %s", cmd)
}
// *nix OS w/sudo
guestCmd, err = NewGuestCommands(UnixOSType, true)
if err != nil {
t.Fatalf("Failed to create new sudo GuestCommands for OS: %s", UnixOSType)
}
cmd = guestCmd.CreateDir("/tmp/tempdir")
if cmd != "sudo mkdir -p '/tmp/tempdir'" {
t.Fatalf("Unexpected Unix sudo create dir cmd: %s", cmd)
}
// Windows OS
guestCmd, err = NewGuestCommands(WindowsOSType, false)
if err != nil {
t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
}
cmd = guestCmd.CreateDir("C:\\Windows\\Temp\\tempdir")
if cmd != "powershell.exe -Command \"New-Item -ItemType directory -Force -ErrorAction SilentlyContinue -Path C:\\Windows\\Temp\\tempdir\"" {
t.Fatalf("Unexpected Windows create dir cmd: %s", cmd)
}
// Windows OS w/ space in path
cmd = guestCmd.CreateDir("C:\\Windows\\Temp\\temp dir")
if cmd != "powershell.exe -Command \"New-Item -ItemType directory -Force -ErrorAction SilentlyContinue -Path C:\\Windows\\Temp\\temp` dir\"" {
t.Fatalf("Unexpected Windows create dir cmd: %s", cmd)
}
}
func TestChmod(t *testing.T) {
// *nix
guestCmd, err := NewGuestCommands(UnixOSType, false)
if err != nil {
t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
}
cmd := guestCmd.Chmod("/usr/local/bin/script.sh", "0666")
if cmd != "chmod 0666 '/usr/local/bin/script.sh'" {
t.Fatalf("Unexpected Unix chmod 0666 cmd: %s", cmd)
}
// sudo *nix
guestCmd, err = NewGuestCommands(UnixOSType, true)
if err != nil {
t.Fatalf("Failed to create new sudo GuestCommands for OS: %s", UnixOSType)
}
cmd = guestCmd.Chmod("/usr/local/bin/script.sh", "+x")
if cmd != "sudo chmod +x '/usr/local/bin/script.sh'" {
t.Fatalf("Unexpected Unix chmod +x cmd: %s", cmd)
}
// Windows
guestCmd, err = NewGuestCommands(WindowsOSType, false)
if err != nil {
t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
}
cmd = guestCmd.Chmod("C:\\Program Files\\SomeApp\\someapp.exe", "+x")
if cmd != "echo 'skipping chmod +x C:\\Program` Files\\SomeApp\\someapp.exe'" {
t.Fatalf("Unexpected Windows chmod +x cmd: %s", cmd)
}
}
func TestRemoveDir(t *testing.T) {
// *nix
guestCmd, err := NewGuestCommands(UnixOSType, false)
if err != nil {
t.Fatalf("Failed to create new GuestCommands for OS: %s", UnixOSType)
}
cmd := guestCmd.RemoveDir("/tmp/somedir")
if cmd != "rm -rf '/tmp/somedir'" {
t.Fatalf("Unexpected Unix remove dir cmd: %s", cmd)
}
// sudo *nix
guestCmd, err = NewGuestCommands(UnixOSType, true)
if err != nil {
t.Fatalf("Failed to create new sudo GuestCommands for OS: %s", UnixOSType)
}
cmd = guestCmd.RemoveDir("/tmp/somedir")
if cmd != "sudo rm -rf '/tmp/somedir'" {
t.Fatalf("Unexpected Unix sudo remove dir cmd: %s", cmd)
}
// Windows OS
guestCmd, err = NewGuestCommands(WindowsOSType, false)
if err != nil {
t.Fatalf("Failed to create new GuestCommands for OS: %s", WindowsOSType)
}
cmd = guestCmd.RemoveDir("C:\\Temp\\SomeDir")
if cmd != "powershell.exe -Command \"rm C:\\Temp\\SomeDir -recurse -force\"" {
t.Fatalf("Unexpected Windows remove dir cmd: %s", cmd)
}
// Windows OS w/ space in path
cmd = guestCmd.RemoveDir("C:\\Temp\\Some Dir")
if cmd != "powershell.exe -Command \"rm C:\\Temp\\Some` Dir -recurse -force\"" {
t.Fatalf("Unexpected Windows remove dir cmd: %s", cmd)
}
}