Skip to content

[v3.30] Migrate k8st tests from nose to pytest for Python 3.12 compatibility#11772

Open
nelljerram wants to merge 3 commits intoprojectcalico:release-v3.30from
nelljerram:auto-pick-of-#11767-origin-release-v3.30
Open

[v3.30] Migrate k8st tests from nose to pytest for Python 3.12 compatibility#11772
nelljerram wants to merge 3 commits intoprojectcalico:release-v3.30from
nelljerram:auto-pick-of-#11767-origin-release-v3.30

Conversation

@nelljerram
Copy link
Member

Cherry-pick history

Python 2 to Python 3 Conversion & Test Runner Migration - Complete ✅

This PR comprehensively modernizes the node/tests/k8st test infrastructure by:

  1. Converting Python 2 code to Python 3
  2. Updating Docker and dependencies
  3. Migrating from nose to pytest test runner
  4. Code cleanup: Removed unused variables

Part 1: Python Test Code Conversion

Files Modified

  • node/tests/k8st/test_base.py - Fixed cmp() and integer division
  • node/tests/k8st/tests/test_local_bgp_peer.py - Fixed 18 regex escapes
  • node/tests/k8st/tests/test_bgp_filter.py - Fixed 8 regex escapes, removed unused variables

Part 2: Test Container Update

Files Modified

  • node/calico_test/Dockerfile - Docker 18.09→25, Python 2→3
  • node/calico_test/requirements.txt - Removed Python 2 backports

Part 3: nose → pytest Migration

Problem Solved

ModuleNotFoundError: No module named 'imp'

The nose test runner is incompatible with Python 3.12 because it imports the imp module which was removed.

Changes Made

Test Assertion Updates

  • Replaced assertRegexpMatchesassertRegex (22 occurrences)
  • Replaced assertNotRegexpMatchesassertNotRegex (2 occurrences)

Dependency Changes

Removed:

  • nose==1.3.7
  • nose-parameterized==0.6.0
  • nose-timer==1.0.1

Kept:

  • pytest==9.0.2

Test Runner Changes (Makefile)

- nosetests $(K8ST_TO_RUN) -v --with-xunit --xunit-file="..." --with-timer
+ pytest $(K8ST_TO_RUN) -v --junit-xml="..."

Part 4: Code Cleanup

Changes Made

  • Removed unused cluster_route_regex_v4 and cluster_route_regex_v6 variables from test functions

Complete PR Summary

Total Impact

  • 8 files modified across all parts
  • Python 2 → Python 3 conversion complete
  • Docker 18.09 → 25 upgrade
  • nose → pytest migration complete
  • Python 3.12+ compatible
  • Code cleanup completed

Verification

  • All Python files compile with Python 3
  • Code review: Issues addressed
  • CodeQL security scan: 0 vulnerabilities
  • pytest can discover and run unittest.TestCase-based tests

Benefits

Python 3.12 compatible
Modern, maintained dependencies
Industry-standard test runner (pytest)
Minimal, surgical changes
No test functionality lost
Clean code without unused variables

Original prompt

Please convert the Python 2 code under node/tests/k8st to Python 3.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…vert-python2-to-python3

Migrate k8st tests from nose to pytest for Python 3.12 compatibility

(cherry picked from commit f89d869)
@nelljerram nelljerram requested a review from a team as a code owner February 5, 2026 16:42
@nelljerram nelljerram added docs-not-required Docs not required for this change release-note-not-required Change has no user-facing impact labels Feb 5, 2026
Copilot AI review requested due to automatic review settings February 5, 2026 16:42
@nelljerram nelljerram added docs-not-required Docs not required for this change release-note-not-required Change has no user-facing impact labels Feb 5, 2026
@marvin-tigera marvin-tigera added this to the Calico v3.30.6 milestone Feb 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR modernizes the k8st test infrastructure for Python 3.12+ compatibility by migrating from the deprecated nose test runner to pytest and updating all associated dependencies and code.

Changes:

  • Converted Python 2 code to Python 3 (cmp(), integer division, subprocess output decoding)
  • Updated Docker base image from 18.09 to 25 and Python 2 to Python 3
  • Migrated from nose to pytest test runner with updated assertion methods
  • Updated Python dependencies to Python 3-compatible versions
  • Cleaned up unused variables and whitespace

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
node/tests/k8st/utils/utils.py Added decode() calls to handle subprocess output as strings in Python 3
node/tests/k8st/tests/test_local_bgp_peer.py Replaced deprecated assertRegexpMatches/assertNotRegexpMatches with assertRegex/assertNotRegex; fixed raw string literals for regex patterns; refactored test method to support inheritance pattern
node/tests/k8st/tests/test_bgp_filter.py Fixed raw string literals for regex patterns; removed unused cluster_route_regex variables; cleaned up trailing whitespace
node/tests/k8st/test_base.py Updated import path for pytest compatibility; replaced cmp() with == operator; fixed integer division for Python 3
node/calico_test/requirements.txt Updated all dependencies to Python 3-compatible versions; removed Python 2 backports; removed nose dependencies and kept pytest
node/calico_test/Dockerfile Updated base image to docker:25; migrated from Python 2 to Python 3 packages; updated comments to reference pytest
node/Makefile Replaced nosetests command with pytest command and updated arguments

Comment on lines 65 to 66
RUN curl -sfL https://github.com/coreos/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-${TARGETARCH}.tar.gz | \
tar xz --strip-components 1 -C /usr/local/bin etcd-${ETCD_VERSION}-linux-${TARGETARCH}/etcdctl
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RUN curl ... | tar pipeline downloads and extracts a remote etcdctl binary from GitHub without any checksum or signature verification, creating a supply-chain risk if the release asset or network traffic is compromised. An attacker who can tamper with that tarball could supply a malicious binary that will be installed into /usr/local/bin and executed with the container’s privileges. To mitigate this, fetch and verify a published checksum or signature for the archive (or use a package-managed etcdctl), and fail the build if verification does not match.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but:

  1. This is an old-ish release branch.
  2. This is only relevant to our own CI, running in a Semaphore VM.
  3. CI does not have write access to our GitHub repos.

caseydavenport and others added 2 commits February 6, 2026 00:13
…1349)

* Fix nested retry loop in service advertisement tests

* Remove DS env updates

* Fix extra spaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-not-required Docs not required for this change release-note-not-required Change has no user-facing impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants