github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/container_pool/fake_container_pool/fake_container_pool.go (about) 1 package fake_container_pool 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/cloudfoundry-incubator/garden" 8 "github.com/cloudfoundry-incubator/garden-linux/linux_backend" 9 "github.com/nu7hatch/gouuid" 10 ) 11 12 type FakeContainerPool struct { 13 DidSetup bool 14 15 MaxContainersValue int 16 17 Pruned bool 18 PruneError error 19 KeptContainers map[string]bool 20 21 CreateError error 22 RestoreError error 23 DestroyError error 24 25 ContainerSetup func(*FakeContainer) 26 27 CreatedContainers []linux_backend.Container 28 DestroyedContainers []linux_backend.Container 29 RestoredSnapshots []io.Reader 30 } 31 32 func New() *FakeContainerPool { 33 return &FakeContainerPool{} 34 } 35 36 func (p *FakeContainerPool) MaxContainers() int { 37 return p.MaxContainersValue 38 } 39 40 func (p *FakeContainerPool) Setup() error { 41 p.DidSetup = true 42 43 return nil 44 } 45 46 func (p *FakeContainerPool) Prune(keep map[string]bool) error { 47 if p.PruneError != nil { 48 return p.PruneError 49 } 50 51 p.Pruned = true 52 p.KeptContainers = keep 53 54 return nil 55 } 56 57 func (p *FakeContainerPool) Create(spec garden.ContainerSpec) (linux_backend.Container, error) { 58 if p.CreateError != nil { 59 return nil, p.CreateError 60 } 61 62 idUUID, err := uuid.NewV4() 63 if err != nil { 64 panic("could not create uuid: " + err.Error()) 65 } 66 67 id := idUUID.String()[:11] 68 69 if spec.Handle == "" { 70 spec.Handle = id 71 } 72 73 container := NewFakeContainer(spec) 74 75 if p.ContainerSetup != nil { 76 p.ContainerSetup(container) 77 } 78 79 p.CreatedContainers = append(p.CreatedContainers, container) 80 81 return container, nil 82 } 83 84 func (p *FakeContainerPool) Restore(snapshot io.Reader) (linux_backend.Container, error) { 85 if p.RestoreError != nil { 86 return nil, p.RestoreError 87 } 88 89 var handle string 90 91 _, err := fmt.Fscanf(snapshot, "%s", &handle) 92 if err != nil && err != io.EOF { 93 return nil, err 94 } 95 96 container := NewFakeContainer( 97 garden.ContainerSpec{ 98 Handle: handle, 99 }, 100 ) 101 102 p.RestoredSnapshots = append(p.RestoredSnapshots, snapshot) 103 104 return container, nil 105 } 106 107 func (p *FakeContainerPool) Destroy(container linux_backend.Container) error { 108 if p.DestroyError != nil { 109 return p.DestroyError 110 } 111 112 p.DestroyedContainers = append(p.DestroyedContainers, container) 113 114 return nil 115 }