summaryrefslogtreecommitdiff
path: root/libvirt/utils_volume_test.go
blob: 577d6057872314dbb5d302a9876c7ca364731431 (plain)
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
package libvirt

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"testing"
	"time"

	"github.com/libvirt/libvirt-go-xml"
)

func TestLocalImageDownload(t *testing.T) {
	content := []byte("this is a qcow image... well, it is not")
	tmpfile, err := ioutil.TempFile("", "test-image-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpfile.Name())

	t.Logf("Adding some content to %s", tmpfile.Name())
	if _, err := tmpfile.Write(content); err != nil {
		t.Fatal(err)
	}
	tmpfileStat, err := tmpfile.Stat()
	if err != nil {
		t.Fatal(err)
	}
	url := fmt.Sprintf("file://%s", tmpfile.Name())
	image, err := newImage(url)
	if err != nil {
		t.Errorf("Could not create local image: %v", err)
	}

	t.Logf("Importing %s", url)
	vol := newDefVolume()
	vol.Target.Timestamps = &libvirtxml.StorageVolumeTargetTimestamps{
		Mtime: fmt.Sprintf("%d.%d", tmpfileStat.ModTime().Unix(), tmpfileStat.ModTime().Nanosecond()),
	}

	copier := func(r io.Reader) error {
		t.Fatalf("ERROR: starting copy of %s... but the file is the same!", url)
		return nil
	}
	if err = image.Import(copier, vol); err != nil {
		t.Fatalf("Could not copy image from %s: %v", url, err)
	}
	t.Log("File not copied because modification time was the same")
}

func TestRemoteImageDownload(t *testing.T) {
	content := []byte("this is a qcow image... well, it is not")
	fws := fileWebServer{}
	if err := fws.Start(); err != nil {
		t.Fatal(err)
	}
	defer fws.Stop()

	url, tmpfile, err := fws.AddFile(content)
	if err != nil {
		t.Fatal(err)
	}

	tmpfileStat, err := tmpfile.Stat()
	if err != nil {
		t.Fatal(err)
	}
	image, err := newImage(url)
	if err != nil {
		t.Errorf("Could not create local image: %v", err)
	}

	t.Logf("Importing %s", url)
	vol := newDefVolume()
	vol.Target.Timestamps = &libvirtxml.StorageVolumeTargetTimestamps{
		Mtime: fmt.Sprintf("%d.%d", tmpfileStat.ModTime().Unix(), tmpfileStat.ModTime().Nanosecond()),
	}
	copier := func(r io.Reader) error {
		t.Fatalf("ERROR: starting copy of %s... but the file is the same!", url)
		return nil
	}
	if err = image.Import(copier, vol); err != nil {
		t.Fatalf("Could not copy image from %s: %v", url, err)
	}
	t.Log("File not copied because modification time was the same")

}

func TestTimeFromEpoch(t *testing.T) {
	if ts := timeFromEpoch(""); ts.UnixNano() > 0 {
		t.Fatalf("expected timestamp '0.0', got %v.%v", ts.Unix(), ts.Nanosecond())
	}

	if ts := timeFromEpoch("abc"); ts.UnixNano() > 0 {
		t.Fatalf("expected timestamp '0.0', got %v.%v", ts.Unix(), ts.Nanosecond())
	}

	if ts := timeFromEpoch("123"); ts.UnixNano() != time.Unix(123, 0).UnixNano() {
		t.Fatalf("expected timestamp '123.0', got %v.%v", ts.Unix(), ts.Nanosecond())
	}

	if ts := timeFromEpoch("123.456"); ts.UnixNano() != time.Unix(123, 456).UnixNano() {
		t.Fatalf("expected timestamp '123.456', got %v.%v", ts.Unix(), ts.Nanosecond())
	}
}