Skip to content

Commit 200513d

Browse files
committed
append doc file
1 parent d8a74fe commit 200513d

File tree

1 file changed

+151
-6
lines changed

1 file changed

+151
-6
lines changed

documentation/development/testing.md

Lines changed: 151 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Testing SDKs
22

33
* [Write Tests](#write-tests)
4-
* [Test Mode Options](#test-mode-options)
5-
* [Routing Request to the Proxy](#routing-requests-to-the-proxy)
6-
* [Writing Tests](#writing-tests)
7-
* [Scrubbing Secrets](#scrubbing-secrets)
8-
* [Live Test Resource Management](#live-test-resource-management)
9-
* [Committing/Updating Recordings](#committingupdating-recordings)
4+
* [Test Mode Options](#test-mode-options)
5+
* [Routing Request to the Proxy](#routing-requests-to-the-proxy)
6+
* [Writing Tests](#writing-tests)
7+
* [Scrubbing Secrets](#scrubbing-secrets)
8+
* [Live Test Resource Management](#live-test-resource-management)
9+
* [Committing/Updating Recordings](#committingupdating-recordings)
1010
* [Write Examples](#write-examples)
1111

1212
## Write Tests
@@ -94,6 +94,8 @@ NOTE: the path to the recordings **must** be in or under a directory named `test
9494

9595
### Writing Tests
9696

97+
#### example about `data plane`
98+
9799
A simple test for `aztables` is shown below:
98100

99101
```go
@@ -138,6 +140,149 @@ Check out the docs for more information about the methods available in the [`req
138140

139141
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.
140142

143+
#### example about `management plane`
144+
145+
A simple test for `armchaos` is shown below:
146+
##### The first step is to download prepared scripts to genrated asset.json in the path and create file utils_test.go
147+
148+
1. Run the following PowerShell commands to download necessary scripts:
149+
150+
```ps
151+
Invoke-WebRequest -OutFile "generate-assets-json.ps1" https://raw.githubusercontent.com/Azure/azure-sdk-tools/main/eng/common/testproxy/onboarding/generate-assets-json.ps1
152+
Invoke-WebRequest -OutFile "common-asset-functions.ps1" https://raw.githubusercontent.com/Azure/azure-sdk-tools/main/eng/common/testproxy/onboarding/common-asset-functions.ps1
153+
```
154+
155+
2. Run the script in the service path:
156+
`.\generate-assets-json.ps1 -InitialPush`
157+
This will create a config file `asset.json` and push recordings to the Azure SDK Assets repo.
158+
159+
```
160+
asset.json
161+
{
162+
"AssetsRepo": "Azure/azure-sdk-assets",
163+
"AssetsRepoPrefixPath": "go",
164+
"TagPrefix": "go/resourcemanager/chaos/armchaos",
165+
"Tag": "go/resourcemanager/chaos/armchaos_fd50b88100"
166+
}
167+
```
168+
169+
3. Before testing, create a utils_test.go file as the entry point for live tests. Modify "package" and pathToPackage to match your service.
170+
171+
```go
172+
utils_test.go
173+
//go:build go1.18
174+
// +build go1.18
175+
176+
// Copyright (c) Microsoft Corporation. All rights reserved.
177+
// Licensed under the MIT License. See License.txt in the project root for license information.
178+
179+
package armchaos_test
180+
181+
import (
182+
"os"
183+
"testing"
184+
185+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil"
186+
)
187+
188+
const (
189+
pathToPackage = "sdk/resourcemanager/chaos/armchaos/testdata"
190+
)
191+
192+
func TestMain(m *testing.M) {
193+
code := run(m)
194+
os.Exit(code)
195+
}
196+
197+
func run(m *testing.M) int {
198+
f := testutil.StartProxy(pathToPackage)
199+
defer f()
200+
return m.Run()
201+
}
202+
203+
```
204+
205+
##### Then you can add the test file
206+
207+
operation_live_test.go
208+
209+
```go
210+
//go:build go1.18
211+
// +build go1.18
212+
213+
// Copyright (c) Microsoft Corporation. All rights reserved.
214+
// Licensed under the MIT License. See License.txt in the project root for license information.
215+
216+
package armchaos_test
217+
218+
import (
219+
"context"
220+
"fmt"
221+
"testing"
222+
223+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
224+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
225+
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
226+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/chaos/armchaos/v2"
227+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3/testutil"
228+
"github.com/stretchr/testify/suite"
229+
)
230+
231+
type OperationsTestSuite struct {
232+
suite.Suite
233+
234+
ctx context.Context
235+
cred azcore.TokenCredential
236+
options *arm.ClientOptions
237+
armEndpoint string
238+
location string
239+
resourceGroupName string
240+
subscriptionId string
241+
}
242+
243+
func (testsuite *OperationsTestSuite) SetupSuite() {
244+
testutil.StartRecording(testsuite.T(), pathToPackage)
245+
246+
testsuite.ctx = context.Background()
247+
testsuite.cred, testsuite.options = testutil.GetCredAndClientOptions(testsuite.T())
248+
testsuite.armEndpoint = "https://management.azure.com"
249+
testsuite.location = recording.GetEnvVariable("LOCATION", "eastus")
250+
testsuite.resourceGroupName = recording.GetEnvVariable("RESOURCE_GROUP_NAME", "scenarioTestTempGroup")
251+
testsuite.subscriptionId = recording.GetEnvVariable("AZURE_SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000")
252+
resourceGroup, _, err := testutil.CreateResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.location)
253+
testsuite.Require().NoError(err)
254+
testsuite.resourceGroupName = *resourceGroup.Name
255+
}
256+
257+
func (testsuite *OperationsTestSuite) TearDownSuite() {
258+
_, err := testutil.DeleteResourceGroup(testsuite.ctx, testsuite.subscriptionId, testsuite.cred, testsuite.options, testsuite.resourceGroupName)
259+
testsuite.Require().NoError(err)
260+
testutil.StopRecording(testsuite.T())
261+
}
262+
263+
func TestOperationsTestSuite(t *testing.T) {
264+
suite.Run(t, new(OperationsTestSuite))
265+
}
266+
267+
// Microsoft.Chaos/operations
268+
func (testsuite *OperationsTestSuite) TestOperation() {
269+
var err error
270+
// From step Operations_ListAll
271+
operationsClient, err := armchaos.NewOperationStatusesClient(testsuite.subscriptionId, testsuite.cred, nil)
272+
testsuite.Require().NoError(err)
273+
_, err = operationsClient.Get(testsuite.ctx, testsuite.location, testsuite.subscriptionId, nil)
274+
testsuite.Require().NoError(err)
275+
}
276+
277+
```
278+
279+
##### At last, you can run test via command `go test -run TestOperationsTestSuite`
280+
281+
1. Set the test mode to "live" using:
282+
`$ENV:AZURE_RECORD_MODE="live"`
283+
2. Once tests pass, switch to "playback" mode and ensure all tests pass in both modes.
284+
3. Push the final assets with `test-proxy push --assets-json-path assets.json`.
285+
141286
### Scrubbing Secrets
142287

143288
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.

0 commit comments

Comments
 (0)