From 4a9f4c31535914e658558d713b83d8dd7cdec7aa Mon Sep 17 00:00:00 2001 From: Ruslan Timofieiev Date: Sat, 14 Sep 2024 21:40:45 +0300 Subject: [PATCH] Fix Mark-Range command: ensure that NS Favorite doesn't exceed the limit (#2881) * ensure that number of favorite namespace is never exceeds hard limit * remove newlines from files --- internal/config/data/ns.go | 9 ++++++++- internal/config/data/ns_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/internal/config/data/ns.go b/internal/config/data/ns.go index 81943035..fb1a515b 100644 --- a/internal/config/data/ns.go +++ b/internal/config/data/ns.go @@ -12,7 +12,7 @@ import ( const ( // MaxFavoritesNS number # favorite namespaces to keep in the configuration. - MaxFavoritesNS = 9 + MaxFavoritesNS = 10 ) // Namespace tracks active and favorites namespaces. @@ -68,6 +68,13 @@ func (n *Namespace) Validate(c client.Connection) { n.rmFavNS(ns) } } + + if len(n.Favorites) > MaxFavoritesNS { + log.Debug().Msgf("[Namespace] Number of favorite exceeds hard limit of %v. Trimming.", MaxFavoritesNS) + for _, ns := range n.Favorites[MaxFavoritesNS:] { + n.rmFavNS(ns) + } + } } // SetActive set the active namespace. diff --git a/internal/config/data/ns_test.go b/internal/config/data/ns_test.go index d66b6903..a6e94aa3 100644 --- a/internal/config/data/ns_test.go +++ b/internal/config/data/ns_test.go @@ -35,6 +35,17 @@ func TestNSValidateNoNS(t *testing.T) { assert.Equal(t, []string{"default"}, ns.Favorites) } +func TestNsValidateMaxNS(t *testing.T) { + allNS := []string{"ns9","ns8","ns7","ns6","ns5","ns4", "ns3", "ns2", "ns1", "all", "default"} + ns := data.NewNamespace() + + ns.Favorites = allNS + + ns.Validate(mock.NewMockConnection()) + + assert.Equal(t, data.MaxFavoritesNS, len(ns.Favorites)) +} + func TestNSSetActive(t *testing.T) { allNS := []string{"ns4", "ns3", "ns2", "ns1", "all", "default"} uu := []struct {