From 200513d08fbcb3306843e9e8a58cc4142a1b2ce1 Mon Sep 17 00:00:00 2001 From: "judy.liu" Date: Fri, 28 Nov 2025 11:39:10 +0800 Subject: [PATCH 01/13] append doc file --- documentation/development/testing.md | 157 ++++++++++++++++++++++++++- 1 file changed, 151 insertions(+), 6 deletions(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 3b82aa47e358..b6359302ff13 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -1,12 +1,12 @@ # Testing SDKs * [Write Tests](#write-tests) - * [Test Mode Options](#test-mode-options) - * [Routing Request to the Proxy](#routing-requests-to-the-proxy) - * [Writing Tests](#writing-tests) - * [Scrubbing Secrets](#scrubbing-secrets) - * [Live Test Resource Management](#live-test-resource-management) - * [Committing/Updating Recordings](#committingupdating-recordings) + * [Test Mode Options](#test-mode-options) + * [Routing Request to the Proxy](#routing-requests-to-the-proxy) + * [Writing Tests](#writing-tests) + * [Scrubbing Secrets](#scrubbing-secrets) + * [Live Test Resource Management](#live-test-resource-management) + * [Committing/Updating Recordings](#committingupdating-recordings) * [Write Examples](#write-examples) ## Write Tests @@ -94,6 +94,8 @@ NOTE: the path to the recordings **must** be in or under a directory named `test ### Writing Tests +#### example about `data plane` + A simple test for `aztables` is shown below: ```go @@ -138,6 +140,149 @@ Check out the docs for more information about the methods available in the [`req If you set the environment variable `AZURE_RECORD_MODE` to "record" and run `go test` with this code and the proper environment variables this test would pass and you would be left with a new directory and file. Test recordings are saved to a `recording` directory in the same directory that your test code lives. Running the above test would also create a file `recording/TestCreateTable.json` with the HTTP interactions persisted on disk. Now you can set `AZURE_RECORD_MODE` to "playback" and run `go test` again, the test will have the same output but without reaching the service. +#### example about `management plane` + +A simple test for `armchaos` is shown below: +##### The first step is to download prepared scripts to genrated asset.json in the path and create file utils_test.go + +1. Run the following PowerShell commands to download necessary scripts: + + ```ps + Invoke-WebRequest -OutFile "generate-assets-json.ps1" https://raw.githubusercontent.com/Azure/azure-sdk-tools/main/eng/common/testproxy/onboarding/generate-assets-json.ps1 + Invoke-WebRequest -OutFile "common-asset-functions.ps1" https://raw.githubusercontent.com/Azure/azure-sdk-tools/main/eng/common/testproxy/onboarding/common-asset-functions.ps1 +``` + +2. Run the script in the service path: +`.\generate-assets-json.ps1 -InitialPush` +This will create a config file `asset.json` and push recordings to the Azure SDK Assets repo. + +``` +asset.json +{ + "AssetsRepo": "Azure/azure-sdk-assets", + "AssetsRepoPrefixPath": "go", + "TagPrefix": "go/resourcemanager/chaos/armchaos", + "Tag": "go/resourcemanager/chaos/armchaos_fd50b88100" +} +``` + +3. Before testing, create a utils_test.go file as the entry point for live tests. Modify "package" and pathToPackage to match your service. + +```go +utils_test.go +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package armchaos_test + +import ( + "os" + "testing" + + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil" +) + +const ( + pathToPackage = "sdk/resourcemanager/chaos/armchaos/testdata" +) + +func TestMain(m *testing.M) { + code := run(m) + os.Exit(code) +} + +func run(m *testing.M) int { + f := testutil.StartProxy(pathToPackage) + defer f() + return m.Run() +} + +``` + +##### Then you can add the test file + +operation_live_test.go + +```go +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package armchaos_test + +import ( + "context" + "fmt" + "testing" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" + "github.com/Azure/azure-sdk-for-go/sdk/internal/recording" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/chaos/armchaos/v2" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil" + "github.com/stretchr/testify/suite" +) + +type OperationsTestSuite struct { + suite.Suite + + ctx context.Context + cred azcore.TokenCredential + options *arm.ClientOptions + armEndpoint string + location string + resourceGroupName string + subscriptionId string +} + +func (testsuite *OperationsTestSuite) SetupSuite() { + testutil.StartRecording(testsuite.T(), pathToPackage) + + testsuite.ctx = context.Background() + testsuite.cred, testsuite.options = testutil.GetCredAndClientOptions(testsuite.T()) + testsuite.armEndpoint = "https://management.azure.com" + testsuite.location = recording.GetEnvVariable("LOCATION", "eastus") + testsuite.resourceGroupName = recording.GetEnvVariable("RESOURCE_GROUP_NAME", "scenarioTestTempGroup") + testsuite.subscriptionId = recording.GetEnvVariable("AZURE_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000") + resourceGroup, _, err := testutil.CreateResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.location) + testsuite.Require().NoError(err) + testsuite.resourceGroupName = *resourceGroup.Name +} + +func (testsuite *OperationsTestSuite) TearDownSuite() { + _, err := testutil.DeleteResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.resourceGroupName) + testsuite.Require().NoError(err) + testutil.StopRecording(testsuite.T()) +} + +func TestOperationsTestSuite(t *testing.T) { + suite.Run(t, new(OperationsTestSuite)) +} + +// Microsoft.Chaos/operations +func (testsuite *OperationsTestSuite) TestOperation() { + var err error + // From step Operations_ListAll + operationsClient, err := armchaos.NewOperationStatusesClient(testsuite.subscriptionId, testsuite.cred, nil) + testsuite.Require().NoError(err) + _, err = operationsClient.Get(testsuite.ctx, testsuite.location, testsuite.subscriptionId, nil) + testsuite.Require().NoError(err) +} + +``` + +##### At last, you can run test via command `go test -run TestOperationsTestSuite` + +1. Set the test mode to "live" using: +`$ENV:AZURE_RECORD_MODE="live"` +2. Once tests pass, switch to "playback" mode and ensure all tests pass in both modes. +3. Push the final assets with `test-proxy push --assets-json-path assets.json`. + ### Scrubbing Secrets Recording files live in the assets repository (`github.com/Azure/azure-sdk-assets`) and must not contain secrets. We use sanitizers with regular expression replacements to prevent recording secrets. The test proxy has many built-in sanitizers enabled by default. However, you may need to add your own by calling functions from the `recording` package. These functions generally take three parameters: the test instance (`t *testing.T`), the value to be removed (ie. an account name or key), and the value to use in replacement. From e126a0421a4bdab1f3590f7e40fab7e80d05e23e Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Fri, 28 Nov 2025 11:54:06 +0800 Subject: [PATCH 02/13] Update documentation/development/testing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index b6359302ff13..533a0867f208 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -143,7 +143,7 @@ If you set the environment variable `AZURE_RECORD_MODE` to "record" and run `go #### example about `management plane` A simple test for `armchaos` is shown below: -##### The first step is to download prepared scripts to genrated asset.json in the path and create file utils_test.go +##### The first step is to download prepared scripts to generated asset.json in the path and create file utils_test.go 1. Run the following PowerShell commands to download necessary scripts: From 6764b491ffb3739f8286d828dbd7bc25f174e831 Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Fri, 28 Nov 2025 11:54:17 +0800 Subject: [PATCH 03/13] Update documentation/development/testing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 533a0867f208..be21cc30223d 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -140,7 +140,7 @@ Check out the docs for more information about the methods available in the [`req If you set the environment variable `AZURE_RECORD_MODE` to "record" and run `go test` with this code and the proper environment variables this test would pass and you would be left with a new directory and file. Test recordings are saved to a `recording` directory in the same directory that your test code lives. Running the above test would also create a file `recording/TestCreateTable.json` with the HTTP interactions persisted on disk. Now you can set `AZURE_RECORD_MODE` to "playback" and run `go test` again, the test will have the same output but without reaching the service. -#### example about `management plane` +#### Example: Management Plane A simple test for `armchaos` is shown below: ##### The first step is to download prepared scripts to generated asset.json in the path and create file utils_test.go From 2f19fc93cdc2eb7ee3c7ee88fbbc947d6a26ce75 Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Fri, 28 Nov 2025 11:54:31 +0800 Subject: [PATCH 04/13] Update documentation/development/testing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index be21cc30223d..972ce83b3032 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -94,7 +94,7 @@ NOTE: the path to the recordings **must** be in or under a directory named `test ### Writing Tests -#### example about `data plane` +#### Example: Data Plane A simple test for `aztables` is shown below: From 85e91914d29126d6a520fd0ed1928ea202bdf149 Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Fri, 28 Nov 2025 11:55:39 +0800 Subject: [PATCH 05/13] Update documentation/development/testing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 972ce83b3032..a17403b1829e 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -169,7 +169,7 @@ asset.json 3. Before testing, create a utils_test.go file as the entry point for live tests. Modify "package" and pathToPackage to match your service. ```go -utils_test.go +`utils_test.go` //go:build go1.18 // +build go1.18 From 9c331d42d67527036fb90008a2c66526d445eb9e Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Fri, 28 Nov 2025 11:55:53 +0800 Subject: [PATCH 06/13] Update documentation/development/testing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- documentation/development/testing.md | 1 - 1 file changed, 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index a17403b1829e..0e2a76e380d5 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -217,7 +217,6 @@ package armchaos_test import ( "context" - "fmt" "testing" "github.com/Azure/azure-sdk-for-go/sdk/azcore" From 7a0c912520f428f6299051effdd0e28b3edfd320 Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Fri, 28 Nov 2025 11:56:03 +0800 Subject: [PATCH 07/13] Update documentation/development/testing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 0e2a76e380d5..6aa0e96ca077 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -204,7 +204,7 @@ func run(m *testing.M) int { ##### Then you can add the test file -operation_live_test.go +`operation_live_test.go` ```go //go:build go1.18 From 14be5a58d35679e83246afabf1f9a437ba9c1a7b Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Wed, 3 Dec 2025 15:02:05 +0800 Subject: [PATCH 08/13] Update documentation/development/testing.md Co-authored-by: JiaqiZhang-Dev --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 6aa0e96ca077..18c153b7faba 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -168,8 +168,8 @@ asset.json 3. Before testing, create a utils_test.go file as the entry point for live tests. Modify "package" and pathToPackage to match your service. -```go `utils_test.go` +```go //go:build go1.18 // +build go1.18 From 8267baf89d2de64b8bd32ffdb280552e06f03ac2 Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Wed, 3 Dec 2025 15:02:15 +0800 Subject: [PATCH 09/13] Update documentation/development/testing.md Co-authored-by: JiaqiZhang-Dev --- documentation/development/testing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 18c153b7faba..8ededb60ddab 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -156,8 +156,8 @@ A simple test for `armchaos` is shown below: `.\generate-assets-json.ps1 -InitialPush` This will create a config file `asset.json` and push recordings to the Azure SDK Assets repo. -``` -asset.json +`assets.json` +```json { "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "go", From 6edf75076f6df38867f540fccc3cfc2dba9d84bd Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Wed, 3 Dec 2025 15:02:52 +0800 Subject: [PATCH 10/13] Update documentation/development/testing.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 8ededb60ddab..c5184d47e2ba 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -147,7 +147,7 @@ A simple test for `armchaos` is shown below: 1. Run the following PowerShell commands to download necessary scripts: - ```ps + ```powershell Invoke-WebRequest -OutFile "generate-assets-json.ps1" https://raw.githubusercontent.com/Azure/azure-sdk-tools/main/eng/common/testproxy/onboarding/generate-assets-json.ps1 Invoke-WebRequest -OutFile "common-asset-functions.ps1" https://raw.githubusercontent.com/Azure/azure-sdk-tools/main/eng/common/testproxy/onboarding/common-asset-functions.ps1 ``` From ceb38f2143e0520a67373c98480a2717c7b21a22 Mon Sep 17 00:00:00 2001 From: Judy Liu Date: Wed, 3 Dec 2025 15:26:10 +0800 Subject: [PATCH 11/13] change code format --- documentation/development/testing.md | 102 +++++++++++++-------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index c5184d47e2ba..04af1b7c99de 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -143,7 +143,7 @@ If you set the environment variable `AZURE_RECORD_MODE` to "record" and run `go #### Example: Management Plane A simple test for `armchaos` is shown below: -##### The first step is to download prepared scripts to generated asset.json in the path and create file utils_test.go +##### The first step is to download prepared scripts to generated assets.json in the path and create file utils_test.go 1. Run the following PowerShell commands to download necessary scripts: @@ -154,7 +154,7 @@ A simple test for `armchaos` is shown below: 2. Run the script in the service path: `.\generate-assets-json.ps1 -InitialPush` -This will create a config file `asset.json` and push recordings to the Azure SDK Assets repo. +This will create a config file `assets.json` and push recordings to the Azure SDK Assets repo. `assets.json` ```json @@ -179,25 +179,25 @@ This will create a config file `asset.json` and push recordings to the Azure SDK package armchaos_test import ( - "os" - "testing" - - "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil" + "os" + "testing" + + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil" ) const ( - pathToPackage = "sdk/resourcemanager/chaos/armchaos/testdata" + pathToPackage = "sdk/resourcemanager/chaos/armchaos/testdata" ) func TestMain(m *testing.M) { - code := run(m) - os.Exit(code) + code := run(m) + os.Exit(code) } func run(m *testing.M) int { - f := testutil.StartProxy(pathToPackage) - defer f() - return m.Run() + f := testutil.StartProxy(pathToPackage) + defer f() + return m.Run() } ``` @@ -216,61 +216,61 @@ func run(m *testing.M) int { package armchaos_test import ( - "context" - "testing" - - "github.com/Azure/azure-sdk-for-go/sdk/azcore" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" - "github.com/Azure/azure-sdk-for-go/sdk/internal/recording" - "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/chaos/armchaos/v2" - "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil" - "github.com/stretchr/testify/suite" + "context" + "testing" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" + "github.com/Azure/azure-sdk-for-go/sdk/internal/recording" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/chaos/armchaos/v2" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil" + "github.com/stretchr/testify/suite" ) type OperationsTestSuite struct { - suite.Suite - - ctx context.Context - cred azcore.TokenCredential - options *arm.ClientOptions - armEndpoint string - location string - resourceGroupName string - subscriptionId string + suite.Suite + + ctx context.Context + cred azcore.TokenCredential + options *arm.ClientOptions + armEndpoint string + location string + resourceGroupName string + subscriptionId string } func (testsuite *OperationsTestSuite) SetupSuite() { - testutil.StartRecording(testsuite.T(), pathToPackage) - - testsuite.ctx = context.Background() - testsuite.cred, testsuite.options = testutil.GetCredAndClientOptions(testsuite.T()) - testsuite.armEndpoint = "https://management.azure.com" - testsuite.location = recording.GetEnvVariable("LOCATION", "eastus") - testsuite.resourceGroupName = recording.GetEnvVariable("RESOURCE_GROUP_NAME", "scenarioTestTempGroup") - testsuite.subscriptionId = recording.GetEnvVariable("AZURE_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000") - resourceGroup, _, err := testutil.CreateResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.location) - testsuite.Require().NoError(err) - testsuite.resourceGroupName = *resourceGroup.Name + testutil.StartRecording(testsuite.T(), pathToPackage) + + testsuite.ctx = context.Background() + testsuite.cred, testsuite.options = testutil.GetCredAndClientOptions(testsuite.T()) + testsuite.armEndpoint = "https://management.azure.com" + testsuite.location = recording.GetEnvVariable("LOCATION", "eastus") + testsuite.resourceGroupName = recording.GetEnvVariable("RESOURCE_GROUP_NAME", "scenarioTestTempGroup") + testsuite.subscriptionId = recording.GetEnvVariable("AZURE_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000") + resourceGroup, _, err := testutil.CreateResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.location) + testsuite.Require().NoError(err) + testsuite.resourceGroupName = *resourceGroup.Name } func (testsuite *OperationsTestSuite) TearDownSuite() { - _, err := testutil.DeleteResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.resourceGroupName) - testsuite.Require().NoError(err) - testutil.StopRecording(testsuite.T()) + _, err := testutil.DeleteResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.resourceGroupName) + testsuite.Require().NoError(err) + testutil.StopRecording(testsuite.T()) } func TestOperationsTestSuite(t *testing.T) { - suite.Run(t, new(OperationsTestSuite)) + suite.Run(t, new(OperationsTestSuite)) } // Microsoft.Chaos/operations func (testsuite *OperationsTestSuite) TestOperation() { - var err error - // From step Operations_ListAll - operationsClient, err := armchaos.NewOperationStatusesClient(testsuite.subscriptionId, testsuite.cred, nil) - testsuite.Require().NoError(err) - _, err = operationsClient.Get(testsuite.ctx, testsuite.location, testsuite.subscriptionId, nil) - testsuite.Require().NoError(err) + var err error + // From step Operations_ListAll + operationsClient, err := armchaos.NewOperationStatusesClient(testsuite.subscriptionId, testsuite.cred, nil) + testsuite.Require().NoError(err) + _, err = operationsClient.Get(testsuite.ctx, testsuite.location, testsuite.subscriptionId, nil) + testsuite.Require().NoError(err) } ``` From 906afefdc0cab2b1a5fb683677a41bfb8ca7b01c Mon Sep 17 00:00:00 2001 From: "judy.liu" Date: Thu, 11 Dec 2025 16:06:29 +0800 Subject: [PATCH 12/13] update testing.md --- documentation/development/testing.md | 1 + 1 file changed, 1 insertion(+) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 04af1b7c99de..0af1c2de6a21 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -203,6 +203,7 @@ func run(m *testing.M) int { ``` ##### Then you can add the test file +this is an example for testing an operation, developer could test their own operations here `operation_live_test.go` From 39bec619026759e3269205ecbb827994e87d885c Mon Sep 17 00:00:00 2001 From: "judy.liu" Date: Thu, 11 Dec 2025 16:36:00 +0800 Subject: [PATCH 13/13] update testing.md --- documentation/development/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/development/testing.md b/documentation/development/testing.md index 0af1c2de6a21..79520430e857 100644 --- a/documentation/development/testing.md +++ b/documentation/development/testing.md @@ -203,7 +203,7 @@ func run(m *testing.M) int { ``` ##### Then you can add the test file -this is an example for testing an operation, developer could test their own operations here +This is an example for testing an operation, developer could test their own operations here `operation_live_test.go`