diff --git a/integration_test.go b/integration_test.go index 7c89bd6..0ec6eb8 100644 --- a/integration_test.go +++ b/integration_test.go @@ -863,3 +863,100 @@ func TestIntegrationDomainInterfaceStats(t *testing.T) { t.Fatal(err) } } + +func TestStorageVolUploadDownload(t *testing.T) { + conn, err := NewVirConnection("lxc:///") + if err != nil { + t.Error(err) + return + } + defer conn.CloseConnection() + + poolPath, err := ioutil.TempDir("", "default-pool-test-1") + if err != nil { + t.Error(err) + return + } + defer os.RemoveAll(poolPath) + pool, err := conn.StoragePoolDefineXML(` + default-pool-test-1 + + `+poolPath+` + + `, 0) + defer func() { + pool.Undefine() + pool.Free() + }() + if err := pool.Create(0); err != nil { + t.Error(err) + return + } + defer pool.Destroy() + vol, err := pool.StorageVolCreateXML(testStorageVolXML("", poolPath), 0) + if err != nil { + t.Error(err) + return + } + defer func() { + vol.Delete(VIR_STORAGE_VOL_DELETE_NORMAL) + vol.Free() + }() + + data := []byte{1, 2, 3, 4, 5, 6} + + // write above data to the vol + // 1. create a stream + stream, err := NewVirStream(&conn, 0) + if err != nil { + t.Fatal(err) + } + defer func() { + stream.Free() + }() + + // 2. set it up to upload from stream + if err := vol.Upload(stream, 0, uint64(len(data)), 0); err != nil { + stream.Abort() + t.Fatal(err) + } + + // 3. do the actual writing + if n, err := stream.Write(data); err != nil || n != len(data) { + t.Fatal(err, n) + } + + // 4. finish! + if err := stream.Close(); err != nil { + t.Fatal(err) + } + + // read back the data + // 1. create a stream + downStream, err := NewVirStream(&conn, 0) + if err != nil { + t.Fatal(err) + } + defer func() { + downStream.Free() + }() + + // 2. set it up to download from stream + if err := vol.Download(downStream, 0, uint64(len(data)), 0); err != nil { + downStream.Abort() + t.Fatal(err) + } + + // 3. do the actual reading + buf := make([]byte, 1024) + if n, err := downStream.Read(buf); err != nil || n != len(data) { + t.Fatal(err, n) + } + + t.Logf("read back: %#v", buf[:len(data)]) + + // 4. finish! + if err := downStream.Close(); err != nil { + t.Fatal(err) + } +} diff --git a/storage_volume.go b/storage_volume.go index 1a4f25a..26b446e 100644 --- a/storage_volume.go +++ b/storage_volume.go @@ -116,3 +116,19 @@ func (v *VirStorageVol) WipePattern(algorithm uint32, flags uint32) error { } return nil } + +func (v *VirStorageVol) Upload(stream *VirStream, offset, length uint64, flags uint32) error { + if C.virStorageVolUpload(v.ptr, stream.ptr, C.ulonglong(offset), + C.ulonglong(length), C.uint(flags)) == -1 { + return GetLastError() + } + return nil +} + +func (v *VirStorageVol) Download(stream *VirStream, offset, length uint64, flags uint32) error { + if C.virStorageVolDownload(v.ptr, stream.ptr, C.ulonglong(offset), + C.ulonglong(length), C.uint(flags)) == -1 { + return GetLastError() + } + return nil +}