convert_test.go 5.42 KB
Newer Older
1 2 3 4
package flatfs_test

import (
	"bytes"
5
	"encoding/base32"
6
	"io/ioutil"
7
	"math/rand"
8 9
	"os"
	"path/filepath"
10
	"runtime"
11
	"testing"
12
	"time"
13 14

	"github.com/ipfs/go-datastore"
15
	flatfs "github.com/ipfs/go-ds-flatfs"
16 17 18 19 20 21 22
)

func TestMove(t *testing.T) {
	tempdir, cleanup := tempdir(t)
	defer cleanup()

	v1dir := filepath.Join(tempdir, "v1")
Kevin Atkinson's avatar
Kevin Atkinson committed
23 24 25
	createDatastore(t, v1dir, flatfs.Prefix(3))

	err := ioutil.WriteFile(filepath.Join(v1dir, "README_ALSO"), []byte("something"), 0666)
26
	if err != nil {
Kevin Atkinson's avatar
Kevin Atkinson committed
27
		t.Fatalf("WriteFile fail: %v\n", err)
28
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
29 30 31 32 33 34 35

	keys, blocks := populateDatastore(t, v1dir)

	v2dir := filepath.Join(tempdir, "v2")
	createDatastore(t, v2dir, flatfs.NextToLast(2))

	err = flatfs.Move(v1dir, v2dir, nil)
36
	if err != nil {
Kevin Atkinson's avatar
Kevin Atkinson committed
37
		t.Fatalf("%v\n", err)
38
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
39 40 41 42 43 44

	// make sure the directory empty
	rmEmptyDatastore(t, v1dir)

	// make sure the README file moved
	_, err = os.Stat(filepath.Join(v2dir, "README_ALSO"))
45
	if err != nil {
Kevin Atkinson's avatar
Kevin Atkinson committed
46
		t.Fatalf(err.Error())
47 48
	}

Kevin Atkinson's avatar
Kevin Atkinson committed
49 50
	// check that all keys are available
	checkKeys(t, v2dir, keys, blocks)
51

Kevin Atkinson's avatar
Kevin Atkinson committed
52 53 54 55 56
	// check that a key is in the correct format
	shard := filepath.Join(v2dir, flatfs.NextToLast(2).Func()(keys[0].String()))
	_, err = os.Stat(shard)
	if err != nil {
		t.Fatalf(err.Error())
57
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
58
}
59

Kevin Atkinson's avatar
Kevin Atkinson committed
60
func TestMoveRestart(t *testing.T) {
61 62 63
	if runtime.GOOS == "windows" {
		t.Skip()
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
64 65 66 67
	tempdir, cleanup := tempdir(t)
	defer cleanup()

	v1dir := filepath.Join(tempdir, "v1")
68
	v2dir := filepath.Join(tempdir, "v2")
Kevin Atkinson's avatar
Kevin Atkinson committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82

	createDatastore(t, v1dir, flatfs.Prefix(3))

	createDatastore(t, v2dir, flatfs.NextToLast(5))

	keys, blocks := populateDatastore(t, v1dir)
	checkKeys(t, v1dir, keys, blocks)

	// get a directory in the datastore
	noslash := keys[0].String()[1:]
	aDir := filepath.Join(tempdir, "v1", flatfs.Prefix(3).Func()(noslash))

	// create a permission problem on the directory
	err := os.Chmod(aDir, 0500)
83
	if err != nil {
Kevin Atkinson's avatar
Kevin Atkinson committed
84
		t.Fatalf("%v\n", err)
85 86
	}

Kevin Atkinson's avatar
Kevin Atkinson committed
87 88 89
	// try the move it should fail partly through
	err = flatfs.Move(v1dir, v2dir, nil)
	if err == nil {
Steven Allen's avatar
Steven Allen committed
90
		t.Fatal("Move should have failed.", err)
91 92
	}

Kevin Atkinson's avatar
Kevin Atkinson committed
93 94
	// okay try to undo should be okay
	err = flatfs.Move(v2dir, v1dir, nil)
95
	if err != nil {
Steven Allen's avatar
Steven Allen committed
96
		t.Fatal("Could not undo the move.", err)
97
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
98
	checkKeys(t, v1dir, keys, blocks)
99

Kevin Atkinson's avatar
Kevin Atkinson committed
100 101
	// there should be nothing left in the new datastore
	rmEmptyDatastore(t, v2dir)
102

Kevin Atkinson's avatar
Kevin Atkinson committed
103 104 105 106
	// try the move again, again should fail
	createDatastore(t, v2dir, flatfs.NextToLast(2))
	err = flatfs.Move(v1dir, v2dir, nil)
	if err == nil {
Steven Allen's avatar
Steven Allen committed
107
		t.Fatal("Move should have failed.", err)
108 109
	}

Kevin Atkinson's avatar
Kevin Atkinson committed
110 111
	// fix the permission problem
	err = os.Chmod(aDir, 0700)
112
	if err != nil {
Kevin Atkinson's avatar
Kevin Atkinson committed
113
		t.Fatalf("%v\n", err)
114
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
115 116 117 118 119

	// restart the move, it should be okay now
	err = flatfs.Move(v1dir, v2dir, nil)
	if err != nil {
		t.Fatalf("Move not okay: %v\n", err)
120 121
	}

Kevin Atkinson's avatar
Kevin Atkinson committed
122 123 124 125 126 127 128
	// make sure everything moved by removing the old directory
	rmEmptyDatastore(t, v1dir)

	// make sure everything moved by checking all keys
	checkKeys(t, v2dir, keys, blocks)

	// check that a key is in the correct format
129 130 131 132 133 134
	shard := filepath.Join(v2dir, flatfs.NextToLast(2).Func()(keys[0].String()))
	_, err = os.Stat(shard)
	if err != nil {
		t.Fatalf(err.Error())
	}
}
Kevin Atkinson's avatar
Kevin Atkinson committed
135 136 137 138 139 140 141 142 143 144

func TestUpgradeDownload(t *testing.T) {
	tempdir, cleanup := tempdir(t)
	defer cleanup()

	createDatastore(t, tempdir, flatfs.Prefix(3))

	keys, blocks := populateDatastore(t, tempdir)
	checkKeys(t, tempdir, keys, blocks)

145 146 147 148
	err := flatfs.UpgradeV0toV1(tempdir, 3)
	if err == nil {
		t.Fatalf("UpgradeV0toV1 on already v1 should fail.")
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
149

150
	err = flatfs.DowngradeV1toV0(tempdir)
Kevin Atkinson's avatar
Kevin Atkinson committed
151 152 153 154 155 156
	if err != nil {
		t.Fatalf("DowngradeV1toV0 fail: %v\n", err)
	}

	_, err = os.Stat(filepath.Join(tempdir, flatfs.SHARDING_FN))
	if err == nil {
Steven Allen's avatar
Steven Allen committed
157
		t.Fatalf("%v not in v0 format, SHARDING FILE exists", tempdir)
Kevin Atkinson's avatar
Kevin Atkinson committed
158 159 160 161 162 163 164 165 166 167 168 169 170
	} else if !os.IsNotExist(err) {
		t.Fatalf("Stat fail: %v\n", err)
	}

	err = flatfs.UpgradeV0toV1(tempdir, 3)
	if err != nil {
		t.Fatalf("UpgradeV0toV1 fail %v\n", err)
	}

	// This will fail unless the repository is in the new version
	checkKeys(t, tempdir, keys, blocks)
}

171 172 173 174 175 176 177 178
func TestDownloadNonPrefix(t *testing.T) {
	tempdir, cleanup := tempdir(t)
	defer cleanup()

	createDatastore(t, tempdir, flatfs.NextToLast(2))

	err := flatfs.DowngradeV1toV0(tempdir)
	if err == nil {
Steven Allen's avatar
Steven Allen committed
179
		t.Fatal("DowngradeV1toV0 should have failed", err)
180 181 182
	}
}

Kevin Atkinson's avatar
Kevin Atkinson committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
func createDatastore(t *testing.T, dir string, fun *flatfs.ShardIdV1) {
	err := flatfs.Create(dir, fun)
	if err != nil {
		t.Fatalf("Create fail: %s: %v\n", dir, err)
	}
}

func rmEmptyDatastore(t *testing.T, dir string) {
	err := os.Remove(dir)
	if err != nil {
		t.Fatalf("Remove fail: %v\n", err)
	}
}

func populateDatastore(t *testing.T, dir string) ([]datastore.Key, [][]byte) {
	ds, err := flatfs.Open(dir, false)
	if err != nil {
		t.Fatalf("Open fail: %v\n", err)
	}
202
	defer ds.Close()
Kevin Atkinson's avatar
Kevin Atkinson committed
203

204
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
Kevin Atkinson's avatar
Kevin Atkinson committed
205 206 207 208 209 210 211
	var blocks [][]byte
	var keys []datastore.Key
	for i := 0; i < 256; i++ {
		blk := make([]byte, 1000)
		r.Read(blk)
		blocks = append(blocks, blk)

212
		key := "X" + base32.StdEncoding.EncodeToString(blk[:8])
Kevin Atkinson's avatar
Kevin Atkinson committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		keys = append(keys, datastore.NewKey(key))
		err := ds.Put(keys[i], blocks[i])
		if err != nil {
			t.Fatalf("Put fail: %v\n", err)
		}
	}

	return keys, blocks
}

func checkKeys(t *testing.T, dir string, keys []datastore.Key, blocks [][]byte) {
	ds, err := flatfs.Open(dir, false)
	if err != nil {
		t.Fatalf("Open fail: %v\n", err)
	}
228
	defer ds.Close()
Kevin Atkinson's avatar
Kevin Atkinson committed
229 230 231 232 233 234

	for i, key := range keys {
		data, err := ds.Get(key)
		if err != nil {
			t.Fatalf("Get fail: %v\n", err)
		}
235
		if !bytes.Equal(data, blocks[i]) {
Kevin Atkinson's avatar
Kevin Atkinson committed
236 237 238 239
			t.Fatalf("block context differ for key %s\n", key.String())
		}
	}
}