Skip to content

{Tests} Add CLI command request and response tests run through the engine - #1501

Draft
MaddyMicrosoft wants to merge 11 commits into
mainfrom
test/cli-command-happy-path-tests
Draft

MaddyMicrosoft wants to merge 11 commits into
mainfrom
test/cli-command-happy-path-tests

Conversation

@MaddyMicrosoft

Copy link
Copy Markdown
Member

Related command
az keyvault secret *, az keyvault certificate *, az group *, az role assignment *, az deployment *, az deployment operation *, az rest, az msgraph *, az login, az logout, az account get-access-token, az profile *, az identity *, az identity federated-credential *

Description

Stacked on the CLI command tests PR (test/cli-command-tests). Please review that one first; this PR only adds the request and response tests.

Adds tests that run CLI commands through the real engine and check what each command sends to Azure and what it does with the reply. The CLI command tests PR covers input that is rejected before anything is sent; this PR covers everything after that.

Like those tests, the real CLI parser, extension index, binder, processor and HTTP pipeline all run, and only the wire is mocked with a strict Moq mock. No recordings, credentials or files are needed. The 632 tests run in about 2 seconds.

There are two kinds of test, one file of each per command group:

  • Request tests (*CommandRequestTests) answer with any valid reply and check what the command sent: method, URL, query string, headers and JSON body.
  • Response tests (*CommandResponseTests) send a specific reply (data, empty or multiple pages, in-progress operations, 403, 404, invalid JSON) and check what the command writes or the error it throws.

For Accounts, the profile, auth and subscription services stand in for the wire: request tests check which service calls are made, and response tests check what the command writes from what they return.

List<string?> sentBodies = _mockWire.RespondTo(HttpMethod.Put, SecretUrl, HttpStatusCode.OK, SecretBundle);
CommandInvocation invocation = CreateInvocation(
    ExtensionName,
    ["keyvault", "secret", "set", "--vault-name", VaultName, "--name", SecretName, "--value", SecretValue, "--tags", "team=a=b"]);
OutputStreams streams = CreateOutput(_output);
using ISession session = _engine.OpenExecutionSession(invocation, streams);

await session.RunAsync();

Assert.Equal("""{"value":"s3cret","tags":{"team":"a=b"}}""", Assert.Single(sentBodies));

What is covered:

Extension Request Response Highlights
KeyVault 59 54 Secret and certificate bodies; list paging never follows a next-page link outside the selected vault, so the token isn't sent elsewhere; certificate policy accepted in snake case, REST API and PowerShell names; create polling, --no-wait, failed and cancelled operations; secret value never written; 403, 404 and invalid JSON
Accounts 32 23 Every login credential type and flag alias; cross-tenant subscription; saved profile; profile list, show, set and remove; logout; access token scopes, tenant, redaction and expiry
MsGraph 70 40 Every OData option; group name to id lookup; consistency-level header; paging; 403 and 404
Managed Identity 51 20 Create with tags and resource restrictions; --ids batches; generic update (--set, --add, --remove); federated credentials; paging
Resources: group, rest, role assignment 84 33 Group CRUD, exists, name aliases, delete polling; az rest with all 11 parameters; role assignment $filter, scope handling, conflict returning the existing assignment
Resources: deployments and operations 81 85 Create, validate, what-if, show, list, cancel, delete and export at group, subscription, management group and tenant scope; polling through Azure-AsyncOperation and Location; what-if result formats; tenant wait; error formatting

CommandTestHelpers gains wire helpers: RespondTo, RespondWithHeaders, RespondToAnyUrl, RespondToAnyRequest, RespondInOrder, RespondInSequence, SentRequests, RequestLines, FullRequestLines, QueryValue and HeaderValues, plus shared ArmUrl and EmptyList consts. CreateEngine sets the engine's polling interval to 0 so a long-running operation without a Retry-After header doesn't wait 15 seconds between polls.

Some tests pin current behaviour that looks unintended, marked with a // Current behaviour: comment. Examples:

  • After a 202, the subscription what-if and validate read the one-shot Location result twice, so the command fails if ARM answers the second read with 404. The other scopes read it once. This looks like a product bug and is left for a separate fix.
  • Identity update merges --tags into the existing tags, while group update replaces them.
  • Secret commands write dates as Unix seconds, while certificate commands write them as DateTimeOffset.
  • A deployment PUT that answers 200 Succeeded is still polled and read again.
  • Subscription and management group export write null without a template, while group and tenant export throw.

Known gaps, left for follow-up PRs: keyvault certificate import, a successful keyvault certificate download, inline --template-file and --parameters-file deployment bodies, and some long-running operation edge cases (failed status, several polls, 202 without Location).

Testing Guide

dotnet test tests/Cli.Tests/Cli.Tests.csproj --filter "FullyQualifiedName\~Cli.Tests.Commands"
dotnet test tests/Cli.Tests/Cli.Tests.csproj -c Release --filter "FullyQualifiedName\~Cli.Tests.Commands"

Results: 957/957 command tests (325 from the base PR plus 632 here) in Debug and Release, in about 2 s. Full Cli.Tests: 3302 passed, 14 skipped.


  • The PR title starts with [Component Name] for customer-facing changes or {Component Name} for non-customer-facing changes, and does not end with trailing punctuation or spaces.
  • Customer-facing changes include a useful release note in the PR title or the History Notes section. (N/A, test-only)
  • I added or updated tests for behavior changes.
  • I updated documentation when behavior, setup, support, or security guidance changed. (N/A, no behaviour change)

MaddyMicrosoft and others added 11 commits November 17, 2026 05:29
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…lidation tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ommand tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…gine

Run each extension's CLI commands through the real engine with only the wire mocked, and check the request each command sends (method, URL, query, headers, body) and how it handles the response (output, paging, polling, errors). Accounts checks the calls to the profile, auth and subscription services instead.

Covers KeyVault, MsGraph, Managed Identity, Resources (groups, deployments, deployment operations, rest, role assignments) and Accounts. Adds wire helpers to CommandTestHelpers (RespondTo, RespondWithHeaders, RespondToAnyUrl, RespondToAnyRequest, RespondInOrder, SentRequests, FullRequestLines, QueryValue, HeaderValues) and shared ArmUrl and EmptyList consts.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ests

Fix tests that could not fail or could pass for the wrong reason (Accounts cross-tenant login, KeyVault policy normalization and base64url thumbprint, Managed Identity tag merge, rest repeated headers, issued-token tenant), correct misleading Current behaviour comments, assert error codes and full paging request lines, move Accounts per-command setups out of the constructor, remove duplicate and read-only tests, and tidy naming and repeated values.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… polls

Set the engine's polling interval to 0 in CreateEngine so a poll without a Retry-After header does not wait the default 15 seconds.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…t Location re-read

Key Vault list commands must not follow a nextLink outside the selected vault's secrets and certificates endpoints, so the token is never sent elsewhere. The subscription what-if and validate read the one-shot Location result twice after a 202, which fails the command when ARM answers the second read with 404; pinned as current behaviour. Adds RespondInSequence to the wire helpers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Resolve CommandTestHelpers.cs by keeping this branch's explicit-type locals and response helpers; main's side differed only in using var.

This branch has not been deployed

No deployments
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