forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corednsapi.go
85 lines (70 loc) · 2.59 KB
/
corednsapi.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
/*
Copyright 2016 The Kubernetes Authors.
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 stubs implements a stub for the EtcdKeysAPI, used primarily for unit testing purposes
package stubs
import (
"strings"
etcd "github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
// Compile time check for interface conformance
var _ EtcdKeysAPI = &EtcdKeysAPIStub{}
type EtcdKeysAPI interface {
Set(context context.Context, key, value string, options *etcd.SetOptions) (*etcd.Response, error)
Get(context context.Context, key string, options *etcd.GetOptions) (*etcd.Response, error)
Delete(context context.Context, key string, options *etcd.DeleteOptions) (*etcd.Response, error)
}
type EtcdKeysAPIStub struct {
writes map[string]string
}
// NewEtcdKeysAPIStub returns an initialized EtcdKeysAPIStub
func NewEtcdKeysAPIStub() *EtcdKeysAPIStub {
return &EtcdKeysAPIStub{make(map[string]string)}
}
func (ec *EtcdKeysAPIStub) Set(context context.Context, key, value string, options *etcd.SetOptions) (*etcd.Response, error) {
ec.writes[key] = value
return nil, nil
}
func (ec *EtcdKeysAPIStub) Delete(context context.Context, key string, options *etcd.DeleteOptions) (*etcd.Response, error) {
for p := range ec.writes {
if (options.Recursive && strings.HasPrefix(p, key)) || (!options.Recursive && p == key) {
delete(ec.writes, p)
}
}
return nil, nil
}
func (ec *EtcdKeysAPIStub) Get(context context.Context, key string, options *etcd.GetOptions) (*etcd.Response, error) {
nodes := ec.GetAll(key)
if len(nodes) == 0 {
return nil, nil
}
if len(nodes) == 1 && nodes[key] != "" {
return &etcd.Response{Node: &etcd.Node{Key: key, Value: nodes[key], Dir: false}}, nil
}
node := &etcd.Node{Key: key, Dir: true, Nodes: etcd.Nodes{}}
for k, v := range nodes {
n := &etcd.Node{Key: k, Value: v}
node.Nodes = append(node.Nodes, n)
}
return &etcd.Response{Node: node}, nil
}
func (ec *EtcdKeysAPIStub) GetAll(key string) map[string]string {
nodes := make(map[string]string)
key = strings.ToLower(key)
for path := range ec.writes {
if strings.HasPrefix(path, key) {
nodes[path] = ec.writes[path]
}
}
return nodes
}