Debian Codegraph @

runtimevar/blobvar/blobvar_test.go

raw
1 // Copyright 2019 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 blobvar
16
17 import (
18 "context"
19 "errors"
20 "net/url"
21 "os"
22 "path/filepath"
23 "strings"
24 "testing"
25
26 "github.com/google/go-cmp/cmp"
27 "gocloud.dev/blob"
28 "gocloud.dev/blob/fileblob"
29 "gocloud.dev/runtimevar"
30 "gocloud.dev/runtimevar/driver"
31 "gocloud.dev/runtimevar/drivertest"
32 )
33
34 type harness struct {
35 dir string
36 bucket *blob.Bucket
37 }
38
39 func newHarness(t *testing.T) (drivertest.Harness, error) {
40 t.Helper()
41
42 dir := t.TempDir()
43
44 b, err := fileblob.OpenBucket(dir, nil)
45 if err != nil {
46 return nil, err
47 }
48
49 return &harness{dir: dir, bucket: b}, nil
50 }
51
52 func (h *harness) MakeWatcher(ctx context.Context, name string, decoder *runtimevar.Decoder) (driver.Watcher, error) {
53 return newWatcher(h.bucket, name, decoder, nil, nil), nil
54 }
55
56 func (h *harness) CreateVariable(ctx context.Context, name string, val []byte) error {
57 return h.bucket.WriteAll(ctx, name, val, nil)
58 }
59
60 func (h *harness) UpdateVariable(ctx context.Context, name string, val []byte) error {
61 return h.bucket.WriteAll(ctx, name, val, nil)
62 }
63
64 func (h *harness) DeleteVariable(ctx context.Context, name string) error {
65 return h.bucket.Delete(ctx, name)
66 }
67
68 func (h *harness) Close() {
69 h.bucket.Close()
70 _ = os.RemoveAll(h.dir)
71 }
72
73 func (h *harness) Mutable() bool { return true }
74
75 func TestConformance(t *testing.T) {
76 drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}})
77 }
78
79 type verifyAs struct{}
80
81 func (verifyAs) Name() string {
82 return "verify As"
83 }
84
85 func (verifyAs) SnapshotCheck(s *runtimevar.Snapshot) error {
86 return nil
87 }
88
89 func (verifyAs) ErrorCheck(v *runtimevar.Variable, err error) error {
90 var perr *os.PathError
91 if !v.ErrorAs(err, &perr) {
92 return errors.New("runtimevar.ErrorAs failed with *os.PathError")
93 }
94 return nil
95 }
96
97 func TestOpenVariable(t *testing.T) {
98 dir := t.TempDir()
99
100 if err := os.WriteFile(filepath.Join(dir, "myvar.json"), []byte(`{"Foo": "Bar"}`), 0o666); err != nil {
101 t.Fatal(err)
102 }
103 if err := os.WriteFile(filepath.Join(dir, "myvar.txt"), []byte("hello world!"), 0o666); err != nil {
104 t.Fatal(err)
105 }
106
107 // Convert dir to a URL path, adding a leading "/" if needed on Windows
108 // (on Unix, dirpath already has a leading "/").
109 dirpath := filepath.ToSlash(dir)
110 if os.PathSeparator != '/' && !strings.HasPrefix(dirpath, "/") {
111 dirpath = "/" + dirpath
112 }
113 bucketURL := "file://" + dirpath
114
115 tests := []struct {
116 BucketURL string
117 URL string
118 WantErr bool
119 WantWatchErr bool
120 Want any
121 }{
122 // myvar does not exist.
123 {"mem://", "blob://myvar", false, true, nil},
124 // badscheme does not exist.
125 {"badscheme://", "blob://myvar", true, false, nil},
126 // directory dirnotfound does not exist, so Bucket creation fails.
127 {"file:///dirnotfound", "blob://myvar.txt", true, false, nil},
128 // filenotfound does not exist so Watch returns an error.
129 {bucketURL, "blob://filenotfound", false, true, nil},
130 // Missing bucket env variable.
131 {"", "blob://myvar.txt", true, false, nil},
132 // Invalid decoder.
133 {bucketURL, "blob://myvar.txt?decoder=notadecoder", true, false, nil},
134 // Invalid arg.
135 {bucketURL, "blob://myvar.txt?param=value", true, false, nil},
136 // Working example with default decoder.
137 {bucketURL, "blob://myvar.txt", false, false, []byte("hello world!")},
138 // Working example with string decoder.
139 {bucketURL, "blob://myvar.txt?decoder=string", false, false, "hello world!"},
140 // Working example with JSON decoder.
141 {bucketURL, "blob://myvar.json?decoder=jsonmap", false, false, &map[string]any{"Foo": "Bar"}},
142 // Setting wait.
143 {bucketURL, "blob://myvar.txt?wait=2m", false, false, []byte("hello world!")},
144 // Invalid wait.
145 {bucketURL, "blob://myvar.txt?wait=x", true, false, nil},
146 }
147
148 ctx := context.Background()
149 for _, test := range tests {
150 t.Run(test.BucketURL, func(t *testing.T) {
151 t.Setenv("BLOBVAR_BUCKET_URL", test.BucketURL)
152
153 opener := &defaultOpener{}
154 defer func() {
155 if opener.opener != nil && opener.opener.Bucket != nil {
156 opener.opener.Bucket.Close()
157 }
158 }()
159 u, err := url.Parse(test.URL)
160 if err != nil {
161 t.Error(err)
162 }
163 v, err := opener.OpenVariableURL(ctx, u)
164 if v != nil {
165 defer v.Close()
166 }
167 if (err != nil) != test.WantErr {
168 t.Errorf("BucketURL %s URL %s: got error %v, want error %v", test.BucketURL, test.URL, err, test.WantErr)
169 }
170 if err != nil {
171 return
172 }
173 defer v.Close()
174 snapshot, err := v.Watch(ctx)
175 if (err != nil) != test.WantWatchErr {
176 t.Errorf("BucketURL %s URL %s: got Watch error %v, want error %v", test.BucketURL, test.URL, err, test.WantWatchErr)
177 }
178 if err != nil {
179 return
180 }
181 if !cmp.Equal(snapshot.Value, test.Want) {
182 t.Errorf("BucketURL %s URL %s: got snapshot value\n%v\n want\n%v", test.BucketURL, test.URL, snapshot.Value, test.Want)
183 }
184 })
185 }
186 }