Remove registry storage testdriver

Signed-off-by: glefloch <glfloch@gmail.com>
This commit is contained in:
glefloch
2018-11-15 10:36:19 +00:00
parent 29b5e79f82
commit 9fb201e23d
5 changed files with 43 additions and 91 deletions

View File

@@ -251,6 +251,8 @@ func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn)
type writer struct {
d *driver
f *file
buffer []byte
buffSize int
closed bool
committed bool
cancelled bool
@@ -274,8 +276,17 @@ func (w *writer) Write(p []byte) (int, error) {
w.d.mutex.Lock()
defer w.d.mutex.Unlock()
if cap(w.buffer) < len(p)+w.buffSize {
data := make([]byte, len(w.buffer), len(p)+w.buffSize)
copy(data, w.buffer)
w.buffer = data
}
return w.f.WriteAt(p, int64(len(w.f.data)))
w.buffer = w.buffer[:w.buffSize+len(p)]
n := copy(w.buffer[w.buffSize:w.buffSize+len(p)], p)
w.buffSize += n
return n, nil
}
func (w *writer) Size() int64 {
@@ -290,6 +301,8 @@ func (w *writer) Close() error {
return fmt.Errorf("already closed")
}
w.closed = true
w.flush()
return nil
}
@@ -316,5 +329,16 @@ func (w *writer) Commit() error {
return fmt.Errorf("already cancelled")
}
w.committed = true
w.flush()
return nil
}
func (w *writer) flush() {
w.d.mutex.Lock()
defer w.d.mutex.Unlock()
w.f.WriteAt(w.buffer, int64(len(w.f.data)))
w.buffer = []byte{}
w.buffSize = 0
}

View File

@@ -1,72 +0,0 @@
package testdriver
import (
"context"
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
"github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
)
const driverName = "testdriver"
func init() {
factory.Register(driverName, &testDriverFactory{})
}
// testDriverFactory implements the factory.StorageDriverFactory interface.
type testDriverFactory struct{}
func (factory *testDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
return New(), nil
}
// TestDriver is a StorageDriver for testing purposes. The Writer returned by this driver
// simulates the case where Write operations are buffered. This causes the value returned by Size to lag
// behind until Close (or Commit, or Cancel) is called.
type TestDriver struct {
storagedriver.StorageDriver
}
type testFileWriter struct {
storagedriver.FileWriter
prevchunk []byte
}
var _ storagedriver.StorageDriver = &TestDriver{}
// New constructs a new StorageDriver for testing purposes. The Writer returned by this driver
// simulates the case where Write operations are buffered. This causes the value returned by Size to lag
// behind until Close (or Commit, or Cancel) is called.
func New() *TestDriver {
return &TestDriver{StorageDriver: inmemory.New()}
}
// Writer returns a FileWriter which will store the content written to it
// at the location designated by "path" after the call to Commit.
func (td *TestDriver) Writer(ctx context.Context, path string, append bool) (storagedriver.FileWriter, error) {
fw, err := td.StorageDriver.Writer(ctx, path, append)
return &testFileWriter{FileWriter: fw}, err
}
func (tfw *testFileWriter) Write(p []byte) (int, error) {
_, err := tfw.FileWriter.Write(tfw.prevchunk)
tfw.prevchunk = make([]byte, len(p))
copy(tfw.prevchunk, p)
return len(p), err
}
func (tfw *testFileWriter) Close() error {
tfw.Write(nil)
return tfw.FileWriter.Close()
}
func (tfw *testFileWriter) Cancel(ctx context.Context) error {
tfw.Write(nil)
return tfw.FileWriter.Cancel(ctx)
}
func (tfw *testFileWriter) Commit() error {
tfw.Write(nil)
return tfw.FileWriter.Commit()
}