-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgexpect_test.go
More file actions
122 lines (107 loc) · 2.34 KB
/
gexpect_test.go
File metadata and controls
122 lines (107 loc) · 2.34 KB
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
121
122
package gexpect
import (
"log"
"strings"
"testing"
)
func TestHelloWorld(*testing.T) {
log.Printf("Testing Hello World... ")
child, err := Spawn("echo \"Hello World\"")
if err != nil {
panic(err)
}
err = child.Expect("Hello World")
if err != nil {
panic(err)
}
log.Printf("Success\n")
}
func TestHelloWorldFailureCase(*testing.T) {
log.Printf("Testing Hello World Failure case... ")
child, err := Spawn("echo \"Hello World\"")
if err != nil {
panic(err)
}
err = child.Expect("YOU WILL NEVER FIND ME")
if err != nil {
log.Printf("Success\n")
return
}
panic("Expected an error for TestHelloWorldFailureCase")
}
func TestBiChannel(*testing.T) {
log.Printf("Testing BiChannel screen... ")
child, err := Spawn("screen")
if err != nil {
panic(err)
}
sender, reciever := child.AsyncInteractChannels()
wait := func(str string) {
for {
msg, open := <-reciever
if !open {
return
}
if strings.Contains(msg, str) {
return
}
}
}
sender <- "\n"
sender <- "echo Hello World\n"
wait("Hello World")
sender <- "times\n"
wait("s")
sender <- "^D\n"
log.Printf("Success\n")
}
func TestExpectRegex(*testing.T) {
log.Printf("Testing ExpectRegex... ")
child, err := Spawn("/bin/sh times")
if err != nil {
panic(err)
}
child.ExpectRegex("Name")
log.Printf("Success\n")
}
func TestCommandStart(*testing.T) {
log.Printf("Testing Command... ")
// Doing this allows you to modify the cmd struct prior to execution, for example to add environment variables
child, err := Command("echo 'Hello World'")
if err != nil {
panic(err)
}
child.Start()
child.Expect("Hello World")
log.Printf("Success\n")
}
func TestExpectFtp(*testing.T) {
log.Printf("Testing Ftp... ")
child, err := Spawn("ftp ftp.openbsd.org")
if err != nil {
panic(err)
}
child.Expect("Name")
child.SendLine("anonymous")
child.Expect("Password")
child.SendLine("pexpect@sourceforge.net")
child.Expect("ftp> ")
child.SendLine("cd /pub/OpenBSD/3.7/packages/i386")
child.Expect("ftp> ")
child.SendLine("bin")
child.Expect("ftp> ")
child.SendLine("prompt")
child.Expect("ftp> ")
child.SendLine("pwd")
child.Expect("ftp> ")
log.Printf("Success\n")
}
func TestInteractPing(*testing.T) {
log.Printf("Testing Ping interact... \n")
child, err := Spawn("ping -c8 8.8.8.8")
if err != nil {
panic(err)
}
child.Interact()
log.Printf("Success\n")
}