Skip to content

test : add unit tests for get_policy_engine singleton in network_policy #2445

Description

@tmdeveloper007

Summary of What Needs to be Done:
Add unit tests for the get_policy_engine singleton factory function in backend/secuscan/network_policy.py. This function creates a global NetworkPolicyEngine instance on first call and returns the cached instance on subsequent calls.

The function requires patching the backend.secuscan.config module (which imports Pydantic) so the test can inject mock settings without requiring the full Pydantic dependency tree. The pattern is to use patch.dict(sys.modules, {...}) to replace the config module before importing get_policy_engine.

Changes that Need to be Made:
Create testing/backend/unit/test_network_policy_engine_singleton.py:

  • Patch sys.modules['backend.secuscan.config'] with a mock config module before importing get_policy_engine and NetworkPolicyEngine
  • Test that first call creates a new NetworkPolicyEngine instance
  • Test that second call returns the same instance (singleton behavior)
  • Test that the engine has expected methods (check_access, add_allow_rule, add_deny_rule)
  • Test that calling after engine.clear_audit_entries() returns the SAME cached instance (singleton is preserved)
  • Reset the global _policy_engine = None before each test to ensure isolation
  • Import the real production functions directly (not mocked copies)
  • Run with python3 -m pytest testing/backend/unit/test_network_policy_engine_singleton.py -v --noconftest

Impact that it would Provide:

  • Tests singleton initialization logic that is currently untested at the unit level
  • Catches regressions if the singleton caching logic is accidentally changed
  • Demonstrates the sys.modules patching pattern for testing modules that import Pydantic-dependent config

Note: This task is being handled by tmdeveloper007 — please assign to that account when picking it up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions