Skip to content

fix: eliminate flaky unit test failures caused by sys.modules import … - #57

Open
ashnanze wants to merge 5 commits into
mainfrom
fix/flaky-unit-tests
Open

fix: eliminate flaky unit test failures caused by sys.modules import …#57
ashnanze wants to merge 5 commits into
mainfrom
fix/flaky-unit-tests

Conversation

@ashnanze

Copy link
Copy Markdown

Fix: Eliminate flaky unit test failures caused by sys.modules import order races

Problem

Unit tests in azext_connectedk8s/tests/unittests/ were intermittently failing depending on pytest's file discovery order. Each test file independently stubbed sys.modules at import time — whichever file was imported first "won" the race, corrupting stubs for others.

Root Cause

  • MagicMock() objects used as module stubs don't have __path__, so Python's import system can't traverse them as packages (e.g., azure.cli.core fails when azure is a plain MagicMock)
  • sys.modules.setdefault() could install stubs before real modules were importable, preventing legitimate imports from succeeding
  • No shared setup meant test files each maintained their own incompatible stub lists

Fix

File Change
conftest.py (new) Centralized session-scoped stubs: tries real __import__() first, only stubs on failure. Uses __path__=[] on mocks so they behave as packages.
test_correlation_id.py Removed sys.path.insert / import os, sys — conftest handles it
test_custom.py Removed sys.path.insert / import os, sys — conftest handles it
test_utils_.py Removed inline sys.path.insert; guards against leftover MagicMock stubs for _utils

Verification

  • All 40 tests pass consistently across multiple runs
  • Passes regardless of file discovery order

…order races

Root cause: each test file independently stubbed sys.modules at import time. When pytest discovered files in varying order, whichever ran first corrupted stubs for others.

Changes:

- Add conftest.py with centralized session-scoped stubs (try real import first, stub on failure)

- Remove inline sys.modules manipulation from test_correlation_id, test_custom, test_utils_

- Use __path__=[] on mock packages so Python's import system can traverse them

- All 63 tests now pass consistently regardless of discovery order
@github-actions

Copy link
Copy Markdown

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

@github-actions

Copy link
Copy Markdown

ashnanze added 4 commits July 14, 2026 19:38
Replace [System.Text.Json.JsonDocument]::Parse() with ConvertFrom-Json
and add null checks before parsing. When 'az connectedk8s show' returns
null (transient failure or cluster not ready), the retry loop now logs
and continues instead of throwing ArgumentNullException.
The resource group hit the 800 connectedClusters quota because failed CI
runs leave orphaned clusters. This adds:
- Cleanup.ps1: deletes clusters older than 2 hours
- Pipeline step: runs cleanup before each test suite
Prevents orphaned connectedClusters when tests fail mid-run. The step
uses condition: always() so it executes regardless of test outcome.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant