-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed device major minor parse failure
Added udevadm settle to wait device ready and refact Signed-off-by: JUN JIE NAN <[email protected]>
- Loading branch information
Showing
2 changed files
with
180 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/lxc/distrobuilder/shared" | ||
) | ||
|
||
func lsblkOutputHelper(t *testing.T, v *vm, args [][]string) (rb func()) { | ||
t.Helper() | ||
// Prepare image file | ||
f, err := os.CreateTemp("", "lsblkOutput*.raw") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
v.imageFile = f.Name() | ||
err = f.Truncate(2e8) // 200MB file for test | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = f.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
v.ctx = context.Background() | ||
// Format disk | ||
if args == nil { | ||
err = v.createPartitions() | ||
} else { | ||
err = v.createPartitions(args...) | ||
} | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// losetup | ||
err = v.losetup() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rb = func() { | ||
err := shared.RunCommand(v.ctx, nil, nil, "losetup", "-d", v.loopDevice) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = os.RemoveAll(v.imageFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func TestLsblkOutput(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
args [][]string | ||
want int | ||
}{ | ||
{"DiskEmpty", [][]string{{"--zap-all"}}, 1}, | ||
{"UEFI", nil, 3}, | ||
{"MBR", [][]string{{"--new=1::"}, {"--gpttombr"}}, 2}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
v := &vm{} | ||
rb := lsblkOutputHelper(t, v, tc.args) | ||
defer rb() | ||
parse, num, err := v.lsblkLoopDevice() | ||
if err != nil || num != tc.want { | ||
t.Fatal(err, num, tc.want) | ||
} | ||
|
||
for i := 0; i < num; i++ { | ||
major, minor, err := parse(i) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if major == 0 && minor == 0 { | ||
t.Fatal(major, minor) | ||
} | ||
} | ||
}) | ||
} | ||
} |