@cryptotaxi247 / kubo / commits / 5a8c17104

add tests for new datastore configuration

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Jun 16, 2017 at 20:28 UTC 5a8c17104d3dd05dcb83cb905095d93b2fb6998d
1 file changed +181
repo/fsrepo/config_test.go new
+181
@@ -0,0 +1,181 @@
1 +package fsrepo
2 +
3 +import (
4 + "encoding/json"
5 + "io/ioutil"
6 + "os"
7 + "reflect"
8 + "testing"
9 +
10 + //"fmt"
11 +
12 + syncmount "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore/syncmount"
13 + //levelds "gx/ipfs/QmaHHmfEozrrotyhyN44omJouyuEtx6ahddqV6W5yRaUSQ/go-ds-leveldb"
14 + config "github.com/ipfs/go-ipfs/repo/config"
15 +)
16 +
17 +var defaultConfig = []byte(`{
18 + "StorageMax": "10GB",
19 + "StorageGCWatermark": 90,
20 + "GCPeriod": "1h",
21 + "Spec": {
22 + "mounts": [
23 + {
24 + "child": {
25 + "path": "blocks",
26 + "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
27 + "sync": true,
28 + "type": "flatfs"
29 + },
30 + "mountpoint": "/blocks",
31 + "prefix": "flatfs.datastore",
32 + "type": "measure"
33 + },
34 + {
35 + "child": {
36 + "compression": "none",
37 + "path": "datastore",
38 + "type": "levelds"
39 + },
40 + "mountpoint": "/",
41 + "prefix": "leveldb.datastore",
42 + "type": "measure"
43 + }
44 + ],
45 + "type": "mount"
46 + },
47 + "HashOnRead": false,
48 + "BloomFilterSize": 0
49 +}`)
50 +
51 +var leveldbConfig = []byte(`{
52 + "compression": "none",
53 + "path": "datastore",
54 + "type": "levelds"
55 +}`)
56 +
57 +var flatfsConfig = []byte(`{
58 + "path": "blocks",
59 + "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
60 + "sync": true,
61 + "type": "flatfs"
62 +}`)
63 +
64 +var measureConfig = []byte(`{
65 + "child": {
66 + "path": "blocks",
67 + "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
68 + "sync": true,
69 + "type": "flatfs"
70 + },
71 + "mountpoint": "/blocks",
72 + "prefix": "flatfs.datastore",
73 + "type": "measure"
74 +}`)
75 +
76 +func TestDefaultDatastoreConfig(t *testing.T) {
77 + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
78 + if err != nil {
79 + t.Fatal(err)
80 + }
81 + defer os.RemoveAll(dir) // clean up
82 + repo := FSRepo{path: dir}
83 +
84 + config := new(config.Datastore)
85 + err = json.Unmarshal(defaultConfig, config)
86 + if err != nil {
87 + t.Fatal(err)
88 + }
89 + ds, err := repo.constructDatastore(config.Spec)
90 + if err != nil {
91 + t.Fatal(err)
92 + }
93 + _, ok := ds.(*syncmount.Datastore)
94 + if !ok {
95 + t.Fatal("expected mount datastore at top level")
96 + }
97 + if typ := reflect.TypeOf(ds).String(); typ != "*syncmount.Datastore" {
98 + t.Errorf("expected '*syncmount.Datastore' got '%s'", typ)
99 + }
100 +}
101 +
102 +func TestLevelDbConfig(t *testing.T) {
103 + config := new(config.Datastore)
104 + err := json.Unmarshal(defaultConfig, config)
105 + if err != nil {
106 + t.Fatal(err)
107 + }
108 + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
109 + if err != nil {
110 + t.Fatal(err)
111 + }
112 + defer os.RemoveAll(dir) // clean up
113 + repo := FSRepo{path: dir}
114 +
115 + spec := make(map[string]interface{})
116 + err = json.Unmarshal(leveldbConfig, &spec)
117 + if err != nil {
118 + t.Fatal(err)
119 + }
120 + ds, err := repo.constructDatastore(spec)
121 + if err != nil {
122 + t.Fatal(err)
123 + }
124 + if typ := reflect.TypeOf(ds).String(); typ != "*leveldb.datastore" {
125 + t.Errorf("expected '*syncmount.Datastore' got '%s'", typ)
126 + }
127 +}
128 +
129 +func TestFlatfsConfig(t *testing.T) {
130 + config := new(config.Datastore)
131 + err := json.Unmarshal(defaultConfig, config)
132 + if err != nil {
133 + t.Fatal(err)
134 + }
135 + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
136 + if err != nil {
137 + t.Fatal(err)
138 + }
139 + defer os.RemoveAll(dir) // clean up
140 + repo := FSRepo{path: dir}
141 +
142 + spec := make(map[string]interface{})
143 + err = json.Unmarshal(flatfsConfig, &spec)
144 + if err != nil {
145 + t.Fatal(err)
146 + }
147 + ds, err := repo.constructDatastore(spec)
148 + if err != nil {
149 + t.Fatal(err)
150 + }
151 + if typ := reflect.TypeOf(ds).String(); typ != "*flatfs.Datastore" {
152 + t.Errorf("expected '*syncmount.Datastore' got '%s'", typ)
153 + }
154 +}
155 +
156 +func TestMeasureConfig(t *testing.T) {
157 + config := new(config.Datastore)
158 + err := json.Unmarshal(defaultConfig, config)
159 + if err != nil {
160 + t.Fatal(err)
161 + }
162 + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
163 + if err != nil {
164 + t.Fatal(err)
165 + }
166 + defer os.RemoveAll(dir) // clean up
167 + repo := FSRepo{path: dir}
168 +
169 + spec := make(map[string]interface{})
170 + err = json.Unmarshal(measureConfig, &spec)
171 + if err != nil {
172 + t.Fatal(err)
173 + }
174 + ds, err := repo.constructDatastore(spec)
175 + if err != nil {
176 + t.Fatal(err)
177 + }
178 + if typ := reflect.TypeOf(ds).String(); typ != "*measure.measure" {
179 + t.Errorf("expected '*syncmount.Datastore' got '%s'", typ)
180 + }
181 +}