diff --git a/cli/azd/cmd/util.go b/cli/azd/cmd/util.go index b9fa7f70d73..00570ff4b1a 100644 --- a/cli/azd/cmd/util.go +++ b/cli/azd/cmd/util.go @@ -238,24 +238,26 @@ func getResourceGroupFollowUp( env *environment.Environment, whatIf bool, ) (followUp string) { - if formatter.Kind() != output.JsonFormat { - subscriptionId := env.GetSubscriptionId() - - if resourceGroupName, err := resourceManager.GetResourceGroupName(ctx, subscriptionId, projectConfig); err == nil { - defaultFollowUpText := fmt.Sprintf( - "You can view the resources created under the resource group %s in Azure Portal:", resourceGroupName) - if whatIf { - defaultFollowUpText = fmt.Sprintf( - "You can view the current resources under the resource group %s in Azure Portal:", resourceGroupName) - } - followUp = fmt.Sprintf("%s\n%s", - defaultFollowUpText, - output.WithLinkFormat(fmt.Sprintf( - "https://portal.azure.com/#@/resource/subscriptions/%s/resourceGroups/%s/overview", - subscriptionId, - resourceGroupName))) + if formatter.Kind() == output.JsonFormat { + return followUp + } + + subscriptionId := env.GetSubscriptionId() + if resourceGroupName, err := resourceManager.GetResourceGroupName(ctx, subscriptionId, projectConfig); err == nil { + defaultFollowUpText := fmt.Sprintf( + "You can view the resources created under the resource group %s in Azure Portal:", resourceGroupName) + if whatIf { + defaultFollowUpText = fmt.Sprintf( + "You can view the current resources under the resource group %s in Azure Portal:", resourceGroupName) } + followUp = fmt.Sprintf("%s\n%s", + defaultFollowUpText, + output.WithLinkFormat(fmt.Sprintf( + "https://portal.azure.com/#@/resource/subscriptions/%s/resourceGroups/%s/overview", + subscriptionId, + resourceGroupName))) } + return followUp } diff --git a/cli/azd/cmd/util_test.go b/cli/azd/cmd/util_test.go index 75ca38770e1..64e3c8920ca 100644 --- a/cli/azd/cmd/util_test.go +++ b/cli/azd/cmd/util_test.go @@ -4,13 +4,21 @@ import ( "context" "errors" "fmt" + "net/http" "os" "path/filepath" + "strings" "testing" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources" + "github.com/azure/azure-dev/cli/azd/pkg/environment" "github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext" "github.com/azure/azure-dev/cli/azd/pkg/input" + "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/mockazcli" "github.com/stretchr/testify/require" ) @@ -87,3 +95,73 @@ func Test_createAndInitEnvironment(t *testing.T) { validName)) }) } + +func Test_getResourceGroupFollowUp(t *testing.T) { + mockContext := mocks.NewMockContext(context.Background()) + azCli := mockazcli.NewAzCliFromMockContext(mockContext) + depOpService := mockazcli.NewDeploymentOperationsServiceFromMockContext(mockContext) + env := environment.EphemeralWithValues("envA", map[string]string{ + environment.SubscriptionIdEnvVarName: "SUBSCRIPTION_ID", + }) + + mockContext.HttpClient.When(func(request *http.Request) bool { + return request.Method == http.MethodGet && strings.Contains(request.URL.Path, "subscriptions/SUBSCRIPTION_ID/") + }).RespondFn(func(request *http.Request) (*http.Response, error) { + resp := &armresources.ResourceGroupListResult{ + Value: []*armresources.ResourceGroup{ + { + Location: to.Ptr("location"), + ID: to.Ptr("id"), + Name: to.Ptr("Name"), + Type: to.Ptr("Type"), + }, + }, + } + return mocks.CreateHttpResponseWithBody(request, 200, resp) + }) + + followUp := getResourceGroupFollowUp( + *mockContext.Context, + &output.NoneFormatter{}, + &project.ProjectConfig{}, + project.NewResourceManager(env, azCli, depOpService), + env, + false) + + require.Contains(t, followUp, "You can view the resources created under the resource group Name in Azure Portal:") +} + +func Test_getResourceGroupFollowUpPreview(t *testing.T) { + mockContext := mocks.NewMockContext(context.Background()) + azCli := mockazcli.NewAzCliFromMockContext(mockContext) + depOpService := mockazcli.NewDeploymentOperationsServiceFromMockContext(mockContext) + env := environment.EphemeralWithValues("envA", map[string]string{ + environment.SubscriptionIdEnvVarName: "SUBSCRIPTION_ID", + }) + + mockContext.HttpClient.When(func(request *http.Request) bool { + return request.Method == http.MethodGet && strings.Contains(request.URL.Path, "subscriptions/SUBSCRIPTION_ID/") + }).RespondFn(func(request *http.Request) (*http.Response, error) { + resp := &armresources.ResourceGroupListResult{ + Value: []*armresources.ResourceGroup{ + { + Location: to.Ptr("location"), + ID: to.Ptr("id"), + Name: to.Ptr("Name"), + Type: to.Ptr("Type"), + }, + }, + } + return mocks.CreateHttpResponseWithBody(request, 200, resp) + }) + + followUp := getResourceGroupFollowUp( + *mockContext.Context, + &output.NoneFormatter{}, + &project.ProjectConfig{}, + project.NewResourceManager(env, azCli, depOpService), + env, + true) + + require.Contains(t, followUp, "You can view the current resources under the resource group Name in Azure Portal:") +} diff --git a/templates/tests/test-templates.sh b/templates/tests/test-templates.sh index 1b8b3e2be9c..dfa94866e8e 100755 --- a/templates/tests/test-templates.sh +++ b/templates/tests/test-templates.sh @@ -98,9 +98,15 @@ function deployTemplate { azd env new "$3" --subscription "$4" --location "$5" --no-prompt fi + echo "Create provision preview for $3..." + azd provision -e "$3" --preview + echo "Provisioning infrastructure for $3..." azd provision -e "$3" + echo "Create (delta) provision preview after provision..." + azd provision -e "$3" --preview + echo "Deploying apps for $3..." azd deploy -e "$3" }