forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockclients.go
210 lines (182 loc) · 5.67 KB
/
mockclients.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package armhelpers
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/Azure/go-autorest/autorest"
)
//MockACSEngineClient is an implemetnation of ACSEngineClient where all requests error out
type MockACSEngineClient struct {
FailDeployTemplate bool
FailEnsureResourceGroup bool
FailListVirtualMachines bool
FailGetVirtualMachine bool
FailDeleteVirtualMachine bool
FailGetStorageClient bool
FailDeleteNetworkInterface bool
}
//DeployTemplate mock
func (mc *MockACSEngineClient) DeployTemplate(resourceGroup, name string, template, parameters map[string]interface{}, cancel <-chan struct{}) (*resources.DeploymentExtended, error) {
if mc.FailDeployTemplate {
return nil, fmt.Errorf("DeployTemplate failed")
}
return nil, nil
}
//EnsureResourceGroup mock
func (mc *MockACSEngineClient) EnsureResourceGroup(resourceGroup, location string) (*resources.Group, error) {
if mc.FailEnsureResourceGroup {
return nil, fmt.Errorf("EnsureResourceGroup failed")
}
return nil, nil
}
//ListVirtualMachines mock
func (mc *MockACSEngineClient) ListVirtualMachines(resourceGroup string) (compute.VirtualMachineListResult, error) {
if mc.FailListVirtualMachines {
return compute.VirtualMachineListResult{}, fmt.Errorf("ListVirtualMachines failed")
}
vm1Name := "k8s-master-12345678-0"
creationSourceString := "creationSource"
orchestratorString := "orchestrator"
resourceNameSuffixString := "resourceNameSuffix"
creationSource := "acsengine-k8s-master-12345678-0"
orchestrator := "Kubernetes:1.5.3"
resourceNameSuffix := "12345678"
tags := map[string]*string{
creationSourceString: &creationSource,
orchestratorString: &orchestrator,
resourceNameSuffixString: &resourceNameSuffix,
}
vm1 := compute.VirtualMachine{
Name: &vm1Name,
Tags: &tags,
VirtualMachineProperties: &compute.VirtualMachineProperties{
StorageProfile: &compute.StorageProfile{
OsDisk: &compute.OSDisk{
Vhd: &compute.VirtualHardDisk{
URI: &validOSDiskResourceName},
},
},
NetworkProfile: &compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{
{
ID: &validNicResourceName,
},
},
},
},
}
vmr := compute.VirtualMachineListResult{}
vmr.Value = &[]compute.VirtualMachine{vm1}
return vmr, nil
}
//GetVirtualMachine mock
func (mc *MockACSEngineClient) GetVirtualMachine(resourceGroup, name string) (compute.VirtualMachine, error) {
if mc.FailGetVirtualMachine {
return compute.VirtualMachine{}, fmt.Errorf("GetVirtualMachine failed")
}
vm1Name := "k8s-master-12345678-0"
creationSourceString := "creationSource"
orchestratorString := "orchestrator"
resourceNameSuffixString := "resourceNameSuffix"
creationSource := "acsengine-k8s-master-12345678-0"
orchestrator := "Kubernetes:1.5.3"
resourceNameSuffix := "12345678"
tags := map[string]*string{
creationSourceString: &creationSource,
orchestratorString: &orchestrator,
resourceNameSuffixString: &resourceNameSuffix,
}
return compute.VirtualMachine{
Name: &vm1Name,
Tags: &tags,
VirtualMachineProperties: &compute.VirtualMachineProperties{
StorageProfile: &compute.StorageProfile{
OsDisk: &compute.OSDisk{
Vhd: &compute.VirtualHardDisk{
URI: &validOSDiskResourceName},
},
},
NetworkProfile: &compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{
{
ID: &validNicResourceName,
},
},
},
},
}, nil
}
//DeleteVirtualMachine mock
func (mc *MockACSEngineClient) DeleteVirtualMachine(resourceGroup, name string, cancel <-chan struct{}) (<-chan compute.OperationStatusResponse, <-chan error) {
if mc.FailDeleteVirtualMachine {
errChan := make(chan error)
respChan := make(chan compute.OperationStatusResponse)
go func() {
defer func() {
close(errChan)
}()
defer func() {
close(respChan)
}()
errChan <- fmt.Errorf("DeleteVirtualMachine failed")
time.Sleep(1 * time.Second)
}()
return respChan, errChan
}
errChan := make(chan error)
respChan := make(chan compute.OperationStatusResponse)
go func() {
defer func() {
close(errChan)
}()
defer func() {
close(respChan)
}()
errChan <- nil
respChan <- compute.OperationStatusResponse{}
time.Sleep(1 * time.Second)
}()
return respChan, errChan
}
//GetStorageClient mock
func (mc *MockACSEngineClient) GetStorageClient(resourceGroup, accountName string) (ACSStorageClient, error) {
if mc.FailGetStorageClient {
return nil, fmt.Errorf("GetStorageClient failed")
}
return &MockStorageClient{}, nil
}
//DeleteNetworkInterface mock
func (mc *MockACSEngineClient) DeleteNetworkInterface(resourceGroup, nicName string, cancel <-chan struct{}) (<-chan autorest.Response, <-chan error) {
if mc.FailDeleteNetworkInterface {
errChan := make(chan error)
respChan := make(chan autorest.Response)
go func() {
defer func() {
close(errChan)
}()
defer func() {
close(respChan)
}()
errChan <- fmt.Errorf("DeleteNetworkInterface failed")
time.Sleep(1 * time.Second)
}()
return respChan, errChan
}
errChan := make(chan error)
respChan := make(chan autorest.Response)
go func() {
defer func() {
close(errChan)
}()
defer func() {
close(respChan)
}()
errChan <- nil
respChan <- autorest.Response{}
time.Sleep(1 * time.Second)
}()
return respChan, errChan
}
var validOSDiskResourceName = "https://00k71r4u927seqiagnt0.blob.core.windows.net/osdisk/k8s-agentpool1-12345678-0-osdisk.vhd"
var validNicResourceName = "/subscriptions/DEC923E3-1EF1-4745-9516-37906D56DEC4/resourceGroups/acsK8sTest/providers/Microsoft.Network/networkInterfaces/k8s-agent-12345678-nic-0"