-
Notifications
You must be signed in to change notification settings - Fork 815
/
fake_example_test.go
79 lines (62 loc) · 2.87 KB
/
fake_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//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 armcompute_test
import (
"context"
"fmt"
"log"
"net/http"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6/fake"
)
func ExampleVirtualMachinesServer() {
// first, create an instance of the fake server for the client you wish to test.
// the type name of the server will be similar to the corresponding client, with
// the suffix "Server" instead of "Client".
fakeVirtualMachinesServer := fake.VirtualMachinesServer{
// next, provide implementations for the APIs you wish to fake.
// this fake corresponds to the VirtualMachinesClient.Get() API.
Get: func(ctx context.Context, resourceGroupName, vmName string, options *armcompute.VirtualMachinesClientGetOptions) (resp azfake.Responder[armcompute.VirtualMachinesClientGetResponse], errResp azfake.ErrorResponder) {
// the values of ctx, resourceGroupName, vmName, and options come from the API call.
// the named return values resp and errResp are used to construct the response
// and are meant to be mutually exclusive. if both responses have been constructed,
// the error response is selected.
// construct the response type, populating fields as required
vmResp := armcompute.VirtualMachinesClientGetResponse{}
vmResp.ID = to.Ptr("/fake/resource/id")
// use resp to set the desired response
resp.SetResponse(http.StatusOK, vmResp, nil)
// to simulate the failure case, use errResp
// errResp.SetResponseError(http.StatusBadRequest, "ThisIsASimulatedError")
return
},
}
// now create the corresponding client, connecting the fake server via the client options
client, err := armcompute.NewVirtualMachinesClient("subscriptionID", &azfake.TokenCredential{}, &arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Transport: fake.NewVirtualMachinesServerTransport(&fakeVirtualMachinesServer),
},
})
if err != nil {
log.Fatal(err)
}
// call the API. the provided values will be passed to the fake's implementation.
// the response or error values returned by the API call are from the fake.
resp, err := client.Get(context.TODO(), "fakeResourceGroup", "fakeVM", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(*resp.ID)
// APIs that haven't been faked will return an error
_, err = client.BeginDeallocate(context.TODO(), "fakeResourceGroup", "fakeVM", nil)
fmt.Println(err.Error())
// Output:
// /fake/resource/id
// fake for method BeginDeallocate not implemented
}