-
Notifications
You must be signed in to change notification settings - Fork 288
fix: stop azd completely when user declines preflight warnings #7329
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
Merged
Merged
Changes from all commits
Commits
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
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
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
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
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,78 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package middleware | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/cmd/actions" | ||
| "github.com/azure/azure-dev/cli/azd/internal" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/alpha" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestUxMiddleware_ErrAbortedByUser_SwallowsError(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
| featureManager := &alpha.FeatureManager{} | ||
| ux := NewUxMiddleware(&Options{}, mockContext.Console, featureManager) | ||
|
|
||
| result, err := ux.Run(*mockContext.Context, func(ctx context.Context) (*actions.ActionResult, error) { | ||
| return nil, internal.ErrAbortedByUser | ||
| }) | ||
|
|
||
| // Error should be swallowed (exit code 0) | ||
| require.NoError(t, err) | ||
| require.Nil(t, result) | ||
| } | ||
|
|
||
| func TestUxMiddleware_ErrAbortedByUser_ChildAction_PassesThrough(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
| childCtx := WithChildAction(*mockContext.Context) | ||
| featureManager := &alpha.FeatureManager{} | ||
| ux := NewUxMiddleware(&Options{}, mockContext.Console, featureManager) | ||
|
|
||
| result, err := ux.Run(childCtx, func(ctx context.Context) (*actions.ActionResult, error) { | ||
| return nil, internal.ErrAbortedByUser | ||
| }) | ||
|
|
||
| // For child actions, error should pass through unchanged | ||
| require.ErrorIs(t, err, internal.ErrAbortedByUser) | ||
| require.Nil(t, result) | ||
| } | ||
|
|
||
| func TestUxMiddleware_OtherErrors_NotSwallowed(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
| featureManager := &alpha.FeatureManager{} | ||
| ux := NewUxMiddleware(&Options{}, mockContext.Console, featureManager) | ||
| someErr := errors.New("deployment failed") | ||
|
|
||
| _, err := ux.Run(*mockContext.Context, func(ctx context.Context) (*actions.ActionResult, error) { | ||
| return nil, someErr | ||
| }) | ||
|
|
||
| // Other errors should still be returned | ||
| require.ErrorIs(t, err, someErr) | ||
| } | ||
|
|
||
| func TestUxMiddleware_Success_ShowsActionResult(t *testing.T) { | ||
| mockContext := mocks.NewMockContext(context.Background()) | ||
| featureManager := &alpha.FeatureManager{} | ||
| ux := NewUxMiddleware(&Options{}, mockContext.Console, featureManager) | ||
|
|
||
| actionResult := &actions.ActionResult{ | ||
| Message: &actions.ResultMessage{ | ||
| Header: "All done!", | ||
| }, | ||
| } | ||
|
|
||
| result, err := ux.Run(*mockContext.Context, func(ctx context.Context) (*actions.ActionResult, error) { | ||
| return actionResult, nil | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, actionResult, result) | ||
| } |
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
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
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
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,186 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/internal" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/alpha" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/cloud" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/config" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/environment" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/ext" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/ioc" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/output" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/project" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks" | ||
| "github.com/azure/azure-dev/cli/azd/test/mocks/mockinput" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // mockProjectManager implements project.ProjectManager for testing. | ||
| type mockProjectManager struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| func (m *mockProjectManager) Initialize(ctx context.Context, projectConfig *project.ProjectConfig) error { | ||
| return m.Called(ctx, projectConfig).Error(0) | ||
| } | ||
|
|
||
| func (m *mockProjectManager) EnsureAllTools( | ||
| ctx context.Context, projectConfig *project.ProjectConfig, _ project.ServiceFilterPredicate, | ||
| ) error { | ||
| return m.Called(ctx, projectConfig).Error(0) | ||
| } | ||
|
|
||
| func (m *mockProjectManager) DefaultServiceFromWd( | ||
| ctx context.Context, projectConfig *project.ProjectConfig, | ||
| ) (*project.ServiceConfig, error) { | ||
| args := m.Called(ctx, projectConfig) | ||
| return args.Get(0).(*project.ServiceConfig), args.Error(1) | ||
| } | ||
|
|
||
| func (m *mockProjectManager) EnsureFrameworkTools( | ||
| ctx context.Context, projectConfig *project.ProjectConfig, _ project.ServiceFilterPredicate, | ||
| ) error { | ||
| return m.Called(ctx, projectConfig).Error(0) | ||
| } | ||
|
|
||
| func (m *mockProjectManager) EnsureServiceTargetTools( | ||
| ctx context.Context, projectConfig *project.ProjectConfig, _ project.ServiceFilterPredicate, | ||
| ) error { | ||
| return m.Called(ctx, projectConfig).Error(0) | ||
| } | ||
|
|
||
| func (m *mockProjectManager) EnsureRestoreTools( | ||
| ctx context.Context, projectConfig *project.ProjectConfig, _ project.ServiceFilterPredicate, | ||
| ) error { | ||
| return m.Called(ctx, projectConfig).Error(0) | ||
| } | ||
|
|
||
| // mockProvider implements provisioning.Provider for testing. | ||
| type mockProvider struct { | ||
| deployResult *provisioning.DeployResult | ||
| deployErr error | ||
| } | ||
|
|
||
| func (p *mockProvider) Name() string { return "test" } | ||
|
|
||
| func (p *mockProvider) Initialize(_ context.Context, _ string, _ provisioning.Options) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (p *mockProvider) State(_ context.Context, _ *provisioning.StateOptions) (*provisioning.StateResult, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (p *mockProvider) Deploy(_ context.Context) (*provisioning.DeployResult, error) { | ||
| return p.deployResult, p.deployErr | ||
| } | ||
|
|
||
| func (p *mockProvider) Preview(_ context.Context) (*provisioning.DeployPreviewResult, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (p *mockProvider) Destroy(_ context.Context, _ provisioning.DestroyOptions) (*provisioning.DestroyResult, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (p *mockProvider) EnsureEnv(_ context.Context) error { return nil } | ||
|
|
||
| func (p *mockProvider) Parameters(_ context.Context) ([]provisioning.Parameter, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // TestProvisionAction_PreflightAborted verifies that when the user declines | ||
| // preflight warnings, ProvisionAction.Run returns ErrAbortedByUser and does NOT | ||
| // attempt to read deployResult.Deployment.Outputs (which would nil-panic). | ||
| // | ||
| // Regression test for https://github.com/Azure/azure-dev/issues/7305 | ||
| func TestProvisionAction_PreflightAborted(t *testing.T) { | ||
| // Set up a temp project with a minimal infra directory so ImportManager works. | ||
| projectDir := t.TempDir() | ||
| infraDir := filepath.Join(projectDir, "infra") | ||
| require.NoError(t, os.MkdirAll(infraDir, 0o755)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(infraDir, "main.bicep"), []byte("targetScope = 'subscription'\n"), 0o600)) | ||
|
|
||
| // Mock provider that simulates preflight abort (user said No). | ||
| provider := &mockProvider{ | ||
| deployResult: &provisioning.DeployResult{ | ||
| SkippedReason: provisioning.PreflightAbortedSkipped, | ||
| }, | ||
| } | ||
|
|
||
| // Register mock provider in IoC so provisioning.Manager.Initialize can resolve it. | ||
| container := ioc.NewNestedContainer(nil) | ||
| ioc.RegisterNamedInstance[provisioning.Provider](container, string(provisioning.Test), provider) | ||
|
|
||
| env := environment.New("test-env") | ||
| env.SetSubscriptionId("00000000-0000-0000-0000-000000000000") | ||
| env.SetLocation("eastus2") | ||
|
|
||
| console := mockinput.NewMockConsole() | ||
|
|
||
| provisionManager := provisioning.NewManager( | ||
| container, | ||
| func() (provisioning.ProviderKind, error) { return provisioning.Test, nil }, | ||
| nil, // envManager — not needed for this test path | ||
| env, | ||
| console, | ||
| alpha.NewFeaturesManagerWithConfig(config.NewEmptyConfig()), | ||
| nil, // fileShareService | ||
| cloud.AzurePublic(), | ||
| ) | ||
|
|
||
| pm := &mockProjectManager{} | ||
| pm.On("Initialize", mock.Anything, mock.Anything).Return(nil) | ||
| pm.On("EnsureAllTools", mock.Anything, mock.Anything).Return(nil) | ||
|
|
||
| projectConfig := &project.ProjectConfig{ | ||
| Name: "test-project", | ||
| Path: projectDir, | ||
| Infra: provisioning.Options{ | ||
| Provider: provisioning.Test, | ||
| Path: "infra", | ||
| Module: "main", | ||
| }, | ||
| } | ||
| projectConfig.EventDispatcher = ext.NewEventDispatcher[project.ProjectLifecycleEventArgs]( | ||
| project.ProjectEvents..., | ||
| ) | ||
|
|
||
| action := &ProvisionAction{ | ||
| flags: &ProvisionFlags{ | ||
| global: &internal.GlobalCommandOptions{}, | ||
| EnvFlag: &internal.EnvFlag{}, | ||
| }, | ||
| provisionManager: provisionManager, | ||
| projectManager: pm, | ||
| importManager: project.NewImportManager(nil), | ||
| projectConfig: projectConfig, | ||
| env: env, | ||
| console: console, | ||
| formatter: &output.NoneFormatter{}, | ||
| writer: io.Discard, | ||
| alphaFeatureManager: alpha.NewFeaturesManagerWithConfig(config.NewEmptyConfig()), | ||
| portalUrlBase: "https://portal.azure.com", | ||
| } | ||
|
|
||
| mockContext := mocks.NewMockContext(context.Background()) | ||
| result, err := action.Run(*mockContext.Context) | ||
|
|
||
| // Must return ErrAbortedByUser (not nil, not a panic) | ||
| require.ErrorIs(t, err, internal.ErrAbortedByUser) | ||
| require.Nil(t, result) | ||
|
|
||
| // Verify project manager was called (action didn't exit prematurely) | ||
| pm.AssertExpectations(t) | ||
| } |
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
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
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.