-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage_test.go
111 lines (93 loc) · 2.82 KB
/
storage_test.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 utils
import (
"fmt"
"github.com/samuel/go-zookeeper/zk"
"os"
"strings"
"testing"
"time"
)
func TestFileStorage(t *testing.T) {
file := "tmp_file_storage.txt"
contents := "hello world"
defer func() {
os.Remove(file)
}()
storage := NewFileStorage(file)
err := storage.Save([]byte(contents))
if err != nil {
t.Fatalf("Error while saving to file storage: %s", err)
}
loadedContents, err := storage.Load()
if err != nil {
t.Fatalf("Error while loading from file storage: %s", err)
}
if string(loadedContents) != contents {
t.Errorf("Expected contents %s, actual %s", contents, string(loadedContents))
}
if storage.String() != file {
t.Errorf("Expected string representation %s, actual %s", file, storage.String())
}
}
func TestZKStorage(t *testing.T) {
zkConnect := "localhost:2181"
zpath := "/tmp/zk/storage"
contents := "hello world"
conn, _, err := zk.Connect([]string{zkConnect}, 30*time.Second)
_, _, err = conn.Exists("/tmp") // check if zk is alive
if err != nil {
t.Skipf("localhost:2181 is not responding (error %s). To run this test please spin up ZK on localhost:2181", err)
}
defer conn.Close()
storage, err := NewZKStorage(fmt.Sprintf("%s%s", zkConnect, zpath))
if err != nil {
t.Fatal(err)
}
err = storage.Save([]byte(contents))
if err != nil {
t.Fatal(err)
}
loadedContents, err := storage.Load()
if err != nil {
t.Fatal(err)
}
if string(loadedContents) != contents {
t.Errorf("Expected contents %s, actual %s", contents, string(loadedContents))
}
err = zkDelete(conn, zpath)
if err != nil {
t.Fatal(err)
}
if storage.String() != fmt.Sprintf("%s%s", zkConnect, zpath) {
t.Errorf("Expected string representation %s, actual %s", fmt.Sprintf("%s%s", zkConnect, zpath), storage.String())
}
}
func zkDelete(conn *zk.Conn, zpath string) error {
if zpath != "" {
_, stat, _ := conn.Get(zpath)
err := conn.Delete(zpath, stat.Version)
if err != nil {
return err
}
index := strings.LastIndex(zpath, "/")
if index != -1 {
return zkDelete(conn, zpath[:index])
} else {
return nil
}
} else {
return nil
}
}