- Improvements on properties, state and status handling of pool and dataset

This commit is contained in:
Faruk Kasumovic 2015-06-09 14:26:35 +02:00
parent 1d6e6a86cb
commit c1288a9a2e
5 changed files with 79 additions and 8 deletions

View File

@ -13,6 +13,8 @@ func Test(t *testing.T) {
zpoolTestImport(t)
zpoolTestExportForce(t)
zpoolTestImport(t)
zpoolTestPoolProp(t)
zpoolTestPoolStatusAndState(t)
zpoolTestPoolOpenAll(t)
zpoolTestFailPoolOpen(t)

6
zfs.go
View File

@ -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"
}

View File

@ -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() {

View File

@ -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
}

View File

@ -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("")
}