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