Skip to content

Commit

Permalink
more ut
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpristas committed Oct 6, 2023
1 parent c733556 commit cb8464e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/pkg/agent/application/monitoring/reload/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func (sr *ServerReloader) Start() {
}

func (sr *ServerReloader) start() {
if sr.s != nil && sr.isServerRunning {
// server is already running
return
}

sr.log.Info("Starting server")
var err error
sr.s, err = sr.newServerFn()
Expand All @@ -70,6 +75,10 @@ func (sr *ServerReloader) Stop() error {
}

func (sr *ServerReloader) stop() error {
if sr.s == nil {
// stopping not started server
return nil
}
sr.log.Info("Stopping server")

sr.isServerRunning = false
Expand Down
129 changes: 129 additions & 0 deletions internal/pkg/agent/application/monitoring/reload/reload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package reload

import (
"testing"

aConfig "github.com/elastic/elastic-agent/internal/pkg/config"
monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
"github.com/elastic/elastic-agent/pkg/core/logger"
"github.com/stretchr/testify/require"
)

func TestReload(t *testing.T) {
tcs := []struct {
name string
currEnabled bool
currMetrics bool
currRunning bool

newConfig string
expectedRunning bool
expectedStart bool
expectedStop bool
}{
{
"start with default config",
false, false, false,
``,
true, true, false,
},
{
"start when not running, monitoring enabled",
false, false, false,
`
agent.monitoring.enabled: true
`,
true, true, false,
},
{
"do not start when not running, only metrics enabled",
false, false, false,
`
agent.monitoring.enabled: false
agent.monitoring.metrics: true
`,
false, false, false,
},

{
"stop when running, monitoring disabled",
true, true, true,
`
agent.monitoring.enabled: false
`,
false, false, true,
},
{
"stop when running, monitoring.metrics disabled",
true, true, true,
`
agent.monitoring.metrics: false
`,
false, false, true,
},
{
"stop stopped server",
false, false, false,
`
agent.monitoring.metrics: false
`,
false, false, false,
},
{
"start started server",
true, true, true,
`
agent.monitoring.enabled: true
`,
true, false, false,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
fsc := &fakeServerController{}
log, _ := logger.NewTesting(tc.name)
cfg := &monitoringCfg.MonitoringConfig{
Enabled: tc.currEnabled,
MonitorMetrics: tc.currMetrics,
}
r := NewServerReloader(
func() (ServerController, error) {
return fsc, nil
},
log,
cfg,
)
r.isServerRunning = tc.currRunning
if tc.currRunning {
r.s = fsc
}

newCfg := aConfig.MustNewConfigFrom(tc.newConfig)
require.NoError(t, r.Reload(newCfg))

require.Equal(t, tc.expectedRunning, r.isServerRunning)
require.Equal(t, tc.expectedStart, fsc.startTriggered)
require.Equal(t, tc.expectedStop, fsc.stopTriggered)
})
}
}

type fakeServerController struct {
startTriggered bool
stopTriggered bool
}

func (fsc *fakeServerController) Start() { fsc.startTriggered = true }
func (fsc *fakeServerController) Stop() error {
fsc.stopTriggered = true
return nil
}
func (fsc *fakeServerController) Reset() {
fsc.startTriggered = false
fsc.stopTriggered = false
}

0 comments on commit cb8464e

Please sign in to comment.