/
fake.go
62 lines (53 loc) · 1.61 KB
/
fake.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
package clitest
import (
"bufio"
"context"
"net"
"os"
"strings"
dockertypes "github.com/docker/docker/api/types"
"github.com/spf13/afero"
testingexec "k8s.io/utils/exec/testing"
"github.com/coder/envbox/dockerutil"
"github.com/coder/envbox/dockerutil/dockerfake"
"github.com/coder/envbox/xunix/xunixfake"
)
func NewMemFS() *xunixfake.MemFS {
return &xunixfake.MemFS{
MemMapFs: &afero.MemMapFs{},
Owner: map[string]xunixfake.FileOwner{},
}
}
func NewFakeExecer() *xunixfake.FakeExec {
return &xunixfake.FakeExec{
Commands: map[string]*xunixfake.FakeCmd{},
DefaultFakeCmd: &xunixfake.FakeCmd{
FakeCmd: &testingexec.FakeCmd{},
FakeProcess: &os.Process{Pid: 1111},
// The main use of exec commands in this repo
// are to spawn daemon processes so ideally the
// default behavior is that they do not exist.
// nolint
WaitFn: func() error { select {} },
},
}
}
func NewFakeDockerClient() dockerutil.DockerClient {
client := &dockerfake.MockClient{}
client.ContainerInspectFn = func(_ context.Context, container string) (dockertypes.ContainerJSON, error) {
return dockertypes.ContainerJSON{
ContainerJSONBase: &dockertypes.ContainerJSONBase{
GraphDriver: dockertypes.GraphDriverData{
Data: map[string]string{"MergedDir": "blah"},
},
},
}, nil
}
client.ContainerExecAttachFn = func(_ context.Context, execID string, config dockertypes.ExecStartCheck) (dockertypes.HijackedResponse, error) {
return dockertypes.HijackedResponse{
Reader: bufio.NewReader(strings.NewReader("root:x:0:0:root:/root:/bin/bash")),
Conn: &net.IPConn{},
}, nil
}
return client
}