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