- Add argument to pool Export for history log string, hard force (ExportForce),

export and import tests, and overall tests fixes and improvements
This commit is contained in:
Faruk Kasumovic 2015-06-08 23:41:22 +02:00
parent 39d6835ce3
commit 507abac683
4 changed files with 111 additions and 30 deletions

View File

@ -9,6 +9,10 @@ import (
func Test(t *testing.T) { func Test(t *testing.T) {
zpoolTestPoolCreate(t) zpoolTestPoolCreate(t)
zpoolTestExport(t)
zpoolTestImport(t)
zpoolTestExportForce(t)
zpoolTestImport(t)
zpoolTestPoolOpenAll(t) zpoolTestPoolOpenAll(t)
zpoolTestFailPoolOpen(t) zpoolTestFailPoolOpen(t)
@ -20,4 +24,6 @@ func Test(t *testing.T) {
zfsTestDatasetDestroy(t) zfsTestDatasetDestroy(t)
zpoolTestPoolDestroy(t) zpoolTestPoolDestroy(t)
cleanupVDisks()
} }

View File

@ -23,8 +23,7 @@ func printDatasets(ds []zfs.Dataset) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Printf(" %30s | %10s\n", path, fmt.Printf(" %30s | %10s\n", path, p.Value)
p.Value)
if len(d.Children) > 0 { if len(d.Children) > 0 {
printDatasets(d.Children) printDatasets(d.Children)
} }
@ -159,13 +158,33 @@ func ExampleDatasetOpen() {
d, err := zfs.DatasetOpen("TESTPOOL/DATASET1") d, err := zfs.DatasetOpen("TESTPOOL/DATASET1")
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
return
} }
defer d.Close() defer d.Close()
var p zfs.Property var p zfs.Property
if p, err = d.GetProperty(zfs.ZFSPropAvailable); err != nil { if p, err = d.GetProperty(zfs.ZFSPropAvailable); err != nil {
panic(err.Error()) panic(err.Error())
return
} }
println(d.PropertyToName(zfs.ZFSPropAvailable), " = ", p.Value) println(d.PropertyToName(zfs.ZFSPropAvailable), " = ", p.Value)
} }
func ExampleDatasetOpenAll() {
datasets, err := zfs.DatasetOpenAll()
if err != nil {
panic(err.Error())
}
defer zfs.DatasetCloseAll(datasets)
// Print out path and type of root datasets
for _, d := range datasets {
path, err := d.Path()
if err != nil {
panic(err.Error())
}
p, err := d.GetProperty(zfs.ZFSPropType)
if err != nil {
panic(err.Error())
}
fmt.Printf("%30s | %10s\n", path, p.Value)
}
}

View File

@ -549,13 +549,20 @@ func (pool *Pool) Destroy(logStr string) (err error) {
// Before exporting the pool, all datasets within the pool are unmounted. // Before exporting the pool, all datasets within the pool are unmounted.
// A pool can not be exported if it has a shared spare that is currently // A pool can not be exported if it has a shared spare that is currently
// being used. // being used.
func (pool *Pool) Export(force bool) (err error) { func (pool *Pool) Export(force bool, log string) (err error) {
var force_t C.boolean_t = 0 var force_t C.boolean_t = 0
if force { if force {
force_t = 1 force_t = 1
} }
retcode := C.zpool_export(pool.list.zph, force_t, nil) if rc := C.zpool_export(pool.list.zph, force_t, C.CString(log)); rc != 0 {
if retcode != 0 { err = LastError()
}
return
}
// Hard force
func (pool *Pool) ExportForce(log string) (err error) {
if rc := C.zpool_export_force(pool.list.zph, C.CString(log)); rc != 0 {
err = LastError() err = LastError()
} }
return return

View File

@ -26,6 +26,36 @@ func CreateTmpSparse(prefix string, size int64) (path string, err error) {
return return
} }
var s1path, s2path, s3path string
// This will create sparse files in tmp directory,
// for purpose of creating test pool.
func createTestpoolVdisks() (err error) {
if s1path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
return
}
if s2path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
// try cleanup
os.Remove(s1path)
return
}
if s3path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
// try cleanup
os.Remove(s1path)
os.Remove(s2path)
return
}
return
}
// Cleanup sparse files used for tests
func cleanupVDisks() {
// try cleanup
os.Remove(s1path)
os.Remove(s2path)
os.Remove(s3path)
}
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
// TESTS: // TESTS:
@ -43,26 +73,13 @@ func zpoolTestPoolCreate(t *testing.T) {
p.Close() p.Close()
TST_POOL_NAME += "0" TST_POOL_NAME += "0"
} }
var s1path, s2path, s3path string
var err error var err error
if s1path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
t.Error(err) if err = createTestpoolVdisks(); err != nil {
return
}
if s2path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
// try cleanup
os.Remove(s1path)
t.Error(err)
return
}
if s3path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
// try cleanup
os.Remove(s1path)
os.Remove(s2path)
t.Error(err) t.Error(err)
return return
} }
disks := [2]string{s1path, s2path} disks := [2]string{s1path, s2path}
var vdevs, mdevs, sdevs []zfs.VDevSpec var vdevs, mdevs, sdevs []zfs.VDevSpec
@ -95,10 +112,7 @@ func zpoolTestPoolCreate(t *testing.T) {
return return
} }
defer pool.Close() defer pool.Close()
// try cleanup
os.Remove(s1path)
os.Remove(s2path)
os.Remove(s3path)
println("PASS\n") println("PASS\n")
} }
@ -159,6 +173,44 @@ func zpoolTestFailPoolOpen(t *testing.T) {
p.Close() p.Close()
} }
func zpoolTestExport(t *testing.T) {
println("TEST POOL Export( ", TST_POOL_NAME, " ) ... ")
p, err := zfs.PoolOpen(TST_POOL_NAME)
if err != nil {
t.Error(err)
return
}
p.Export(false, "Test exporting pool")
defer p.Close()
println("PASS\n")
}
func zpoolTestExportForce(t *testing.T) {
println("TEST POOL ExportForce( ", TST_POOL_NAME, " ) ... ")
p, err := zfs.PoolOpen(TST_POOL_NAME)
if err != nil {
t.Error(err)
return
}
p.ExportForce("Test force exporting pool")
defer p.Close()
println("PASS\n")
}
func zpoolTestImport(t *testing.T) {
println("TEST POOL Import( ", TST_POOL_NAME, " ) ... ")
p, err := zfs.PoolImport(TST_POOL_NAME, []string{"/tmp"})
if err != nil {
t.Error(err)
return
}
defer p.Close()
println("PASS\n")
}
/* ------------------------------------------------------------------------- */
// EXAMPLES:
func ExamplePoolProp() { func ExamplePoolProp() {
if pool, err := zfs.PoolOpen("SSD"); err == nil { if pool, err := zfs.PoolOpen("SSD"); err == nil {
print("Pool size is: ", pool.Properties[zfs.PoolPropSize].Value) print("Pool size is: ", pool.Properties[zfs.PoolPropSize].Value)
@ -169,9 +221,6 @@ func ExamplePoolProp() {
} }
} }
/* ------------------------------------------------------------------------- */
// EXAMPLES:
// Open and list all pools on system with them properties // Open and list all pools on system with them properties
func ExamplePoolOpenAll() { func ExamplePoolOpenAll() {
// Lets open handles to all active pools on system // Lets open handles to all active pools on system