-
Notifications
You must be signed in to change notification settings - Fork 21
/
disk_create_mock.go
115 lines (95 loc) · 2.25 KB
/
disk_create_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
package ovirtclient
import (
"sync"
"time"
)
func (m *mockClient) StartCreateDisk(
storageDomainID StorageDomainID,
format ImageFormat,
size uint64,
params CreateDiskOptionalParameters,
_ ...RetryStrategy,
) (DiskCreation, error) {
m.lock.Lock()
defer m.lock.Unlock()
disk, err := m.createDisk(storageDomainID, format, size, params)
if err != nil {
return nil, err
}
creation := &mockDiskCreation{
client: m,
disk: disk,
done: make(chan struct{}),
}
creation.do()
return creation, nil
}
func (m *mockClient) createDisk(
storageDomainID StorageDomainID,
format ImageFormat,
size uint64,
params CreateDiskOptionalParameters,
) (*diskWithData, error) {
if err := validateDiskCreationParameters(format, size); err != nil {
return nil, err
}
if _, ok := m.storageDomains[storageDomainID]; !ok {
return nil, newError(ENotFound, "storage domain with ID %s not found", storageDomainID)
}
disk := &diskWithData{
disk: disk{
client: m,
id: DiskID(m.GenerateUUID()),
format: format,
provisionedSize: size,
totalSize: size,
storageDomainIDs: []StorageDomainID{storageDomainID},
status: DiskStatusLocked,
},
lock: &sync.Mutex{},
data: nil,
}
if params != nil {
if alias := params.Alias(); alias != "" {
disk.disk.alias = alias
}
if sparse := params.Sparse(); sparse != nil {
disk.disk.sparse = *sparse
}
}
m.disks[disk.id] = disk
return disk, nil
}
func (m *mockClient) CreateDisk(
storageDomainID StorageDomainID,
format ImageFormat,
size uint64,
params CreateDiskOptionalParameters,
retries ...RetryStrategy,
) (Disk, error) {
result, err := m.StartCreateDisk(storageDomainID, format, size, params, retries...)
if err != nil {
return nil, err
}
return result.Wait()
}
type mockDiskCreation struct {
client *mockClient
disk *diskWithData
done chan struct{}
}
func (c *mockDiskCreation) Disk() Disk {
c.client.lock.Lock()
defer c.client.lock.Unlock()
return c.disk
}
func (c *mockDiskCreation) Wait(_ ...RetryStrategy) (Disk, error) {
<-c.done
return c.disk, nil
}
func (c *mockDiskCreation) do() {
// Sleep to trigger potential race conditions / improper status handling.
time.Sleep(time.Second)
c.disk.Unlock()
close(c.done)
}