config_test.go 4.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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
package fsrepo

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

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

var defaultConfig = []byte(`{
    "StorageMax": "10GB",
    "StorageGCWatermark": 90,
    "GCPeriod": "1h",
    "Spec": {
      "mounts": [
        {
          "child": {
            "path": "blocks",
            "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
            "sync": true,
            "type": "flatfs"
          },
          "mountpoint": "/blocks",
          "prefix": "flatfs.datastore",
          "type": "measure"
        },
        {
          "child": {
            "compression": "none",
            "path": "datastore",
            "type": "levelds"
          },
          "mountpoint": "/",
          "prefix": "leveldb.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
84 85 86 87 88 89 90 91 92 93 94 95

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

	expected := "/blocks:{flatfs;blocks;/repo/flatfs/shard/v1/next-to-last/2};/:{levelds;datastore};"
	if dsc.DiskId() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskId())
	}

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

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	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
122 123 124 125 126 127 128 129 130 131 132 133

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

	expected := "levelds;datastore"
	if dsc.DiskId() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskId())
	}

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

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

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
160 161 162 163 164 165 166 167 168 169 170 171

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

	expected := "flatfs;blocks;/repo/flatfs/shard/v1/next-to-last/2"
	if dsc.DiskId() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskId())
	}

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

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

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
198 199 200 201 202 203 204 205 206 207 208 209

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

	expected := "flatfs;blocks;/repo/flatfs/shard/v1/next-to-last/2"
	if dsc.DiskId() != expected {
		t.Errorf("expected '%s' got '%s' as DiskId", expected, dsc.DiskId())
	}

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

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