-
Notifications
You must be signed in to change notification settings - Fork 20
/
disk_test.go
115 lines (100 loc) · 3.16 KB
/
disk_test.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_test
import (
"fmt"
"testing"
ovirtclientlog "github.com/ovirt/go-ovirt-client-log/v3"
ovirtclient "github.com/ovirt/go-ovirt-client/v3"
)
// ExampleDiskClient_CreateDisk is an example of creating an empty disk. This example works with the test
// helper, but can be modified in production.
func ExampleDiskClient_CreateDisk() {
// Create a logger. This can be adapter to use your own logger.
logger := ovirtclientlog.NewNOOPLogger()
// Create the test helper. This will give us our test storage domain.
helper, err := ovirtclient.NewMockTestHelper(logger)
if err != nil {
panic(fmt.Errorf("failed to create mock test helper (%w)", err))
}
// Create the client. Replace with ovirtclient.New() for production use.
client := helper.GetClient()
// Obtain the storage domain used for testing.
storageDomainID := helper.GetStorageDomainID()
// Let's create a raw disk.
imageFormat := ovirtclient.ImageFormatRaw
// 512 bytes are enough, 1M is the minimum disk size for oVirt.
diskSize := 1048576
// Create the disk and wait for it to become available. Use StartCreateDisk to skip the wait.
disk, err := client.CreateDisk(
storageDomainID,
imageFormat,
uint64(diskSize),
ovirtclient.CreateDiskParams().MustWithAlias("test_disk"),
)
if err != nil {
panic(err)
}
// Remove the disk we just created.
if err := disk.Remove(); err != nil {
panic(err)
}
// Output:
}
func TestDiskCreationAndUpdate(t *testing.T) {
t.Parallel()
helper := getHelper(t)
client := helper.GetClient()
diskName := fmt.Sprintf("client_test_%s", helper.GenerateRandomID(5))
disk, err := client.CreateDisk(
helper.GetStorageDomainID(),
ovirtclient.ImageFormatRaw,
1048576,
ovirtclient.CreateDiskParams().MustWithAlias(diskName),
)
if err != nil {
t.Fatal(err)
}
if disk.Status() != ovirtclient.DiskStatusOK {
t.Fatalf(
"Disk is not in %s status after creation, instead it is %s",
ovirtclient.DiskStatusOK,
disk.Status(),
)
}
t.Cleanup(func() {
err := disk.Remove()
if err != nil {
t.Fatalf("Failed to remove disk after disk creation test (%v)", err)
}
})
checkDiskAfterCreation(disk, t, diskName)
fetchedDisk, err := client.GetDisk(disk.ID())
if err != nil {
t.Fatalf("failed to fetch disk after creation (%v)", err)
}
checkDiskAfterCreation(fetchedDisk, t, diskName)
params := ovirtclient.UpdateDiskParams()
params.MustWithAlias("changed_disk_name")
updatedDisk, err := fetchedDisk.Update(params)
if err != nil {
t.Fatalf("failed to update disk (%v)", err)
}
checkDiskAfterCreation(updatedDisk, t, "changed_disk_name")
}
func checkDiskAfterCreation(disk ovirtclient.Disk, t *testing.T, name string) {
if disk.ProvisionedSize() < 512 {
t.Fatalf("Incorrect provisioned disk size after creation: %d", disk.ProvisionedSize())
}
if disk.TotalSize() < 512 {
t.Fatalf("Incorrect total disk size after creation: %d", disk.TotalSize())
}
if disk.Status() != ovirtclient.DiskStatusOK {
t.Fatalf(
"Disk is not in %s status after creation, instead it is %s",
ovirtclient.DiskStatusOK,
disk.Status(),
)
}
if disk.Alias() != name {
t.Fatalf("Incorrect disk alias after creation (%s instead of %s)", disk.Alias(), name)
}
}