github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/fake_etcd_client.go (about)

     1  /*
     2  Copyright 2014 Google Inc. All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package registry
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/coreos/go-etcd/etcd"
    23  )
    24  
    25  type EtcdResponseWithError struct {
    26  	R *etcd.Response
    27  	E error
    28  }
    29  
    30  type FakeEtcdClient struct {
    31  	Data        map[string]EtcdResponseWithError
    32  	deletedKeys []string
    33  	err         error
    34  	t           *testing.T
    35  }
    36  
    37  func MakeFakeEtcdClient(t *testing.T) *FakeEtcdClient {
    38  	return &FakeEtcdClient{
    39  		t:    t,
    40  		Data: map[string]EtcdResponseWithError{},
    41  	}
    42  }
    43  
    44  func (f *FakeEtcdClient) AddChild(key, data string, ttl uint64) (*etcd.Response, error) {
    45  	return f.Set(key, data, ttl)
    46  }
    47  
    48  func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error) {
    49  	result := f.Data[key]
    50  	if result.R == nil {
    51  		f.t.Errorf("Unexpected get for %s", key)
    52  		return &etcd.Response{}, &etcd.EtcdError{ErrorCode: 100}
    53  	}
    54  	return result.R, result.E
    55  }
    56  
    57  func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, error) {
    58  	result := EtcdResponseWithError{
    59  		R: &etcd.Response{
    60  			Node: &etcd.Node{
    61  				Value: value,
    62  			},
    63  		},
    64  	}
    65  	f.Data[key] = result
    66  	return result.R, f.err
    67  }
    68  func (f *FakeEtcdClient) Create(key, value string, ttl uint64) (*etcd.Response, error) {
    69  	return f.Set(key, value, ttl)
    70  }
    71  func (f *FakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, error) {
    72  	f.deletedKeys = append(f.deletedKeys, key)
    73  	return &etcd.Response{}, f.err
    74  }
    75  
    76  func (f *FakeEtcdClient) Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error) {
    77  	return nil, fmt.Errorf("Unimplemented")
    78  }
    79  
    80  func MakeTestEtcdRegistry(client EtcdClient, machines []string) *EtcdRegistry {
    81  	registry := MakeEtcdRegistry(client, machines)
    82  	registry.manifestFactory = &BasicManifestFactory{
    83  		serviceRegistry: &MockServiceRegistry{},
    84  	}
    85  	return registry
    86  }