Skip to content

Commit

Permalink
recipe: Lazily initialize LVM
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Oct 29, 2023
1 parent a66f844 commit cc14085
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
58 changes: 29 additions & 29 deletions core/lvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

var (
lvmInstance lvm.Lvm
device string
lvmpart string
testLvm lvm.Lvm
device string
lvmpart string
)

func TestMain(m *testing.M) {
// Create LVM wrapper instance
lvmInstance = lvm.NewLvm()
testLvm = lvm.NewLvm()

// Setup testing device
// Create dummy image
Expand Down Expand Up @@ -68,103 +68,103 @@ func TestMain(m *testing.M) {
}

// Cleanup
lvmInstance.Dispose()
testLvm.Dispose()
os.Exit(status)
}

func TestPvcreate(t *testing.T) {
err := lvmInstance.Pvcreate(lvmpart + "1")
err := testLvm.Pvcreate(lvmpart + "1")
if err != nil {
t.Fatal(err)
}
}

func TestPvs(t *testing.T) {
pvs, err := lvmInstance.Pvs()
pvs, err := testLvm.Pvs()
fmt.Printf(" -> Returned: %v\n", pvs)
if err != nil {
t.Fatal(err)
}
}

func TestPvResize(t *testing.T) {
pvs, err := lvmInstance.Pvs()
pvs, err := testLvm.Pvs()
if err != nil {
t.Fatal(err)
}
err = lvmInstance.Pvresize(&pvs[0])
err = testLvm.Pvresize(&pvs[0])
if err != nil {
t.Fatal(err)
}
}

func TestPvShrink(t *testing.T) {
pvs, err := lvmInstance.Pvs()
pvs, err := testLvm.Pvs()
if err != nil {
t.Fatal(err)
}
err = lvmInstance.Pvresize(&pvs[0], 10.0)
err = testLvm.Pvresize(&pvs[0], 10.0)
if err != nil {
t.Fatal(err)
}

pvs, err = lvmInstance.Pvs()
pvs, err = testLvm.Pvs()
fmt.Printf(" -> New size: %v\n", pvs)
if err != nil {
t.Fatal(err)
}
}

func TestPvRemoveStr(t *testing.T) {
err := lvmInstance.Pvremove(lvmpart + "1")
err := testLvm.Pvremove(lvmpart + "1")
if err != nil {
t.Fatal(err)
}
}

func TestPvRemoveStruct(t *testing.T) {
// Recreate PV removed by previous test
err := lvmInstance.Pvcreate(lvmpart + "1")
err := testLvm.Pvcreate(lvmpart + "1")
if err != nil {
t.Fatal(err)
}

pvs, err := lvmInstance.Pvs(lvmpart + "1")
pvs, err := testLvm.Pvs(lvmpart + "1")
if err != nil {
t.Fatal(err)
}

err = lvmInstance.Pvremove(&pvs[0])
err = testLvm.Pvremove(&pvs[0])
if err != nil {
t.Fatal(err)
}
}

func TestVgCreate(t *testing.T) {
// Create two testing PVs
err := lvmInstance.Pvcreate(lvmpart + "1")
err := testLvm.Pvcreate(lvmpart + "1")
if err != nil {
t.Fatal(err)
}
err = lvmInstance.Pvcreate(lvmpart + "2")
err = testLvm.Pvcreate(lvmpart + "2")
if err != nil {
t.Fatal(err)
}

// Pass one PV as struct and another as string
pvs, err := lvmInstance.Pvs(lvmpart + "1")
pvs, err := testLvm.Pvs(lvmpart + "1")
if err != nil {
t.Fatal(err)
}

err = lvmInstance.Vgcreate("MyTestingVG", &pvs[0], lvmpart+"2")
err = testLvm.Vgcreate("MyTestingVG", &pvs[0], lvmpart+"2")
if err != nil {
t.Fatal(err)
}
}

func TestVgs(t *testing.T) {
vgs, err := lvmInstance.Vgs()
vgs, err := testLvm.Vgs()
fmt.Printf(" -> Returned: %v\n", vgs)
if err != nil {
t.Fatal(err)
Expand All @@ -173,7 +173,7 @@ func TestVgs(t *testing.T) {

func TestVgrename(t *testing.T) {
// Retrieve Vg
vgs, err := lvmInstance.Vgs()
vgs, err := testLvm.Vgs()
if err != nil {
t.Fatal(err)
}
Expand All @@ -183,7 +183,7 @@ func TestVgrename(t *testing.T) {
t.Fatal(err)
}

vgs, err = lvmInstance.Vgs("MyTestingVG1")
vgs, err = testLvm.Vgs("MyTestingVG1")
fmt.Printf(" -> Returned: %v\n", vgs)
if err != nil {
t.Fatal(err)
Expand All @@ -192,7 +192,7 @@ func TestVgrename(t *testing.T) {

func TestVgReduce(t *testing.T) {
// Retrieve Vg
vgs, err := lvmInstance.Vgs()
vgs, err := testLvm.Vgs()
if err != nil {
t.Fatal(err)
}
Expand All @@ -203,7 +203,7 @@ func TestVgReduce(t *testing.T) {
}

// Retrieve Vg
vgs, err = lvmInstance.Vgs()
vgs, err = testLvm.Vgs()
fmt.Printf(" -> Returned: %v\n", vgs)
if err != nil {
t.Fatal(err)
Expand All @@ -212,7 +212,7 @@ func TestVgReduce(t *testing.T) {

func TestVgExtend(t *testing.T) {
// Retrieve Vg
vgs, err := lvmInstance.Vgs()
vgs, err := testLvm.Vgs()
if err != nil {
t.Fatal(err)
}
Expand All @@ -223,22 +223,22 @@ func TestVgExtend(t *testing.T) {
}

// Retrieve Vg
vgs, err = lvmInstance.Vgs()
vgs, err = testLvm.Vgs()
fmt.Printf(" -> Returned: %v\n", vgs)
if err != nil {
t.Fatal(err)
}
}

func TestLvCreate(t *testing.T) {
err := lvmInstance.Lvcreate("MyLv0", "MyTestingVG1", lvm.LV_TYPE_LINEAR, 30)
err := testLvm.Lvcreate("MyLv0", "MyTestingVG1", lvm.LV_TYPE_LINEAR, 30)
if err != nil {
t.Fatal(err)
}
}

func TestLvs(t *testing.T) {
lvs, err := lvmInstance.Lvs()
lvs, err := testLvm.Lvs()
fmt.Printf(" -> Returned: %v\n", lvs)
if err != nil {
t.Fatal(err)
Expand Down
37 changes: 23 additions & 14 deletions core/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type PostStep struct {
Params []interface{}
}

var LvmInstance lvm.Lvm = lvm.NewLvm()
var lvmInstance *lvm.Lvm = nil

func ReadRecipe(path string) (*Recipe, error) {
content, err := os.ReadFile(path)
Expand Down Expand Up @@ -286,7 +286,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
*/
case "pvcreate":
part := args[0].(string)
err := LvmInstance.Pvcreate(part)
err := getOrInitLvm().Pvcreate(part)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -303,9 +303,9 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
var err error
if len(args) > 1 {
size := args[1].(float64)
err = LvmInstance.Pvresize(part, size)
err = getOrInitLvm().Pvresize(part, size)
} else {
err = LvmInstance.Pvresize(part)
err = getOrInitLvm().Pvresize(part)
}
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
Expand All @@ -319,7 +319,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
*/
case "pvremove":
part := args[0].(string)
err := LvmInstance.Pvremove(part)
err := getOrInitLvm().Pvremove(part)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -343,7 +343,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
for i, p := range pvs {
pvList[i] = p
}
err := LvmInstance.Vgcreate(name, pvList...)
err := getOrInitLvm().Vgcreate(name, pvList...)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -358,7 +358,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
case "vgrename":
oldName := args[0].(string)
newName := args[1].(string)
_, err := LvmInstance.Vgrename(oldName, newName)
_, err := getOrInitLvm().Vgrename(oldName, newName)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -380,7 +380,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
for i, p := range pvs {
pvList[i] = p
}
err := LvmInstance.Vgextend(name, pvList...)
err := getOrInitLvm().Vgextend(name, pvList...)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -402,7 +402,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
for i, p := range pvs {
pvList[i] = p
}
err := LvmInstance.Vgreduce(name, pvList...)
err := getOrInitLvm().Vgreduce(name, pvList...)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -415,7 +415,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
*/
case "vgremove":
name := args[0].(string)
err := LvmInstance.Vgremove(name)
err := getOrInitLvm().Vgremove(name)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -434,7 +434,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
vg := args[1].(string)
lvType := args[2].(string)
vgSize := args[3].(float64)
err := LvmInstance.Lvcreate(name, vg, lvm.LVType(lvType), vgSize)
err := getOrInitLvm().Lvcreate(name, vg, lvm.LVType(lvType), vgSize)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -451,7 +451,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
oldName := args[0].(string)
newName := args[1].(string)
vg := args[2].(string)
_, err := LvmInstance.Lvrename(oldName, newName, vg)
_, err := getOrInitLvm().Lvrename(oldName, newName, vg)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand All @@ -464,7 +464,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
*/
case "lvremove":
name := args[0].(string)
err := LvmInstance.Lvremove(name)
err := getOrInitLvm().Lvremove(name)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand Down Expand Up @@ -498,7 +498,7 @@ func runSetupOperation(diskLabel, operation string, args []interface{}) error {
vg := args[1].(string)
vgSize := args[2].(float64)
thinPool := args[3].(string)
err := LvmInstance.LvThinCreate(name, vg, thinPool, vgSize)
err := getOrInitLvm().LvThinCreate(name, vg, thinPool, vgSize)
if err != nil {
return fmt.Errorf("failed to execute operation %s: %s", operation, err)
}
Expand Down Expand Up @@ -1021,3 +1021,12 @@ func (recipe *Recipe) Install() error {

return nil
}

func getOrInitLvm() *lvm.Lvm {
if lvmInstance == nil {
l := lvm.NewLvm()
lvmInstance = &l
}

return lvmInstance
}

0 comments on commit cc14085

Please sign in to comment.