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