Skip to content

Commit

Permalink
test(disk): add direct filesystem tests
Browse files Browse the repository at this point in the history
  • Loading branch information
masahiro331 committed Jun 25, 2024
1 parent 95ceb2d commit 843ca5e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
SECTORS = 32768
SECTOR_SIZE = 512
TARGET_MBR = mbr.bin
TARGET_IMG = linux.img

mbr.bin:
dd if=/dev/zero of=$(TARGET_MBR) seek=$(SECTORS) bs=$(SECTOR_SIZE) count=1
fdisk -i -y $(TARGET_MBR)
fdisk -i -y $(TARGET_MBR)

# Linux only and require admin authority
#!!! Extract xfs from gpt using cmd/main.go.
linux.img: main
$(eval DEVICE := $(shell losetup -f))
dd of=$(TARGET_FS) count=0 seek=1 bs=41943040
losetup $(DEVICE) $(TARGET_FS)
parted $(DEVICE) -s mklabel gpt -s mkpart primary xfs 0 100%
mkfs.xfs $(DEVICE)p1
losetup -d $(DEVICE)

main: cmd/main.go
go build -o main cmd/main.go

fs.bin: linux.img main
./main linux.img
mv primary0 fs.bin
6 changes: 4 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"fmt"
"github.com/masahiro331/go-disk"
"io"
"log"
"os"

"github.com/masahiro331/go-disk"
"github.com/masahiro331/go-xfs-filesystem/xfs"
)

func main() {
Expand All @@ -21,7 +23,7 @@ func main() {
log.Fatal(err)
}
r := io.NewSectionReader(f, 0, fi.Size())
driver, err := disk.NewDriver(r)
driver, err := disk.NewDriver(r, xfs.Check)
if err != nil {
log.Fatal(err)
}
Expand Down
57 changes: 57 additions & 0 deletions disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package disk_test

import (
"io"
"os"
"strings"
"testing"

"github.com/masahiro331/go-disk"
"github.com/masahiro331/go-disk/fs"
"github.com/masahiro331/go-xfs-filesystem/xfs"
)

func TestNewDriver(t *testing.T) {
tests := []struct {
name string
inputFile string
wantErr string
checkFsFuncs []fs.CheckFsFunc
}{
{
name: "Happy path, Direct filesystem",
inputFile: "testdata/fs.bin",
wantErr: "",
checkFsFuncs: []fs.CheckFsFunc{xfs.Check},
},
{
name: "Invalid path, Direct filesystem no check fs functions",
inputFile: "testdata/fs.bin",
wantErr: "Invalid master boot record signature",
checkFsFuncs: []fs.CheckFsFunc{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(tt.inputFile)
if err != nil {
t.Fatal(err)
}
defer f.Close()

info, err := f.Stat()
if err != nil {
t.Fatal(err)
}
sr := io.NewSectionReader(f, 0, info.Size())
_, err = disk.NewDriver(sr, tt.checkFsFuncs...)

if tt.wantErr == "" && err != nil {
t.Errorf("input: %s, required no error = %v", tt.inputFile, err)
} else if tt.wantErr != "" && !strings.HasSuffix(err.Error(), tt.wantErr) {
t.Errorf("input: %s, expected: %q, actual: %q", tt.inputFile, tt.wantErr, err.Error())
}
})
}
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/masahiro331/go-disk

go 1.19

require golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
require (
github.com/masahiro331/go-xfs-filesystem v0.0.0-20231205045356-1b22259a6c44
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
)

require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.23.0 // indirect
)
24 changes: 22 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/masahiro331/go-xfs-filesystem v0.0.0-20231205045356-1b22259a6c44 h1:VmSjn0UCyfXUNdePDr7uM/uZTnGSp+mKD5+cYkEoLx4=
github.com/masahiro331/go-xfs-filesystem v0.0.0-20231205045356-1b22259a6c44/go.mod h1:QKBZqdn6teT0LK3QhAf3K6xakItd1LonOShOEC44idQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Binary file added testdata/fs.bin
Binary file not shown.

0 comments on commit 843ca5e

Please sign in to comment.