forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_template_test.go
218 lines (191 loc) · 6.85 KB
/
client_template_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
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
package ovirtclient_test
import (
"fmt"
"testing"
ovirtclient "github.com/ovirt/go-ovirt-client"
)
// TestTemplateCreation is the simplest test for creating and using a template.
func TestTemplateCreation(t *testing.T) {
t.Parallel()
helper := getHelper(t)
vm := assertCanCreateVM(t, helper, fmt.Sprintf("test-%s", helper.GenerateRandomID(5)), nil)
template := assertCanCreateTemplate(t, helper, vm)
tpl := assertCanGetTemplateOK(t, helper, template.ID())
if tpl.ID() != template.ID() {
t.Fatalf("IDs of the returned template don't match.")
}
}
// TestTemplateCPU tests if the CPU settings are properly replicated when creating or using a template.
func TestTemplateCPU(t *testing.T) {
t.Parallel()
helper := getHelper(t)
vm1 := assertCanCreateVM(
t,
helper,
fmt.Sprintf("test-%s", helper.GenerateRandomID(5)),
ovirtclient.CreateVMParams().MustWithCPUParameters(1, 2, 1),
)
tpl := assertCanCreateTemplate(t, helper, vm1)
if tpl.CPU() == nil {
t.Fatalf("Template with explicit CPU options returned a nil CPU.")
}
if tpl.CPU().Topo() == nil {
t.Fatalf("Template with explicit CPU options returned a nil topo.")
}
if cores := tpl.CPU().Topo().Cores(); cores != vm1.CPU().Topo().Cores() {
t.Fatalf(
"Template with explicit CPU options returned the incorrect number of cores (%d instead of %d).",
cores,
vm1.CPU().Topo().Cores(),
)
}
if threads := tpl.CPU().Topo().Threads(); threads != vm1.CPU().Topo().Threads() {
t.Fatalf(
"Template with explicit CPU options returned the incorrect number of threads (%d instead of %d).",
threads,
vm1.CPU().Topo().Threads(),
)
}
if sockets := tpl.CPU().Topo().Sockets(); sockets != vm1.CPU().Topo().Sockets() {
t.Fatalf(
"Template with explicit CPU options returned the incorrect number of sockets (%d instead of %d).",
sockets,
vm1.CPU().Topo().Sockets(),
)
}
vm2 := assertCanCreateVMFromTemplate(t, helper, "test2", tpl.ID(), nil)
if vm1.CPU().Topo().Cores() != vm2.CPU().Topo().Cores() {
t.Fatalf(
"VM created from template returned incorrect number of cores (%d instead of %d).",
vm1.CPU().Topo().Cores(),
vm2.CPU().Topo().Cores(),
)
}
if vm1.CPU().Topo().Threads() != vm2.CPU().Topo().Threads() {
t.Fatalf(
"VM created from template returned incorrect number of threads (%d instead of %d).",
vm1.CPU().Topo().Threads(),
vm2.CPU().Topo().Threads(),
)
}
if vm1.CPU().Topo().Sockets() != vm2.CPU().Topo().Sockets() {
t.Fatalf(
"VM created from template returned incorrect number of sockets (%d instead of %d).",
vm1.CPU().Topo().Sockets(),
vm2.CPU().Topo().Sockets(),
)
}
}
func TestTemplateDisk(t *testing.T) {
t.Parallel()
helper := getHelper(t)
disk := assertCanCreateDisk(t, helper)
vm := assertCanCreateVM(t, helper, fmt.Sprintf("test-%s", helper.GenerateRandomID(5)), nil)
assertCanAttachDisk(t, vm, disk)
template := assertCanCreateTemplate(t, helper, vm)
vm2 := assertCanCreateVMFromTemplate(
t,
helper,
fmt.Sprintf("test-%s", helper.GenerateRandomID(5)), template.ID(),
nil,
)
diskAttachments := assertCanListDiskAttachments(t, vm2)
if len(diskAttachments) != 1 {
t.Fatalf(
"Incorrect number of disk attachments after using template (%d instead of %d).",
len(diskAttachments),
1,
)
}
newDisk := assertCanGetDiskFromAttachment(t, diskAttachments[0])
if newDisk.ProvisionedSize() != disk.ProvisionedSize() {
t.Fatalf(
"Incorrect disk size after template use (%d bytes instead of %d).",
newDisk.ProvisionedSize(),
disk.ProvisionedSize(),
)
}
}
// TestTemplateDiskCopy Copying template disks between storageDomains.
func TestTemplateDiskCopy(t *testing.T) {
t.Parallel()
helper := getHelper(t)
disk := assertCanCreateDisk(t, helper)
vm := assertCanCreateVM(t, helper, fmt.Sprintf("test-%s", helper.GenerateRandomID(5)), nil)
assertCanAttachDisk(t, vm, disk)
template := assertCanCreateTemplate(t, helper, vm)
tpl := assertCanGetTemplateOK(t, helper, template.ID())
diskAttachments := assertCanListTemplateDiskAttachments(t, tpl)
if len(diskAttachments) != 1 {
t.Fatalf(
"Incorrect number of disk attachments after using template (%d instead of %d).",
len(diskAttachments),
1,
)
}
templateDisk := assertCanGetDiskFromTemplateAttachment(t, helper, diskAttachments[0])
secondarySD := helper.GetSecondaryStorageDomainID(t)
t.Logf("Copying template disk %s to storage domain %s", templateDisk.ID(), secondarySD)
newDisk, err := helper.GetClient().CopyTemplateDiskToStorageDomain(templateDisk.ID(), secondarySD)
if err != nil {
t.Fatalf("Failed to copy template disk to storage domain %s.", secondarySD)
}
assertCanGetDiskFromStorageDomain(t, helper, secondarySD, newDisk)
}
func assertCanGetDiskFromStorageDomain(t *testing.T, helper ovirtclient.TestHelper, storageDomainID string, disk ovirtclient.Disk) ovirtclient.Disk {
newDisk, err := helper.GetClient().GetDiskFromStorageDomain(storageDomainID, disk.ID())
if err != nil {
t.Fatalf("failed to get Disk %s from storage domain %s", disk.ID(), storageDomainID)
}
for _, diskStorageDomain := range newDisk.StorageDomainIDs() {
if diskStorageDomain == storageDomainID {
return newDisk
}
}
t.Fatalf("failed to get Disk %s from storage domain %s", disk.ID(), storageDomainID)
return nil
}
func assertCanGetDiskFromAttachment(t *testing.T, diskAttachment ovirtclient.DiskAttachment) ovirtclient.Disk {
newDisk, err := diskAttachment.Disk()
if err != nil {
t.Fatalf("Failed to get disk %s.", diskAttachment.DiskID())
}
return newDisk
}
func assertCanListDiskAttachments(t *testing.T, vm ovirtclient.VM) []ovirtclient.DiskAttachment {
diskAttachments, err := vm.ListDiskAttachments()
if err != nil {
t.Fatalf("Failed to list disk attachments for VM %s. (%v)", vm.ID(), err)
}
return diskAttachments
}
func assertCanGetTemplateOK(t *testing.T, helper ovirtclient.TestHelper, id ovirtclient.TemplateID) ovirtclient.Template {
tpl, err := helper.GetClient().GetTemplate(id)
if err != nil {
t.Fatalf("Failed to get template %s. (%v)", id, err)
}
t.Logf("Waiting for template %s to become \"ok\"...", tpl.ID())
tpl, err = tpl.WaitForStatus(ovirtclient.TemplateStatusOK)
if err != nil {
t.Fatalf("Failed to wait for template %s to reach \"ok\" status. (%v)", tpl.ID(), err)
}
return tpl
}
func assertCanCreateTemplate(t *testing.T, helper ovirtclient.TestHelper, vm ovirtclient.VM) ovirtclient.Template {
t.Logf("Creating test template from VM %s...", vm.Name())
template, err := helper.GetClient().CreateTemplate(
vm.ID(),
fmt.Sprintf("test-%s", helper.GenerateRandomID(5)),
nil,
)
if err != nil {
t.Fatalf("Failed to create template from VM %s (%v)", vm.ID(), err)
}
t.Cleanup(func() {
t.Logf("Cleaning up template %s...", template.ID())
if err := template.Remove(); err != nil && !ovirtclient.HasErrorCode(err, ovirtclient.ENotFound) {
t.Fatalf("Failed to clean up template %s after test. (%v)", template.ID(), err)
}
})
return template
}