-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c733556
commit cb8464e
Showing
2 changed files
with
138 additions
and
0 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
129 changes: 129 additions & 0 deletions
129
internal/pkg/agent/application/monitoring/reload/reload_test.go
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,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 | ||
} |