Skip to content

Commit

Permalink
test templates
Browse files Browse the repository at this point in the history
  • Loading branch information
vhvb1989 committed Jul 20, 2023
1 parent 6670561 commit 74ca8b6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 16 deletions.
34 changes: 18 additions & 16 deletions cli/azd/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
78 changes: 78 additions & 0 deletions cli/azd/cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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:")
}
6 changes: 6 additions & 0 deletions templates/tests/test-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 74ca8b6

Please sign in to comment.