github.com/google/cloudprober@v0.11.3/targets/rtc/rtcservice/rtcservice_stub.go (about)

     1  // Copyright 2017 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rtcservice
    16  
    17  import (
    18  	"encoding/base64"
    19  	"errors"
    20  
    21  	runtimeconfig "google.golang.org/api/runtimeconfig/v1beta1"
    22  )
    23  
    24  // Stub provides a stubbed version of RTC that can be used for testing larger
    25  // components.
    26  type Stub struct {
    27  	m map[string]*runtimeconfig.Variable
    28  }
    29  
    30  // NewStub returns a Stub interface to RTC which will store all variables in an
    31  // in-memory map.
    32  func NewStub() *Stub {
    33  	return &Stub{make(map[string]*runtimeconfig.Variable)}
    34  }
    35  
    36  // GetProject is needed in order to correctly implement the Configuration interface
    37  func (s *Stub) GetProject() string {
    38  	return ""
    39  }
    40  
    41  // Write will add or change a key/val pair.
    42  func (s *Stub) Write(key string, val []byte) error {
    43  	encoded := base64.StdEncoding.EncodeToString(val)
    44  	s.m[key] = &runtimeconfig.Variable{Name: key, Value: encoded}
    45  	return nil
    46  }
    47  
    48  // WriteTime adds or changes a key/val pair, but with the UpdateTime field
    49  // set to the given string. This is passed in as a string rather than a time so
    50  // invalid times can be tested.
    51  func (s *Stub) WriteTime(key string, val string, time string) error {
    52  	s.m[key] = &runtimeconfig.Variable{Name: key, Value: val, UpdateTime: time}
    53  	return nil
    54  }
    55  
    56  // Delete removes a key/val pair.
    57  func (s *Stub) Delete(key string) error {
    58  	_, ok := s.m[key]
    59  	if ok {
    60  		delete(s.m, key)
    61  		return nil
    62  	}
    63  	return errors.New("rtc_stub: key not in map")
    64  }
    65  
    66  // Val gets the value associated with a variable.
    67  func (s *Stub) Val(v *runtimeconfig.Variable) ([]byte, error) {
    68  	return base64.StdEncoding.DecodeString(v.Value)
    69  }
    70  
    71  // List provides all vals in the map.
    72  func (s *Stub) List() ([]*runtimeconfig.Variable, error) {
    73  	vals := make([]*runtimeconfig.Variable, 0, len(s.m))
    74  	for _, v := range s.m {
    75  		vals = append(vals, &runtimeconfig.Variable{
    76  			Name:       v.Name,
    77  			Value:      v.Value,
    78  			UpdateTime: v.UpdateTime,
    79  		})
    80  	}
    81  	return vals, nil
    82  }
    83  
    84  // FilterList is not supported by this Stub. Simply returns s.List().
    85  func (s *Stub) FilterList(filter string) ([]*runtimeconfig.Variable, error) {
    86  	return s.List()
    87  }