-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathexample_vm_uploadimage_test.go
114 lines (99 loc) · 3.14 KB
/
example_vm_uploadimage_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
package ovirtclient_test
import (
"context"
"fmt"
"os"
"time"
ovirtclientlog "github.com/ovirt/go-ovirt-client-log/v3"
ovirtclient "github.com/ovirt/go-ovirt-client/v3"
)
// This example demonstrates the simplest way to upload an image without special timeout handling. The call still times
// out after a built-in timeout.
func ExampleDiskClient_uploadImage() {
// Open image file to upload
fh, err := os.Open("/path/to/test.img")
if err != nil {
panic(fmt.Errorf("failed to open image file (%w)", err))
}
defer func() {
if err = fh.Close(); err != nil {
panic(err)
}
}()
// Get the file size
stat, err := fh.Stat()
if err != nil {
panic(fmt.Errorf("failed to stat image file (%w)", err))
}
// Obtain oVirt client. Alternatively, you can call ovirtclient.New() to do this directly.
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
if err != nil {
panic(fmt.Errorf("failed to create live test helper (%w)", err))
}
client := helper.GetClient()
imageName := fmt.Sprintf("client_test_%s", helper.GenerateRandomID(5))
// Upload image and wait for result.
uploadResult, err := client.UploadToNewDisk(
helper.GetStorageDomainID(),
ovirtclient.ImageFormatRaw,
uint64(stat.Size()),
ovirtclient.CreateDiskParams().MustWithAlias(imageName).MustWithSparse(true),
fh,
)
if err != nil {
panic(fmt.Errorf("failed to upload image (%w)", err))
}
fmt.Printf("Uploaded as disk %s\n", uploadResult.Disk().ID())
}
// This example demonstrates how to upload a VM image into a disk while being able to cancel the process manually.
func ExampleDiskClient_uploadImageWithCancel() {
// Open image file to upload
fh, err := os.Open("/path/to/test.img")
if err != nil {
panic(fmt.Errorf("failed to open image file (%w)", err))
}
defer func() {
if err = fh.Close(); err != nil {
panic(err)
}
}()
// Get the file size
stat, err := fh.Stat()
if err != nil {
panic(fmt.Errorf("failed to stat image file (%w)", err))
}
// Obtain oVirt client. Alternatively, you can call ovirtclient.New() to do this directly.
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
if err != nil {
panic(fmt.Errorf("failed to create live test helper (%w)", err))
}
client := helper.GetClient()
imageName := fmt.Sprintf("client_test_%s", helper.GenerateRandomID(5))
// Set up context so we can cancel the upload
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
uploadResult, err := client.StartUploadToNewDisk(
helper.GetStorageDomainID(),
ovirtclient.ImageFormatRaw,
uint64(stat.Size()),
ovirtclient.CreateDiskParams().MustWithSparse(true).MustWithAlias(imageName),
fh,
ovirtclient.ContextStrategy(ctx),
)
if err != nil {
panic(fmt.Errorf("failed to upload image (%w)", err))
}
// Wait for image upload to initialize.
select {
case <-time.After(10 * time.Minute):
// Cancel upload
cancel()
// Wait for it to actually finish.
<-uploadResult.Done()
case <-uploadResult.Done():
}
if err := uploadResult.Err(); err != nil {
panic(fmt.Errorf("failed to upload image (%w)", err))
}
fmt.Printf("Uploaded as disk %s\n", uploadResult.Disk().ID())
}