forked from cloudfoundry-attic/garden-linux
/
fake_container_pool.go
115 lines (83 loc) · 2.11 KB
/
fake_container_pool.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
package fake_container_pool
import (
"fmt"
"io"
"github.com/cloudfoundry-incubator/garden-linux/old/linux_backend"
"github.com/cloudfoundry-incubator/garden/api"
"github.com/nu7hatch/gouuid"
)
type FakeContainerPool struct {
DidSetup bool
MaxContainersValue int
Pruned bool
PruneError error
KeptContainers map[string]bool
CreateError error
RestoreError error
DestroyError error
ContainerSetup func(*FakeContainer)
CreatedContainers []linux_backend.Container
DestroyedContainers []linux_backend.Container
RestoredSnapshots []io.Reader
}
func New() *FakeContainerPool {
return &FakeContainerPool{}
}
func (p *FakeContainerPool) MaxContainers() int {
return p.MaxContainersValue
}
func (p *FakeContainerPool) Setup() error {
p.DidSetup = true
return nil
}
func (p *FakeContainerPool) Prune(keep map[string]bool) error {
if p.PruneError != nil {
return p.PruneError
}
p.Pruned = true
p.KeptContainers = keep
return nil
}
func (p *FakeContainerPool) Create(spec api.ContainerSpec) (linux_backend.Container, error) {
if p.CreateError != nil {
return nil, p.CreateError
}
idUUID, err := uuid.NewV4()
if err != nil {
panic("could not create uuid: " + err.Error())
}
id := idUUID.String()[:11]
if spec.Handle == "" {
spec.Handle = id
}
container := NewFakeContainer(spec)
if p.ContainerSetup != nil {
p.ContainerSetup(container)
}
p.CreatedContainers = append(p.CreatedContainers, container)
return container, nil
}
func (p *FakeContainerPool) Restore(snapshot io.Reader) (linux_backend.Container, error) {
if p.RestoreError != nil {
return nil, p.RestoreError
}
var handle string
_, err := fmt.Fscanf(snapshot, "%s", &handle)
if err != nil && err != io.EOF {
return nil, err
}
container := NewFakeContainer(
api.ContainerSpec{
Handle: handle,
},
)
p.RestoredSnapshots = append(p.RestoredSnapshots, snapshot)
return container, nil
}
func (p *FakeContainerPool) Destroy(container linux_backend.Container) error {
if p.DestroyError != nil {
return p.DestroyError
}
p.DestroyedContainers = append(p.DestroyedContainers, container)
return nil
}