config_test.go 5.29 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1
package fsrepo_test
2 3 4 5 6 7 8 9

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"reflect"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
10 11 12
	"github.com/ipfs/go-ipfs/plugin/loader"
	"github.com/ipfs/go-ipfs/repo/fsrepo"

Łukasz Magiera's avatar
Łukasz Magiera committed
13
	"gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
14 15
)

Kevin Atkinson's avatar
Kevin Atkinson committed
16 17
// note: to test sorting of the mountpoints in the disk spec they are
// specified out of order in the test config
18 19 20 21 22 23
var defaultConfig = []byte(`{
    "StorageMax": "10GB",
    "StorageGCWatermark": 90,
    "GCPeriod": "1h",
    "Spec": {
      "mounts": [
Kevin Atkinson's avatar
Kevin Atkinson committed
24 25 26 27 28 29 30 31 32 33
        {
          "child": {
            "compression": "none",
            "path": "datastore",
            "type": "levelds"
          },
          "mountpoint": "/",
          "prefix": "leveldb.datastore",
          "type": "measure"
        },
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
        {
          "child": {
            "path": "blocks",
            "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
            "sync": true,
            "type": "flatfs"
          },
          "mountpoint": "/blocks",
          "prefix": "flatfs.datastore",
          "type": "measure"
        }
      ],
      "type": "mount"
    },
    "HashOnRead": false,
    "BloomFilterSize": 0
}`)

var leveldbConfig = []byte(`{
            "compression": "none",
            "path": "datastore",
            "type": "levelds"
}`)

var flatfsConfig = []byte(`{
            "path": "blocks",
            "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
            "sync": true,
            "type": "flatfs"
}`)

var measureConfig = []byte(`{
          "child": {
            "path": "blocks",
            "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
            "sync": true,
            "type": "flatfs"
          },
          "mountpoint": "/blocks",
          "prefix": "flatfs.datastore",
          "type": "measure"
}`)

func TestDefaultDatastoreConfig(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
78 79
	loader.LoadPlugins("")

80 81 82 83 84 85 86 87 88 89 90
	dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir) // clean up

	config := new(config.Datastore)
	err = json.Unmarshal(defaultConfig, config)
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
91

Łukasz Magiera's avatar
Łukasz Magiera committed
92
	dsc, err := fsrepo.AnyDatastoreConfig(config.Spec)
Kevin Atkinson's avatar
Kevin Atkinson committed
93 94 95 96
	if err != nil {
		t.Fatal(err)
	}

Kevin Atkinson's avatar
Kevin Atkinson committed
97
	expected := `{"mounts":[{"mountpoint":"/blocks","path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"},{"mountpoint":"/","path":"datastore","type":"levelds"}],"type":"mount"}`
98 99
	if dsc.DiskSpec().String() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String())
Kevin Atkinson's avatar
Kevin Atkinson committed
100 101 102
	}

	ds, err := dsc.Create(dir)
103 104 105
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
106

107 108
	if typ := reflect.TypeOf(ds).String(); typ != "*mount.Datastore" {
		t.Errorf("expected '*mount.Datastore' got '%s'", typ)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	}
}

func TestLevelDbConfig(t *testing.T) {
	config := new(config.Datastore)
	err := json.Unmarshal(defaultConfig, config)
	if err != nil {
		t.Fatal(err)
	}
	dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir) // clean up

	spec := make(map[string]interface{})
	err = json.Unmarshal(leveldbConfig, &spec)
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
129

Łukasz Magiera's avatar
Łukasz Magiera committed
130
	dsc, err := fsrepo.AnyDatastoreConfig(spec)
Kevin Atkinson's avatar
Kevin Atkinson committed
131 132 133 134
	if err != nil {
		t.Fatal(err)
	}

135 136 137
	expected := `{"path":"datastore","type":"levelds"}`
	if dsc.DiskSpec().String() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String())
Kevin Atkinson's avatar
Kevin Atkinson committed
138 139 140
	}

	ds, err := dsc.Create(dir)
141 142 143
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
144

145
	if typ := reflect.TypeOf(ds).String(); typ != "*leveldb.datastore" {
146
		t.Errorf("expected '*leveldb.datastore' got '%s'", typ)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	}
}

func TestFlatfsConfig(t *testing.T) {
	config := new(config.Datastore)
	err := json.Unmarshal(defaultConfig, config)
	if err != nil {
		t.Fatal(err)
	}
	dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir) // clean up

	spec := make(map[string]interface{})
	err = json.Unmarshal(flatfsConfig, &spec)
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
167

Łukasz Magiera's avatar
Łukasz Magiera committed
168
	dsc, err := fsrepo.AnyDatastoreConfig(spec)
Kevin Atkinson's avatar
Kevin Atkinson committed
169 170 171 172
	if err != nil {
		t.Fatal(err)
	}

173 174 175
	expected := `{"path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"}`
	if dsc.DiskSpec().String() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String())
Kevin Atkinson's avatar
Kevin Atkinson committed
176 177 178
	}

	ds, err := dsc.Create(dir)
179 180 181
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
182

183
	if typ := reflect.TypeOf(ds).String(); typ != "*flatfs.Datastore" {
184
		t.Errorf("expected '*flatfs.Datastore' got '%s'", typ)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	}
}

func TestMeasureConfig(t *testing.T) {
	config := new(config.Datastore)
	err := json.Unmarshal(defaultConfig, config)
	if err != nil {
		t.Fatal(err)
	}
	dir, err := ioutil.TempDir("", "ipfs-datastore-config-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir) // clean up

	spec := make(map[string]interface{})
	err = json.Unmarshal(measureConfig, &spec)
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
205

Łukasz Magiera's avatar
Łukasz Magiera committed
206
	dsc, err := fsrepo.AnyDatastoreConfig(spec)
Kevin Atkinson's avatar
Kevin Atkinson committed
207 208 209 210
	if err != nil {
		t.Fatal(err)
	}

211 212 213
	expected := `{"path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"}`
	if dsc.DiskSpec().String() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String())
Kevin Atkinson's avatar
Kevin Atkinson committed
214 215 216
	}

	ds, err := dsc.Create(dir)
217 218 219
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
220

221
	if typ := reflect.TypeOf(ds).String(); typ != "*measure.measure" {
222
		t.Errorf("expected '*measure.measure' got '%s'", typ)
223 224
	}
}