-
Notifications
You must be signed in to change notification settings - Fork 3
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
9a4d135
commit 504817c
Showing
1 changed file
with
336 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,336 @@ | ||
package sweeper | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mfreeman451/serviceradar/pkg/models" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
var errTest = errors.New("test error") | ||
|
||
func TestMockSweeper(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockSweeper := NewMockSweeper(ctrl) | ||
ctx := context.Background() | ||
|
||
t.Run("Start and Stop", func(t *testing.T) { | ||
// Test Start | ||
mockSweeper.EXPECT(). | ||
Start(gomock.Any()). | ||
Return(nil) | ||
|
||
err := mockSweeper.Start(ctx) | ||
assert.NoError(t, err) | ||
|
||
// Test Stop | ||
mockSweeper.EXPECT(). | ||
Stop(). | ||
Return(nil) | ||
|
||
err = mockSweeper.Stop() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("GetConfig", func(t *testing.T) { | ||
expectedConfig := models.Config{ | ||
Networks: []string{"192.168.1.0/24"}, | ||
Ports: []int{80, 443}, | ||
SweepModes: []models.SweepMode{models.ModeTCP}, | ||
Interval: time.Second * 30, | ||
} | ||
|
||
mockSweeper.EXPECT(). | ||
GetConfig(). | ||
Return(expectedConfig) | ||
|
||
config := mockSweeper.GetConfig() | ||
assert.Equal(t, expectedConfig, config) | ||
}) | ||
|
||
t.Run("GetResults", func(t *testing.T) { | ||
filter := &models.ResultFilter{ | ||
Host: "192.168.1.1", | ||
Port: 80, | ||
} | ||
|
||
expectedResults := []models.Result{ | ||
{ | ||
Target: models.Target{ | ||
Host: "192.168.1.1", | ||
Port: 80, | ||
}, | ||
Available: true, | ||
}, | ||
} | ||
|
||
mockSweeper.EXPECT(). | ||
GetResults(gomock.Any(), filter). | ||
Return(expectedResults, nil) | ||
|
||
results, err := mockSweeper.GetResults(ctx, filter) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedResults, results) | ||
}) | ||
|
||
t.Run("UpdateConfig", func(t *testing.T) { | ||
newConfig := models.Config{ | ||
Networks: []string{"10.0.0.0/24"}, | ||
Ports: []int{8080}, | ||
} | ||
|
||
mockSweeper.EXPECT(). | ||
UpdateConfig(newConfig). | ||
Return(nil) | ||
|
||
err := mockSweeper.UpdateConfig(newConfig) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestMockResultProcessor(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockProcessor := NewMockResultProcessor(ctrl) | ||
ctx := context.Background() | ||
|
||
t.Run("Process Result", func(t *testing.T) { | ||
result := &models.Result{ | ||
Target: models.Target{ | ||
Host: "192.168.1.1", | ||
Port: 80, | ||
}, | ||
Available: true, | ||
} | ||
|
||
mockProcessor.EXPECT(). | ||
Process(result). | ||
Return(nil) | ||
|
||
err := mockProcessor.Process(result) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("Get Summary", func(t *testing.T) { | ||
expectedSummary := &models.SweepSummary{ | ||
TotalHosts: 10, | ||
AvailableHosts: 5, | ||
LastSweep: time.Now().Unix(), | ||
} | ||
|
||
mockProcessor.EXPECT(). | ||
GetSummary(gomock.Any()). | ||
Return(expectedSummary, nil) | ||
|
||
summary, err := mockProcessor.GetSummary(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedSummary, summary) | ||
}) | ||
|
||
t.Run("Reset", func(t *testing.T) { | ||
mockProcessor.EXPECT(). | ||
Reset() | ||
|
||
mockProcessor.Reset() | ||
}) | ||
} | ||
|
||
func TestMockStore(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockStore := NewMockStore(ctrl) | ||
ctx := context.Background() | ||
|
||
t.Run("SaveResult", func(t *testing.T) { | ||
result := &models.Result{ | ||
Target: models.Target{ | ||
Host: "192.168.1.1", | ||
Port: 80, | ||
}, | ||
Available: true, | ||
} | ||
|
||
mockStore.EXPECT(). | ||
SaveResult(gomock.Any(), result). | ||
Return(nil) | ||
|
||
err := mockStore.SaveResult(ctx, result) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("GetResults", func(t *testing.T) { | ||
filter := &models.ResultFilter{ | ||
Host: "192.168.1.1", | ||
Port: 80, | ||
StartTime: time.Now().Add(-time.Hour), | ||
EndTime: time.Now(), | ||
} | ||
|
||
expectedResults := []models.Result{ | ||
{ | ||
Target: models.Target{ | ||
Host: "192.168.1.1", | ||
Port: 80, | ||
}, | ||
Available: true, | ||
}, | ||
} | ||
|
||
mockStore.EXPECT(). | ||
GetResults(gomock.Any(), filter). | ||
Return(expectedResults, nil) | ||
|
||
results, err := mockStore.GetResults(ctx, filter) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedResults, results) | ||
}) | ||
|
||
t.Run("PruneResults", func(t *testing.T) { | ||
retention := 24 * time.Hour | ||
|
||
mockStore.EXPECT(). | ||
PruneResults(gomock.Any(), retention). | ||
Return(nil) | ||
|
||
err := mockStore.PruneResults(ctx, retention) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("GetSweepSummary", func(t *testing.T) { | ||
expectedSummary := &models.SweepSummary{ | ||
TotalHosts: 100, | ||
AvailableHosts: 75, | ||
LastSweep: time.Now().Unix(), | ||
} | ||
|
||
mockStore.EXPECT(). | ||
GetSweepSummary(gomock.Any()). | ||
Return(expectedSummary, nil) | ||
|
||
summary, err := mockStore.GetSweepSummary(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedSummary, summary) | ||
}) | ||
} | ||
|
||
func TestMockReporter(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockReporter := NewMockReporter(ctrl) | ||
ctx := context.Background() | ||
|
||
t.Run("Report", func(t *testing.T) { | ||
summary := &models.SweepSummary{ | ||
TotalHosts: 50, | ||
AvailableHosts: 30, | ||
LastSweep: time.Now().Unix(), | ||
} | ||
|
||
mockReporter.EXPECT(). | ||
Report(gomock.Any(), summary). | ||
Return(nil) | ||
|
||
err := mockReporter.Report(ctx, summary) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestMockSweepService(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockService := NewMockSweepService(ctrl) | ||
ctx := context.Background() | ||
|
||
t.Run("Start and Stop", func(t *testing.T) { | ||
mockService.EXPECT(). | ||
Start(gomock.Any()). | ||
Return(nil) | ||
|
||
err := mockService.Start(ctx) | ||
assert.NoError(t, err) | ||
|
||
mockService.EXPECT(). | ||
Stop(). | ||
Return(nil) | ||
|
||
err = mockService.Stop() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("GetStatus", func(t *testing.T) { | ||
expectedStatus := &models.SweepSummary{ | ||
TotalHosts: 200, | ||
AvailableHosts: 150, | ||
LastSweep: time.Now().Unix(), | ||
} | ||
|
||
mockService.EXPECT(). | ||
GetStatus(gomock.Any()). | ||
Return(expectedStatus, nil) | ||
|
||
status, err := mockService.GetStatus(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedStatus, status) | ||
}) | ||
|
||
t.Run("UpdateConfig", func(t *testing.T) { | ||
config := models.Config{ | ||
Networks: []string{"172.16.0.0/16"}, | ||
Ports: []int{22, 80, 443}, | ||
SweepModes: []models.SweepMode{models.ModeTCP, models.ModeICMP}, | ||
} | ||
|
||
mockService.EXPECT(). | ||
UpdateConfig(config). | ||
Return(nil) | ||
|
||
err := mockService.UpdateConfig(config) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
// Helper function to verify gomock matchers | ||
func TestGomockMatchers(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
t.Run("Context Matcher", func(t *testing.T) { | ||
mockStore := NewMockStore(ctrl) | ||
ctx := context.Background() | ||
|
||
// Test that any context matches | ||
mockStore.EXPECT(). | ||
GetResults(gomock.Any(), gomock.Any()). | ||
Return(nil, nil) | ||
|
||
_, err := mockStore.GetResults(ctx, &models.ResultFilter{}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("Filter Matcher", func(t *testing.T) { | ||
mockStore := NewMockStore(ctrl) | ||
filter := &models.ResultFilter{ | ||
Host: "192.168.1.1", | ||
Port: 80, | ||
} | ||
|
||
// Test exact filter matching | ||
mockStore.EXPECT(). | ||
GetResults(gomock.Any(), filter). | ||
Return(nil, nil) | ||
|
||
_, err := mockStore.GetResults(context.Background(), filter) | ||
require.NoError(t, err) | ||
}) | ||
} |