-
Notifications
You must be signed in to change notification settings - Fork 286
Detect active deployments before provisioning #7251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spboyer
wants to merge
22
commits into
main
Choose a base branch
from
fix/deployment-active-conflict
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
41b60d5
Detect active deployments before provisioning (#7248)
spboyer 0a466de
Retrigger CI
spboyer 2e8c4d7
Address review: fix range, scope-agnostic wording, extract filter hel…
spboyer 82d4a35
Restore range-over-int (Go 1.26, enforced by go fix)
spboyer c233cef
Retrigger CI (ADO pool flake)
spboyer 9ad06e0
Retrigger CI
spboyer 8118c17
Retrigger CI
spboyer 04dfd68
Fix: remove ListActiveDeployments from Scope interface
spboyer d5a72e8
Remove orphaned ListActiveDeployments from mockedScope
spboyer 85d5080
Fix: use scopeForTemplate instead of deployment for active check
spboyer eab4e32
Fix review: handle ErrDeploymentsNotFound in poll loop
spboyer 6364000
Address review feedback: spinner status, error handling, scope-agnost…
spboyer 6289d69
Fix timer leak and racy mock in active deployment check
spboyer 6a79186
Re-trigger CI (flaky Test_DeploymentStacks/Test_StorageBlobClient fai…
spboyer d7bd93f
Scope active deployment check to current deployment name
spboyer 244e9e7
Re-trigger CI (flaky Test_DeploymentStacks/Test_StorageBlobClient)
spboyer 3bf8d2e
Re-trigger CI (Test_DeploymentStacks/Test_StorageBlobClient unrelated…
spboyer c31d3dc
Re-trigger CI
spboyer b7043b3
Make active deployment check best-effort to avoid blocking recorded t…
spboyer ade82fe
Remove dead ListActiveDeployments code, add name-filter test
spboyer 4511c47
Re-trigger CI (Linux-only infra failure, no test name in annotation)
spboyer 00d85f9
Re-trigger CI (ADO Linux infra failure)
spboyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package azapi | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIsActiveDeploymentState(t *testing.T) { | ||
| active := []DeploymentProvisioningState{ | ||
| DeploymentProvisioningStateAccepted, | ||
| DeploymentProvisioningStateCanceling, | ||
| DeploymentProvisioningStateCreating, | ||
| DeploymentProvisioningStateDeleting, | ||
| DeploymentProvisioningStateDeletingResources, | ||
| DeploymentProvisioningStateDeploying, | ||
| DeploymentProvisioningStateRunning, | ||
| DeploymentProvisioningStateUpdating, | ||
| DeploymentProvisioningStateUpdatingDenyAssignments, | ||
| DeploymentProvisioningStateValidating, | ||
| DeploymentProvisioningStateWaiting, | ||
| } | ||
|
|
||
| for _, state := range active { | ||
| t.Run(string(state), func(t *testing.T) { | ||
| require.True(t, IsActiveDeploymentState(state), | ||
| "expected %s to be active", state) | ||
| }) | ||
| } | ||
|
|
||
| inactive := []DeploymentProvisioningState{ | ||
| DeploymentProvisioningStateSucceeded, | ||
| DeploymentProvisioningStateFailed, | ||
| DeploymentProvisioningStateCanceled, | ||
| DeploymentProvisioningStateDeleted, | ||
| DeploymentProvisioningStateNotSpecified, | ||
| DeploymentProvisioningStateReady, | ||
| } | ||
|
|
||
| for _, state := range inactive { | ||
| t.Run(string(state), func(t *testing.T) { | ||
| require.False(t, IsActiveDeploymentState(state), | ||
| "expected %s to be inactive", state) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
cli/azd/pkg/infra/provisioning/bicep/active_deployment_check_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package bicep | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/pkg/azapi" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/infra" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks/mockinput" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // activeDeploymentScope is a test helper that implements infra.Scope and lets | ||
| // the caller control what ListDeployments returns on each call. | ||
| type activeDeploymentScope struct { | ||
| // calls tracks how many times ListDeployments has been invoked. | ||
| calls atomic.Int32 | ||
| // activePerCall maps a 0-based call index to the list of deployments | ||
| // returned for that call. If the index is missing, nil is returned. | ||
| activePerCall map[int][]*azapi.ResourceDeployment | ||
| // errOnCall, if non-nil, maps a call index to an error to return. | ||
| errOnCall map[int]error | ||
| } | ||
|
|
||
| func (s *activeDeploymentScope) SubscriptionId() string { return "test-sub" } | ||
|
|
||
| func (s *activeDeploymentScope) Deployment(_ string) infra.Deployment { return nil } | ||
|
|
||
| func (s *activeDeploymentScope) ListDeployments( | ||
| _ context.Context, | ||
| ) ([]*azapi.ResourceDeployment, error) { | ||
| idx := int(s.calls.Add(1)) - 1 | ||
| if s.errOnCall != nil { | ||
| if e, ok := s.errOnCall[idx]; ok { | ||
| return nil, e | ||
| } | ||
| } | ||
| if s.activePerCall != nil { | ||
| return s.activePerCall[idx], nil | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| // newTestProvider returns a BicepProvider with fast poll settings for tests. | ||
| func newTestProvider() *BicepProvider { | ||
| return &BicepProvider{ | ||
| console: mockinput.NewMockConsole(), | ||
| activeDeployPollInterval: 10 * time.Millisecond, | ||
| activeDeployTimeout: 2 * time.Second, | ||
| } | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_NoActive(t *testing.T) { | ||
| scope := &activeDeploymentScope{} | ||
| p := newTestProvider() | ||
|
|
||
| err := p.waitForActiveDeployments(t.Context(), scope, "test-deploy") | ||
| require.NoError(t, err) | ||
| require.Equal(t, int32(1), scope.calls.Load(), | ||
| "should call ListDeployments once") | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_InitialListError_NotFound(t *testing.T) { | ||
| scope := &activeDeploymentScope{ | ||
| errOnCall: map[int]error{ | ||
| 0: fmt.Errorf("listing: %w", infra.ErrDeploymentsNotFound), | ||
| }, | ||
| } | ||
| p := newTestProvider() | ||
|
|
||
| // ErrDeploymentsNotFound (resource group doesn't exist yet) is safe to ignore. | ||
| err := p.waitForActiveDeployments(t.Context(), scope, "test-deploy") | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_InitialListError_Other(t *testing.T) { | ||
| scope := &activeDeploymentScope{ | ||
| errOnCall: map[int]error{ | ||
| 0: fmt.Errorf("auth failure: access denied"), | ||
| }, | ||
| } | ||
| p := newTestProvider() | ||
|
|
||
| // Non-NotFound errors are logged and skipped — the check is best-effort. | ||
| err := p.waitForActiveDeployments(t.Context(), scope, "test-deploy") | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_ActiveThenClear(t *testing.T) { | ||
| running := []*azapi.ResourceDeployment{ | ||
| { | ||
| Name: "deploy-1", | ||
| ProvisioningState: azapi.DeploymentProvisioningStateRunning, | ||
| }, | ||
| } | ||
| scope := &activeDeploymentScope{ | ||
| activePerCall: map[int][]*azapi.ResourceDeployment{ | ||
| 0: running, // first call: active | ||
| // second call (index 1): missing key → returns nil (no active) | ||
| }, | ||
| } | ||
| p := newTestProvider() | ||
|
|
||
| err := p.waitForActiveDeployments(t.Context(), scope, "deploy-1") | ||
| require.NoError(t, err) | ||
| require.Equal(t, int32(2), scope.calls.Load(), | ||
| "should poll once, then see clear") | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_CancelledContext(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(t.Context()) | ||
|
|
||
| running := []*azapi.ResourceDeployment{ | ||
| { | ||
| Name: "deploy-forever", | ||
| ProvisioningState: azapi.DeploymentProvisioningStateRunning, | ||
| }, | ||
| } | ||
| scope := &activeDeploymentScope{ | ||
| // Always return active deployments. | ||
| // Seed multiple indices so a tick before ctx.Done doesn't hit a missing key. | ||
| activePerCall: map[int][]*azapi.ResourceDeployment{ | ||
| 0: running, | ||
| 1: running, | ||
| 2: running, | ||
| 3: running, | ||
| 4: running, | ||
| }, | ||
| } | ||
| p := newTestProvider() | ||
|
|
||
| // Cancel immediately so the wait loop exits on the first select. | ||
| cancel() | ||
|
|
||
| err := p.waitForActiveDeployments(ctx, scope, "deploy-forever") | ||
| require.ErrorIs(t, err, context.Canceled) | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_PollError(t *testing.T) { | ||
| running := []*azapi.ResourceDeployment{ | ||
| { | ||
| Name: "deploy-1", | ||
| ProvisioningState: azapi.DeploymentProvisioningStateRunning, | ||
| }, | ||
| } | ||
| scope := &activeDeploymentScope{ | ||
| activePerCall: map[int][]*azapi.ResourceDeployment{ | ||
| 0: running, | ||
| }, | ||
| errOnCall: map[int]error{ | ||
| 1: fmt.Errorf("transient ARM failure"), | ||
| }, | ||
| } | ||
| p := newTestProvider() | ||
|
|
||
| err := p.waitForActiveDeployments(t.Context(), scope, "deploy-1") | ||
| // Transient poll errors are logged and treated as cleared. | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_PollNotFound(t *testing.T) { | ||
| // If the resource group is deleted externally while polling, | ||
| // ListDeployments returns ErrDeploymentsNotFound. The wait should | ||
| // treat this as "no active deployments" and return nil. | ||
| running := []*azapi.ResourceDeployment{ | ||
| { | ||
| Name: "deploy-1", | ||
| ProvisioningState: azapi.DeploymentProvisioningStateRunning, | ||
| }, | ||
| } | ||
| scope := &activeDeploymentScope{ | ||
| activePerCall: map[int][]*azapi.ResourceDeployment{ | ||
| 0: running, | ||
| }, | ||
| errOnCall: map[int]error{ | ||
| 1: infra.ErrDeploymentsNotFound, | ||
| }, | ||
| } | ||
| p := newTestProvider() | ||
|
|
||
| err := p.waitForActiveDeployments(t.Context(), scope, "deploy-1") | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_Timeout(t *testing.T) { | ||
| running := []*azapi.ResourceDeployment{ | ||
| { | ||
| Name: "stuck-deploy", | ||
| ProvisioningState: azapi.DeploymentProvisioningStateRunning, | ||
| }, | ||
| } | ||
| // Return active on every call. | ||
| perCall := make(map[int][]*azapi.ResourceDeployment) | ||
| for i := range 200 { | ||
| perCall[i] = running | ||
| } | ||
|
|
||
| scope := &activeDeploymentScope{activePerCall: perCall} | ||
| p := &BicepProvider{ | ||
| console: mockinput.NewMockConsole(), | ||
| activeDeployPollInterval: 5 * time.Millisecond, | ||
| activeDeployTimeout: 50 * time.Millisecond, | ||
| } | ||
|
|
||
| err := p.waitForActiveDeployments(t.Context(), scope, "stuck-deploy") | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "timed out") | ||
| require.Contains(t, err.Error(), "stuck-deploy") | ||
| } | ||
|
|
||
| func TestWaitForActiveDeployments_DifferentNameNotBlocked(t *testing.T) { | ||
| running := []*azapi.ResourceDeployment{{ | ||
| Name: "other-deploy", | ||
| ProvisioningState: azapi.DeploymentProvisioningStateRunning, | ||
| }} | ||
| scope := &activeDeploymentScope{ | ||
| activePerCall: map[int][]*azapi.ResourceDeployment{0: running}, | ||
| } | ||
| p := newTestProvider() | ||
| err := p.waitForActiveDeployments(t.Context(), scope, "my-deploy") | ||
| require.NoError(t, err) | ||
| require.Equal(t, int32(1), scope.calls.Load()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.