Skip to content

[DevTest Labs] az lab vm create: Fix --expiration-date always failing with a datetime comparison error - #34066

Open
om singhal (Om-singhaI) wants to merge 1 commit into
Azure:devfrom
Om-singhaI:fix/lab-expiration-date-tz-compare
Open

[DevTest Labs] az lab vm create: Fix --expiration-date always failing with a datetime comparison error#34066
om singhal (Om-singhaI) wants to merge 1 commit into
Azure:devfrom
Om-singhaI:fix/lab-expiration-date-tz-compare

Conversation

@Om-singhaI

Copy link
Copy Markdown

Related command

az lab vm create --expiration-date

Description

--expiration-date is unusable on az lab vm create. Every date fails, which is what #31611 reports.

By the time _validate_expiration_date runs, AAZDateTimeArg has already normalized the argument into a UTC timestamp, since AAZCommand._handler calls format_args() before pre_operations. So the validator always parses an offset aware datetime. It compared that against datetime.datetime.utcnow(), which is naive, and Python won't compare the two.

The validator does no I/O, so this reproduces on dev with no Azure credentials. The snippet sets the argument up through the same AAZCommandCtx.format_args() call the command goes through:

from azure.cli.core.aaz import AAZArgumentsSchema, AAZDateTimeArg
from azure.cli.core.aaz._command_ctx import AAZCommandCtx
from azure.cli.core.mock import DummyCli
from azure.cli.command_modules.lab.validators import _validate_expiration_date

schema = AAZArgumentsSchema()
schema.expiration_date = AAZDateTimeArg(options=['--expiration-date'])
ctx = AAZCommandCtx(cli_ctx=DummyCli(), schema=schema,
                    command_args={'expiration_date': '2030-06-17T22:00:00.000Z'})
ctx.format_args()
_validate_expiration_date(ctx.args)
TypeError: can't compare offset-naive and offset-aware datetimes

Swap in 2030-06-17 22:00:00, 2030-06-17 22:00:00+00:00 or 2030-06-17 22:00:00.000+00:00 and the traceback is identical. Those are the four date strings from the issue, with the year bumped so the date is still in the future.

I switched the comparison to datetime.datetime.now(datetime.timezone.utc). That drops a utcnow() call too, which has been deprecated since Python 3.12.

Testing Guide

ExpirationDateValidatorTest in test_lab_validators.py sets the argument up the same way, then checks that a future date passes, a past date still raises ArgumentUsageError, and an omitted date is left alone. Revert validators.py and both date tests fail with the TypeError above.

The recorded scenario tests never passed --expiration-date, which is why none of them caught this. They still pass:

python -m pytest src/azure-cli/azure/cli/command_modules/lab/tests/latest/ -q
8 passed, 1 skipped

…ling with a datetime comparison error

AAZDateTimeArg normalizes --expiration-date into an offset aware UTC
timestamp before pre_operations runs, so _validate_expiration_date
always parsed an offset aware datetime. It compared that against the
naive datetime.datetime.utcnow(), which raises TypeError for every
value, making the argument unusable.

Compare against datetime.datetime.now(datetime.timezone.utc) instead.
That also drops a utcnow() call deprecated since Python 3.12.

Fixes Azure#31611
Copilot AI lite review requested due to automatic review settings September 11, 2026 07:44
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the customer-reported Issues that are reported by GitHub users external to the Azure organization. label Sep 11, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

Thank you for your contribution om singhal (@Om-singhaI)! We will review the pull request and get back to you soon.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The fix and regression tests address the reported failure with no unresolved blocking issues.

Pull request overview

Fixes az lab vm create --expiration-date datetime comparison failures.

Changes:

  • Uses timezone-aware UTC validation.
  • Adds future, past, and omitted expiration-date tests.
File summaries
File Description
src/azure-cli/azure/cli/command_modules/lab/validators.py Corrects expiration-date comparison.
src/azure-cli/azure/cli/command_modules/lab/tests/latest/test_lab_validators.py Adds expiration-date validator coverage.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@yonzhan

Copy link
Copy Markdown
Collaborator

DevTest Labs

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

Labels

customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants