From 507abac6835d8533ff2bf2e1a545b765fc4383a2 Mon Sep 17 00:00:00 2001 From: Faruk Kasumovic Date: Mon, 8 Jun 2015 23:41:22 +0200 Subject: [PATCH] - Add argument to pool Export for history log string, hard force (ExportForce), export and import tests, and overall tests fixes and improvements --- a_test.go | 6 ++++ zfs_test.go | 27 ++++++++++++--- zpool.go | 13 +++++-- zpool_test.go | 95 ++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 111 insertions(+), 30 deletions(-) diff --git a/a_test.go b/a_test.go index fae4a3c..7665100 100644 --- a/a_test.go +++ b/a_test.go @@ -9,6 +9,10 @@ import ( func Test(t *testing.T) { zpoolTestPoolCreate(t) + zpoolTestExport(t) + zpoolTestImport(t) + zpoolTestExportForce(t) + zpoolTestImport(t) zpoolTestPoolOpenAll(t) zpoolTestFailPoolOpen(t) @@ -20,4 +24,6 @@ func Test(t *testing.T) { zfsTestDatasetDestroy(t) zpoolTestPoolDestroy(t) + + cleanupVDisks() } diff --git a/zfs_test.go b/zfs_test.go index 5f82b3a..ae7858b 100644 --- a/zfs_test.go +++ b/zfs_test.go @@ -23,8 +23,7 @@ func printDatasets(ds []zfs.Dataset) error { if err != nil { return err } - fmt.Printf(" %30s | %10s\n", path, - p.Value) + fmt.Printf(" %30s | %10s\n", path, p.Value) if len(d.Children) > 0 { printDatasets(d.Children) } @@ -159,13 +158,33 @@ func ExampleDatasetOpen() { d, err := zfs.DatasetOpen("TESTPOOL/DATASET1") if err != nil { panic(err.Error()) - return } defer d.Close() var p zfs.Property if p, err = d.GetProperty(zfs.ZFSPropAvailable); err != nil { panic(err.Error()) - return } 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) + } + +} diff --git a/zpool.go b/zpool.go index cc1b392..efeb768 100644 --- a/zpool.go +++ b/zpool.go @@ -549,13 +549,20 @@ func (pool *Pool) Destroy(logStr string) (err error) { // 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 // 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 if force { force_t = 1 } - retcode := C.zpool_export(pool.list.zph, force_t, nil) - if retcode != 0 { + if rc := C.zpool_export(pool.list.zph, force_t, C.CString(log)); rc != 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() } return diff --git a/zpool_test.go b/zpool_test.go index 3a19add..2f2e5e0 100644 --- a/zpool_test.go +++ b/zpool_test.go @@ -26,6 +26,36 @@ func CreateTmpSparse(prefix string, size int64) (path string, err error) { 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: @@ -43,26 +73,13 @@ func zpoolTestPoolCreate(t *testing.T) { p.Close() TST_POOL_NAME += "0" } - - var s1path, s2path, s3path string var err error - if s1path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil { - t.Error(err) - 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) + + if err = createTestpoolVdisks(); err != nil { t.Error(err) return } + disks := [2]string{s1path, s2path} var vdevs, mdevs, sdevs []zfs.VDevSpec @@ -95,10 +112,7 @@ func zpoolTestPoolCreate(t *testing.T) { return } defer pool.Close() - // try cleanup - os.Remove(s1path) - os.Remove(s2path) - os.Remove(s3path) + println("PASS\n") } @@ -159,6 +173,44 @@ func zpoolTestFailPoolOpen(t *testing.T) { 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() { if pool, err := zfs.PoolOpen("SSD"); err == nil { 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 func ExamplePoolOpenAll() { // Lets open handles to all active pools on system