forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_disk_downloadimage_test.go
95 lines (82 loc) · 2.35 KB
/
client_disk_downloadimage_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
package ovirtclient_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
ovirtclient "github.com/ovirt/go-ovirt-client"
)
func TestImageDownload(t *testing.T) {
t.Parallel()
testImageData := getTestImageData(t)
fh, stat := getTestImageFile(t)
defer func() {
_ = fh.Close()
}()
helper := getHelper(t)
client := helper.GetClient()
imageName := fmt.Sprintf("client_test_%s", helper.GenerateRandomID(5))
uploadResult, err := client.UploadToNewDisk(
helper.GetStorageDomainID(),
ovirtclient.ImageFormatRaw,
uint64(stat.Size()),
ovirtclient.CreateDiskParams().MustWithSparse(true).MustWithAlias(imageName),
fh,
)
t.Cleanup(func() {
disk := uploadResult.Disk()
if disk != nil {
diskID := uploadResult.Disk().ID()
if err := client.RemoveDisk(diskID); err != nil {
t.Fatal(fmt.Errorf("failed to remove disk (%w)", err))
}
}
})
if err != nil {
t.Fatal(fmt.Errorf("failed to upload image (%w)", err))
}
data := downloadImage(t, client, uploadResult)
// Note about this check: this will work only on RAW images. For QCOW images the blocks may
// be reordered, resulting in different files after download.
if !bytes.Equal(data[:len(testImageData)], testImageData) {
t.Fatal(fmt.Errorf("the downloaded image did not match the original upload"))
}
}
const testImageFile = "./testimage/image"
func getTestImageFile(t *testing.T) (*os.File, os.FileInfo) {
fh, err := os.Open(testImageFile)
if err != nil {
t.Fatal(fmt.Errorf("failed to open test image file %s (%w)", testImageFile, err))
}
stat, err := fh.Stat()
if err != nil {
t.Fatal(fmt.Errorf("failed to stat test image file %s (%w)", testImageFile, err))
}
return fh, stat
}
func getTestImageData(t *testing.T) []byte {
testImageData, err := ioutil.ReadFile(testImageFile)
if err != nil {
t.Fatal(fmt.Errorf("failed to read test image file %s (%w)", testImageFile, err))
}
return testImageData
}
func downloadImage(
t *testing.T,
client ovirtclient.Client,
uploadResult ovirtclient.UploadImageResult,
) []byte {
imageDownload, err := client.DownloadDisk(uploadResult.Disk().ID(), ovirtclient.ImageFormatRaw)
if err != nil {
t.Fatal(fmt.Errorf("failed to download image (%w)", err))
}
defer func() {
_ = imageDownload.Close()
}()
data, err := ioutil.ReadAll(imageDownload)
if err != nil {
t.Fatal(fmt.Errorf("failed to download image (%w)", err))
}
return data
}