-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_test.go
118 lines (97 loc) · 2.52 KB
/
shell_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
package expect
import (
"os/exec"
"testing"
"github.com/stretchr/testify/require"
)
const DockerImage = "docker-test-image"
func Test_ShellExpect(t *testing.T) {
tests := []struct {
shell string
shellArgs []string
env []string
}{
{
shell: "zsh",
shellArgs: []string{"--no-globalrcs", "--no-rcs", "--no-zle", "--no-promptcr"},
env: []string{
"PROMPT=##\n",
"TESTVAR=foobar",
},
},
{
shell: "bash",
shellArgs: []string{"--noprofile", "--norc"},
env: []string{
"PS1=##\n",
"TESTVAR=foobar",
},
},
}
for _, tt := range tests {
t.Run(tt.shell, func(t *testing.T) {
shellPath, err := exec.LookPath(tt.shell)
if err != nil {
t.Skipf("shell executable not found for %s (%s)", tt.shell, err)
}
ep := NewExpectWithEnv(shellPath, tt.shellArgs, tt.env)
err = ep.Start()
require.NoError(t, err)
shell := NewShellExpect(ep, "##\n")
t.Run("init", func(t *testing.T) {
err = shell.Init()
require.NoError(t, err)
})
t.Run("echo", func(t *testing.T) {
output, err := shell.Run("echo $TESTVAR")
require.NoError(t, err)
require.Equal(t, []string{"foobar"}, output)
})
})
}
}
func Test_ShellExpect_Docker_Bash(t *testing.T) {
args := []string{
"docker", "run", "-ti", "--rm",
"-e", "PS1=##\n",
"-e", "TESTVAR=foobar",
"--entrypoint", "/bin/bash",
DockerImage,
"--noprofile", "--norc",
}
ep := NewExpect(args[0], args[1:]...)
err := ep.Start()
require.NoError(t, err)
shell := NewShellExpect(ep, "##\n")
err = shell.Init()
require.NoError(t, err)
output, err := shell.Run("stty -echo") // disable echo inside the container
require.NoError(t, err)
require.Equal(t, []string{"stty -echo"}, output)
output, err = shell.Run("echo $TESTVAR")
require.NoError(t, err)
require.Equal(t, []string{"foobar"}, output)
}
func Test_ShellExpect_Docker_Zsh(t *testing.T) {
args := []string{
"docker", "run", "-ti", "--rm",
"-e", "PROMPT=##\n",
"-e", "TESTVAR=foobar",
"--entrypoint", "/bin/zsh",
DockerImage,
"--no-globalrcs", "--no-rcs", "--no-zle", "--no-promptcr",
}
ep := NewExpect(args[0], args[1:]...)
err := ep.Start()
require.NoError(t, err)
ep.Debug = true
shell := NewShellExpect(ep, "##\n")
err = shell.Init()
require.NoError(t, err)
output, err := shell.Run("stty -echo") // disable echo inside the container
require.NoError(t, err)
require.Equal(t, []string{"stty -echo"}, output)
output, err = shell.Run("echo $TESTVAR")
require.NoError(t, err)
require.Equal(t, []string{"foobar"}, output)
}