Debian Codegraph @

runtimevar/etcdvar/etcdvar_test.go

raw
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 etcdvar
16
17 import (
18 "context"
19 "errors"
20 "os"
21 "testing"
22 "time"
23
24 "github.com/google/go-cmp/cmp"
25 "go.etcd.io/etcd/client/v3"
26 "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
27 "gocloud.dev/internal/testing/setup"
28 "gocloud.dev/runtimevar"
29 "gocloud.dev/runtimevar/driver"
30 "gocloud.dev/runtimevar/drivertest"
31 )
32
33 // To run these tests against a local etcd server, first run ./localetcd.sh.
34 // Then wait a few seconds for the server to be ready.
35
36 type harness struct {
37 client *clientv3.Client
38 }
39
40 func newHarness(t *testing.T) (drivertest.Harness, error) {
41 if !setup.HasDockerTestEnvironment() {
42 t.Skip("Skipping etcd tests since the etcd server is not available")
43 }
44 c, err := clientv3.NewFromURL("http://localhost:2379")
45 if err != nil {
46 t.Fatalf("No local etcd server running: %v; see runtimevar/etcdvar/localetcd.sh", err)
47 }
48 return &harness{client: c}, nil
49 }
50
51 func (h *harness) MakeWatcher(ctx context.Context, name string, decoder *runtimevar.Decoder) (driver.Watcher, error) {
52 return newWatcher(h.client, name, decoder, nil), nil
53 }
54
55 func (h *harness) CreateVariable(ctx context.Context, name string, val []byte) error {
56 _, err := h.client.Put(ctx, name, string(val))
57 return err
58 }
59
60 func (h *harness) UpdateVariable(ctx context.Context, name string, val []byte) error {
61 return h.CreateVariable(ctx, name, val)
62 }
63
64 func (h *harness) DeleteVariable(ctx context.Context, name string) error {
65 _, err := h.client.Delete(ctx, name)
66 return err
67 }
68
69 func (h *harness) Mutable() bool { return true }
70
71 func (h *harness) Close() {}
72
73 func TestConformance(t *testing.T) {
74 drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}})
75 }
76
77 type verifyAs struct{}
78
79 func (verifyAs) Name() string {
80 return "verify As"
81 }
82
83 func (verifyAs) SnapshotCheck(s *runtimevar.Snapshot) error {
84 var resp *clientv3.GetResponse
85 if !s.As(&resp) {
86 return errors.New("Snapshot.As failed")
87 }
88 return nil
89 }
90
91 func (verifyAs) ErrorCheck(v *runtimevar.Variable, err error) error {
92 // etcdvar returns a fmt.Errorf error for "not found", so this is expected
93 // to fail.
94 var to rpctypes.EtcdError
95 if v.ErrorAs(err, &to) {
96 return errors.New("ErrorAs expected to fail")
97 }
98 // Try with a real etcd error.
99 if !v.ErrorAs(rpctypes.ErrUnhealthy, &to) {
100 return errors.New("ErrorAs expected to succeed with real etcd error")
101 }
102 return nil
103 }
104
105 // Etcd-specific tests.
106
107 func TestEquivalentError(t *testing.T) {
108 tests := []struct {
109 Err1, Err2 error
110 Want bool
111 }{
112 {Err1: errors.New("not etcd"), Err2: errors.New("not etcd"), Want: true},
113 {Err1: errors.New("not etcd"), Err2: errors.New("not etcd but different")},
114 {Err1: errors.New("not etcd"), Err2: rpctypes.ErrUnhealthy},
115 {Err1: rpctypes.ErrUnhealthy, Err2: rpctypes.ErrRequestTooLarge},
116 {Err1: rpctypes.ErrUnhealthy, Err2: rpctypes.ErrUnhealthy, Want: true},
117 }
118
119 for _, test := range tests {
120 got := equivalentError(test.Err1, test.Err2)
121 if got != test.Want {
122 t.Errorf("%v vs %v: got %v want %v", test.Err1, test.Err2, got, test.Want)
123 }
124 }
125 }
126
127 func TestNoConnectionError(t *testing.T) {
128 cli, err := clientv3.NewFromURL("http://no.server.here:999")
129 if err != nil {
130 t.Fatal(err)
131 }
132 v, err := OpenVariable(cli, "variable-name", nil, nil)
133 if err != nil {
134 t.Fatal(err)
135 }
136 defer v.Close()
137 // Watch will block for quite a while trying to connect,
138 // so use a short timeout.
139 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
140 defer cancel()
141 _, err = v.Watch(ctx)
142 if err == nil {
143 t.Error("got nil want error")
144 }
145 }
146
147 func TestOpenVariable(t *testing.T) {
148 h, err := newHarness(t)
149 if err != nil {
150 t.Fatal(err)
151 }
152 os.Setenv("ETCD_SERVER_URL", "http://localhost:2379")
153
154 ctx := context.Background()
155 if err := h.CreateVariable(ctx, "string-var", []byte("hello world")); err != nil {
156 t.Fatal(err)
157 }
158 if err := h.CreateVariable(ctx, "json-var", []byte(`{"Foo": "Bar"}`)); err != nil {
159 t.Fatal(err)
160 }
161
162 tests := []struct {
163 URL string
164 WantErr bool
165 WantWatchErr bool
166 Want any
167 }{
168 // Nonexistentvar does not exist, so we get an error from Watch.
169 {"etcd://nonexistentvar", false, true, nil},
170 // Invalid decoder arg.
171 {"etcd://string-var?decoder=notadecoder", true, false, nil},
172 // Invalid parameter.
173 {"etcd://string-var?param=value", true, false, nil},
174 // Working example with string decoder.
175 {"etcd://string-var?decoder=string", false, false, "hello world"},
176 // Working example with default decoder.
177 {"etcd://string-var", false, false, []byte("hello world")},
178 // Working example with JSON decoder.
179 {"etcd://json-var?decoder=jsonmap", false, false, &map[string]any{"Foo": "Bar"}},
180 }
181
182 for _, test := range tests {
183 t.Run(test.URL, func(t *testing.T) {
184 v, err := runtimevar.OpenVariable(ctx, test.URL)
185 if (err != nil) != test.WantErr {
186 t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr)
187 }
188 if err != nil {
189 return
190 }
191 defer v.Close()
192 snapshot, err := v.Watch(ctx)
193 if (err != nil) != test.WantWatchErr {
194 t.Errorf("%s: got Watch error %v, want error %v", test.URL, err, test.WantWatchErr)
195 }
196 if err != nil {
197 return
198 }
199 if !cmp.Equal(snapshot.Value, test.Want) {
200 t.Errorf("%s: got snapshot value\n%v\n want\n%v", test.URL, snapshot.Value, test.Want)
201 }
202 })
203 }
204 }