/
utils.go
81 lines (71 loc) · 1.9 KB
/
utils.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
package nsmgr_test
import (
"path/filepath"
"time"
"github.com/cri-o/cri-o/internal/config/nsmgr"
"github.com/cri-o/cri-o/internal/oci"
"github.com/cri-o/cri-o/internal/storage"
"github.com/cri-o/cri-o/internal/storage/references"
"github.com/opencontainers/runtime-spec/specs-go"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
type SpoofedNamespace struct {
NsType nsmgr.NSType
EmptyPath bool
}
func (s *SpoofedNamespace) Type() nsmgr.NSType {
return s.NsType
}
func (s *SpoofedNamespace) Remove() error {
return nil
}
func (s *SpoofedNamespace) Path() string {
if s.EmptyPath {
return ""
}
return filepath.Join("tmp", string(s.NsType))
}
func (s *SpoofedNamespace) Close() error {
return nil
}
var AllSpoofedNamespaces = []nsmgr.Namespace{
&SpoofedNamespace{
NsType: nsmgr.IPCNS,
},
&SpoofedNamespace{
NsType: nsmgr.UTSNS,
},
&SpoofedNamespace{
NsType: nsmgr.NETNS,
},
&SpoofedNamespace{
NsType: nsmgr.USERNS,
},
}
func ContainerWithPid(pid int) (*oci.Container, error) {
imageName, err := references.ParseRegistryImageReferenceFromOutOfProcessData("example.com/some-image:latest")
if err != nil {
return nil, err
}
imageID, err := storage.ParseStorageImageIDFromOutOfProcessData("2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812")
if err != nil {
return nil, err
}
testContainer, err := oci.NewContainer("testid", "testname", "",
"/container/logs", map[string]string{},
map[string]string{}, map[string]string{}, "image",
&imageName, &imageID, "", &types.ContainerMetadata{},
"testsandboxid", false, false, false, "",
"/root/for/container", time.Now(), "SIGKILL")
if err != nil {
return nil, err
}
cstate := &oci.ContainerState{}
cstate.State = specs.State{
Pid: pid,
}
// eat error here because callers may send invalid pids to test against
_ = cstate.SetInitPid(pid) // nolint:errcheck
testContainer.SetState(cstate)
return testContainer, nil
}