Skip to content

Commit

Permalink
Add disk-related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Feb 5, 2024
1 parent fc87767 commit 9482535
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deb-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

- name: Build debian package
run: |
dpkg-buildpackage --no-sign
make deb
mv ../*.deb ../albius.deb
- uses: softprops/action-gh-release@v1
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
apt-get install -y gcc pkg-config libbtrfs-dev libdevmapper-dev libgpgme-dev lvm2
- name: Build
run: go build -v ./...
run: make build

test:
runs-on: ubuntu-latest
Expand All @@ -40,4 +40,4 @@ jobs:
- name: Test
run: |
distrobox enter -r albius_test -- sudo go test -v ./...
distrobox enter -r albius_test -- sudo make test
128 changes: 128 additions & 0 deletions core/disk/disk_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,136 @@
package disk

import (
"os"
"os/exec"
"runtime"
"strings"
"testing"

luks "github.com/vanilla-os/albius/core/disk/luks"
)

var diskPath string

func TestMain(m *testing.M) {
_, filename, _, _ := runtime.Caller(0)
projRoot, _, _ := strings.Cut(filename, "core/")

device, err := exec.Command(projRoot+"utils/create_test_device.sh", "-o", "test.img", "-s", "51200").Output()
if err != nil {
panic(err)
}
diskPath = strings.TrimSpace(string(device))

status := m.Run()

err = exec.Command(projRoot+"utils/remove_test_device.sh", diskPath, "test.img").Run()
if err != nil {
panic(err)
}
os.Exit(status)
}

func TestLocateDisk(t *testing.T) {
d, err := LocateDisk(diskPath)
if err != nil {
t.Error(err)
}

if d.Path != diskPath {
t.Errorf("Located incorrect disk: %v", d)
}
}

func TestLabelDisk(t *testing.T) {
d, err := LocateDisk(diskPath)
if err != nil {
t.Error(err)
}

err = d.LabelDisk(GPT)
if err != nil {
t.Error(err)
}
}

func TestNewPartition(t *testing.T) {
d, err := LocateDisk(diskPath)
if err != nil {
t.Error(err)
}

_, err = d.NewPartition("", EXT4, 1, 25)
if err != nil {
t.Error(err)
}

_, err = d.NewPartition("", EXT4, 26, -1)
if err != nil {
t.Error(err)
}
}

func TestGetPartition(t *testing.T) {
d, err := LocateDisk(diskPath)
if err != nil {
t.Error(err)
}

if p := d.GetPartition(1); p == nil {
t.Error(err)
}
}

func TestLuksFormat(t *testing.T) {
d, err := LocateDisk(diskPath)
if err != nil {
t.Error(err)
}

if err = luks.LuksFormat(&d.Partitions[0], "test"); err != nil {
t.Error(err)
}
}

func TestIsLuks(t *testing.T) {
d, err := LocateDisk(diskPath)
if err != nil {
t.Error(err)
}

isLuks, err := luks.IsLuks(&d.Partitions[0])
if err != nil {
t.Error(err)
}
if !isLuks {
t.Error("Failed to detect partition as LUKS-encrypted")
}

isLuks, err = luks.IsLuks(&d.Partitions[1])
if err != nil {
t.Error(err)
}
if isLuks {
t.Error("Wrongly detected partition as LUKS-encrypted")
}
}

func TestLuksOpen(t *testing.T) {
d, err := LocateDisk(diskPath)
if err != nil {
t.Error(err)
}

err = luks.LuksOpen(&d.Partitions[0], "luks-test", "test")
if err != nil {
t.Error(err)
}
}

func TestLuksClose(t *testing.T) {
err := luks.LuksClose("luks-test")
if err != nil {
t.Error(err)
}
}
4 changes: 3 additions & 1 deletion utils/create_test_device.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ done
dd if=/dev/zero of="$output" bs=1024 count="$size" > /dev/null 2>&1
device=$(losetup --find --show "$output")

parted -s "$device" mklabel gpt
if [ ${#partitions[@]} != 0 ]; then
parted -s "$device" mklabel gpt
fi

for partition in "${partitions[@]}"; do
IFS=";" read -r -a args <<< "${partition}"
Expand Down
2 changes: 1 addition & 1 deletion utils/create_test_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -e

distrobox-create -r -I -Y -ap "golang libbtrfs-dev libdevmapper-dev libgpgme-dev build-essential pkg-config lvm2 parted udev" -i ghcr.io/vanilla-os/dev:main albius_test
distrobox-create -r -I -Y -ap "golang libbtrfs-dev libdevmapper-dev libgpgme-dev build-essential pkg-config lvm2 cryptsetup parted udev" -i ghcr.io/vanilla-os/dev:main albius_test

0 comments on commit 9482535

Please sign in to comment.