| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package memblob |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"net/http" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"gocloud.dev/blob" |
| 23 |
"gocloud.dev/blob/driver" |
| 24 |
"gocloud.dev/blob/drivertest" |
| 25 |
) |
| 26 |
|
| 27 |
type harness struct { |
| 28 |
prefix string |
| 29 |
} |
| 30 |
|
| 31 |
func newHarness(ctx context.Context, t *testing.T, prefix string) (drivertest.Harness, error) { |
| 32 |
t.Helper() |
| 33 |
|
| 34 |
return &harness{prefix: prefix}, nil |
| 35 |
} |
| 36 |
|
| 37 |
func (h *harness) HTTPClient() *http.Client { |
| 38 |
return nil |
| 39 |
} |
| 40 |
|
| 41 |
func (h *harness) MakeDriver(ctx context.Context) (driver.Bucket, error) { |
| 42 |
drv := openBucket(nil) |
| 43 |
if h.prefix == "" { |
| 44 |
return drv, nil |
| 45 |
} |
| 46 |
return driver.NewPrefixedBucket(drv, h.prefix), nil |
| 47 |
} |
| 48 |
|
| 49 |
func (h *harness) MakeDriverForNonexistentBucket(ctx context.Context) (driver.Bucket, error) { |
| 50 |
// Does not make sense for this driver. |
| 51 |
return nil, nil |
| 52 |
} |
| 53 |
|
| 54 |
func (h *harness) Close() {} |
| 55 |
|
| 56 |
func TestConformance(t *testing.T) { |
| 57 |
newHarnessNoPrefix := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 58 |
t.Helper() |
| 59 |
|
| 60 |
return newHarness(ctx, t, "") |
| 61 |
} |
| 62 |
drivertest.RunConformanceTests(t, newHarnessNoPrefix, nil) |
| 63 |
} |
| 64 |
|
| 65 |
func TestConformanceWithPrefix(t *testing.T) { |
| 66 |
const prefix = "some/prefix/dir/" |
| 67 |
newHarnessWithPrefix := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 68 |
t.Helper() |
| 69 |
|
| 70 |
return newHarness(ctx, t, prefix) |
| 71 |
} |
| 72 |
drivertest.RunConformanceTests(t, newHarnessWithPrefix, nil) |
| 73 |
} |
| 74 |
|
| 75 |
func BenchmarkMemblob(b *testing.B) { |
| 76 |
drivertest.RunBenchmarks(b, OpenBucket(nil)) |
| 77 |
} |
| 78 |
|
| 79 |
func TestOpenBucketFromURL(t *testing.T) { |
| 80 |
tests := []struct { |
| 81 |
URL string |
| 82 |
WantErr bool |
| 83 |
}{ |
| 84 |
// OK. |
| 85 |
{"mem://", false}, |
| 86 |
// NoMD5. |
| 87 |
{"mem://?nomd5", false}, |
| 88 |
// With prefix. |
| 89 |
{"mem://?prefix=foo/bar", false}, |
| 90 |
// Invalid parameter. |
| 91 |
{"mem://?param=value", true}, |
| 92 |
} |
| 93 |
|
| 94 |
ctx := context.Background() |
| 95 |
for _, test := range tests { |
| 96 |
b, err := blob.OpenBucket(ctx, test.URL) |
| 97 |
if b != nil { |
| 98 |
defer b.Close() |
| 99 |
} |
| 100 |
if (err != nil) != test.WantErr { |
| 101 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 102 |
} |
| 103 |
} |
| 104 |
} |