Debian Codegraph @

examples/complex-etcd/main.go

raw
1 // Example and test
2
3 package main
4
5 import (
6 "context"
7 "fmt"
8 "log"
9 "os/exec"
10 "strings"
11 "time"
12
13 "github.com/knadh/koanf/parsers/json"
14 "github.com/knadh/koanf/providers/etcd/v2"
15 "github.com/knadh/koanf/providers/file"
16 "github.com/knadh/koanf/v2"
17
18 clientv3 "go.etcd.io/etcd/client/v3"
19 )
20
21 var (
22 kData = koanf.New(".")
23 kReq = koanf.New(".")
24 kCheck = koanf.New(".")
25 )
26
27 func main() {
28 if err := kData.Load(file.Provider("data.json"), json.Parser()); err != nil {
29 log.Fatalf("error loading config: %v", err)
30 }
31
32 cli, err := clientv3.New(clientv3.Config{
33 Endpoints: []string{"localhost:2379"},
34 DialTimeout: time.Second * 2,
35 })
36
37 if err != nil {
38 log.Fatal("Cannot create a client by clientv3.New().")
39 }
40 defer cli.Close()
41
42 keysData := kData.Keys()
43 for i := 0; i < len(keysData); i++ {
44 ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
45 _, err := cli.Put(ctx, keysData[i], kData.String(keysData[i]))
46 cancel()
47
48 if err != nil {
49 log.Printf("Couldn't put key.")
50 }
51 }
52
53 // Single key/value.
54 var (
55 sKey = "single_key"
56 sVal = "single_val"
57 )
58
59 ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
60 _, err = cli.Put(ctx, sKey, sVal)
61 defer cancel()
62
63 if err != nil {
64 log.Printf("Couldn't put key.")
65 }
66
67 providerCfg := etcd.Config{
68 Endpoints: []string{"localhost:2379"},
69 DialTimeout: time.Second * 5,
70 Prefix: false,
71 Key: "single_key",
72 }
73 provider, err := etcd.Provider(providerCfg)
74 if err != nil {
75 log.Fatalf("Failed to instantiate etcd provider: %v", err)
76 }
77
78 if err := kCheck.Load(provider, nil); err != nil {
79 log.Fatalf("error loading config: %v", err)
80 }
81
82 if len(kCheck.Keys()) != 1 {
83 fmt.Println(len(kCheck.Keys()))
84 fmt.Printf("Single key: FAILED\n")
85 return
86 }
87
88 if strings.Compare(sKey, kCheck.Keys()[0]) != 0 {
89 fmt.Printf("Single key: key comparison FAILED\n")
90 return
91 }
92
93 if strings.Compare(sVal, kCheck.String(kCheck.Keys()[0])) != 0 {
94 fmt.Printf("Single key: value comparison FAILED\n")
95 return
96 }
97
98 fmt.Printf("\nSingle key test passed.\n")
99 kCheck.Delete("")
100
101 // first request test
102 // analog of the command:
103 // etcdctl get --prefix parent
104
105 if err := kReq.Load(file.Provider("req1.json"), json.Parser()); err != nil {
106 log.Fatalf("error loading config: %v", err)
107 }
108
109 providerCfg = etcd.Config{
110 Endpoints: []string{"localhost:2379"},
111 DialTimeout: time.Second * 5,
112 Prefix: true,
113 Key: "parent",
114 }
115 provider, err = etcd.Provider(providerCfg)
116 if err != nil {
117 log.Fatalf("Failed to instantiate etcd provider: %v", err)
118 }
119
120 if err := kCheck.Load(provider, nil); err != nil {
121 log.Fatalf("error loading config: %v", err)
122 }
123
124 keysReq := kReq.Keys()
125 keysCheck := kCheck.Keys()
126
127 if len(keysReq) != len(keysCheck) {
128 fmt.Printf("First request: keys FAILED\n")
129 return
130 }
131
132 for i := 0; i < len(keysReq); i++ {
133 if strings.Compare(keysReq[i], keysCheck[i]) != 0 {
134 fmt.Printf("First request: key comparison FAILED\n")
135 return
136 }
137
138 if strings.Compare(kReq.String(keysReq[i]), kCheck.String(keysCheck[i])) != 0 {
139 fmt.Printf("First request: value comparison FAILED\n")
140 return
141 }
142 }
143
144 fmt.Printf("First request test passed.\n")
145 kReq.Delete("")
146 kCheck.Delete("")
147
148 // second request test
149 // analog of the command:
150 // etcdctl get --prefix child
151
152 if err := kReq.Load(file.Provider("req2.json"), json.Parser()); err != nil {
153 log.Fatalf("error loading config: %v", err)
154 }
155
156 providerCfg = etcd.Config{
157 Endpoints: []string{"localhost:2379"},
158 DialTimeout: time.Second * 5,
159 Prefix: true,
160 Key: "child",
161 }
162
163 provider, err = etcd.Provider(providerCfg)
164 if err != nil {
165 log.Fatalf("Failed to instantiate etcd provider: %v", err)
166 }
167
168 if err := kCheck.Load(provider, nil); err != nil {
169 log.Fatalf("error loading config: %v", err)
170 }
171
172 keysReq = kReq.Keys()
173 keysCheck = kCheck.Keys()
174
175 if len(keysReq) != len(keysCheck) {
176 fmt.Printf("Second request: keys FAILED\n")
177 return
178 }
179
180 for i := 0; i < len(keysReq); i++ {
181 if strings.Compare(keysReq[i], keysCheck[i]) != 0 {
182 fmt.Printf("Second request: key comparison FAILED\n")
183 return
184 }
185
186 if strings.Compare(kReq.String(keysReq[i]), kCheck.String(keysCheck[i])) != 0 {
187 fmt.Printf("Second request: value comparison FAILED\n")
188 return
189 }
190 }
191
192 fmt.Printf("Second request test passed.\n")
193 kReq.Delete("")
194 kCheck.Delete("")
195
196 // third (combined prefix + limit) request test
197 // analog of the command:
198 // etcdctl get --prefix child --limit=4
199
200 if err := kReq.Load(file.Provider("req3.json"), json.Parser()); err != nil {
201 log.Fatalf("error loading config: %v", err)
202 }
203
204 providerCfg = etcd.Config{
205 Endpoints: []string{"localhost:2379"},
206 DialTimeout: time.Second * 5,
207 Prefix: true,
208 Limit: true,
209 NLimit: 4,
210 Key: "child",
211 }
212
213 provider, err = etcd.Provider(providerCfg)
214 if err != nil {
215 log.Fatalf("Failed to instantiate etcd provider: %v", err)
216 }
217
218 if err := kCheck.Load(provider, nil); err != nil {
219 log.Fatalf("error loading config: %v", err)
220 }
221
222 keysReq = kReq.Keys()
223 keysCheck = kCheck.Keys()
224
225 if len(keysReq) != len(keysCheck) {
226 fmt.Printf("Third request: keys FAILED\n")
227 return
228 }
229
230 for i := 0; i < len(keysReq); i++ {
231 if strings.Compare(keysReq[i], keysCheck[i]) != 0 {
232 fmt.Printf("Third request: key comparison FAILED\n")
233 return
234 }
235
236 if strings.Compare(kReq.String(keysReq[i]), kCheck.String(keysCheck[i])) != 0 {
237 fmt.Printf("Third request: value comparison FAILED\n")
238 return
239 }
240 }
241
242 fmt.Printf("Third (combined) request test passed.\n")
243
244 kCheck.Delete("")
245
246 // Watch test
247
248 sKey = "child"
249 providerCfg = etcd.Config{
250 Endpoints: []string{"localhost:2379"},
251 DialTimeout: time.Second * 5,
252 Prefix: true,
253 Key: "child",
254 }
255
256 provider, err = etcd.Provider(providerCfg)
257 if err != nil {
258 log.Fatalf("Failed to instantiate etcd provider: %v", err)
259 }
260
261 if err := kCheck.Load(provider, nil); err != nil {
262 log.Fatalf("error loading config: %v", err)
263 }
264
265 changedC := make(chan string, 1)
266
267 provider.Watch(func(event any, err error) {
268 if err != nil {
269 fmt.Printf("Unexpected error: %v", err)
270 return
271 }
272
273 kCheck.Load(provider, nil)
274 changedC <- kCheck.String(string(event.(*clientv3.Event).Kv.Key))
275 })
276
277 var newVal string = "Brian"
278 cmd := exec.Command("etcdctl", "put", "child1", newVal)
279 err = cmd.Run()
280 if err != nil {
281 log.Fatal(err)
282 }
283
284 if strings.Compare(newVal, <-changedC) != 0 {
285 fmt.Printf("Watch failed: new value comparison FAILED\n")
286 return
287 }
288
289 newVal = "Kate"
290 cmd = exec.Command("etcdctl", "put", "child2", newVal)
291 err = cmd.Run()
292 if err != nil {
293 log.Fatal(err)
294 }
295
296 if strings.Compare(newVal, <-changedC) != 0 {
297 fmt.Printf("Watch failed: new value comparison FAILED\n")
298 return
299 }
300 fmt.Printf("Watch test passed.\n")
301
302 fmt.Printf("ALL TESTS PASSED\n")
303 }