github.com/greenboxal/deis@v1.12.1/deisctl/test/mock/config.go (about) 1 package mock 2 3 import ( 4 "fmt" 5 "regexp" 6 7 "github.com/deis/deis/deisctl/config/model" 8 ) 9 10 // Store for a mocked config backend 11 type Store []*model.ConfigNode 12 13 // ConfigBackend is an in memory "mock" config datastore used for testing 14 type ConfigBackend struct { 15 Expected Store 16 } 17 18 // Get a value by key from an in memory config backend 19 func (cb ConfigBackend) Get(key string) (value string, err error) { 20 for _, expect := range cb.Expected { 21 if expect.Key == key { 22 return expect.Value, nil 23 } 24 } 25 return "", fmt.Errorf("%s does not exist", cb.Expected) 26 } 27 28 // GetWithDefault gets a value by key from an in memory config backend and 29 // return a default value if not found 30 func (cb ConfigBackend) GetWithDefault(key string, defaultValue string) (string, error) { 31 for _, expect := range cb.Expected { 32 if expect.Key == key { 33 return expect.Value, nil 34 } 35 } 36 return defaultValue, nil 37 } 38 39 // Set a value for the specified key in an in memory config backend 40 func (cb ConfigBackend) Set(key, value string) (returnedValue string, err error) { 41 for _, expect := range cb.Expected { 42 if expect.Key == key { 43 return value, nil 44 } 45 } 46 return "", fmt.Errorf("%s does not exist", cb.Expected) 47 } 48 49 // Delete a key/value pair by key from an in memory config backend 50 func (cb ConfigBackend) Delete(key string) (err error) { 51 for _, expect := range cb.Expected { 52 if expect.Key == key { 53 return nil 54 } 55 } 56 return fmt.Errorf("%s does not exist", cb.Expected) 57 } 58 59 // GetRecursive returns a slice of all key/value pairs "under" a specified key 60 // in an in memory config backend (this is assuming some hierarchichal 61 // order exists wherein the value corresponding to a key may in fact be another 62 // key/value pair) 63 func (cb ConfigBackend) GetRecursive(key string) ([]*model.ConfigNode, error) { 64 r, _ := regexp.Compile(`^deis/services\s+`) 65 var configNodes []*model.ConfigNode 66 67 for _, expect := range cb.Expected { 68 if r.MatchString(expect.Key) { 69 configNodes = append(configNodes, expect) 70 } 71 } 72 return configNodes, nil 73 } 74 75 // SetWithTTL sets a value for the specified key in an in memory config 76 // backend-- with a time to live 77 func (cb ConfigBackend) SetWithTTL(key string, value string, ttl uint64) (string, error) { 78 for _, expect := range cb.Expected { 79 if expect.Key == key { 80 expect.TTL = int64(ttl) 81 return expect.Key, nil 82 } 83 } 84 return "", fmt.Errorf("%s does not exist", cb.Expected) 85 }