-
Notifications
You must be signed in to change notification settings - Fork 1
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
0f6b5dc
commit 7b27b76
Showing
2 changed files
with
236 additions
and
2 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,139 @@ | ||
package policy_test | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
"testing" | ||
|
||
"github.com/netboxlabs/diode-sdk-go/diode" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/netboxlabs/orb-discovery/network-discovery/config" | ||
"github.com/netboxlabs/orb-discovery/network-discovery/policy" | ||
) | ||
|
||
// MockRunner mocks the Runner | ||
type MockRunner struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockRunner) Configure(ctx context.Context, logger *slog.Logger, name string, policy config.Policy, client diode.Client) error { | ||
args := m.Called(ctx, logger, name, policy, client) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockRunner) Start() { | ||
m.Called() | ||
} | ||
|
||
func (m *MockRunner) Stop() error { | ||
args := m.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func TestManagerConfigure(t *testing.T) { | ||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: false})) | ||
mockClient := new(MockClient) | ||
|
||
manager := &policy.Manager{} | ||
err := manager.Configure(context.Background(), logger, mockClient) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, manager) | ||
} | ||
|
||
func TestManagerParsePolicies(t *testing.T) { | ||
manager := &policy.Manager{} | ||
|
||
t.Run("Valid Policies", func(t *testing.T) { | ||
yamlData := []byte(` | ||
network: | ||
policies: | ||
policy1: | ||
config: | ||
defaults: | ||
site: New York NY | ||
scope: | ||
targets: | ||
- 192.168.1.1/24 | ||
`) | ||
|
||
policies, err := manager.ParsePolicies(yamlData) | ||
assert.NoError(t, err) | ||
assert.Contains(t, policies, "policy1") | ||
assert.Equal(t, "New York NY", policies["policy1"].Config.Defaults["site"]) | ||
}) | ||
|
||
t.Run("No Policies", func(t *testing.T) { | ||
yamlData := []byte(`network: {}`) | ||
_, err := manager.ParsePolicies(yamlData) | ||
assert.Error(t, err) | ||
assert.Equal(t, "no policies found in the request", err.Error()) | ||
}) | ||
} | ||
|
||
func TestManagerPolicyLifecycle(t *testing.T) { | ||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: false})) | ||
manager := &policy.Manager{} | ||
err := manager.Configure(context.Background(), logger, nil) | ||
assert.NoError(t, err) | ||
yamlData := []byte(` | ||
network: | ||
policies: | ||
policy1: | ||
scope: | ||
targets: | ||
- 192.168.1.1/24 | ||
policy2: | ||
scope: | ||
targets: | ||
- 192.168.2.1/24 | ||
policy3: | ||
scope: | ||
targets: [] | ||
`) | ||
|
||
policies, err := manager.ParsePolicies(yamlData) | ||
assert.NoError(t, err) | ||
|
||
// Start policies | ||
err = manager.StartPolicy("policy1", policies["policy1"]) | ||
assert.NoError(t, err) | ||
err = manager.StartPolicy("policy2", policies["policy2"]) | ||
assert.NoError(t, err) | ||
|
||
// Try to start policy 3 | ||
err = manager.StartPolicy("policy3", policies["policy3"]) | ||
assert.Contains(t, err.Error(), "no targets found in the policy") | ||
|
||
// Check if the policies exist | ||
assert.True(t, manager.HasPolicy("policy1")) | ||
assert.True(t, manager.HasPolicy("policy2")) | ||
assert.False(t, manager.HasPolicy("policy3")) | ||
|
||
// Stop policy 1 | ||
err = manager.StopPolicy("policy1") | ||
assert.NoError(t, err) | ||
|
||
// Check if the policy exists | ||
assert.False(t, manager.HasPolicy("policy1")) | ||
assert.True(t, manager.HasPolicy("policy2")) | ||
assert.False(t, manager.HasPolicy("policy3")) | ||
|
||
// Stop Manager | ||
err = manager.Stop() | ||
assert.NoError(t, err) | ||
|
||
// Check if the policies exist | ||
assert.False(t, manager.HasPolicy("policy1")) | ||
assert.False(t, manager.HasPolicy("policy2")) | ||
assert.False(t, manager.HasPolicy("policy3")) | ||
} | ||
|
||
func TestManagerGetCapabilities(t *testing.T) { | ||
manager := &policy.Manager{} | ||
|
||
capabilities := manager.GetCapabilities() | ||
assert.Equal(t, []string{"targets, ports"}, capabilities) | ||
} |
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