|
| 1 | +//go:build go1.18 |
| 2 | +// +build go1.18 |
| 3 | + |
| 4 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | + |
| 7 | +package armchaos_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 15 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" |
| 16 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
| 17 | + "github.com/Azure/azure-sdk-for-go/sdk/internal/recording" |
| 18 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/chaos/armchaos" |
| 19 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2/testutil" |
| 20 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources" |
| 21 | + "github.com/stretchr/testify/suite" |
| 22 | +) |
| 23 | + |
| 24 | +type CapabilitiesTestSuite struct { |
| 25 | + suite.Suite |
| 26 | + |
| 27 | + ctx context.Context |
| 28 | + cred azcore.TokenCredential |
| 29 | + options *arm.ClientOptions |
| 30 | + armEndpoint string |
| 31 | + capabilityName string |
| 32 | + cosmosAccountId string |
| 33 | + cosmosaccountName string |
| 34 | + targetName string |
| 35 | + location string |
| 36 | + resourceGroupName string |
| 37 | + subscriptionId string |
| 38 | +} |
| 39 | + |
| 40 | +func (testsuite *CapabilitiesTestSuite) SetupSuite() { |
| 41 | + testutil.StartRecording(testsuite.T(), "sdk/resourcemanager/chaos/armchaos/testdata") |
| 42 | + |
| 43 | + testsuite.ctx = context.Background() |
| 44 | + testsuite.cred, testsuite.options = testutil.GetCredAndClientOptions(testsuite.T()) |
| 45 | + testsuite.armEndpoint = "https://management.azure.com" |
| 46 | + testsuite.capabilityName = "Failover-1.0" |
| 47 | + testsuite.cosmosaccountName, _ = recording.GenerateAlphaNumericID(testsuite.T(), "account", 13, true) |
| 48 | + testsuite.targetName = "Microsoft-CosmosDB" |
| 49 | + testsuite.location = recording.GetEnvVariable("LOCATION", "eastus") |
| 50 | + testsuite.resourceGroupName = recording.GetEnvVariable("RESOURCE_GROUP_NAME", "scenarioTestTempGroup") |
| 51 | + testsuite.subscriptionId = recording.GetEnvVariable("AZURE_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000") |
| 52 | + resourceGroup, _, err := testutil.CreateResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.location) |
| 53 | + testsuite.Require().NoError(err) |
| 54 | + testsuite.resourceGroupName = *resourceGroup.Name |
| 55 | + testsuite.Prepare() |
| 56 | +} |
| 57 | + |
| 58 | +func (testsuite *CapabilitiesTestSuite) TearDownSuite() { |
| 59 | + _, err := testutil.DeleteResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.resourceGroupName) |
| 60 | + testsuite.Require().NoError(err) |
| 61 | + testutil.StopRecording(testsuite.T()) |
| 62 | +} |
| 63 | + |
| 64 | +func TestCapabilitiesTestSuite(t *testing.T) { |
| 65 | + suite.Run(t, new(CapabilitiesTestSuite)) |
| 66 | +} |
| 67 | + |
| 68 | +func (testsuite *CapabilitiesTestSuite) Prepare() { |
| 69 | + var err error |
| 70 | + // From step Create_CosmosAccount |
| 71 | + template := map[string]any{ |
| 72 | + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", |
| 73 | + "contentVersion": "1.0.0.0", |
| 74 | + "outputs": map[string]any{ |
| 75 | + "cosmosAccountId": map[string]any{ |
| 76 | + "type": "string", |
| 77 | + "value": "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosaccountName'))]", |
| 78 | + }, |
| 79 | + }, |
| 80 | + "parameters": map[string]any{ |
| 81 | + "cosmosaccountName": map[string]any{ |
| 82 | + "type": "string", |
| 83 | + "defaultValue": testsuite.cosmosaccountName, |
| 84 | + }, |
| 85 | + "location": map[string]any{ |
| 86 | + "type": "string", |
| 87 | + "defaultValue": testsuite.location, |
| 88 | + }, |
| 89 | + }, |
| 90 | + "resources": []any{ |
| 91 | + map[string]any{ |
| 92 | + "name": "[parameters('cosmosaccountName')]", |
| 93 | + "type": "Microsoft.DocumentDB/databaseAccounts", |
| 94 | + "apiVersion": "2023-09-15", |
| 95 | + "location": "[parameters('location')]", |
| 96 | + "properties": map[string]any{ |
| 97 | + "databaseAccountOfferType": "Standard", |
| 98 | + "locations": []any{ |
| 99 | + map[string]any{ |
| 100 | + "failoverPriority": "0", |
| 101 | + "is_zone_redundant": false, |
| 102 | + "locationName": "westus", |
| 103 | + }, |
| 104 | + map[string]any{ |
| 105 | + "failoverPriority": "1", |
| 106 | + "locationName": "eastus", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + "variables": map[string]any{}, |
| 113 | + } |
| 114 | + deployment := armresources.Deployment{ |
| 115 | + Properties: &armresources.DeploymentProperties{ |
| 116 | + Template: template, |
| 117 | + Mode: to.Ptr(armresources.DeploymentModeIncremental), |
| 118 | + }, |
| 119 | + } |
| 120 | + deploymentExtend, err := testutil.CreateDeployment(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.resourceGroupName, "Create_CosmosAccount", &deployment) |
| 121 | + testsuite.Require().NoError(err) |
| 122 | + testsuite.cosmosAccountId = deploymentExtend.Properties.Outputs.(map[string]interface{})["cosmosAccountId"].(map[string]interface{})["value"].(string) |
| 123 | +} |
| 124 | + |
| 125 | +// Microsoft.Chaos/targets/{targetName}/capabilities/{capabilityName} |
| 126 | +func (testsuite *CapabilitiesTestSuite) TestCapabilities() { |
| 127 | + parentProviderNamespace := "Microsoft.DocumentDB" |
| 128 | + parentResourceName := testsuite.cosmosaccountName |
| 129 | + parentResourceType := "databaseAccounts" |
| 130 | + var err error |
| 131 | + // From step Targets_CreateOrUpdate |
| 132 | + fmt.Println("Call operation: Targets_CreateOrUpdate") |
| 133 | + targetsClient, err := armchaos.NewTargetsClient(testsuite.subscriptionId, testsuite.cred, testsuite.options) |
| 134 | + testsuite.Require().NoError(err) |
| 135 | + _, err = targetsClient.CreateOrUpdate(testsuite.ctx, testsuite.resourceGroupName, parentProviderNamespace, parentResourceType, parentResourceName, testsuite.targetName, armchaos.Target{ |
| 136 | + Properties: map[string]any{ |
| 137 | + "identities": []any{ |
| 138 | + map[string]any{ |
| 139 | + "type": "CertificateSubjectIssuer", |
| 140 | + "subject": "CN=example.subject", |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, nil) |
| 145 | + testsuite.Require().NoError(err) |
| 146 | + |
| 147 | + // From step Capabilities_CreateOrUpdate |
| 148 | + fmt.Println("Call operation: Capabilities_CreateOrUpdate") |
| 149 | + capabilitiesClient, err := armchaos.NewCapabilitiesClient(testsuite.subscriptionId, testsuite.cred, testsuite.options) |
| 150 | + testsuite.Require().NoError(err) |
| 151 | + _, err = capabilitiesClient.CreateOrUpdate(testsuite.ctx, testsuite.resourceGroupName, parentProviderNamespace, parentResourceType, parentResourceName, testsuite.targetName, testsuite.capabilityName, armchaos.Capability{ |
| 152 | + Properties: &armchaos.CapabilityProperties{}, |
| 153 | + }, nil) |
| 154 | + testsuite.Require().NoError(err) |
| 155 | + |
| 156 | + // From step Capabilities_Get |
| 157 | + fmt.Println("Call operation: Capabilities_Get") |
| 158 | + _, err = capabilitiesClient.Get(testsuite.ctx, testsuite.resourceGroupName, parentProviderNamespace, parentResourceType, parentResourceName, testsuite.targetName, testsuite.capabilityName, nil) |
| 159 | + testsuite.Require().NoError(err) |
| 160 | + |
| 161 | + // From step Capabilities_List |
| 162 | + fmt.Println("Call operation: Capabilities_List") |
| 163 | + capabilitiesClientNewListPager := capabilitiesClient.NewListPager(testsuite.resourceGroupName, parentProviderNamespace, parentResourceType, parentResourceName, testsuite.targetName, &armchaos.CapabilitiesClientListOptions{ContinuationToken: nil}) |
| 164 | + for capabilitiesClientNewListPager.More() { |
| 165 | + _, err := capabilitiesClientNewListPager.NextPage(testsuite.ctx) |
| 166 | + testsuite.Require().NoError(err) |
| 167 | + break |
| 168 | + } |
| 169 | + |
| 170 | + // From step Capabilities_Delete |
| 171 | + fmt.Println("Call operation: Capabilities_Delete") |
| 172 | + _, err = capabilitiesClient.Delete(testsuite.ctx, testsuite.resourceGroupName, parentProviderNamespace, parentResourceType, parentResourceName, testsuite.targetName, testsuite.capabilityName, nil) |
| 173 | + testsuite.Require().NoError(err) |
| 174 | +} |
0 commit comments