github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/cgroups_manager/fake_cgroups_manager/fake_cgroups_manager.go (about) 1 package fake_cgroups_manager 2 3 import ( 4 "path" 5 ) 6 7 type FakeCgroupsManager struct { 8 cgroupsPath string 9 id string 10 11 SetError error 12 13 setValues []SetValue 14 getCallbacks []GetCallback 15 setCallbacks []SetCallback 16 } 17 18 type SetValue struct { 19 Subsystem string 20 Name string 21 Value string 22 } 23 24 type GetCallback struct { 25 Subsystem string 26 Name string 27 Callback func() (string, error) 28 } 29 30 type SetCallback struct { 31 Subsystem string 32 Name string 33 Callback func() error 34 } 35 36 func New(cgroupsPath, id string) *FakeCgroupsManager { 37 return &FakeCgroupsManager{ 38 cgroupsPath: cgroupsPath, 39 id: id, 40 } 41 } 42 43 func (m *FakeCgroupsManager) Set(subsystem, name, value string) error { 44 if m.SetError != nil { 45 return m.SetError 46 } 47 48 for _, cb := range m.setCallbacks { 49 if cb.Subsystem == subsystem && cb.Name == name { 50 return cb.Callback() 51 } 52 } 53 54 m.setValues = append(m.setValues, SetValue{subsystem, name, value}) 55 56 return nil 57 } 58 59 func (m *FakeCgroupsManager) Get(subsytem, name string) (string, error) { 60 for _, cb := range m.getCallbacks { 61 if cb.Subsystem == subsytem && cb.Name == name { 62 return cb.Callback() 63 } 64 } 65 66 for _, val := range m.setValues { 67 if val.Subsystem == subsytem && val.Name == name { 68 return val.Value, nil 69 } 70 } 71 72 return "", nil 73 } 74 75 func (m *FakeCgroupsManager) SubsystemPath(subsystem string) string { 76 return path.Join(m.cgroupsPath, subsystem, "instance-"+m.id) 77 } 78 79 func (m *FakeCgroupsManager) SetValues() []SetValue { 80 return m.setValues 81 } 82 83 func (m *FakeCgroupsManager) WhenGetting(subsystem, name string, callback func() (string, error)) { 84 m.getCallbacks = append(m.getCallbacks, GetCallback{subsystem, name, callback}) 85 } 86 87 func (m *FakeCgroupsManager) WhenSetting(subsystem, name string, callback func() error) { 88 m.setCallbacks = append(m.setCallbacks, SetCallback{subsystem, name, callback}) 89 }