config_test.go 5.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package fsrepo

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

	config "github.com/ipfs/go-ipfs/repo/config"
)

Kevin Atkinson's avatar
Kevin Atkinson committed
13 14
// note: to test sorting of the mountpoints in the disk spec they are
// specified out of order in the test config
15 16 17 18 19 20
var defaultConfig = []byte(`{
    "StorageMax": "10GB",
    "StorageGCWatermark": 90,
    "GCPeriod": "1h",
    "Spec": {
      "mounts": [
Kevin Atkinson's avatar
Kevin Atkinson committed
21 22 23 24 25 26 27 28 29 30
        {
          "child": {
            "compression": "none",
            "path": "datastore",
            "type": "levelds"
          },
          "mountpoint": "/",
          "prefix": "leveldb.datastore",
          "type": "measure"
        },
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
        {
          "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) {
	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
86 87 88 89 90 91

	dsc, err := AnyDatastoreConfig(config.Spec)
	if err != nil {
		t.Fatal(err)
	}

Kevin Atkinson's avatar
Kevin Atkinson committed
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 94
	if dsc.DiskSpec().String() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskSpec().String())
Kevin Atkinson's avatar
Kevin Atkinson committed
95 96 97
	}

	ds, err := dsc.Create(dir)
98 99 100
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	if typ := reflect.TypeOf(ds).String(); typ != "*syncmount.Datastore" {
		t.Errorf("expected '*syncmount.Datastore' got '%s'", typ)
	}
}

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
124 125 126 127 128 129

	dsc, err := AnyDatastoreConfig(spec)
	if err != nil {
		t.Fatal(err)
	}

130 131 132
	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
133 134 135
	}

	ds, err := dsc.Create(dir)
136 137 138
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
139

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

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
162 163 164 165 166 167

	dsc, err := AnyDatastoreConfig(spec)
	if err != nil {
		t.Fatal(err)
	}

168 169 170
	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
171 172 173
	}

	ds, err := dsc.Create(dir)
174 175 176
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
177

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

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
200 201 202 203 204 205

	dsc, err := AnyDatastoreConfig(spec)
	if err != nil {
		t.Fatal(err)
	}

206 207 208
	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
209 210 211
	}

	ds, err := dsc.Create(dir)
212 213 214
	if err != nil {
		t.Fatal(err)
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
215

216
	if typ := reflect.TypeOf(ds).String(); typ != "*measure.measure" {
217
		t.Errorf("expected '*measure.measure' got '%s'", typ)
218 219
	}
}