2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package netstate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
)
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
// MockDB mocks db functionality for testing
|
|
|
|
type MockDB struct {
|
2018-05-15 01:31:26 +01:00
|
|
|
timesCalled int
|
2018-05-30 03:47:40 +01:00
|
|
|
puts []boltdb.PointerEntry
|
|
|
|
pathKeys [][]byte
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
func (m *MockDB) Put(f boltdb.PointerEntry) error {
|
2018-05-15 01:31:26 +01:00
|
|
|
m.timesCalled++
|
|
|
|
m.puts = append(m.puts, f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
func (m *MockDB) Get(path []byte) ([]byte, error) {
|
2018-05-15 01:31:26 +01:00
|
|
|
m.timesCalled++
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
for _, pointerEntry := range m.puts {
|
|
|
|
if bytes.Equal(path, pointerEntry.Path) {
|
|
|
|
return pointerEntry.Pointer, nil
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("failed to get the given file")
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
func (m *MockDB) List() ([][]byte, error) {
|
2018-05-15 01:31:26 +01:00
|
|
|
m.timesCalled++
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
for _, putReq := range m.puts {
|
|
|
|
m.pathKeys = append(m.pathKeys, putReq.Path)
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
return m.pathKeys, nil
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
func (m *MockDB) Delete(path []byte) error {
|
2018-05-15 01:31:26 +01:00
|
|
|
m.timesCalled++
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
for i, pointerEntry := range m.puts {
|
|
|
|
if bytes.Equal(path, pointerEntry.Path) {
|
2018-05-15 01:31:26 +01:00
|
|
|
m.puts = append(m.puts[:i], m.puts[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|