ARO-RP/pkg/util/arm/deploy_test.go

121 строка
3.5 KiB
Go
Исходник Обычный вид История

2022-05-03 04:45:00 +03:00
package arm
2021-02-24 15:33:10 +03:00
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"testing"
mgmtfeatures "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/features"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/golang/mock/gomock"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
mock_features "github.com/Azure/ARO-RP/pkg/util/mocks/azureclient/mgmt/features"
)
const deploymentName = "test"
func TestDeployARMTemplate(t *testing.T) {
ctx := context.Background()
resourceGroup := "fakeResourceGroup"
2022-05-03 04:45:00 +03:00
armTemplate := &Template{}
2021-02-24 15:33:10 +03:00
params := map[string]interface{}{}
deployment := mgmtfeatures.Deployment{
Properties: &mgmtfeatures.DeploymentProperties{
Template: armTemplate,
Parameters: params,
Mode: mgmtfeatures.Incremental,
},
}
activeErr := autorest.NewErrorWithError(azure.RequestError{
ServiceError: &azure.ServiceError{Code: "DeploymentActive"},
}, "", "", nil, "")
for _, tt := range []struct {
name string
mocks func(*mock_features.MockDeploymentsClient)
wantErr string
}{
{
name: "Deployment successful with no errors",
mocks: func(dc *mock_features.MockDeploymentsClient) {
dc.EXPECT().
CreateOrUpdateAndWait(ctx, resourceGroup, deploymentName, deployment).
Return(nil)
},
},
{
name: "Deployment active error, then wait successfully",
mocks: func(dc *mock_features.MockDeploymentsClient) {
dc.EXPECT().
CreateOrUpdateAndWait(ctx, resourceGroup, deploymentName, deployment).
Return(activeErr)
dc.EXPECT().
Wait(ctx, resourceGroup, deploymentName).
Return(nil)
},
},
{
name: "Deployment active error, then timeout",
mocks: func(dc *mock_features.MockDeploymentsClient) {
dc.EXPECT().
CreateOrUpdateAndWait(ctx, resourceGroup, deploymentName, deployment).
Return(activeErr)
dc.EXPECT().
Wait(ctx, resourceGroup, deploymentName).
Return(wait.ErrWaitTimeout)
},
wantErr: "timed out waiting for the condition",
},
{
name: "DetailedError which should be returned to user",
mocks: func(dc *mock_features.MockDeploymentsClient) {
dc.EXPECT().
CreateOrUpdateAndWait(ctx, resourceGroup, deploymentName, deployment).
Return(autorest.DetailedError{
Original: &azure.ServiceError{
Code: "AccountIsDisabled",
},
})
},
wantErr: `400: DeploymentFailed: : Deployment failed. Details: : : {"code":"AccountIsDisabled","message":"","target":null,"details":null,"innererror":null,"additionalInfo":null}`,
},
{
name: "ServiceError which should be returned to user",
mocks: func(dc *mock_features.MockDeploymentsClient) {
dc.EXPECT().
CreateOrUpdateAndWait(ctx, resourceGroup, deploymentName, deployment).
Return(&azure.ServiceError{
Code: "AccountIsDisabled",
})
},
wantErr: `400: DeploymentFailed: : Deployment failed. Details: : : {"code":"AccountIsDisabled","message":"","target":null,"details":null,"innererror":null,"additionalInfo":null}`,
},
} {
t.Run(tt.name, func(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
deploymentsClient := mock_features.NewMockDeploymentsClient(controller)
tt.mocks(deploymentsClient)
2022-05-03 04:45:00 +03:00
log := logrus.NewEntry(logrus.StandardLogger())
2021-02-24 15:33:10 +03:00
2022-05-03 04:45:00 +03:00
err := DeployTemplate(ctx, log, deploymentsClient, resourceGroup, deploymentName, armTemplate, params)
2021-02-24 15:33:10 +03:00
if err != nil && err.Error() != tt.wantErr ||
err == nil && tt.wantErr != "" {
t.Error(err)
}
})
}
}