diff options
author | Alvaro Saurin <alvaro.saurin@gmail.com> | 2017-03-15 12:53:28 +0100 |
---|---|---|
committer | Alvaro Saurin <alvaro.saurin@gmail.com> | 2017-03-29 18:17:23 +0200 |
commit | 81f7527f3597740b67d0430534e12ee0a1da1713 (patch) | |
tree | 63dabe1c1157ff71c7af5d0c333790bc67ed81a3 /libvirt/utils_volume_test.go | |
parent | a286dc5494691c2b04c48ef6695ed0c902912c0f (diff) | |
download | terraform-provider-libvirt-81f7527f3597740b67d0430534e12ee0a1da1713.tar terraform-provider-libvirt-81f7527f3597740b67d0430534e12ee0a1da1713.tar.gz |
Use If-Modified-Since for downloading images
Diffstat (limited to 'libvirt/utils_volume_test.go')
-rw-r--r-- | libvirt/utils_volume_test.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/libvirt/utils_volume_test.go b/libvirt/utils_volume_test.go new file mode 100644 index 00000000..5d8188fb --- /dev/null +++ b/libvirt/utils_volume_test.go @@ -0,0 +1,88 @@ +package libvirt + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "testing" +) + +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() + modTime := UnixTimestamp{tmpfileStat.ModTime()} + vol.Target.Timestamps = &defTimestamps{ + Modification: &modTime, + } + + 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.Fatal("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() + modTime := UnixTimestamp{tmpfileStat.ModTime()} + vol.Target.Timestamps = &defTimestamps{ + Modification: &modTime, + } + 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.Fatal("Could not copy image from %s: %v", url, err) + } + t.Log("File not copied because modification time was the same") + +} |