This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 560
/
mockclients.go
680 lines (587 loc) · 24.4 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
package armhelpers
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/Azure/azure-sdk-for-go/services/authorization/mgmt/2015-07-01/authorization"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/azure-sdk-for-go/services/preview/msi/mgmt/2015-08-31-preview/msi"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/go-autorest/autorest"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
)
//MockACSEngineClient is an implementation of ACSEngineClient where all requests error out
type MockACSEngineClient struct {
FailDeployTemplate bool
FailDeployTemplateQuota bool
FailDeployTemplateConflict bool
FailDeployTemplateWithProperties bool
FailEnsureResourceGroup bool
FailListVirtualMachines bool
FailListVirtualMachineScaleSets bool
FailGetVirtualMachine bool
FailDeleteVirtualMachine bool
FailDeleteVirtualMachineScaleSetVM bool
FailSetVirtualMachineScaleSetCapacity bool
FailListVirtualMachineScaleSetVMs bool
FailGetStorageClient bool
FailDeleteNetworkInterface bool
FailGetKubernetesClient bool
FailListProviders bool
ShouldSupportVMIdentity bool
FailDeleteRoleAssignment bool
MockKubernetesClient *MockKubernetesClient
}
//MockStorageClient mock implementation of StorageClient
type MockStorageClient struct{}
//MockKubernetesClient mock implementation of KubernetesClient
type MockKubernetesClient struct {
FailListPods bool
FailGetNode bool
UpdateNodeFunc func(*v1.Node) (*v1.Node, error)
FailUpdateNode bool
FailDeleteNode bool
FailSupportEviction bool
FailDeletePod bool
FailEvictPod bool
FailWaitForDelete bool
ShouldSupportEviction bool
PodsList *v1.PodList
}
// MockVirtualMachineListResultPage contains a page of VirtualMachine values.
type MockVirtualMachineListResultPage struct {
Fn func(compute.VirtualMachineListResult) (compute.VirtualMachineListResult, error)
Vmlr compute.VirtualMachineListResult
}
// Next advances to the next page of values. If there was an error making
// the request the page does not advance and the error is returned.
func (page *MockVirtualMachineListResultPage) Next() error {
next, err := page.Fn(page.Vmlr)
if err != nil {
return err
}
page.Vmlr = next
return nil
}
// NotDone returns true if the page enumeration should be started or is not yet complete.
func (page MockVirtualMachineListResultPage) NotDone() bool {
return !page.Vmlr.IsEmpty()
}
// Response returns the raw server response from the last page request.
func (page MockVirtualMachineListResultPage) Response() compute.VirtualMachineListResult {
return page.Vmlr
}
// Values returns the slice of values for the current page or nil if there are no values.
func (page MockVirtualMachineListResultPage) Values() []compute.VirtualMachine {
if page.Vmlr.IsEmpty() {
return nil
}
return *page.Vmlr.Value
}
// MockDeploymentOperationsListResultPage contains a page of DeploymentOperation values.
type MockDeploymentOperationsListResultPage struct {
Fn func(resources.DeploymentOperationsListResult) (resources.DeploymentOperationsListResult, error)
Dolr resources.DeploymentOperationsListResult
}
// Next advances to the next page of values. If there was an error making
// the request the page does not advance and the error is returned.
func (page *MockDeploymentOperationsListResultPage) Next() error {
next, err := page.Fn(page.Dolr)
if err != nil {
return err
}
page.Dolr = next
return nil
}
// NotDone returns true if the page enumeration should be started or is not yet complete.
func (page MockDeploymentOperationsListResultPage) NotDone() bool {
return !page.Dolr.IsEmpty()
}
// Response returns the raw server response from the last page request.
func (page MockDeploymentOperationsListResultPage) Response() resources.DeploymentOperationsListResult {
return page.Dolr
}
// Values returns the slice of values for the current page or nil if there are no values.
func (page MockDeploymentOperationsListResultPage) Values() []resources.DeploymentOperation {
if page.Dolr.IsEmpty() {
return nil
}
return *page.Dolr.Value
}
// MockRoleAssignmentListResultPage contains a page of RoleAssignment values.
type MockRoleAssignmentListResultPage struct {
Fn func(authorization.RoleAssignmentListResult) (authorization.RoleAssignmentListResult, error)
Ralr authorization.RoleAssignmentListResult
}
// Next advances to the next page of values. If there was an error making
// the request the page does not advance and the error is returned.
func (page *MockRoleAssignmentListResultPage) Next() error {
next, err := page.Fn(page.Ralr)
if err != nil {
return err
}
page.Ralr = next
return nil
}
// NotDone returns true if the page enumeration should be started or is not yet complete.
func (page MockRoleAssignmentListResultPage) NotDone() bool {
return !page.Ralr.IsEmpty()
}
// Response returns the raw server response from the last page request.
func (page MockRoleAssignmentListResultPage) Response() authorization.RoleAssignmentListResult {
return page.Ralr
}
// Values returns the slice of values for the current page or nil if there are no values.
func (page MockRoleAssignmentListResultPage) Values() []authorization.RoleAssignment {
if page.Ralr.IsEmpty() {
return nil
}
return *page.Ralr.Value
}
//ListPods returns all Pods running on the passed in node
func (mkc *MockKubernetesClient) ListPods(node *v1.Node) (*v1.PodList, error) {
if mkc.FailListPods {
return nil, errors.New("ListPods failed")
}
if mkc.PodsList != nil {
return mkc.PodsList, nil
}
return &v1.PodList{}, nil
}
//GetNode returns details about node with passed in name
func (mkc *MockKubernetesClient) GetNode(name string) (*v1.Node, error) {
if mkc.FailGetNode {
return nil, errors.New("GetNode failed")
}
node := &v1.Node{}
node.Status.Conditions = append(node.Status.Conditions, v1.NodeCondition{Type: v1.NodeReady, Status: v1.ConditionTrue})
return node, nil
}
//UpdateNode updates the node in the api server with the passed in info
func (mkc *MockKubernetesClient) UpdateNode(node *v1.Node) (*v1.Node, error) {
if mkc.UpdateNodeFunc != nil {
return mkc.UpdateNodeFunc(node)
}
if mkc.FailUpdateNode {
return nil, errors.New("UpdateNode failed")
}
return node, nil
}
//DeleteNode deregisters node in the api server
func (mkc *MockKubernetesClient) DeleteNode(name string) error {
if mkc.FailDeleteNode {
return errors.New("DeleteNode failed")
}
return nil
}
//SupportEviction queries the api server to discover if it supports eviction, and returns supported type if it is supported
func (mkc *MockKubernetesClient) SupportEviction() (string, error) {
if mkc.FailSupportEviction {
return "", errors.New("SupportEviction failed")
}
if mkc.ShouldSupportEviction {
return "version", nil
}
return "", nil
}
//DeletePod deletes the passed in pod
func (mkc *MockKubernetesClient) DeletePod(pod *v1.Pod) error {
if mkc.FailDeletePod {
return errors.New("DeletePod failed")
}
return nil
}
//EvictPod evicts the passed in pod using the passed in api version
func (mkc *MockKubernetesClient) EvictPod(pod *v1.Pod, policyGroupVersion string) error {
if mkc.FailEvictPod {
return errors.New("EvictPod failed")
}
return nil
}
//WaitForDelete waits until all pods are deleted. Returns all pods not deleted and an error on failure
func (mkc *MockKubernetesClient) WaitForDelete(logger *log.Entry, pods []v1.Pod, usingEviction bool) ([]v1.Pod, error) {
if mkc.FailWaitForDelete {
return nil, errors.New("WaitForDelete failed")
}
return []v1.Pod{}, nil
}
//DeleteBlob mock
func (msc *MockStorageClient) DeleteBlob(container, blob string) error {
return nil
}
//AddAcceptLanguages mock
func (mc *MockACSEngineClient) AddAcceptLanguages(languages []string) {}
// AddAuxiliaryTokens mock
func (mc *MockACSEngineClient) AddAuxiliaryTokens(tokens []string) {}
//DeployTemplate mock
func (mc *MockACSEngineClient) DeployTemplate(ctx context.Context, resourceGroup, name string, template, parameters map[string]interface{}) (de resources.DeploymentExtended, err error) {
switch {
case mc.FailDeployTemplate:
return de, errors.New("DeployTemplate failed")
case mc.FailDeployTemplateQuota:
errmsg := `resources.DeploymentsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error.`
resp := `{
"error":{
"code":"InvalidTemplateDeployment",
"message":"The template deployment is not valid according to the validation procedure. The tracking id is 'b5bd7d6b-fddf-4ec3-a3b0-ce285a48bd31'. See inner errors for details. Please see https://aka.ms/arm-deploy for usage details.",
"details":[{
"code":"QuotaExceeded",
"message":"Operation results in exceeding quota limits of Core. Maximum allowed: 10, Current in use: 10, Additional requested: 2. Please read more about quota increase at http://aka.ms/corequotaincrease."
}]}}`
return resources.DeploymentExtended{
Response: autorest.Response{
Response: &http.Response{
Status: "400 Bad Request",
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewReader([]byte(resp))),
}}},
errors.New(errmsg)
case mc.FailDeployTemplateConflict:
errmsg := `resources.DeploymentsClient#CreateOrUpdate: Failure sending request: StatusCode=200 -- Original Error: Long running operation terminated with status 'Failed': Code="DeploymentFailed" Message="At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.`
resp := `{
"status":"Failed",
"error":{
"code":"DeploymentFailed",
"message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.",
"details":[{
"code":"Conflict",
"message":"{\r\n \"error\": {\r\n \"code\": \"PropertyChangeNotAllowed\",\r\n \"target\": \"dataDisk.createOption\",\r\n \"message\": \"Changing property 'dataDisk.createOption' is not allowed.\"\r\n }\r\n}"
}]}}`
return resources.DeploymentExtended{
Response: autorest.Response{
Response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte(resp))),
}}},
errors.New(errmsg)
case mc.FailDeployTemplateWithProperties:
errmsg := `resources.DeploymentsClient#CreateOrUpdate: Failure sending request: StatusCode=200 -- Original Error: Long running operation terminated with status 'Failed': Code="DeploymentFailed" Message="At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.`
resp := `{
"status":"Failed",
"error":{
"code":"DeploymentFailed",
"message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.",
"details":[{
"code":"Conflict",
"message":"{\r\n \"error\": {\r\n \"code\": \"PropertyChangeNotAllowed\",\r\n \"target\": \"dataDisk.createOption\",\r\n \"message\": \"Changing property 'dataDisk.createOption' is not allowed.\"\r\n }\r\n}"
}]}}`
provisioningState := "Failed"
return resources.DeploymentExtended{
Response: autorest.Response{
Response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte(resp))),
}},
Properties: &resources.DeploymentPropertiesExtended{
ProvisioningState: &provisioningState,
}},
errors.New(errmsg)
default:
return de, nil
}
}
//EnsureResourceGroup mock
func (mc *MockACSEngineClient) EnsureResourceGroup(ctx context.Context, resourceGroup, location string, managedBy *string) (*resources.Group, error) {
if mc.FailEnsureResourceGroup {
return nil, errors.New("EnsureResourceGroup failed")
}
return nil, nil
}
//ListVirtualMachines mock
func (mc *MockACSEngineClient) ListVirtualMachines(ctx context.Context, resourceGroup string) (VirtualMachineListResultPage, error) {
if mc.FailListVirtualMachines {
return &MockVirtualMachineListResultPage{
Vmlr: compute.VirtualMachineListResult{
Value: &[]compute.VirtualMachine{{}},
},
}, errors.New("ListVirtualMachines failed")
}
vm1Name := "k8s-agentpool1-12345678-0"
creationSourceString := "creationSource"
orchestratorString := "orchestrator"
resourceNameSuffixString := "resourceNameSuffix"
poolnameString := "poolName"
creationSource := "acsengine-k8s-agentpool1-12345678-0"
orchestrator := "Kubernetes:1.6.9"
resourceNameSuffix := "12345678"
poolname := "agentpool1"
tags := map[string]*string{
creationSourceString: &creationSource,
orchestratorString: &orchestrator,
resourceNameSuffixString: &resourceNameSuffix,
poolnameString: &poolname,
}
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 &MockVirtualMachineListResultPage{
Fn: func(lastResults compute.VirtualMachineListResult) (compute.VirtualMachineListResult, error) {
return compute.VirtualMachineListResult{}, nil
},
Vmlr: vmr,
}, nil
}
//ListVirtualMachineScaleSets mock
func (mc *MockACSEngineClient) ListVirtualMachineScaleSets(ctx context.Context, resourceGroup string) (compute.VirtualMachineScaleSetListResultPage, error) {
if mc.FailListVirtualMachineScaleSets {
return compute.VirtualMachineScaleSetListResultPage{}, errors.New("ListVirtualMachines failed")
}
return compute.VirtualMachineScaleSetListResultPage{}, nil
}
//GetVirtualMachine mock
func (mc *MockACSEngineClient) GetVirtualMachine(ctx context.Context, resourceGroup, name string) (compute.VirtualMachine, error) {
if mc.FailGetVirtualMachine {
return compute.VirtualMachine{}, errors.New("GetVirtualMachine failed")
}
vm1Name := "k8s-agentpool1-12345678-0"
creationSourceString := "creationSource"
orchestratorString := "orchestrator"
resourceNameSuffixString := "resourceNameSuffix"
poolnameString := "poolName"
creationSource := "acsengine-k8s-agentpool1-12345678-0"
orchestrator := "Kubernetes:1.6.9"
resourceNameSuffix := "12345678"
poolname := "agentpool1"
principalID := "00000000-1111-2222-3333-444444444444"
tags := map[string]*string{
creationSourceString: &creationSource,
orchestratorString: &orchestrator,
resourceNameSuffixString: &resourceNameSuffix,
poolnameString: &poolname,
}
var vmIdentity *compute.VirtualMachineIdentity
if mc.ShouldSupportVMIdentity {
vmIdentity = &compute.VirtualMachineIdentity{PrincipalID: &principalID}
}
return compute.VirtualMachine{
Name: &vm1Name,
Tags: tags,
Identity: vmIdentity,
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(ctx context.Context, resourceGroup, name string) error {
if mc.FailDeleteVirtualMachine {
return errors.New("DeleteVirtualMachine failed")
}
return nil
}
//DeleteVirtualMachineScaleSetVM mock
func (mc *MockACSEngineClient) DeleteVirtualMachineScaleSetVM(ctx context.Context, resourceGroup, virtualMachineScaleSet, instanceID string) error {
if mc.FailDeleteVirtualMachineScaleSetVM {
return errors.New("DeleteVirtualMachineScaleSetVM failed")
}
return nil
}
//SetVirtualMachineScaleSetCapacity mock
func (mc *MockACSEngineClient) SetVirtualMachineScaleSetCapacity(ctx context.Context, resourceGroup, virtualMachineScaleSet string, sku compute.Sku, location string) error {
if mc.FailSetVirtualMachineScaleSetCapacity {
return errors.New("SetVirtualMachineScaleSetCapacity failed")
}
return nil
}
//ListVirtualMachineScaleSetVMs mock
func (mc *MockACSEngineClient) ListVirtualMachineScaleSetVMs(ctx context.Context, resourceGroup, virtualMachineScaleSet string) (compute.VirtualMachineScaleSetVMListResultPage, error) {
if mc.FailDeleteVirtualMachineScaleSetVM {
return compute.VirtualMachineScaleSetVMListResultPage{}, errors.New("DeleteVirtualMachineScaleSetVM failed")
}
return compute.VirtualMachineScaleSetVMListResultPage{}, nil
}
//GetStorageClient mock
func (mc *MockACSEngineClient) GetStorageClient(ctx context.Context, resourceGroup, accountName string) (ACSStorageClient, error) {
if mc.FailGetStorageClient {
return nil, errors.New("GetStorageClient failed")
}
return &MockStorageClient{}, nil
}
//DeleteNetworkInterface mock
func (mc *MockACSEngineClient) DeleteNetworkInterface(ctx context.Context, resourceGroup, nicName string) error {
if mc.FailDeleteNetworkInterface {
return errors.New("DeleteNetworkInterface failed")
}
return nil
}
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"
// Active Directory
// Mocks
// Graph Mocks
// CreateGraphApplication creates an application via the graphrbac client
func (mc *MockACSEngineClient) CreateGraphApplication(ctx context.Context, applicationCreateParameters graphrbac.ApplicationCreateParameters) (graphrbac.Application, error) {
return graphrbac.Application{}, nil
}
// CreateGraphPrincipal creates a service principal via the graphrbac client
func (mc *MockACSEngineClient) CreateGraphPrincipal(ctx context.Context, servicePrincipalCreateParameters graphrbac.ServicePrincipalCreateParameters) (graphrbac.ServicePrincipal, error) {
return graphrbac.ServicePrincipal{}, nil
}
// CreateApp is a simpler method for creating an application
func (mc *MockACSEngineClient) CreateApp(ctx context.Context, applicationName, applicationURL string, replyURLs *[]string, requiredResourceAccess *[]graphrbac.RequiredResourceAccess) (applicationID, servicePrincipalObjectID, secret string, err error) {
return "app-id", "client-id", "client-secret", nil
}
// User Assigned MSI
//CreateUserAssignedID - Creates a user assigned msi.
func (mc *MockACSEngineClient) CreateUserAssignedID(location string, resourceGroup string, userAssignedID string) (*msi.Identity, error) {
return &msi.Identity{}, nil
}
// RBAC Mocks
// CreateRoleAssignment creates a role assignment via the authorization client
func (mc *MockACSEngineClient) CreateRoleAssignment(ctx context.Context, scope string, roleAssignmentName string, parameters authorization.RoleAssignmentCreateParameters) (authorization.RoleAssignment, error) {
return authorization.RoleAssignment{}, nil
}
// CreateRoleAssignmentSimple is a wrapper around RoleAssignmentsClient.Create
func (mc *MockACSEngineClient) CreateRoleAssignmentSimple(ctx context.Context, applicationID, roleID string) error {
return nil
}
// DeleteManagedDisk is a wrapper around disksClient.Delete
func (mc *MockACSEngineClient) DeleteManagedDisk(ctx context.Context, resourceGroupName string, diskName string) error {
return nil
}
// ListManagedDisksByResourceGroup is a wrapper around disksClient.ListManagedDisksByResourceGroup
func (mc *MockACSEngineClient) ListManagedDisksByResourceGroup(ctx context.Context, resourceGroupName string) (result compute.DiskListPage, err error) {
return compute.DiskListPage{}, nil
}
//GetKubernetesClient mock
func (mc *MockACSEngineClient) GetKubernetesClient(masterURL, kubeConfig string, interval, timeout time.Duration) (KubernetesClient, error) {
if mc.FailGetKubernetesClient {
return nil, errors.New("GetKubernetesClient failed")
}
if mc.MockKubernetesClient == nil {
mc.MockKubernetesClient = &MockKubernetesClient{}
}
return mc.MockKubernetesClient, nil
}
// ListProviders mock
func (mc *MockACSEngineClient) ListProviders(ctx context.Context) (resources.ProviderListResultPage, error) {
if mc.FailListProviders {
return resources.ProviderListResultPage{}, errors.New("ListProviders failed")
}
return resources.ProviderListResultPage{}, nil
}
// ListDeploymentOperations gets all deployments operations for a deployment.
func (mc *MockACSEngineClient) ListDeploymentOperations(ctx context.Context, resourceGroupName string, deploymentName string, top *int32) (result DeploymentOperationsListResultPage, err error) {
resp := `{
"properties": {
"provisioningState":"Failed",
"correlationId":"d5062e45-6e9f-4fd3-a0a0-6b2c56b15757",
"error":{
"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see http://aka.ms/arm-debug for usage details.",
"details":[{"code":"Conflict","message":"{\r\n \"error\": {\r\n \"message\": \"Conflict\",\r\n \"code\": \"Conflict\"\r\n }\r\n}"}]
}
}
}`
provisioningState := "Failed"
id := "00000000"
operationID := "d5062e45-6e9f-4fd3-a0a0-6b2c56b15757"
nextLink := fmt.Sprintf("https://management.azure.com/subscriptions/11111/resourcegroups/%s/deployments/%s/operations?$top=%s&api-version=2018-05-01", resourceGroupName, deploymentName, "5")
return &MockDeploymentOperationsListResultPage{
Fn: func(lastResults resources.DeploymentOperationsListResult) (result resources.DeploymentOperationsListResult, err error) {
if lastResults.NextLink != nil {
return resources.DeploymentOperationsListResult{
Response: autorest.Response{
Response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte(resp))),
},
},
Value: &[]resources.DeploymentOperation{
{
ID: &id,
OperationID: &operationID,
Properties: &resources.DeploymentOperationProperties{
ProvisioningState: &provisioningState,
},
},
},
}, nil
}
return resources.DeploymentOperationsListResult{}, nil
},
Dolr: resources.DeploymentOperationsListResult{
Response: autorest.Response{
Response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader([]byte(resp))),
},
},
Value: &[]resources.DeploymentOperation{
{
ID: &id,
OperationID: &operationID,
Properties: &resources.DeploymentOperationProperties{
ProvisioningState: &provisioningState,
},
},
},
NextLink: &nextLink,
},
}, nil
}
// ListDeploymentOperationsNextResults retrieves the next set of results, if any.
func (mc *MockACSEngineClient) ListDeploymentOperationsNextResults(lastResults resources.DeploymentOperationsListResult) (result resources.DeploymentOperationsListResult, err error) {
return resources.DeploymentOperationsListResult{}, nil
}
// DeleteRoleAssignmentByID deletes a roleAssignment via its unique identifier
func (mc *MockACSEngineClient) DeleteRoleAssignmentByID(ctx context.Context, roleAssignmentID string) (authorization.RoleAssignment, error) {
if mc.FailDeleteRoleAssignment {
return authorization.RoleAssignment{}, errors.New("DeleteRoleAssignmentByID failed")
}
return authorization.RoleAssignment{}, nil
}
// ListRoleAssignmentsForPrincipal (e.g. a VM) via the scope and the unique identifier of the principal
func (mc *MockACSEngineClient) ListRoleAssignmentsForPrincipal(ctx context.Context, scope string, principalID string) (RoleAssignmentListResultPage, error) {
roleAssignments := []authorization.RoleAssignment{}
if mc.ShouldSupportVMIdentity {
var assignmentID = "role-assignment-id"
var assignment = authorization.RoleAssignment{
ID: &assignmentID}
roleAssignments = append(roleAssignments, assignment)
}
return &MockRoleAssignmentListResultPage{
Ralr: authorization.RoleAssignmentListResult{
Value: &roleAssignments,
},
}, nil
}