forked from yext/cloudprober
/
rtcservice_stub.go
87 lines (75 loc) · 2.54 KB
/
rtcservice_stub.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
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rtcservice
import (
"encoding/base64"
"errors"
runtimeconfig "google.golang.org/api/runtimeconfig/v1beta1"
)
// Stub provides a stubbed version of RTC that can be used for testing larger
// components.
type Stub struct {
m map[string]*runtimeconfig.Variable
}
// NewStub returns a Stub interface to RTC which will store all variables in an
// in-memory map.
func NewStub() *Stub {
return &Stub{make(map[string]*runtimeconfig.Variable)}
}
// GetProject is needed in order to correctly implement the Configuration interface
func (s *Stub) GetProject() string {
return ""
}
// Write will add or change a key/val pair.
func (s *Stub) Write(key string, val []byte) error {
encoded := base64.StdEncoding.EncodeToString(val)
s.m[key] = &runtimeconfig.Variable{Name: key, Value: encoded}
return nil
}
// WriteTime adds or changes a key/val pair, but with the UpdateTime field
// set to the given string. This is passed in as a string rather than a time so
// invalid times can be tested.
func (s *Stub) WriteTime(key string, val string, time string) error {
s.m[key] = &runtimeconfig.Variable{Name: key, Value: val, UpdateTime: time}
return nil
}
// Delete removes a key/val pair.
func (s *Stub) Delete(key string) error {
_, ok := s.m[key]
if ok {
delete(s.m, key)
return nil
}
return errors.New("rtc_stub: key not in map")
}
// Val gets the value associated with a variable.
func (s *Stub) Val(v *runtimeconfig.Variable) ([]byte, error) {
return base64.StdEncoding.DecodeString(v.Value)
}
// List provides all vals in the map.
func (s *Stub) List() ([]*runtimeconfig.Variable, error) {
vals := make([]*runtimeconfig.Variable, 0, len(s.m))
for _, v := range s.m {
vals = append(vals, &runtimeconfig.Variable{
Name: v.Name,
Value: v.Value,
UpdateTime: v.UpdateTime,
})
}
return vals, nil
}
// FilterList is not supported by this Stub. Simply returns s.List().
func (s *Stub) FilterList(filter string) ([]*runtimeconfig.Variable, error) {
return s.List()
}