| 1 |
// Copyright 2018 Google LLC |
| 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 |
// http://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 stiface |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"io/ioutil" |
| 22 |
"os" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"cloud.google.com/go/storage" |
| 26 |
) |
| 27 |
|
| 28 |
func TestIntegration(t *testing.T) { |
| 29 |
if testing.Short() { |
| 30 |
t.Skip("integration tests skipped in short mode") |
| 31 |
} |
| 32 |
name := os.Getenv("STIFACE_BUCKET") |
| 33 |
if name == "" { |
| 34 |
t.Skip("missing STIFACE_BUCKET environment variable") |
| 35 |
} |
| 36 |
ctx := context.Background() |
| 37 |
c, err := storage.NewClient(ctx) |
| 38 |
if err != nil { |
| 39 |
t.Fatal(err) |
| 40 |
} |
| 41 |
client := AdaptClient(c) |
| 42 |
defer client.Close() |
| 43 |
bkt := client.Bucket(name) |
| 44 |
basicTests(t, name, bkt) |
| 45 |
} |
| 46 |
|
| 47 |
func basicTests(t *testing.T, bucketName string, bkt BucketHandle) { |
| 48 |
ctx := context.Background() |
| 49 |
attrs, err := bkt.Attrs(ctx) |
| 50 |
if err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
if got, want := attrs.Name, bucketName; got != want { |
| 54 |
t.Errorf("name: got %v, want %v", got, want) |
| 55 |
} |
| 56 |
|
| 57 |
const contents = "hello, stiface" |
| 58 |
obj := bkt.Object("stiface-test") |
| 59 |
w := obj.NewWriter(ctx) |
| 60 |
if _, err := fmt.Fprint(w, contents); err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
if err := w.Close(); err != nil { |
| 64 |
t.Fatal(err) |
| 65 |
} |
| 66 |
bytes := readObject(t, obj) |
| 67 |
if got, want := string(bytes), contents; got != want { |
| 68 |
t.Errorf("got %q, want %q", got, want) |
| 69 |
} |
| 70 |
if err := obj.Delete(ctx); err != nil { |
| 71 |
t.Errorf("deleting: %v", err) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func readObject(t *testing.T, obj ObjectHandle) []byte { |
| 76 |
r, err := obj.NewReader(context.Background()) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("reading %v: %v", obj, err) |
| 79 |
} |
| 80 |
defer r.Close() |
| 81 |
bytes, err := ioutil.ReadAll(r) |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("reading %v: %v", obj, err) |
| 84 |
} |
| 85 |
return bytes |
| 86 |
} |
| 87 |
|
| 88 |
// This test demonstrates how to use this package to create a simple fake for the storage client. |
| 89 |
func TestFake(t *testing.T) { |
| 90 |
ctx := context.Background() |
| 91 |
client := newFakeClient() |
| 92 |
|
| 93 |
bkt := client.Bucket("my-bucket") |
| 94 |
if err := bkt.Create(ctx, "my-project", nil); err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
basicTests(t, "my-bucket", bkt) |
| 98 |
} |
| 99 |
|
| 100 |
type fakeClient struct { |
| 101 |
Client |
| 102 |
buckets map[string]*fakeBucket |
| 103 |
} |
| 104 |
|
| 105 |
type fakeBucket struct { |
| 106 |
attrs *storage.BucketAttrs |
| 107 |
objects map[string][]byte |
| 108 |
} |
| 109 |
|
| 110 |
func newFakeClient() Client { |
| 111 |
return &fakeClient{buckets: map[string]*fakeBucket{}} |
| 112 |
} |
| 113 |
|
| 114 |
func (c *fakeClient) Bucket(name string) BucketHandle { |
| 115 |
return fakeBucketHandle{c: c, name: name} |
| 116 |
} |
| 117 |
|
| 118 |
type fakeBucketHandle struct { |
| 119 |
BucketHandle |
| 120 |
c *fakeClient |
| 121 |
name string |
| 122 |
} |
| 123 |
|
| 124 |
func (b fakeBucketHandle) Create(_ context.Context, _ string, attrs *storage.BucketAttrs) error { |
| 125 |
if _, ok := b.c.buckets[b.name]; ok { |
| 126 |
return fmt.Errorf("bucket %q already exists", b.name) |
| 127 |
} |
| 128 |
if attrs == nil { |
| 129 |
attrs = &storage.BucketAttrs{} |
| 130 |
} |
| 131 |
attrs.Name = b.name |
| 132 |
b.c.buckets[b.name] = &fakeBucket{attrs: attrs, objects: map[string][]byte{}} |
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
func (b fakeBucketHandle) Attrs(context.Context) (*storage.BucketAttrs, error) { |
| 137 |
bkt, ok := b.c.buckets[b.name] |
| 138 |
if !ok { |
| 139 |
return nil, fmt.Errorf("bucket %q does not exist", b.name) |
| 140 |
} |
| 141 |
return bkt.attrs, nil |
| 142 |
} |
| 143 |
|
| 144 |
func (b fakeBucketHandle) Object(name string) ObjectHandle { |
| 145 |
return fakeObjectHandle{c: b.c, bucketName: b.name, name: name} |
| 146 |
} |
| 147 |
|
| 148 |
type fakeObjectHandle struct { |
| 149 |
ObjectHandle |
| 150 |
c *fakeClient |
| 151 |
bucketName string |
| 152 |
name string |
| 153 |
} |
| 154 |
|
| 155 |
func (o fakeObjectHandle) NewReader(context.Context) (Reader, error) { |
| 156 |
bkt, ok := o.c.buckets[o.bucketName] |
| 157 |
if !ok { |
| 158 |
return nil, fmt.Errorf("bucket %q not found", o.bucketName) |
| 159 |
} |
| 160 |
contents, ok := bkt.objects[o.name] |
| 161 |
if !ok { |
| 162 |
return nil, fmt.Errorf("object %q not found in bucket %q", o.name, o.bucketName) |
| 163 |
} |
| 164 |
return fakeReader{r: bytes.NewReader(contents)}, nil |
| 165 |
} |
| 166 |
|
| 167 |
func (o fakeObjectHandle) Delete(context.Context) error { |
| 168 |
bkt, ok := o.c.buckets[o.bucketName] |
| 169 |
if !ok { |
| 170 |
return fmt.Errorf("bucket %q not found", o.bucketName) |
| 171 |
} |
| 172 |
delete(bkt.objects, o.name) |
| 173 |
return nil |
| 174 |
} |
| 175 |
|
| 176 |
type fakeReader struct { |
| 177 |
Reader |
| 178 |
r *bytes.Reader |
| 179 |
} |
| 180 |
|
| 181 |
func (r fakeReader) Read(buf []byte) (int, error) { |
| 182 |
return r.r.Read(buf) |
| 183 |
} |
| 184 |
|
| 185 |
func (r fakeReader) Close() error { |
| 186 |
return nil |
| 187 |
} |
| 188 |
|
| 189 |
func (o fakeObjectHandle) NewWriter(context.Context) Writer { |
| 190 |
return &fakeWriter{obj: o} |
| 191 |
} |
| 192 |
|
| 193 |
type fakeWriter struct { |
| 194 |
Writer |
| 195 |
obj fakeObjectHandle |
| 196 |
buf bytes.Buffer |
| 197 |
} |
| 198 |
|
| 199 |
func (w *fakeWriter) Write(data []byte) (int, error) { |
| 200 |
return w.buf.Write(data) |
| 201 |
} |
| 202 |
|
| 203 |
func (w *fakeWriter) Close() error { |
| 204 |
bkt, ok := w.obj.c.buckets[w.obj.bucketName] |
| 205 |
if !ok { |
| 206 |
return fmt.Errorf("bucket %q not found", w.obj.bucketName) |
| 207 |
} |
| 208 |
bkt.objects[w.obj.name] = w.buf.Bytes() |
| 209 |
return nil |
| 210 |
} |