~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/altoros/gosigma/drive_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 ALTOROS
 
2
// Licensed under the AGPLv3, see LICENSE file for details.
 
3
 
 
4
package gosigma
 
5
 
 
6
import (
 
7
        "testing"
 
8
 
 
9
        "github.com/altoros/gosigma/data"
 
10
        "github.com/altoros/gosigma/mock"
 
11
)
 
12
 
 
13
func newDataDrive(uuid string) *data.Drive {
 
14
        result := &data.Drive{
 
15
                Resource: data.Resource{URI: "uri", UUID: uuid},
 
16
                LibraryDrive: data.LibraryDrive{
 
17
                        Arch:      "arch",
 
18
                        ImageType: "image-type",
 
19
                        OS:        "os",
 
20
                        Paid:      true,
 
21
                },
 
22
                Affinities:      []string{"aff1", "aff2"},
 
23
                AllowMultimount: true,
 
24
                Jobs: []data.Resource{
 
25
                        data.Resource{URI: "uri-0", UUID: "uuid-0"},
 
26
                        data.Resource{URI: "uri-1", UUID: "uuid-1"},
 
27
                },
 
28
                Media:       "media",
 
29
                Meta:        map[string]string{"key1": "value1", "key2": "value2"},
 
30
                Name:        "name",
 
31
                Owner:       &data.Resource{URI: "owner-uri", UUID: "owner-uuid"},
 
32
                Size:        1000,
 
33
                Status:      "status",
 
34
                StorageType: "storage-type",
 
35
        }
 
36
        return result
 
37
}
 
38
 
 
39
func testDrive(t *testing.T, d Drive, uuid string, short bool) {
 
40
        checkv := func(v, wants string) {
 
41
                if v != wants {
 
42
                        t.Errorf("value %q, wants %q", v, wants)
 
43
                }
 
44
        }
 
45
        checkb := func(v, wants bool) {
 
46
                if v != wants {
 
47
                        t.Errorf("value %v, wants %v", v, wants)
 
48
                }
 
49
        }
 
50
        checki := func(v, wants uint64) {
 
51
                if v != wants {
 
52
                        t.Errorf("value %d, wants %d", v, wants)
 
53
                }
 
54
        }
 
55
        checkg := func(d Drive, k, wants string) {
 
56
                if v, ok := d.Get(k); !ok || v != wants {
 
57
                        t.Errorf("value of Get(%q) = %q, %v; wants %s", k, v, ok, wants)
 
58
                }
 
59
        }
 
60
 
 
61
        checkv(d.URI(), "uri")
 
62
        checkv(d.UUID(), uuid)
 
63
        checkv(d.Owner().URI(), "owner-uri")
 
64
        checkv(d.Owner().UUID(), "owner-uuid")
 
65
        checkv(d.Status(), "status")
 
66
 
 
67
        if short {
 
68
                checkv(d.Name(), "")
 
69
                checkv(d.Media(), "")
 
70
                checkv(d.StorageType(), "")
 
71
                return
 
72
        }
 
73
 
 
74
        checkv(d.Arch(), "arch")
 
75
        checkv(d.ImageType(), "image-type")
 
76
        checkv(d.OS(), "os")
 
77
        checkb(d.Paid(), true)
 
78
 
 
79
        if v := d.Affinities(); len(v) != 2 {
 
80
                t.Error("Affinities failed:", len(v), v)
 
81
        } else {
 
82
                checkv(v[0], "aff1")
 
83
                checkv(v[1], "aff2")
 
84
        }
 
85
 
 
86
        checkb(d.AllowMultimount(), true)
 
87
 
 
88
        if v := d.Jobs(); len(v) != 2 {
 
89
                t.Error("Jobs failed:", v)
 
90
        } else {
 
91
                checkv(v[0].URI(), "uri-0")
 
92
                checkv(v[0].UUID(), "uuid-0")
 
93
                checkv(v[1].URI(), "uri-1")
 
94
                checkv(v[1].UUID(), "uuid-1")
 
95
        }
 
96
 
 
97
        checkv(d.Media(), "media")
 
98
 
 
99
        checkg(d, "key1", "value1")
 
100
        checkg(d, "key2", "value2")
 
101
        checkv(d.Name(), "name")
 
102
        checki(d.Size(), 1000)
 
103
        checkv(d.StorageType(), "storage-type")
 
104
}
 
105
 
 
106
func TestClientDriveEmpty(t *testing.T) {
 
107
        d := &drive{obj: &data.Drive{}}
 
108
        if d.Owner() != nil {
 
109
                t.Error("invalid owner")
 
110
        }
 
111
}
 
112
 
 
113
func TestClientDrivesEmpty(t *testing.T) {
 
114
        mock.ResetDrives()
 
115
 
 
116
        cli, err := createTestClient(t)
 
117
        if err != nil || cli == nil {
 
118
                t.Error("NewClient() failed:", err, cli)
 
119
                return
 
120
        }
 
121
 
 
122
        check := func(rqspec RequestSpec, libspec LibrarySpec) {
 
123
                drives, err := cli.Drives(rqspec, libspec)
 
124
                if err != nil {
 
125
                        t.Error(err)
 
126
                }
 
127
                if len(drives) > 0 {
 
128
                        t.Errorf("%v", drives)
 
129
                }
 
130
        }
 
131
 
 
132
        check(RequestShort, LibraryAccount)
 
133
        check(RequestDetail, LibraryAccount)
 
134
}
 
135
 
 
136
func TestClientDrives(t *testing.T) {
 
137
        mock.ResetDrives()
 
138
 
 
139
        mock.Drives.Add(newDataDrive("uuid-0"))
 
140
        mock.Drives.Add(newDataDrive("uuid-1"))
 
141
 
 
142
        cli, err := createTestClient(t)
 
143
        if err != nil {
 
144
                t.Error(err)
 
145
                return
 
146
        }
 
147
 
 
148
        drives, err := cli.Drives(RequestShort, LibraryAccount)
 
149
        if err != nil {
 
150
                t.Error(err)
 
151
                return
 
152
        }
 
153
 
 
154
        if len(drives) != 2 {
 
155
                t.Errorf("invalid len: %v", drives)
 
156
                return
 
157
        }
 
158
 
 
159
        for i, uuid := range []string{"uuid-0", "uuid-1"} {
 
160
                d := drives[i]
 
161
 
 
162
                if d.String() == "" {
 
163
                        t.Error("Empty string representation")
 
164
                        return
 
165
                }
 
166
 
 
167
                testDrive(t, d, uuid, true)
 
168
 
 
169
                // refresh
 
170
                if err := d.Refresh(); err != nil {
 
171
                        t.Error(err)
 
172
                        return
 
173
                }
 
174
 
 
175
                testDrive(t, d, uuid, false)
 
176
 
 
177
                if err := d.Remove(); err != nil {
 
178
                        t.Error("Drive remove fail:", err)
 
179
                        return
 
180
                }
 
181
 
 
182
                // failed refresh
 
183
                if err := d.Refresh(); err == nil {
 
184
                        t.Error("Drive refresh must fail")
 
185
                        return
 
186
                }
 
187
        }
 
188
 
 
189
        mock.ResetDrives()
 
190
}
 
191
 
 
192
func TestClientDrive(t *testing.T) {
 
193
        mock.ResetDrives()
 
194
 
 
195
        mock.Drives.Add(newDataDrive("uuid"))
 
196
 
 
197
        cli, err := createTestClient(t)
 
198
        if err != nil {
 
199
                t.Error(err)
 
200
                return
 
201
        }
 
202
 
 
203
        d, err := cli.Drive("uuid", LibraryAccount)
 
204
        if err != nil {
 
205
                t.Error(err)
 
206
                return
 
207
        }
 
208
 
 
209
        if d.String() == "" {
 
210
                t.Error("Empty string representation")
 
211
                return
 
212
        }
 
213
 
 
214
        testDrive(t, d, "uuid", false)
 
215
 
 
216
        if err := d.Remove(); err != nil {
 
217
                t.Error("Drive remove fail:", err)
 
218
                return
 
219
        }
 
220
 
 
221
        // failed refresh
 
222
        if err := d.Refresh(); err == nil {
 
223
                t.Error("Drive refresh must fail")
 
224
                return
 
225
        }
 
226
 
 
227
        mock.ResetDrives()
 
228
}
 
229
 
 
230
func TestDriveClone(t *testing.T) {
 
231
        mock.ResetDrives()
 
232
 
 
233
        mock.LibDrives.Add(newDataDrive("uuid"))
 
234
 
 
235
        cli, err := createTestClient(t)
 
236
        if err != nil {
 
237
                t.Error(err)
 
238
                return
 
239
        }
 
240
 
 
241
        d, err := cli.Drive("uuid", LibraryMedia)
 
242
        if err != nil {
 
243
                t.Error(err)
 
244
                return
 
245
        }
 
246
 
 
247
        params := CloneParams{Name: "test-name", Media: "ssd"}
 
248
        newDrive, err := d.Clone(params, []string{"avoid-uuid-0", "avoid-uuid-1"})
 
249
        if err != nil {
 
250
                t.Error("Drive clone fail:", err)
 
251
                return
 
252
        }
 
253
 
 
254
        if newDrive.UUID() == d.UUID() {
 
255
                t.Errorf("Drive.Clone(), invalid new drive UUID %s", newDrive.UUID())
 
256
        }
 
257
        if newDrive.Size() != d.Size() {
 
258
                t.Errorf("Drive.Clone(), invalid size %d, should be %d", newDrive.Size(), d.Size())
 
259
        }
 
260
        if newDrive.Library() != LibraryAccount {
 
261
                t.Errorf("Drive.Clone(), invalid library spec %v", newDrive.Library())
 
262
        }
 
263
        if newDrive.Name() != "test-name" {
 
264
                t.Errorf("Drive.Clone(), invalid name %q", newDrive.Name())
 
265
        }
 
266
        if newDrive.Media() != "ssd" {
 
267
                t.Errorf("Drive.Clone(), invalid media %q", newDrive.Media())
 
268
        }
 
269
 
 
270
        mock.ResetDrives()
 
271
}
 
272
 
 
273
func TestDriveCloneWait(t *testing.T) {
 
274
        mock.ResetDrives()
 
275
 
 
276
        mock.Drives.Add(newDataDrive("uuid"))
 
277
 
 
278
        cli, err := createTestClient(t)
 
279
        if err != nil {
 
280
                t.Error(err)
 
281
                return
 
282
        }
 
283
 
 
284
        d, err := cli.Drive("uuid", LibraryAccount)
 
285
        if err != nil {
 
286
                t.Error(err)
 
287
                return
 
288
        }
 
289
 
 
290
        params := CloneParams{Name: "test-name", Media: "ssd"}
 
291
        newDrive, err := d.CloneWait(params, nil)
 
292
        if err != nil {
 
293
                t.Error("Drive clone fail:", err)
 
294
                return
 
295
        }
 
296
 
 
297
        if newDrive.UUID() == d.UUID() {
 
298
                t.Errorf("Drive.Clone(), invalid new drive UUID %s", newDrive.UUID())
 
299
        }
 
300
        if newDrive.Size() != d.Size() {
 
301
                t.Errorf("Drive.Clone(), invalid size %d, should be %d", newDrive.Size(), d.Size())
 
302
        }
 
303
        if newDrive.Library() != LibraryAccount {
 
304
                t.Errorf("Drive.Clone(), invalid library spec %v", newDrive.Library())
 
305
        }
 
306
        if newDrive.Name() != "test-name" {
 
307
                t.Errorf("Drive.Clone(), invalid name %q", newDrive.Name())
 
308
        }
 
309
        if newDrive.Media() != "ssd" {
 
310
                t.Errorf("Drive.Clone(), invalid media %q", newDrive.Media())
 
311
        }
 
312
 
 
313
        mock.ResetDrives()
 
314
}
 
315
 
 
316
func TestDriveCloneFail(t *testing.T) {
 
317
        mock.ResetDrives()
 
318
 
 
319
        mock.LibDrives.Add(newDataDrive("uuid"))
 
320
 
 
321
        cli, err := createTestClient(t)
 
322
        if err != nil {
 
323
                t.Error(err)
 
324
                return
 
325
        }
 
326
 
 
327
        d, err := cli.Drive("uuid", LibraryMedia)
 
328
        if err != nil {
 
329
                t.Error(err)
 
330
                return
 
331
        }
 
332
 
 
333
        mock.ResetDrives()
 
334
 
 
335
        params := CloneParams{Name: "test-name", Media: "ssd"}
 
336
        newDrive, err := d.Clone(params, nil)
 
337
        if err == nil || newDrive != nil {
 
338
                t.Errorf("Drive clone must fail err=%v, rc=%v", err, newDrive)
 
339
                return
 
340
        }
 
341
 
 
342
        t.Logf("OK. Drive.Clone(), err = %v", err)
 
343
 
 
344
        mock.ResetDrives()
 
345
}