From c1288a9a2ebb6c385730f99a664f6d30d5255db0 Mon Sep 17 00:00:00 2001 From: Faruk Kasumovic Date: Tue, 9 Jun 2015 14:26:35 +0200 Subject: [PATCH] - Improvements on properties, state and status handling of pool and dataset --- a_test.go | 2 ++ zfs.go | 6 ++++- zfs_test.go | 2 +- zpool.go | 12 ++++++---- zpool_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 79 insertions(+), 8 deletions(-) diff --git a/a_test.go b/a_test.go index 7665100..d5b8204 100644 --- a/a_test.go +++ b/a_test.go @@ -13,6 +13,8 @@ func Test(t *testing.T) { zpoolTestImport(t) zpoolTestExportForce(t) zpoolTestImport(t) + zpoolTestPoolProp(t) + zpoolTestPoolStatusAndState(t) zpoolTestPoolOpenAll(t) zpoolTestFailPoolOpen(t) diff --git a/zfs.go b/zfs.go index 6b07fba..c4a1043 100644 --- a/zfs.go +++ b/zfs.go @@ -266,6 +266,10 @@ func (d *Dataset) SetProperty(p ZFSProp, value string) (err error) { if errcode != 0 { err = LastError() } + // Update Properties member with change made + if _, err = d.GetProperty(p); err != nil { + return + } return } @@ -398,7 +402,7 @@ func (d *Dataset) UnmountAll(flags int) (err error) { // ( returns built in string representation of property name). // This is optional, you can represent each property with string // name of choice. -func (d *Dataset) PropertyToName(p ZFSProp) (name string) { +func DatasetPropertyToName(p ZFSProp) (name string) { if p == ZFSNumProps { return "numofprops" } diff --git a/zfs_test.go b/zfs_test.go index ae7858b..2e4d833 100644 --- a/zfs_test.go +++ b/zfs_test.go @@ -164,7 +164,7 @@ func ExampleDatasetOpen() { if p, err = d.GetProperty(zfs.ZFSPropAvailable); err != nil { panic(err.Error()) } - println(d.PropertyToName(zfs.ZFSPropAvailable), " = ", p.Value) + println(zfs.DatasetPropertyToName(zfs.ZFSPropAvailable), " = ", p.Value) } func ExampleDatasetOpenAll() { diff --git a/zpool.go b/zpool.go index efeb768..0ef1c33 100644 --- a/zpool.go +++ b/zpool.go @@ -127,7 +127,7 @@ func PoolCloseAll(pools []Pool) { // ( returns built in string representation of property name). // This is optional, you can represent each property with string // name of choice. -func (pool *Pool) PropertyToName(p PoolProp) (name string) { +func PoolPropertyToName(p PoolProp) (name string) { if p == PoolNumProps { return "numofprops" } @@ -171,7 +171,7 @@ func (pool *Pool) GetProperty(p PoolProp) (prop Property, err error) { // First check if property exist at all if p < PoolPropName || p > PoolNumProps { err = errors.New(fmt.Sprint("Unknown zpool property: ", - pool.PropertyToName(p))) + PoolPropertyToName(p))) return } var list C.property_list_t @@ -210,15 +210,17 @@ func (pool *Pool) SetProperty(p PoolProp, value string) (err error) { // First check if property exist at all if p < PoolPropName || p > PoolNumProps { err = errors.New(fmt.Sprint("Unknown zpool property: ", - pool.PropertyToName(p))) + PoolPropertyToName(p))) return } - r := C.zpool_set_prop(pool.list.zph, C.CString(pool.PropertyToName(p)), C.CString(value)) + r := C.zpool_set_prop(pool.list.zph, C.CString(PoolPropertyToName(p)), C.CString(value)) if r != 0 { err = LastError() } else { // Update Properties member with change made - _, err = pool.GetProperty(p) + if _, err = pool.GetProperty(p); err != nil { + return + } } return } diff --git a/zpool_test.go b/zpool_test.go index d08678b..43f6de5 100644 --- a/zpool_test.go +++ b/zpool_test.go @@ -208,6 +208,59 @@ func zpoolTestImport(t *testing.T) { println("PASS\n") } +func zpoolTestPoolProp(t *testing.T) { + println("TEST PoolProp on ", TST_POOL_NAME, " ... ") + if pool, err := zfs.PoolOpen(TST_POOL_NAME); err == nil { + defer pool.Close() + // Turn on snapshot listing for pool + pool.SetProperty(zfs.PoolPropListsnaps, "on") + // Verify change is succesfull + if pool.Properties[zfs.PoolPropListsnaps].Value != "on" { + t.Error(fmt.Errorf("Update of pool property failed")) + return + } + + // Test fetching property + _, err := pool.GetProperty(zfs.PoolPropHealth) + if err != nil { + t.Error(err) + return + } + + // fetch all properties + if err = pool.ReloadProperties(); err != nil { + t.Error(err) + return + } + } else { + t.Error(err) + return + } + println("PASS\n") +} + +func zpoolTestPoolStatusAndState(t *testing.T) { + println("TEST pool Status/State ( ", TST_POOL_NAME, " ) ... ") + pool, err := zfs.PoolOpen(TST_POOL_NAME) + if err != nil { + t.Error(err.Error()) + return + } + defer pool.Close() + + if _, err = pool.Status(); err != nil { + t.Error(err.Error()) + return + } + + if _, err = pool.State(); err != nil { + t.Error(err.Error()) + return + } + + println("PASS\n") +} + /* ------------------------------------------------------------------------- */ // EXAMPLES: @@ -216,6 +269,15 @@ func ExamplePoolProp() { print("Pool size is: ", pool.Properties[zfs.PoolPropSize].Value) // Turn on snapshot listing for pool pool.SetProperty(zfs.PoolPropListsnaps, "on") + println("Changed property", + zfs.PoolPropertyToName(zfs.PoolPropListsnaps), "to value:", + pool.Properties[zfs.PoolPropListsnaps].Value) + + prop, err := pool.GetProperty(zfs.PoolPropHealth) + if err != nil { + panic(err) + } + println("Update and print out pool health:", prop.Value) } else { print("Error: ", err) } @@ -244,7 +306,8 @@ func ExamplePoolOpenAll() { if pkey == zfs.PoolPropName { continue // Skip name its already printed above } - fmt.Printf("|%14s | %20s | %15s |\n", p.PropertyToName(pkey), + fmt.Printf("|%14s | %20s | %15s |\n", + zfs.PoolPropertyToName(pkey), prop.Value, prop.Source) println("") }