From 08a4c3ceb20d576fc5969d2032a99eeab5e9c1d4 Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 28 Sep 2022 12:03:45 +0200 Subject: [PATCH 1/2] fix status reporter initialization --- internal/pkg/core/status/reporter.go | 8 ++++---- internal/pkg/core/status/reporter_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/internal/pkg/core/status/reporter.go b/internal/pkg/core/status/reporter.go index 50f34651fa1..5f2ec99a97b 100644 --- a/internal/pkg/core/status/reporter.go +++ b/internal/pkg/core/status/reporter.go @@ -89,8 +89,8 @@ type controller struct { func NewController(log *logger.Logger) Controller { return &controller{ status: Healthy, - reporters: make(map[string]*reporter), - appReporters: make(map[string]*reporter), + reporters: map[string]*reporter{}, + appReporters: map[string]*reporter{}, log: log, } } @@ -154,12 +154,12 @@ func (r *controller) RegisterLocalComponent(componentIdentifier string) Reporter return rep } -// Register registers new component for status updates. +// RegisterComponent registers new component for status updates. func (r *controller) RegisterComponent(componentIdentifier string) Reporter { return r.RegisterComponentWithPersistance(componentIdentifier, false) } -// Register registers new component for status updates. +// RegisterComponentWithPersistance registers new component for status updates. func (r *controller) RegisterComponentWithPersistance(componentIdentifier string, persistent bool) Reporter { id := componentIdentifier + "-" + uuid.New().String()[:8] rep := &reporter{ diff --git a/internal/pkg/core/status/reporter_test.go b/internal/pkg/core/status/reporter_test.go index 09a66661fc5..dcee758c8a4 100644 --- a/internal/pkg/core/status/reporter_test.go +++ b/internal/pkg/core/status/reporter_test.go @@ -14,6 +14,23 @@ import ( "github.com/elastic/elastic-agent/pkg/core/logger" ) +func TestNewController_ensure_all_is_initialzed(t *testing.T) { + l, _ := logger.New("", false) + + newController := NewController(l) + + c, ok := newController.(*controller) + if !ok { + t.Fatalf("expected c %T, not c %T", controller{}, newController) + } + + c.localReporters["ignore"] = &reporter{} + c.appReporters["ignore"] = &reporter{} + if c.log == nil { + t.Error("logger shouldn't be nil, it was not correctly assigned") + } +} + func TestReporter(t *testing.T) { l, _ := logger.New("", false) t.Run("healthy by default", func(t *testing.T) { From 785b03127387a308c1127e1d582a2e45f2cba31a Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 28 Sep 2022 12:41:15 +0200 Subject: [PATCH 2/2] . --- internal/pkg/core/status/reporter.go | 9 +++++---- internal/pkg/core/status/reporter_test.go | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/pkg/core/status/reporter.go b/internal/pkg/core/status/reporter.go index 5f2ec99a97b..e123beab2a2 100644 --- a/internal/pkg/core/status/reporter.go +++ b/internal/pkg/core/status/reporter.go @@ -88,10 +88,11 @@ type controller struct { // NewController creates a new reporter. func NewController(log *logger.Logger) Controller { return &controller{ - status: Healthy, - reporters: map[string]*reporter{}, - appReporters: map[string]*reporter{}, - log: log, + status: Healthy, + reporters: make(map[string]*reporter), + localReporters: make(map[string]*reporter), + appReporters: make(map[string]*reporter), + log: log, } } diff --git a/internal/pkg/core/status/reporter_test.go b/internal/pkg/core/status/reporter_test.go index dcee758c8a4..c4f6796fb30 100644 --- a/internal/pkg/core/status/reporter_test.go +++ b/internal/pkg/core/status/reporter_test.go @@ -24,6 +24,7 @@ func TestNewController_ensure_all_is_initialzed(t *testing.T) { t.Fatalf("expected c %T, not c %T", controller{}, newController) } + c.reporters["ignore"] = &reporter{} c.localReporters["ignore"] = &reporter{} c.appReporters["ignore"] = &reporter{} if c.log == nil {