khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "fmt" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | ETCD_KV = "etcd" |
| 26 | CONSUL_KV = "consul" |
| 27 | INVALID_KV = "invalid" |
| 28 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 29 | etcd_host = "10.104.149.247" |
| 30 | etcd_port = 2379 |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | To debug locally with the remote ETCD container |
| 34 | |
| 35 | ssh -f -N vagrant@10.100.198.220 -L 22379:10.104.149.247:2379 |
| 36 | */ |
| 37 | //etcd_host = "localhost" |
| 38 | //etcd_port = 22379 |
| 39 | consul_host = "k8s-consul" |
| 40 | consul_port = 30080 |
| 41 | timeout = 5 |
| 42 | prefix = "backend/test" |
| 43 | key = "stephane/1" |
| 44 | value = "barbarie" |
| 45 | ) |
| 46 | |
| 47 | var ( |
| 48 | etcd_backend *Backend |
| 49 | consul_backend *Backend |
| 50 | ) |
| 51 | |
| 52 | func Test_Etcd_Backend_New(t *testing.T) { |
| 53 | etcd_backend = NewBackend(ETCD_KV, etcd_host, etcd_port, timeout, prefix) |
| 54 | } |
| 55 | |
| 56 | func Test_Etcd_Backend_Put(t *testing.T) { |
| 57 | etcd_backend.Put(key, value) |
| 58 | |
| 59 | } |
| 60 | |
| 61 | func Test_Etcd_Backend_Get(t *testing.T) { |
| 62 | if pair, err := etcd_backend.Client.Get("service/voltha/data/core/0001/root", timeout); err != nil { |
| 63 | t.Errorf("backend get failed - %s", err.Error()) |
| 64 | } else { |
| 65 | j, _ := json.Marshal(pair) |
| 66 | t.Logf("pair: %s", string(j)) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func Test_Etcd_Backend_GetRoot(t *testing.T) { |
| 71 | if pair, err := etcd_backend.Get(key); err != nil { |
| 72 | t.Errorf("backend get failed - %s", err.Error()) |
| 73 | } else { |
| 74 | j, _ := json.Marshal(pair) |
| 75 | t.Logf("pair: %s", string(j)) |
| 76 | if pair.Key != (prefix + "/" + key) { |
| 77 | t.Errorf("backend key differs - key: %s, expected: %s", pair.Key, key) |
| 78 | } |
| 79 | |
| 80 | s := fmt.Sprintf("%s", pair.Value) |
| 81 | if s != value { |
| 82 | t.Errorf("backend value differs - value: %s, expected:%s", pair.Value, value) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func Test_Etcd_Backend_Delete(t *testing.T) { |
| 88 | if err := etcd_backend.Delete(key); err != nil { |
| 89 | t.Errorf("backend delete failed - %s", err.Error()) |
| 90 | } |
| 91 | //if _, err := backend.Client.Get(key, Timeout); err == nil { |
| 92 | // t.Errorf("backend delete failed - %s", err.Error()) |
| 93 | //} |
| 94 | } |
| 95 | |
| 96 | func Test_Consul_Backend_New(t *testing.T) { |
| 97 | consul_backend = NewBackend(CONSUL_KV, consul_host, consul_port, timeout, prefix) |
| 98 | } |
| 99 | |
| 100 | func Test_Consul_Backend_Put(t *testing.T) { |
| 101 | consul_backend.Put(key, value) |
| 102 | |
| 103 | } |
| 104 | |
| 105 | func Test_Consul_Backend_Get(t *testing.T) { |
| 106 | if pair, err := consul_backend.Get(key); err != nil { |
| 107 | t.Errorf("backend get failed - %s", err.Error()) |
| 108 | } else { |
| 109 | j, _ := json.Marshal(pair) |
| 110 | t.Logf("pair: %s", string(j)) |
| 111 | if pair.Key != (prefix + "/" + key) { |
| 112 | t.Errorf("backend key differs - key: %s, expected: %s", pair.Key, key) |
| 113 | } |
| 114 | |
| 115 | v := fmt.Sprintf("%s", pair.Value) |
| 116 | if v != value { |
| 117 | t.Errorf("backend value differs - value: %s, expected:%s", pair.Value, value) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func Test_Consul_Backend_Delete(t *testing.T) { |
| 123 | if err := consul_backend.Delete(key); err != nil { |
| 124 | t.Errorf("backend delete failed - %s", err.Error()) |
| 125 | } |
| 126 | //if _, err := backend.Client.Get(key, Timeout); err == nil { |
| 127 | // t.Errorf("backend delete failed - %s", err.Error()) |
| 128 | //} |
| 129 | } |