This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Mirantis/virtlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_storage.go
264 lines (230 loc) · 7.31 KB
/
fake_storage.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
/*
Copyright 2017 Mirantis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fake
import (
"fmt"
"path"
"sort"
libvirtxml "github.com/libvirt/libvirt-go-xml"
testutils "github.com/Mirantis/virtlet/pkg/utils/testing"
"github.com/Mirantis/virtlet/pkg/virt"
)
var capacityUnits = map[string]uint64{
"b": 1,
"bytes": 1,
"KB": 1000,
"k": 1024,
"KiB": 1024,
"": 1024, // libvirt defaults to KiB
"MB": 1000000,
"M": 1048576,
"MiB": 1048576,
"GB": 1000000000,
"G": 1073741824,
"GiB": 1073741824,
"TB": 1000000000000,
"T": 1099511627776,
"TiB": 1099511627776,
}
// FakeStorageConnection is a fake implementation of StorageConnection.
type FakeStorageConnection struct {
rec testutils.Recorder
pools map[string]*FakeStoragePool
}
// NewFakeStorageConnection creates a new FakeStorageConnection using
// the specified Recorder to record any changes.
func NewFakeStorageConnection(rec testutils.Recorder) *FakeStorageConnection {
return &FakeStorageConnection{
rec: rec,
pools: make(map[string]*FakeStoragePool),
}
}
// CreateStoragePool implements CreateStoragePool method of StorageConnection interface.
func (sc *FakeStorageConnection) CreateStoragePool(def *libvirtxml.StoragePool) (virt.StoragePool, error) {
sc.rec.Rec("CreateStoragePool", mustMarshal(def))
if _, found := sc.pools[def.Name]; found {
return nil, fmt.Errorf("storage pool already exists: %v", def.Name)
}
p := NewFakeStoragePool(testutils.NewChildRecorder(sc.rec, def.Name), def)
sc.pools[def.Name] = p
return p, nil
}
// LookupStoragePoolByName implements LookupStoragePoolByName method of StorageConnection interface.
func (sc *FakeStorageConnection) LookupStoragePoolByName(name string) (virt.StoragePool, error) {
if p, found := sc.pools[name]; found {
return p, nil
}
return nil, virt.ErrStoragePoolNotFound
}
// ListPools implements ListPools method of StorageConnection interface.
func (sc *FakeStorageConnection) ListPools() ([]virt.StoragePool, error) {
r := make([]virt.StoragePool, len(sc.pools))
names := make([]string, 0, len(sc.pools))
for name := range sc.pools {
names = append(names, name)
}
sort.Strings(names)
for n, name := range names {
r[n] = sc.pools[name]
}
return r, nil
}
// PutFiles implements PutFiles method of StorageConnection interface.
func (sc *FakeStorageConnection) PutFiles(imagePath string, files map[string][]byte) error {
fileStrs := make(map[string]string)
for filename, content := range files {
fileStrs[filename] = string(content)
}
sc.rec.Rec("PutFiles", map[string]interface{}{
"imagePath": fixPath(imagePath),
"files": fileStrs,
})
return nil
}
// FakeStoragePool is a fake implementation of StoragePool interface.
type FakeStoragePool struct {
rec testutils.Recorder
name string
path string
volumes map[string]*FakeStorageVolume
def *libvirtxml.StoragePool
}
// NewFakeStoragePool creates a new StoragePool using the specified
// recorder, name and pool path.
func NewFakeStoragePool(rec testutils.Recorder, def *libvirtxml.StoragePool) *FakeStoragePool {
poolPath := "/"
if def.Target != nil {
poolPath = def.Target.Path
}
return &FakeStoragePool{
rec: rec,
name: def.Name,
path: poolPath,
volumes: make(map[string]*FakeStorageVolume),
def: def,
}
}
func (p *FakeStoragePool) createStorageVol(def *libvirtxml.StorageVolume) (virt.StorageVolume, error) {
if _, found := p.volumes[def.Name]; found {
return nil, fmt.Errorf("storage volume already exists: %v", def.Name)
}
v, err := newFakeStorageVolume(testutils.NewChildRecorder(p.rec, def.Name), p, def)
if err != nil {
return nil, err
}
p.volumes[def.Name] = v
return v, nil
}
// CreateStorageVol implements CreateStorageVol method of StoragePool interface.
func (p *FakeStoragePool) CreateStorageVol(def *libvirtxml.StorageVolume) (virt.StorageVolume, error) {
p.rec.Rec("CreateStorageVol", mustMarshal(def))
return p.createStorageVol(def)
}
// ListVolumes implements ListVolumes method of StoragePool interface.
func (p *FakeStoragePool) ListVolumes() ([]virt.StorageVolume, error) {
r := make([]virt.StorageVolume, len(p.volumes))
names := make([]string, 0, len(p.volumes))
for name := range p.volumes {
names = append(names, name)
}
sort.Strings(names)
for n, name := range names {
r[n] = p.volumes[name]
}
return r, nil
}
// LookupVolumeByName implements LookupVolumeByName method of StoragePool interface.
func (p *FakeStoragePool) LookupVolumeByName(name string) (virt.StorageVolume, error) {
if v, found := p.volumes[name]; found {
return v, nil
}
return nil, virt.ErrStorageVolumeNotFound
}
func (p *FakeStoragePool) removeVolumeByName(name string) error {
if _, found := p.volumes[name]; !found {
return nil
}
delete(p.volumes, name)
return nil
}
// RemoveVolumeByName implements RemoveVolumeByName method of StoragePool interface.
func (p *FakeStoragePool) RemoveVolumeByName(name string) error {
p.rec.Rec("RemoveVolumeByName", name)
return p.removeVolumeByName(name)
}
// XML implements XML method of StoragePool interface.
func (p *FakeStoragePool) XML() (*libvirtxml.StoragePool, error) {
return p.def, nil
}
// FakeStorageVolume is a fake implementation of StorageVolume interface.
type FakeStorageVolume struct {
rec testutils.Recorder
pool *FakeStoragePool
name string
path string
size uint64
def *libvirtxml.StorageVolume
}
func newFakeStorageVolume(rec testutils.Recorder, pool *FakeStoragePool, def *libvirtxml.StorageVolume) (*FakeStorageVolume, error) {
volPath := ""
if def.Target != nil {
volPath = def.Target.Path
}
if volPath == "" {
volPath = path.Join(pool.path, def.Name)
}
v := &FakeStorageVolume{
rec: rec,
pool: pool,
name: def.Name,
path: volPath,
def: def,
}
if def.Capacity != nil {
coef, found := capacityUnits[def.Capacity.Unit]
if !found {
return nil, fmt.Errorf("bad capacity units: %q", def.Capacity.Unit)
}
v.size = def.Capacity.Value * coef
}
return v, nil
}
func (v *FakeStorageVolume) descriptiveName() string {
return v.pool.name + "." + v.name
}
// Name implements Name method of StorageVolume interface.
func (v *FakeStorageVolume) Name() string {
return v.name
}
// Size implements Size method of StorageVolume interface.
func (v *FakeStorageVolume) Size() (uint64, error) {
return v.size, nil
}
// Path implements Path method of StorageVolume interface.
func (v *FakeStorageVolume) Path() (string, error) {
return v.path, nil
}
// Remove implements Remove method of StorageVolume interface.
func (v *FakeStorageVolume) Remove() error {
v.rec.Rec("Remove", nil)
return v.pool.removeVolumeByName(v.name)
}
// Format implements Format method of StorageVolume interface.
func (v *FakeStorageVolume) Format() error {
v.rec.Rec("Format", nil)
return nil
}
// XML implements XML method of StorageVolume interface.
func (v *FakeStorageVolume) XML() (*libvirtxml.StorageVolume, error) {
return v.def, nil
}