Skip to content

Commit

Permalink
[#1163] Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fivitti committed Jan 25, 2024
1 parent 3ecb1ad commit f7fa3ec
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions backend/agent/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"testing"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
gomock "go.uber.org/mock/gomock"
Expand Down Expand Up @@ -211,6 +212,75 @@ func TestDetectApps(t *testing.T) {
require.EqualValues(t, 5678, am.apps[1].GetBaseApp().Pid)
}

// Test that the processes for which the command line cannot be read are
// not skipped.
func TestDetectAppsContinueOnNotAvailableCommandLine(t *testing.T) {
// Arrange
ctrl := gomock.NewController(t)
defer ctrl.Finish()

bind9Process := NewMockProcess(ctrl)
bind9Process.EXPECT().GetName().Return("named", nil)
bind9Process.EXPECT().GetCmdline().Return("named -c /etc/named.conf", nil)
bind9Process.EXPECT().GetCwd().Return("", errors.New("no current working directory"))
bind9Process.EXPECT().GetPid().Return(int32(5678))

processManager := NewMockProcessManager(ctrl)
processManager.EXPECT().ListProcesses().Return([]Process{
bind9Process,
}, nil)

executor := newTestCommandExecutorDefault()
am := &appMonitor{processManager: processManager, commander: executor}
hm := NewHookManager()
httpClient := NewHTTPClient()
sa := NewStorkAgent("foo", 42, am, httpClient, httpClient, hm)

// Act
am.detectApps(sa)

// Assert
require.Len(t, am.apps, 1)
require.Equal(t, AppTypeBind9, am.apps[0].GetBaseApp().Type)
}

// Test that the processes for which the current working directory cannot be
// read are skipped.
func TestDetectAppsSkipOnNotAvailableCwd(t *testing.T) {
// Arrange
ctrl := gomock.NewController(t)
defer ctrl.Finish()

noCwdProcess := NewMockProcess(ctrl)
noCwdProcess.EXPECT().GetName().Return("kea-ctrl-agent", nil)
noCwdProcess.EXPECT().GetCmdline().Return("kea-ctrl-agent -c /etc/kea/kea.conf", nil)
noCwdProcess.EXPECT().GetCwd().Return("", errors.New("no current working directory"))

bind9Process := NewMockProcess(ctrl)
bind9Process.EXPECT().GetName().Return("named", nil)
bind9Process.EXPECT().GetCmdline().Return("named -c /etc/named.conf", nil)
bind9Process.EXPECT().GetCwd().Return("/etc", nil)
bind9Process.EXPECT().GetPid().Return(int32(5678))

processManager := NewMockProcessManager(ctrl)
processManager.EXPECT().ListProcesses().Return([]Process{
noCwdProcess, bind9Process,
}, nil)

executor := newTestCommandExecutorDefault()
am := &appMonitor{processManager: processManager, commander: executor}
hm := NewHookManager()
httpClient := NewHTTPClient()
sa := NewStorkAgent("foo", 42, am, httpClient, httpClient, hm)

// Act
am.detectApps(sa)

// Assert
require.Len(t, am.apps, 1)
require.Equal(t, AppTypeBind9, am.apps[0].GetBaseApp().Type)
}

// Test that detectAllowedLogs does not panic when Kea server is unreachable.
func TestDetectAllowedLogsKeaUnreachable(t *testing.T) {
am := &appMonitor{}
Expand Down

0 comments on commit f7fa3ec

Please sign in to comment.