-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: unexport the watcher type and update NewUpdater
The watcher semantics are still useful for plumbing, but it's hard to use correctly, so unexport it. Now that watcher is unexported, move its tests from a separate package into an internal package test. It is now no longer necessary to have the "watcher" method, so remove it. An Updater now no longer accepts a watcher (which is unexported), but instead takes a *Store and the desired secret name directly. Rework the constructor to allow reporting an error. Add a StaticUpdater constructor to make an Updater that vends a static value, analogous to StaticSecret.
- Loading branch information
1 parent
cd5c757
commit 24b6f2e
Showing
6 changed files
with
282 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Tailscale Inc & AUTHORS | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
package setec | ||
|
||
import ( | ||
"context" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/tailscale/setec/setectest" | ||
) | ||
|
||
func TestWatcher(t *testing.T) { | ||
d := setectest.NewDB(t, nil) | ||
d.MustPut(d.Superuser, "green", "eggs and ham") // active | ||
v2 := d.MustPut(d.Superuser, "green", "grow the rushes oh") | ||
|
||
ts := setectest.NewServer(t, d, nil) | ||
hs := httptest.NewServer(ts.Mux) | ||
defer hs.Close() | ||
|
||
ctx := context.Background() | ||
cli := Client{Server: hs.URL, DoHTTP: hs.Client().Do} | ||
|
||
pollTicker := setectest.NewFakeTicker() | ||
st, err := NewStore(ctx, StoreConfig{ | ||
Client: cli, | ||
Secrets: []string{"green"}, | ||
PollTicker: pollTicker, | ||
}) | ||
if err != nil { | ||
t.Fatalf("NewStore: unexpected error: %v", err) | ||
} | ||
defer st.Close() | ||
|
||
// With lookups disabled, an unknown watcher reports an error. | ||
if w, err := st.lookupWatcher(ctx, "nonesuch"); err == nil { | ||
t.Errorf("Lookup: got %v, want error", w) | ||
} | ||
|
||
// Observe the initial value of the secret. | ||
w, err := st.lookupWatcher(ctx, "green") | ||
if err != nil { | ||
t.Errorf("Initial value: unexpected error: %v", err) | ||
} else if got, want := string(w.Get()), "eggs and ham"; got != want { | ||
t.Errorf("Initial value: got %q, want %q", got, want) | ||
} | ||
|
||
// The secret gets updated... | ||
if err := cli.Activate(ctx, "green", v2); err != nil { | ||
t.Fatalf("Activate to %v: unexpected error: %v", v2, err) | ||
} | ||
|
||
// The next poll occurs... | ||
pollTicker.Poll() | ||
|
||
// The watcher should get notified in a timely manner. | ||
select { | ||
case <-w.Ready(): | ||
t.Logf("✓ A new version of the secret is available") | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("Timed out waiting for a watcher update") | ||
} | ||
|
||
if got, want := string(w.Get()), "grow the rushes oh"; got != want { | ||
t.Errorf("Updated value: got %q, want %q", got, want) | ||
} | ||
|
||
// With no updates, the watchers should not appear ready. | ||
select { | ||
case <-w.Ready(): | ||
t.Error("Watcher is unexpectedly ready after no update") | ||
case <-time.After(100 * time.Millisecond): | ||
// OK | ||
} | ||
} |
Oops, something went wrong.