| 1 |
package testdriver |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
|
| 6 |
storagedriver "github.com/docker/distribution/registry/storage/driver" |
| 7 |
"github.com/docker/distribution/registry/storage/driver/factory" |
| 8 |
"github.com/docker/distribution/registry/storage/driver/inmemory" |
| 9 |
) |
| 10 |
|
| 11 |
const driverName = "testdriver" |
| 12 |
|
| 13 |
func init() { |
| 14 |
factory.Register(driverName, &testDriverFactory{}) |
| 15 |
} |
| 16 |
|
| 17 |
// testDriverFactory implements the factory.StorageDriverFactory interface. |
| 18 |
type testDriverFactory struct{} |
| 19 |
|
| 20 |
func (factory *testDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) { |
| 21 |
return New(), nil |
| 22 |
} |
| 23 |
|
| 24 |
// TestDriver is a StorageDriver for testing purposes. The Writer returned by this driver |
| 25 |
// simulates the case where Write operations are buffered. This causes the value returned by Size to lag |
| 26 |
// behind until Close (or Commit, or Cancel) is called. |
| 27 |
type TestDriver struct { |
| 28 |
storagedriver.StorageDriver |
| 29 |
} |
| 30 |
|
| 31 |
type testFileWriter struct { |
| 32 |
storagedriver.FileWriter |
| 33 |
prevchunk []byte |
| 34 |
} |
| 35 |
|
| 36 |
var _ storagedriver.StorageDriver = &TestDriver{} |
| 37 |
|
| 38 |
// New constructs a new StorageDriver for testing purposes. The Writer returned by this driver |
| 39 |
// simulates the case where Write operations are buffered. This causes the value returned by Size to lag |
| 40 |
// behind until Close (or Commit, or Cancel) is called. |
| 41 |
func New() *TestDriver { |
| 42 |
return &TestDriver{StorageDriver: inmemory.New()} |
| 43 |
} |
| 44 |
|
| 45 |
// Writer returns a FileWriter which will store the content written to it |
| 46 |
// at the location designated by "path" after the call to Commit. |
| 47 |
func (td *TestDriver) Writer(ctx context.Context, path string, append bool) (storagedriver.FileWriter, error) { |
| 48 |
fw, err := td.StorageDriver.Writer(ctx, path, append) |
| 49 |
return &testFileWriter{FileWriter: fw}, err |
| 50 |
} |
| 51 |
|
| 52 |
func (tfw *testFileWriter) Write(p []byte) (int, error) { |
| 53 |
_, err := tfw.FileWriter.Write(tfw.prevchunk) |
| 54 |
tfw.prevchunk = make([]byte, len(p)) |
| 55 |
copy(tfw.prevchunk, p) |
| 56 |
return len(p), err |
| 57 |
} |
| 58 |
|
| 59 |
func (tfw *testFileWriter) Close() error { |
| 60 |
tfw.Write(nil) |
| 61 |
return tfw.FileWriter.Close() |
| 62 |
} |
| 63 |
|
| 64 |
func (tfw *testFileWriter) Cancel() error { |
| 65 |
tfw.Write(nil) |
| 66 |
return tfw.FileWriter.Cancel() |
| 67 |
} |
| 68 |
|
| 69 |
func (tfw *testFileWriter) Commit() error { |
| 70 |
tfw.Write(nil) |
| 71 |
return tfw.FileWriter.Commit() |
| 72 |
} |