|
| 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 armadvisor_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "regexp" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 17 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" |
| 18 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
| 19 | + "github.com/Azure/azure-sdk-for-go/sdk/internal/recording" |
| 20 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/advisor/armadvisor" |
| 21 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/testutil" |
| 22 | + "github.com/stretchr/testify/suite" |
| 23 | +) |
| 24 | + |
| 25 | +type AdvisorTestSuite struct { |
| 26 | + suite.Suite |
| 27 | + |
| 28 | + ctx context.Context |
| 29 | + cred azcore.TokenCredential |
| 30 | + options *arm.ClientOptions |
| 31 | + name string |
| 32 | + armEndpoint string |
| 33 | + location string |
| 34 | + resourceGroupName string |
| 35 | + subscriptionId string |
| 36 | +} |
| 37 | + |
| 38 | +func (testsuite *AdvisorTestSuite) SetupSuite() { |
| 39 | + testutil.StartRecording(testsuite.T(), "sdk/resourcemanager/advisor/armadvisor/testdata") |
| 40 | + |
| 41 | + testsuite.ctx = context.Background() |
| 42 | + testsuite.cred, testsuite.options = testutil.GetCredAndClientOptions(testsuite.T()) |
| 43 | + testsuite.name, _ = recording.GenerateAlphaNumericID(testsuite.T(), "suppressiona", 18, false) |
| 44 | + testsuite.armEndpoint = "https://management.azure.com" |
| 45 | + testsuite.location = recording.GetEnvVariable("LOCATION", "westus") |
| 46 | + testsuite.resourceGroupName = recording.GetEnvVariable("RESOURCE_GROUP_NAME", "scenarioTestTempGroup") |
| 47 | + testsuite.subscriptionId = recording.GetEnvVariable("AZURE_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000") |
| 48 | + resourceGroup, _, err := testutil.CreateResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.location) |
| 49 | + testsuite.Require().NoError(err) |
| 50 | + testsuite.resourceGroupName = *resourceGroup.Name |
| 51 | +} |
| 52 | + |
| 53 | +func (testsuite *AdvisorTestSuite) TearDownSuite() { |
| 54 | + _, err := testutil.DeleteResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.resourceGroupName) |
| 55 | + testsuite.Require().NoError(err) |
| 56 | + testutil.StopRecording(testsuite.T()) |
| 57 | +} |
| 58 | + |
| 59 | +func TestAdvisorTestSuite(t *testing.T) { |
| 60 | + suite.Run(t, new(AdvisorTestSuite)) |
| 61 | +} |
| 62 | + |
| 63 | +// Microsoft.Advisor/generateRecommendations |
| 64 | +func (testsuite *AdvisorTestSuite) TestRecommendations() { |
| 65 | + var recommendationId string |
| 66 | + var resourceUri string |
| 67 | + var err error |
| 68 | + // From step Recommendations_Generate |
| 69 | + fmt.Println("Call operation: Recommendations_Generate") |
| 70 | + recommendationsClient, err := armadvisor.NewRecommendationsClient(testsuite.subscriptionId, testsuite.cred, testsuite.options) |
| 71 | + testsuite.Require().NoError(err) |
| 72 | + recommendationsClientGenerateResponse, err := recommendationsClient.Generate(testsuite.ctx, nil) |
| 73 | + testsuite.Require().NoError(err) |
| 74 | + locationId := *recommendationsClientGenerateResponse.Location |
| 75 | + operationId := regexp.MustCompile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}").FindAllString(locationId, -1)[1] |
| 76 | + |
| 77 | + // From step Recommendations_GetGenerateStatus |
| 78 | + fmt.Println("Call operation: Recommendations_GetGenerateStatus") |
| 79 | + _, err = recommendationsClient.GetGenerateStatus(testsuite.ctx, operationId, nil) |
| 80 | + testsuite.Require().NoError(err) |
| 81 | + |
| 82 | + // From step Recommendations_List |
| 83 | + fmt.Println("Call operation: Recommendations_List") |
| 84 | + recommendationsClientNewListPager := recommendationsClient.NewListPager(&armadvisor.RecommendationsClientListOptions{Filter: nil, |
| 85 | + Top: to.Ptr[int32](10), |
| 86 | + SkipToken: nil, |
| 87 | + }) |
| 88 | + for recommendationsClientNewListPager.More() { |
| 89 | + recommendationsClientListResponse, err := recommendationsClientNewListPager.NextPage(testsuite.ctx) |
| 90 | + testsuite.Require().NoError(err) |
| 91 | + |
| 92 | + resourceUri, _, _ = strings.Cut(*recommendationsClientListResponse.Value[0].ID, "/providers/Microsoft.Advisor/recommendations") |
| 93 | + recommendationId = *recommendationsClientListResponse.Value[0].Name |
| 94 | + break |
| 95 | + } |
| 96 | + |
| 97 | + // From step Recommendations_Get |
| 98 | + fmt.Println("Call operation: Recommendations_Get") |
| 99 | + _, err = recommendationsClient.Get(testsuite.ctx, resourceUri, recommendationId, nil) |
| 100 | + testsuite.Require().NoError(err) |
| 101 | + |
| 102 | + // From step Suppressions_Create |
| 103 | + fmt.Println("Call operation: Suppressions_Create") |
| 104 | + suppressionsClient, err := armadvisor.NewSuppressionsClient(testsuite.subscriptionId, testsuite.cred, testsuite.options) |
| 105 | + testsuite.Require().NoError(err) |
| 106 | + _, err = suppressionsClient.Create(testsuite.ctx, resourceUri, recommendationId, testsuite.name, armadvisor.SuppressionContract{ |
| 107 | + Properties: &armadvisor.SuppressionProperties{ |
| 108 | + TTL: to.Ptr("07:00:00:00"), |
| 109 | + }, |
| 110 | + }, nil) |
| 111 | + testsuite.Require().NoError(err) |
| 112 | + |
| 113 | + // From step Suppressions_List |
| 114 | + fmt.Println("Call operation: Suppressions_List") |
| 115 | + suppressionsClientNewListPager := suppressionsClient.NewListPager(&armadvisor.SuppressionsClientListOptions{Top: nil, |
| 116 | + SkipToken: nil, |
| 117 | + }) |
| 118 | + for suppressionsClientNewListPager.More() { |
| 119 | + _, err := suppressionsClientNewListPager.NextPage(testsuite.ctx) |
| 120 | + testsuite.Require().NoError(err) |
| 121 | + break |
| 122 | + } |
| 123 | + |
| 124 | + // From step Suppressions_Get |
| 125 | + fmt.Println("Call operation: Suppressions_Get") |
| 126 | + _, err = suppressionsClient.Get(testsuite.ctx, resourceUri, recommendationId, testsuite.name, nil) |
| 127 | + testsuite.Require().NoError(err) |
| 128 | + |
| 129 | + // From step Suppressions_Delete |
| 130 | + fmt.Println("Call operation: Suppressions_Delete") |
| 131 | + _, err = suppressionsClient.Delete(testsuite.ctx, resourceUri, recommendationId, testsuite.name, nil) |
| 132 | + testsuite.Require().NoError(err) |
| 133 | +} |
| 134 | + |
| 135 | +// Microsoft.Advisor/configurations/{configurationName} |
| 136 | +func (testsuite *AdvisorTestSuite) TestConfigurations() { |
| 137 | + resourceGroup := testsuite.resourceGroupName |
| 138 | + var err error |
| 139 | + // From step Configurations_CreateInSubscription |
| 140 | + fmt.Println("Call operation: Configurations_CreateInSubscription") |
| 141 | + configurationsClient, err := armadvisor.NewConfigurationsClient(testsuite.subscriptionId, testsuite.cred, testsuite.options) |
| 142 | + testsuite.Require().NoError(err) |
| 143 | + _, err = configurationsClient.CreateInSubscription(testsuite.ctx, armadvisor.ConfigurationNameDefault, armadvisor.ConfigData{ |
| 144 | + Properties: &armadvisor.ConfigDataProperties{ |
| 145 | + LowCPUThreshold: to.Ptr(armadvisor.CPUThresholdFive), |
| 146 | + }, |
| 147 | + }, nil) |
| 148 | + testsuite.Require().NoError(err) |
| 149 | + |
| 150 | + // From step Configurations_ListBySubscription |
| 151 | + fmt.Println("Call operation: Configurations_ListBySubscription") |
| 152 | + configurationsClientNewListBySubscriptionPager := configurationsClient.NewListBySubscriptionPager(nil) |
| 153 | + for configurationsClientNewListBySubscriptionPager.More() { |
| 154 | + _, err := configurationsClientNewListBySubscriptionPager.NextPage(testsuite.ctx) |
| 155 | + testsuite.Require().NoError(err) |
| 156 | + break |
| 157 | + } |
| 158 | + |
| 159 | + // From step Configurations_CreateInResourceGroup |
| 160 | + fmt.Println("Call operation: Configurations_CreateInResourceGroup") |
| 161 | + _, err = configurationsClient.CreateInResourceGroup(testsuite.ctx, armadvisor.ConfigurationNameDefault, resourceGroup, armadvisor.ConfigData{ |
| 162 | + Properties: &armadvisor.ConfigDataProperties{ |
| 163 | + Exclude: to.Ptr(false), |
| 164 | + }, |
| 165 | + }, nil) |
| 166 | + testsuite.Require().NoError(err) |
| 167 | + |
| 168 | + // From step Configurations_ListByResourceGroup |
| 169 | + fmt.Println("Call operation: Configurations_ListByResourceGroup") |
| 170 | + configurationsClientNewListByResourceGroupPager := configurationsClient.NewListByResourceGroupPager(resourceGroup, nil) |
| 171 | + for configurationsClientNewListByResourceGroupPager.More() { |
| 172 | + _, err := configurationsClientNewListByResourceGroupPager.NextPage(testsuite.ctx) |
| 173 | + testsuite.Require().NoError(err) |
| 174 | + break |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +// Microsoft.Advisor/metadata |
| 179 | +func (testsuite *AdvisorTestSuite) TestRecommendationMetadata() { |
| 180 | + var err error |
| 181 | + var recommendationMetadataName string |
| 182 | + // From step RecommendationMetadata_List |
| 183 | + fmt.Println("Call operation: RecommendationMetadata_List") |
| 184 | + recommendationMetadataClient, err := armadvisor.NewRecommendationMetadataClient(testsuite.cred, testsuite.options) |
| 185 | + testsuite.Require().NoError(err) |
| 186 | + recommendationMetadataClientNewListPager := recommendationMetadataClient.NewListPager(nil) |
| 187 | + for recommendationMetadataClientNewListPager.More() { |
| 188 | + recommendationMetadataClientListResponse, err := recommendationMetadataClientNewListPager.NextPage(testsuite.ctx) |
| 189 | + testsuite.Require().NoError(err) |
| 190 | + recommendationMetadataName = *recommendationMetadataClientListResponse.Value[0].Name |
| 191 | + break |
| 192 | + } |
| 193 | + |
| 194 | + // From step RecommendationMetadata_Get |
| 195 | + fmt.Println("Call operation: RecommendationMetadata_Get") |
| 196 | + _, err = recommendationMetadataClient.Get(testsuite.ctx, recommendationMetadataName, nil) |
| 197 | + testsuite.Require().NoError(err) |
| 198 | +} |
| 199 | + |
| 200 | +// Microsoft.Advisor/operations |
| 201 | +func (testsuite *AdvisorTestSuite) TestOperations() { |
| 202 | + var err error |
| 203 | + // From step Operations_List |
| 204 | + fmt.Println("Call operation: Operations_List") |
| 205 | + operationsClient, err := armadvisor.NewOperationsClient(testsuite.cred, testsuite.options) |
| 206 | + testsuite.Require().NoError(err) |
| 207 | + operationsClientNewListPager := operationsClient.NewListPager(nil) |
| 208 | + for operationsClientNewListPager.More() { |
| 209 | + _, err := operationsClientNewListPager.NextPage(testsuite.ctx) |
| 210 | + testsuite.Require().NoError(err) |
| 211 | + break |
| 212 | + } |
| 213 | +} |
0 commit comments