| 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 constantvar |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/google/go-cmp/cmp" |
| 24 |
"gocloud.dev/runtimevar" |
| 25 |
"gocloud.dev/runtimevar/driver" |
| 26 |
"gocloud.dev/runtimevar/drivertest" |
| 27 |
) |
| 28 |
|
| 29 |
type harness struct { |
| 30 |
// vars stores the variable value(s) that have been set using CreateVariable. |
| 31 |
vars map[string][]byte |
| 32 |
} |
| 33 |
|
| 34 |
func newHarness(t *testing.T) (drivertest.Harness, error) { |
| 35 |
t.Helper() |
| 36 |
|
| 37 |
return &harness{vars: map[string][]byte{}}, nil |
| 38 |
} |
| 39 |
|
| 40 |
func (h *harness) MakeWatcher(ctx context.Context, name string, decoder *runtimevar.Decoder) (driver.Watcher, error) { |
| 41 |
rawVal, found := h.vars[name] |
| 42 |
if !found { |
| 43 |
// The variable isn't set. Create a Variable that always returns an error. |
| 44 |
return &watcher{err: errNotExist}, nil |
| 45 |
} |
| 46 |
val, err := decoder.Decode(ctx, rawVal) |
| 47 |
if err != nil { |
| 48 |
// The variable didn't decode. |
| 49 |
return &watcher{err: err}, nil |
| 50 |
} |
| 51 |
return &watcher{value: val, t: time.Now()}, nil |
| 52 |
} |
| 53 |
|
| 54 |
func (h *harness) CreateVariable(ctx context.Context, name string, val []byte) error { |
| 55 |
h.vars[name] = val |
| 56 |
return nil |
| 57 |
} |
| 58 |
|
| 59 |
func (h *harness) UpdateVariable(ctx context.Context, name string, val []byte) error { |
| 60 |
return errors.New("not supported") |
| 61 |
} |
| 62 |
|
| 63 |
func (h *harness) DeleteVariable(ctx context.Context, name string) error { |
| 64 |
return errors.New("not supported") |
| 65 |
} |
| 66 |
|
| 67 |
func (h *harness) Close() {} |
| 68 |
|
| 69 |
func (h *harness) Mutable() bool { return false } |
| 70 |
|
| 71 |
func TestConformance(t *testing.T) { |
| 72 |
drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}}) |
| 73 |
} |
| 74 |
|
| 75 |
type verifyAs struct{} |
| 76 |
|
| 77 |
func (verifyAs) Name() string { |
| 78 |
return "verify As" |
| 79 |
} |
| 80 |
|
| 81 |
func (verifyAs) SnapshotCheck(s *runtimevar.Snapshot) error { |
| 82 |
var ss string |
| 83 |
if s.As(&ss) { |
| 84 |
return errors.New("Snapshot.As expected to fail") |
| 85 |
} |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
func (verifyAs) ErrorCheck(v *runtimevar.Variable, err error) error { |
| 90 |
var ss string |
| 91 |
if v.ErrorAs(err, &ss) { |
| 92 |
return errors.New("runtimevar.ErrorAs expected to fail") |
| 93 |
} |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
func TestNew(t *testing.T) { |
| 98 |
ctx := context.Background() |
| 99 |
|
| 100 |
// Use New with an error value; it should be plumbed through as a Value. |
| 101 |
errFail := errors.New("fail") |
| 102 |
v := New(errFail) |
| 103 |
defer v.Close() |
| 104 |
val, err := v.Watch(ctx) |
| 105 |
if err != nil { |
| 106 |
t.Fatal(err) |
| 107 |
} |
| 108 |
if val.Value != errFail { |
| 109 |
t.Errorf("got %v want %v", val.Value, errFail) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestNewBytes(t *testing.T) { |
| 114 |
ctx := context.Background() |
| 115 |
content := "hello world" |
| 116 |
|
| 117 |
// Decode succeeds. |
| 118 |
v := NewBytes([]byte(content), runtimevar.StringDecoder) |
| 119 |
defer v.Close() |
| 120 |
val, err := v.Watch(ctx) |
| 121 |
if err != nil { |
| 122 |
t.Fatal(err) |
| 123 |
} |
| 124 |
if val.Value != content { |
| 125 |
t.Errorf("got %v want %v", val.Value, content) |
| 126 |
} |
| 127 |
|
| 128 |
// Decode fails. |
| 129 |
var jsonData []string |
| 130 |
v = NewBytes([]byte(content), runtimevar.NewDecoder(jsonData, runtimevar.JSONDecode)) |
| 131 |
defer v.Close() |
| 132 |
val, err = v.Watch(ctx) |
| 133 |
if err == nil { |
| 134 |
t.Errorf("got nil error and %v, want error", val) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestNewFromEnv(t *testing.T) { |
| 139 |
ctx := context.Background() |
| 140 |
const ( |
| 141 |
content = "hello world" |
| 142 |
name = "RUNTIMEVAR_CONST_TEST" |
| 143 |
) |
| 144 |
t.Setenv(name, content) |
| 145 |
|
| 146 |
// Decode succeeds. |
| 147 |
v := NewFromEnv(name, runtimevar.StringDecoder) |
| 148 |
defer v.Close() |
| 149 |
val, err := v.Watch(ctx) |
| 150 |
if err != nil { |
| 151 |
t.Fatal(err) |
| 152 |
} |
| 153 |
if val.Value != content { |
| 154 |
t.Errorf("got %v want %v", val.Value, content) |
| 155 |
} |
| 156 |
|
| 157 |
// Decode fails. |
| 158 |
var jsonData []string |
| 159 |
v = NewFromEnv(name, runtimevar.NewDecoder(jsonData, runtimevar.JSONDecode)) |
| 160 |
defer v.Close() |
| 161 |
val, err = v.Watch(ctx) |
| 162 |
if err == nil { |
| 163 |
t.Errorf("got nil error and %v, want error", val) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestNewError(t *testing.T) { |
| 168 |
ctx := context.Background() |
| 169 |
|
| 170 |
v := NewError(errors.New("fail")) |
| 171 |
defer v.Close() |
| 172 |
_, err := v.Watch(ctx) |
| 173 |
if err == nil { |
| 174 |
t.Errorf("got nil err want fail err") |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func TestOpenVariable(t *testing.T) { |
| 179 |
t.Setenv("RUNTIMEVAR_CONST_TEST", "hello world") |
| 180 |
tests := []struct { |
| 181 |
URL string |
| 182 |
WantErr bool |
| 183 |
WantWatchErr bool |
| 184 |
Want any |
| 185 |
}{ |
| 186 |
// Empty URL results in empty byte slice. |
| 187 |
{"constant://", false, false, []byte("")}, |
| 188 |
// Invalid query param. |
| 189 |
{"constant://?param=value", true, false, nil}, |
| 190 |
// String value. |
| 191 |
{"constant://?val=hello+world&decoder=string", false, false, "hello world"}, |
| 192 |
// JSON value; val parameter is {"Foo": "Bar"}, URL-encoded. |
| 193 |
{"constant://?val=%7B%22Foo%22%3A%22Bar%22%7d&decoder=jsonmap", false, false, &map[string]any{"Foo": "Bar"}}, |
| 194 |
// Environment variable value. |
| 195 |
{"constant://?envvar=RUNTIMEVAR_CONST_TEST&decoder=string", false, false, "hello world"}, |
| 196 |
// Error. |
| 197 |
{"constant://?err=fail", false, true, nil}, |
| 198 |
// Invalid decoder. |
| 199 |
{"constant://?decoder=notadecoder", true, false, nil}, |
| 200 |
} |
| 201 |
|
| 202 |
ctx := context.Background() |
| 203 |
for _, test := range tests { |
| 204 |
t.Run(test.URL, func(t *testing.T) { |
| 205 |
v, err := runtimevar.OpenVariable(ctx, test.URL) |
| 206 |
if (err != nil) != test.WantErr { |
| 207 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 208 |
} |
| 209 |
if err != nil { |
| 210 |
return |
| 211 |
} |
| 212 |
defer v.Close() |
| 213 |
snapshot, err := v.Watch(ctx) |
| 214 |
if (err != nil) != test.WantWatchErr { |
| 215 |
t.Errorf("%s: got Watch error %v, want error %v", test.URL, err, test.WantWatchErr) |
| 216 |
} |
| 217 |
if err != nil { |
| 218 |
return |
| 219 |
} |
| 220 |
if !cmp.Equal(snapshot.Value, test.Want) { |
| 221 |
t.Errorf("%s: got snapshot value\n%v\n want\n%v", test.URL, snapshot.Value, test.Want) |
| 222 |
} |
| 223 |
}) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestDecryptWithNoURLEnv(t *testing.T) { |
| 228 |
if _, err := runtimevar.OpenVariable(context.Background(), "constant://?decoder=decrypt"); err == nil { |
| 229 |
t.Error("got nil error, want environment variable not set") |
| 230 |
} |
| 231 |
} |