/
disk_manager_mock.go
198 lines (168 loc) · 4.82 KB
/
disk_manager_mock.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
/*
* Copyright (C) 2017-2019 Alibaba Group Holding Limited
*/
package mock
import (
"bosh-alicloud-cpi/alicloud"
"fmt"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
const (
MaxDiskSizeGB = 32768
)
type DiskManagerMock struct {
mc *TestContext
}
func NewDiskManagerMock(mc TestContext) alicloud.DiskManager {
return DiskManagerMock{&mc}
}
func (a DiskManagerMock) GetDisks(instCid string) ([]ecs.Disk, error) {
r := []ecs.Disk{}
for _, d := range a.mc.Disks {
if d.InstanceId == instCid {
r = append(r, *d)
}
}
return r, nil
}
func (a DiskManagerMock) GetDisk(diskCid string) (*ecs.Disk, error) {
if d, ok := a.mc.Disks[diskCid]; ok {
return d, nil
} else {
return nil, nil
}
}
func (a DiskManagerMock) CreateDisk(region string, args *ecs.CreateDiskRequest) (string, error) {
size, err := strconv.Atoi(string(args.Size))
if err != nil {
return "", fmt.Errorf("convert size %s got an error: %#v.", args.Size, err)
}
if size < 5 || size > MaxDiskSizeGB {
return "", fmt.Errorf("CreateDisk size too small or large %d", args.Size)
}
if args.ZoneId == "" {
return "", fmt.Errorf("CreateDisk zone can't be empty")
}
if args.DiskCategory == "" {
return "", fmt.Errorf("CreateDisk category empty")
}
id, d := a.mc.NewDisk("")
d.Size = size
d.RegionId = args.RegionId
d.ZoneId = args.ZoneId
d.Status = string(alicloud.DiskStatusAvailable)
d.Category = args.DiskCategory
return id, nil
}
func (a DiskManagerMock) DeleteDisk(diskCid string) error {
if _, ok := a.mc.Disks[diskCid]; ok {
delete(a.mc.Disks, diskCid)
return nil
} else {
return fmt.Errorf("DeleteDisk disk not exists id=%s", diskCid)
}
}
func (a DiskManagerMock) AttachDisk(instCid string, diskCid string) error {
disk, ok := a.mc.Disks[diskCid]
if !ok {
return fmt.Errorf("AttachDisk disk not exists id=%s", diskCid)
}
if disk.Status != string(alicloud.DiskStatusAvailable) {
return fmt.Errorf("AttachDisk unexcepted Disk.Status: %s", disk.Status)
}
if disk.InstanceId != "" {
return fmt.Errorf("AttachDisk, disk %s alrady attach to %s", diskCid, disk.InstanceId)
}
disk.InstanceId = instCid
disk.Status = string(alicloud.DiskStatusInUse)
return nil
}
func (a DiskManagerMock) DetachDisk(instCid string, diskCid string) error {
disk, ok := a.mc.Disks[diskCid]
if !ok {
return fmt.Errorf("DetachDisk disk not exists id=%s", diskCid)
}
if disk.Status != string(alicloud.DiskStatusInUse) {
return fmt.Errorf("DetachDisk unexcepted Disk.Status: %s", disk.Status)
}
if disk.InstanceId != instCid {
return fmt.Errorf("DetachDisk, disk %s instance except %s but get %s", diskCid, instCid, disk.InstanceId)
}
disk.InstanceId = ""
disk.Status = string(alicloud.DiskStatusAvailable)
return nil
}
func (a DiskManagerMock) ResizeDisk(diskCid string, sizeGB int) error {
disk, ok := a.mc.Disks[diskCid]
if !ok {
return fmt.Errorf("ResizeDisk disk %s not exists", diskCid)
}
if sizeGB < disk.Size {
return fmt.Errorf("ResizeDisk %s to %d can not smaller than before %d", diskCid, sizeGB, disk.Size)
}
disk.Size = sizeGB
return nil
}
func (a DiskManagerMock) ModifyDiskAttribute(diskCid string, name string, description string) error {
disk, ok := a.mc.Disks[diskCid]
if !ok {
return fmt.Errorf("ModifyDiskAttribute disk %s not exists", diskCid)
}
disk.DiskName = name
disk.Description = description
return nil
}
func (a DiskManagerMock) CreateSnapshot(diskCid string, snapshotName string) (string, error) {
_, ok := a.mc.Disks[diskCid]
if !ok {
return "", fmt.Errorf("CreateSnapshot disk %s not exists", diskCid)
}
ssid := NewSnapshotId()
a.mc.Snapshots[ssid] = diskCid
return ssid, nil
}
func (a DiskManagerMock) DeleteSnapshot(snapshotCid string) error {
_, ok := a.mc.Snapshots[snapshotCid]
if !ok {
return fmt.Errorf("DeleteSnapshot %s not found", snapshotCid)
}
delete(a.mc.Snapshots, snapshotCid)
return nil
}
func (a DiskManagerMock) WaitForDiskStatus(diskCid string, toStatus alicloud.DiskStatus) (string, error) {
disk, ok := a.mc.Disks[diskCid]
if !ok {
return "", fmt.Errorf("WaitForDiskStatus disk not exists id=%s", diskCid)
}
if disk.Status != string(toStatus) {
return "", fmt.Errorf("WaitForDiskStatus %s disk.Status expect %s but get %s", diskCid, toStatus, disk.Status)
}
return disk.Device, nil
}
func (a DiskManagerMock) ChangeDiskStatus(cid string, toStatus alicloud.DiskStatus, checkFunc func(disk *ecs.Disk) (bool, error)) error {
disk, err := a.GetDisk(cid)
if err != nil {
return err
}
ok, err := checkFunc(disk)
if err != nil {
return err
}
if ok {
return nil
}
disk, err = a.GetDisk(cid)
if err != nil {
return err
}
if disk == nil {
return fmt.Errorf("missing disk %s", cid)
}
status := disk.Status
if status == string(toStatus) {
return nil
} else {
return fmt.Errorf("<MOCK> expect instance %s status is %s but get %s", cid, toStatus, status)
}
}