• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..30-Aug-2021-

README.mdH A D30-Aug-20216.2 KiB167129

recording.goH A D30-Aug-202112.1 KiB436319

recording_test.goH A D30-Aug-202111.4 KiB375254

request_matcher.goH A D30-Aug-20215.7 KiB183142

request_matcher_test.goH A D30-Aug-20216.3 KiB211145

sanitizer.goH A D30-Aug-20212.7 KiB8956

sanitizer_test.goH A D30-Aug-20214.5 KiB165126

testcontext.goH A D30-Aug-20211 KiB5232

README.md

1# Azure SDK for Go Recorded Test Framework
2
3[![Build Status](https://dev.azure.com/azure-sdk/public/_apis/build/status/go/Azure.azure-sdk-for-go?branchName=master)](https://dev.azure.com/azure-sdk/public/_build/latest?definitionId=1842&branchName=master)
4
5The `testframework` package makes it easy to add recorded tests to your track-2 client package.
6Below are some examples that walk through setting up a recorded test end to end.
7
8## Examples
9
10### Initializing a Recording instance for a test
11
12The first step in instrumenting a client to interact with recorded tests is to create a `TestContext`.
13This acts as the interface between the recorded test framework and your chosen test package.
14In these examples we'll use testify's [assert](https://pkg.go.dev/github.com/stretchr/testify/assert),
15but you can use the framework of your choice.
16
17In the snippet below, demonstrates an example test setup func in which we are initializing the `TestContext`
18with the methods that will be invoked when your recorded test needs to Log, Fail, get the Name of the test,
19or indicate that the test IsFailed.
20
21***Note**: an instance of TestContext should be initialized for each test.*
22
23```go
24type testState struct {
25    recording *recording.Recording
26    client    *TableServiceClient
27    context   *recording.TestContext
28}
29// a map to store our created test contexts
30var clientsMap map[string]*testState = make(map[string]*testState)
31
32// recordedTestSetup is called before each test execution by the test suite's BeforeTest method
33func recordedTestSetup(t *testing.T, testName string, mode recording.RecordMode) {
34    var accountName string
35    var suffix string
36    var cred *SharedKeyCredential
37    var secret string
38    var uri string
39    assert := assert.New(t)
40
41    // init the test framework
42    context := recording.NewTestContext(func(msg string) { assert.FailNow(msg) }, func(msg string) { t.Log(msg) }, func() string { return testName })
43    //mode should be recording.Playback. This will automatically record if no test recording is available and playback if it is.
44    recording, err := recording.NewRecording(context, mode)
45    assert.Nil(err)
46```
47
48After creating the TestContext, it must be passed to a new instance of `Recording` along with the current test mode.
49`Recording` is the main component of the testframework package.
50
51```go
52//func recordedTestSetup(t *testing.T, testName string, mode recording.RecordMode) {
53//  <...>
54    record, err := recording.NewRecording(context, mode)
55    assert.Nil(err)
56```
57
58### Initializing recorded variables
59
60A key component to recorded tests is recorded variables.
61They allow creation of values that stay with the test recording so that playback of service operations is consistent.
62
63In the snippet below we are calling `GetRecordedVariable` to acquire details such as the service account name and
64client secret to configure the client.
65
66```go
67//func recordedTestSetup(t *testing.T, testName string, mode recording.RecordMode) {
68//  <...>
69    accountName, err := record.GetEnvVar(storageAccountNameEnvVar, recording.NoSanitization)
70    suffix := record.GetOptionalEnvVar(storageEndpointSuffixEnvVar, DefaultStorageSuffix, recording.NoSanitization)
71    secret, err := record.GetEnvVar(storageAccountKeyEnvVar, recording.Secret_Base64String)
72    cred, _ := NewSharedKeyCredential(accountName, secret)
73    uri := storageURI(accountName, suffix)
74```
75
76The last step is to instrument your client by replacing its transport with your `Recording` instance.
77`Recording` satisfies the `azcore.Transport` interface.
78
79```go
80//func recordedTestSetup(t *testing.T, testName string, mode recording.RecordMode) {
81//  <...>
82    // Set our client's HTTPClient to our recording instance.
83    // Optionally, we can also configure MaxRetries to -1 to avoid the default retry behavior.
84    client, err := NewTableServiceClient(uri, cred, &TableClientOptions{HTTPClient: recording, Retry: azcore.RetryOptions{MaxRetries: -1}})
85    assert.Nil(err)
86
87    // either return your client instance, or store it somewhere that your test can use it for test execution.
88    clientsMap[testName] = &testState{client: client, recording: recording, context: &context}
89}
90
91
92func getTestState(key string) *testState {
93    return clientsMap[key]
94}
95```
96
97### Completing the recorded test session
98
99After the test run completes we need to signal the `Recording` instance to save the recording.
100
101```go
102// recordedTestTeardown fetches the context from our map based on test name and calls Stop on the Recording instance.
103func recordedTestTeardown(key string) {
104    context, ok := clientsMap[key]
105    if ok && !(*context.context).IsFailed() {
106        context.recording.Stop()
107    }
108}
109```
110
111### Setting up a test to use our Recording instance
112
113Test frameworks like testify suite allow for configuration of a `BeforeTest` method to be executed before each test.
114We can use this to call our `recordedTestSetup` method
115
116Below is an example test setup which executes a single test.
117
118```go
119package aztable
120
121import (
122    "errors"
123    "fmt"
124    "net/http"
125    "testing"
126
127    "github.com/Azure/azure-sdk-for-go/sdk/internal/runtime"
128    "github.com/Azure/azure-sdk-for-go/sdk/internal/testframework"
129    "github.com/stretchr/testify/assert"
130    "github.com/stretchr/testify/suite"
131)
132
133type tableServiceClientLiveTests struct {
134    suite.Suite
135    mode         recording.RecordMode
136}
137
138// Hookup to the testing framework
139func TestServiceClient_Storage(t *testing.T) {
140    storage := tableServiceClientLiveTests{mode: recording.Playback /* change to Record to re-record tests */}
141    suite.Run(t, &storage)
142}
143
144func (s *tableServiceClientLiveTests) TestCreateTable() {
145    assert := assert.New(s.T())
146    context := getTestState(s.T().Name())
147    // generate a random recorded value for our table name.
148    tableName, err := context.recording.GenerateAlphaNumericID(tableNamePrefix, 20, true)
149
150    resp, err := context.client.Create(ctx, tableName)
151    defer context.client.Delete(ctx, tableName)
152
153    assert.Nil(err)
154    assert.Equal(*resp.TableResponse.TableName, tableName)
155}
156
157func (s *tableServiceClientLiveTests) BeforeTest(suite string, test string) {
158    // setup the test environment
159    recordedTestSetup(s.T(), s.T().Name(), s.mode)
160}
161
162func (s *tableServiceClientLiveTests) AfterTest(suite string, test string) {
163    // teardown the test context
164    recordedTestTeardown(s.T().Name())
165}
166```
167