forked from rai-project/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
141 lines (106 loc) · 2.52 KB
/
Copy pathexec_test.go
File metadata and controls
141 lines (106 loc) · 2.52 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package docker
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type ExecTestSuite struct {
suite.Suite
client *Client
}
func NewExecTestSuite(t *testing.T) (*ExecTestSuite, error) {
client, err := NewClient(
Stdout(os.Stdout),
Stderr(os.Stderr),
)
assert.NoError(t, err)
if err != nil {
return nil, err
}
return &ExecTestSuite{
client: client,
}, nil
}
func (suite *ExecTestSuite) TestRun() {
t := suite.T()
client := suite.client
cont, err := NewContainer(client)
assert.NoError(t, err)
if err != nil {
return
}
defer func() {
err := cont.Stop()
assert.NoError(t, err)
}()
err = cont.Start()
assert.NoError(t, err)
exec, err := NewExecution(cont, "/bin/sh", "-c", "ls", "-l", "/")
assert.NoError(t, err)
assert.NotNil(t, exec)
var stdout, stderr bytes.Buffer
exec.Stderr = &stderr
exec.Stdout = &stdout
err = exec.Run()
assert.NoError(t, err, "execution should not return an error")
assert.Empty(t, stderr.Bytes())
assert.NotEmpty(t, stdout.Bytes())
assert.Equal(t, stdout.String(), "bin dev home lib64\tmnt proc run\t srv tmp var\r\nboot etc lib\t media\topt root sbin sys usr\r\n")
}
func (suite *ExecTestSuite) TestExecutionOutput() {
t := suite.T()
client := suite.client
cont, err := NewContainer(client)
assert.NoError(t, err)
if err != nil {
return
}
defer func() {
err := cont.Stop()
assert.NoError(t, err)
}()
err = cont.Start()
assert.NoError(t, err)
exec, err := NewExecutionFromString(cont, "ls -l")
assert.NoError(t, err)
assert.NotNil(t, exec)
out, err := exec.Output()
assert.NoError(t, err, "execution should not return an error")
assert.NotEmpty(t, out, "the output cannot be nil")
}
/*
func TestExecutionOutput2(t *testing.T) {
config.Init()
client, err := NewClient()
assert.NoError(t, err)
assert.NotNil(t, client)
cont, err := NewContainer(client)
assert.NoError(t, err)
assert.NotNil(t, cont)
defer func() {
err := cont.Stop()
assert.NoError(t, err)
}()
err = cont.Start()
assert.NoError(t, err)
run := func(cmd string) {
exec, err := NewExecutionFromString(cont, cmd)
assert.NoError(t, err)
assert.NotNil(t, exec)
out, err := exec.Output()
assert.NoError(t, err, "execution should not return an error")
assert.NotEmpty(t, out, "the output cannot be nil")
}
run("ls -l")
run("ls -l /")
}
*/
func TestExec(t *testing.T) {
c, err := NewExecTestSuite(t)
if !assert.NoError(t, err, "Failed to create docker client") {
return
}
suite.Run(t, c)
}