|
| 1 | +package fsrepo |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + config "github.com/ipfs/go-ipfs/repo/config" |
| 11 | +) |
| 12 | + |
| 13 | +// note: to test sorting of the mountpoints in the disk spec they are |
| 14 | +// specified out of order in the test config |
| 15 | +var defaultConfig = []byte(`{ |
| 16 | + "StorageMax": "10GB", |
| 17 | + "StorageGCWatermark": 90, |
| 18 | + "GCPeriod": "1h", |
| 19 | + "Spec": { |
| 20 | + "mounts": [ |
| 21 | + { |
| 22 | + "child": { |
| 23 | + "compression": "none", |
| 24 | + "path": "datastore", |
| 25 | + "type": "levelds" |
| 26 | + }, |
| 27 | + "mountpoint": "/", |
| 28 | + "prefix": "leveldb.datastore", |
| 29 | + "type": "measure" |
| 30 | + }, |
| 31 | + { |
| 32 | + "child": { |
| 33 | + "path": "blocks", |
| 34 | + "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2", |
| 35 | + "sync": true, |
| 36 | + "type": "flatfs" |
| 37 | + }, |
| 38 | + "mountpoint": "/blocks", |
| 39 | + "prefix": "flatfs.datastore", |
| 40 | + "type": "measure" |
| 41 | + } |
| 42 | + ], |
| 43 | + "type": "mount" |
| 44 | + }, |
| 45 | + "HashOnRead": false, |
| 46 | + "BloomFilterSize": 0 |
| 47 | +}`) |
| 48 | + |
| 49 | +var leveldbConfig = []byte(`{ |
| 50 | + "compression": "none", |
| 51 | + "path": "datastore", |
| 52 | + "type": "levelds" |
| 53 | +}`) |
| 54 | + |
| 55 | +var flatfsConfig = []byte(`{ |
| 56 | + "path": "blocks", |
| 57 | + "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2", |
| 58 | + "sync": true, |
| 59 | + "type": "flatfs" |
| 60 | +}`) |
| 61 | + |
| 62 | +var measureConfig = []byte(`{ |
| 63 | + "child": { |
| 64 | + "path": "blocks", |
| 65 | + "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2", |
| 66 | + "sync": true, |
| 67 | + "type": "flatfs" |
| 68 | + }, |
| 69 | + "mountpoint": "/blocks", |
| 70 | + "prefix": "flatfs.datastore", |
| 71 | + "type": "measure" |
| 72 | +}`) |
| 73 | + |
| 74 | +func TestDefaultDatastoreConfig(t *testing.T) { |
| 75 | + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test") |
| 76 | + if err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + defer os.RemoveAll(dir) // clean up |
| 80 | + |
| 81 | + config := new(config.Datastore) |
| 82 | + err = json.Unmarshal(defaultConfig, config) |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + |
| 87 | + dsc, err := AnyDatastoreConfig(config.Spec) |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + |
| 92 | + expected := `{"mounts":[{"mountpoint":"/blocks","path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"},{"mountpoint":"/","path":"datastore","type":"levelds"}],"type":"mount"}` |
| 93 | + if dsc.DiskSpec().String() != expected { |
| 94 | + t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String()) |
| 95 | + } |
| 96 | + |
| 97 | + ds, err := dsc.Create(dir) |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + |
| 102 | + if typ := reflect.TypeOf(ds).String(); typ != "*syncmount.Datastore" { |
| 103 | + t.Errorf("expected '*syncmount.Datastore' got '%s'", typ) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestLevelDbConfig(t *testing.T) { |
| 108 | + config := new(config.Datastore) |
| 109 | + err := json.Unmarshal(defaultConfig, config) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test") |
| 114 | + if err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + defer os.RemoveAll(dir) // clean up |
| 118 | + |
| 119 | + spec := make(map[string]interface{}) |
| 120 | + err = json.Unmarshal(leveldbConfig, &spec) |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + |
| 125 | + dsc, err := AnyDatastoreConfig(spec) |
| 126 | + if err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + |
| 130 | + expected := `{"path":"datastore","type":"levelds"}` |
| 131 | + if dsc.DiskSpec().String() != expected { |
| 132 | + t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String()) |
| 133 | + } |
| 134 | + |
| 135 | + ds, err := dsc.Create(dir) |
| 136 | + if err != nil { |
| 137 | + t.Fatal(err) |
| 138 | + } |
| 139 | + |
| 140 | + if typ := reflect.TypeOf(ds).String(); typ != "*leveldb.datastore" { |
| 141 | + t.Errorf("expected '*leveldb.datastore' got '%s'", typ) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestFlatfsConfig(t *testing.T) { |
| 146 | + config := new(config.Datastore) |
| 147 | + err := json.Unmarshal(defaultConfig, config) |
| 148 | + if err != nil { |
| 149 | + t.Fatal(err) |
| 150 | + } |
| 151 | + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test") |
| 152 | + if err != nil { |
| 153 | + t.Fatal(err) |
| 154 | + } |
| 155 | + defer os.RemoveAll(dir) // clean up |
| 156 | + |
| 157 | + spec := make(map[string]interface{}) |
| 158 | + err = json.Unmarshal(flatfsConfig, &spec) |
| 159 | + if err != nil { |
| 160 | + t.Fatal(err) |
| 161 | + } |
| 162 | + |
| 163 | + dsc, err := AnyDatastoreConfig(spec) |
| 164 | + if err != nil { |
| 165 | + t.Fatal(err) |
| 166 | + } |
| 167 | + |
| 168 | + expected := `{"path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"}` |
| 169 | + if dsc.DiskSpec().String() != expected { |
| 170 | + t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String()) |
| 171 | + } |
| 172 | + |
| 173 | + ds, err := dsc.Create(dir) |
| 174 | + if err != nil { |
| 175 | + t.Fatal(err) |
| 176 | + } |
| 177 | + |
| 178 | + if typ := reflect.TypeOf(ds).String(); typ != "*flatfs.Datastore" { |
| 179 | + t.Errorf("expected '*flatfs.Datastore' got '%s'", typ) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func TestMeasureConfig(t *testing.T) { |
| 184 | + config := new(config.Datastore) |
| 185 | + err := json.Unmarshal(defaultConfig, config) |
| 186 | + if err != nil { |
| 187 | + t.Fatal(err) |
| 188 | + } |
| 189 | + dir, err := ioutil.TempDir("", "ipfs-datastore-config-test") |
| 190 | + if err != nil { |
| 191 | + t.Fatal(err) |
| 192 | + } |
| 193 | + defer os.RemoveAll(dir) // clean up |
| 194 | + |
| 195 | + spec := make(map[string]interface{}) |
| 196 | + err = json.Unmarshal(measureConfig, &spec) |
| 197 | + if err != nil { |
| 198 | + t.Fatal(err) |
| 199 | + } |
| 200 | + |
| 201 | + dsc, err := AnyDatastoreConfig(spec) |
| 202 | + if err != nil { |
| 203 | + t.Fatal(err) |
| 204 | + } |
| 205 | + |
| 206 | + expected := `{"path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"}` |
| 207 | + if dsc.DiskSpec().String() != expected { |
| 208 | + t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String()) |
| 209 | + } |
| 210 | + |
| 211 | + ds, err := dsc.Create(dir) |
| 212 | + if err != nil { |
| 213 | + t.Fatal(err) |
| 214 | + } |
| 215 | + |
| 216 | + if typ := reflect.TypeOf(ds).String(); typ != "*measure.measure" { |
| 217 | + t.Errorf("expected '*measure.measure' got '%s'", typ) |
| 218 | + } |
| 219 | +} |
0 commit comments