The first public release of Fabled Minstrel #158
@@ -151,7 +151,7 @@ func TestAdminUpdateCoverSource_FlippingEnabledBumpsVersion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAdminUpdateCoverSource_KeyOnlyDoesNotBump(t *testing.T) {
|
func TestAdminUpdateCoverSource_ReplacingAKeyDoesNotBump(t *testing.T) {
|
||||||
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
|
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
|
||||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||||
}
|
}
|
||||||
@@ -163,26 +163,32 @@ func TestAdminUpdateCoverSource_KeyOnlyDoesNotBump(t *testing.T) {
|
|||||||
h, _ := testHandlersWithCoverSettings(t)
|
h, _ := testHandlersWithCoverSettings(t)
|
||||||
admin := seedUser(t, h.pool, "cskey", "pw", true)
|
admin := seedUser(t, h.pool, "cskey", "pw", true)
|
||||||
|
|
||||||
newKey := "newkey"
|
// The first key on an enabled source moves the version (it searched
|
||||||
body, _ := json.Marshal(updateProviderSourceReq{APIKey: &newKey})
|
// without one until now); replacing that key with another doesn't.
|
||||||
req := httptest.NewRequest(http.MethodPatch, "/api/admin/cover-sources/key-prov", bytes.NewReader(body))
|
for i, step := range []struct {
|
||||||
req.Header.Set("Content-Type", "application/json")
|
key string
|
||||||
req = withUser(req, admin)
|
wantBumped bool
|
||||||
rec := httptest.NewRecorder()
|
}{{"firstkey", true}, {"newkey", false}} {
|
||||||
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
|
body, _ := json.Marshal(updateProviderSourceReq{APIKey: &step.key})
|
||||||
|
req := httptest.NewRequest(http.MethodPatch, "/api/admin/cover-sources/key-prov", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req = withUser(req, admin)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
|
||||||
|
|
||||||
if rec.Code != http.StatusOK {
|
if rec.Code != http.StatusOK {
|
||||||
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
|
t.Fatalf("step %d: status = %d, want 200; body=%s", i, rec.Code, rec.Body.String())
|
||||||
}
|
}
|
||||||
var resp updateProviderSourceResp
|
var resp updateProviderSourceResp
|
||||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||||
t.Fatalf("decode: %v", err)
|
t.Fatalf("decode: %v", err)
|
||||||
}
|
}
|
||||||
if resp.VersionBumped {
|
if resp.VersionBumped != step.wantBumped {
|
||||||
t.Error("version_bumped = true, want false for key-only change")
|
t.Errorf("key %q: version_bumped = %v, want %v", step.key, resp.VersionBumped, step.wantBumped)
|
||||||
}
|
}
|
||||||
if !resp.APIKeySet {
|
if !resp.APIKeySet {
|
||||||
t.Error("api_key_set = false, want true after setting key")
|
t.Error("api_key_set = false, want true after setting key")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ func TestAdminUpdateTagSource_FlippingEnabledBumpsVersion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAdminUpdateTagSource_KeyOnlyDoesNotBump(t *testing.T) {
|
func TestAdminUpdateTagSource_ReplacingAKeyDoesNotBump(t *testing.T) {
|
||||||
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
|
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
|
||||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||||
}
|
}
|
||||||
@@ -145,26 +145,32 @@ func TestAdminUpdateTagSource_KeyOnlyDoesNotBump(t *testing.T) {
|
|||||||
h := testHandlersWithTagSettings(t)
|
h := testHandlersWithTagSettings(t)
|
||||||
admin := seedUser(t, h.pool, "tskey", "pw", true)
|
admin := seedUser(t, h.pool, "tskey", "pw", true)
|
||||||
|
|
||||||
newKey := "newkey"
|
// The first key on an enabled source moves the version (it searched
|
||||||
body, _ := json.Marshal(updateProviderSourceReq{APIKey: &newKey})
|
// without one until now); replacing that key with another doesn't.
|
||||||
req := httptest.NewRequest(http.MethodPatch, "/api/admin/tag-sources/key-tag", bytes.NewReader(body))
|
for i, step := range []struct {
|
||||||
req.Header.Set("Content-Type", "application/json")
|
key string
|
||||||
req = withUser(req, admin)
|
wantBumped bool
|
||||||
rec := httptest.NewRecorder()
|
}{{"firstkey", true}, {"newkey", false}} {
|
||||||
newAdminTagSourcesRouter(h).ServeHTTP(rec, req)
|
body, _ := json.Marshal(updateProviderSourceReq{APIKey: &step.key})
|
||||||
|
req := httptest.NewRequest(http.MethodPatch, "/api/admin/tag-sources/key-tag", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req = withUser(req, admin)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
newAdminTagSourcesRouter(h).ServeHTTP(rec, req)
|
||||||
|
|
||||||
if rec.Code != http.StatusOK {
|
if rec.Code != http.StatusOK {
|
||||||
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
|
t.Fatalf("step %d: status = %d, want 200; body=%s", i, rec.Code, rec.Body.String())
|
||||||
}
|
}
|
||||||
var resp updateProviderSourceResp
|
var resp updateProviderSourceResp
|
||||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||||
t.Fatalf("decode: %v", err)
|
t.Fatalf("decode: %v", err)
|
||||||
}
|
}
|
||||||
if resp.VersionBumped {
|
if resp.VersionBumped != step.wantBumped {
|
||||||
t.Error("version_bumped = true, want false for key-only change")
|
t.Errorf("key %q: version_bumped = %v, want %v", step.key, resp.VersionBumped, step.wantBumped)
|
||||||
}
|
}
|
||||||
if !resp.APIKeySet {
|
if !resp.APIKeySet {
|
||||||
t.Error("api_key_set = false, want true after setting key")
|
t.Error("api_key_set = false, want true after setting key")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ func TestSettingsService_UpdateProvider_FlippingEnabledBumpsVersion(t *testing.T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingsService_UpdateProvider_KeyOnlyDoesNotBumpVersion(t *testing.T) {
|
func TestSettingsService_UpdateProvider_ReplacingAKeyDoesNotBumpVersion(t *testing.T) {
|
||||||
pool := dbtest.Pool(t)
|
pool := dbtest.Pool(t)
|
||||||
resetRegistryForTests()
|
resetRegistryForTests()
|
||||||
defer resetRegistryForTests()
|
defer resetRegistryForTests()
|
||||||
@@ -105,6 +105,11 @@ func TestSettingsService_UpdateProvider_KeyOnlyDoesNotBumpVersion(t *testing.T)
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The first key moves the version: the source searched without one.
|
||||||
|
firstKey := "first1"
|
||||||
|
if bumped, err := s.UpdateProvider(context.Background(), "fake-album", ProviderUpdatePatch{APIKey: &firstKey}); err != nil || !bumped {
|
||||||
|
t.Fatalf("first key: bumped=%v err=%v, want true nil", bumped, err)
|
||||||
|
}
|
||||||
startVersion := s.CurrentVersion()
|
startVersion := s.CurrentVersion()
|
||||||
|
|
||||||
newKey := "abc123"
|
newKey := "abc123"
|
||||||
@@ -113,7 +118,7 @@ func TestSettingsService_UpdateProvider_KeyOnlyDoesNotBumpVersion(t *testing.T)
|
|||||||
t.Fatalf("UpdateProvider: %v", err)
|
t.Fatalf("UpdateProvider: %v", err)
|
||||||
}
|
}
|
||||||
if bumped {
|
if bumped {
|
||||||
t.Error("versionBumped = true, want false for key-only change")
|
t.Error("versionBumped = true, want false for a replaced key")
|
||||||
}
|
}
|
||||||
if s.CurrentVersion() != startVersion {
|
if s.CurrentVersion() != startVersion {
|
||||||
t.Errorf("CurrentVersion changed: %d → %d, want unchanged", startVersion, s.CurrentVersion())
|
t.Errorf("CurrentVersion changed: %d → %d, want unchanged", startVersion, s.CurrentVersion())
|
||||||
|
|||||||
@@ -72,8 +72,8 @@ type Config struct {
|
|||||||
// Service reconciles a family's registry with its settings table, caches
|
// Service reconciles a family's registry with its settings table, caches
|
||||||
// each provider's enabled flag and key, and owns the sources version: the
|
// each provider's enabled flag and key, and owns the sources version: the
|
||||||
// stamp the enricher writes onto rows it has tried. The version moves
|
// stamp the enricher writes onto rows it has tried. The version moves
|
||||||
// whenever the enabled set changes, so rows that found nothing get another
|
// whenever the enabled set changes, or an enabled source gains or loses its
|
||||||
// try through the new chain.
|
// key, so rows that found nothing get another try through the new chain.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
cfg Config
|
cfg Config
|
||||||
|
|
||||||
@@ -168,7 +168,8 @@ func (s *Service) CurrentVersion() int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateProvider applies an admin patch and re-Configures the provider.
|
// UpdateProvider applies an admin patch and re-Configures the provider.
|
||||||
// versionBumped is true when the enabled set changed; a key change alone
|
// versionBumped is true when the sources signature changed: the enabled set,
|
||||||
|
// or whether an enabled source has a key. Replacing one key with another
|
||||||
// doesn't move the version.
|
// doesn't move the version.
|
||||||
func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch UpdatePatch) (versionBumped bool, err error) {
|
func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch UpdatePatch) (versionBumped bool, err error) {
|
||||||
p, err := s.cfg.Registry.ByID(providerID)
|
p, err := s.cfg.Registry.ByID(providerID)
|
||||||
@@ -176,7 +177,7 @@ func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch U
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
before := enabledSetSignature(s.enabledIDs)
|
before := s.sourcesSignature()
|
||||||
enabled := s.enabledIDs[providerID]
|
enabled := s.enabledIDs[providerID]
|
||||||
s.mu.RUnlock()
|
s.mu.RUnlock()
|
||||||
if patch.Enabled != nil {
|
if patch.Enabled != nil {
|
||||||
@@ -200,7 +201,7 @@ func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch U
|
|||||||
s.cache(r)
|
s.cache(r)
|
||||||
}
|
}
|
||||||
s.configure(p)
|
s.configure(p)
|
||||||
if enabledSetSignature(s.enabledIDs) == before {
|
if s.sourcesSignature() == before {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
v, err := s.cfg.Store.BumpVersion(ctx)
|
v, err := s.cfg.Store.BumpVersion(ctx)
|
||||||
@@ -211,13 +212,24 @@ func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch U
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// enabledSetSignature is the sorted, comma-joined enabled provider IDs.
|
// sourcesSignature is the sorted, comma-joined enabled provider IDs, each
|
||||||
func enabledSetSignature(enabledIDs map[string]bool) string {
|
// marked "+key" when it has a key; the caller holds mu (read or write).
|
||||||
ids := make([]string, 0, len(enabledIDs))
|
//
|
||||||
for id, on := range enabledIDs {
|
// The key belongs in it because a source that needs one, such as Last.fm,
|
||||||
if on {
|
// answers "not found" without it, and the enricher settles a row tried
|
||||||
ids = append(ids, id)
|
// through it as found-nothing at the current version. Switching Last.fm on
|
||||||
|
// before entering its key would otherwise settle every gap without it, and
|
||||||
|
// the key, once saved, would never bring Last.fm back to them.
|
||||||
|
func (s *Service) sourcesSignature() string {
|
||||||
|
ids := make([]string, 0, len(s.enabledIDs))
|
||||||
|
for id, on := range s.enabledIDs {
|
||||||
|
if !on {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
if s.apiKeys[id] != "" {
|
||||||
|
id += "+key"
|
||||||
|
}
|
||||||
|
ids = append(ids, id)
|
||||||
}
|
}
|
||||||
sort.Strings(ids)
|
sort.Strings(ids)
|
||||||
return strings.Join(ids, ",")
|
return strings.Join(ids, ",")
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeProvider struct {
|
type fakeProvider struct {
|
||||||
@@ -126,12 +128,12 @@ func TestUpdateProvider_BumpsTheVersionOnlyWhenTheEnabledSetChanges(t *testing.T
|
|||||||
if !p.configured.Enabled || s.CurrentVersion() != store.version {
|
if !p.configured.Enabled || s.CurrentVersion() != store.version {
|
||||||
t.Fatalf("after enable: configured=%v version=%d store=%d", p.configured.Enabled, s.CurrentVersion(), store.version)
|
t.Fatalf("after enable: configured=%v version=%d store=%d", p.configured.Enabled, s.CurrentVersion(), store.version)
|
||||||
}
|
}
|
||||||
// Clearing the key: a key-only change. (An empty key is stored as
|
// Clearing a key that was never set changes nothing a search uses. (An
|
||||||
// NULL without sealing, so the test needs no sealing key.)
|
// empty key is stored as NULL without sealing.)
|
||||||
key := ""
|
key := ""
|
||||||
bumped, err = s.UpdateProvider(ctx, "p", UpdatePatch{APIKey: &key})
|
bumped, err = s.UpdateProvider(ctx, "p", UpdatePatch{APIKey: &key})
|
||||||
if err != nil || bumped {
|
if err != nil || bumped {
|
||||||
t.Fatalf("key only: bumped=%v err=%v, want false nil", bumped, err)
|
t.Fatalf("clearing an unset key: bumped=%v err=%v, want false nil", bumped, err)
|
||||||
}
|
}
|
||||||
if _, err := s.UpdateProvider(ctx, "nope", UpdatePatch{}); err != ErrProviderNotFound {
|
if _, err := s.UpdateProvider(ctx, "nope", UpdatePatch{}); err != ErrProviderNotFound {
|
||||||
t.Fatalf("unknown provider: err=%v, want ErrProviderNotFound", err)
|
t.Fatalf("unknown provider: err=%v, want ErrProviderNotFound", err)
|
||||||
@@ -159,3 +161,31 @@ func TestBumpVersionIfProvidersChanged_WritesOnlyOnAChange(t *testing.T) {
|
|||||||
t.Fatalf("same set: bumped=%v version %d→%d, want no write", bumped, before, store.version)
|
t.Fatalf("same set: bumped=%v version %d→%d, want no write", bumped, before, store.version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A source switched on before its key was entered searched without it, so
|
||||||
|
// the key arriving moves the version and the gaps it left are tried again.
|
||||||
|
// Swapping one key for another doesn't.
|
||||||
|
func TestUpdateProvider_AKeyArrivingOnAnEnabledSourceBumpsTheVersion(t *testing.T) {
|
||||||
|
secret.Install([]byte("providers tests: not a deployment key"))
|
||||||
|
p := &fakeProvider{id: "p"}
|
||||||
|
s, _ := newTestService(t, p)
|
||||||
|
ctx := context.Background()
|
||||||
|
on := true
|
||||||
|
if _, err := s.UpdateProvider(ctx, "p", UpdatePatch{Enabled: &on}); err != nil {
|
||||||
|
t.Fatalf("enable: %v", err)
|
||||||
|
}
|
||||||
|
for _, step := range []struct {
|
||||||
|
key string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"first-key", true},
|
||||||
|
{"second-key", false},
|
||||||
|
{"", true},
|
||||||
|
} {
|
||||||
|
key := step.key
|
||||||
|
bumped, err := s.UpdateProvider(ctx, "p", UpdatePatch{APIKey: &key})
|
||||||
|
if err != nil || bumped != step.want {
|
||||||
|
t.Fatalf("key %q: bumped=%v err=%v, want %v", step.key, bumped, err, step.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user