Skip to content

Commit

Permalink
Test temp dir
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 23, 2024
1 parent 00b735d commit 6ad4c77
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
50 changes: 42 additions & 8 deletions cmd/mkuimage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
"testing"
Expand All @@ -23,6 +24,20 @@ import (
"golang.org/x/sync/errgroup"
)

func hasTempDir(t *testing.T, output string) {
t.Helper()
tempDir := regexp.MustCompile(`Keeping temp dir (.+)`).FindStringSubmatch(output)
if tempDir == nil {
t.Errorf("Keeping temp dir not found in output")
return
}
if fi, err := os.Stat(tempDir[1]); err != nil {
t.Error(err)
} else if !fi.IsDir() {
t.Errorf("Stat(%s) = %v, want directory", tempDir[1], fi)
}
}

func TestUrootCmdline(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -63,6 +78,7 @@ func TestUrootCmdline(t *testing.T) {
args []string
exitCode int
validators []itest.ArchiveValidator
wantOutput func(*testing.T, string)
}

noCmdTests := []testCase{
Expand Down Expand Up @@ -153,9 +169,21 @@ func TestUrootCmdline(t *testing.T) {
env: []string{"GOARCH=amd64"},
args: []string{
"-defaultsh=echo",
"github.com/u-root/u-root/cmds/core/echo",
"github.com/u-root/u-root/cmds/core/init",
},
},
{
name: "AMD64 build with temp dir",
env: []string{"GOARCH=amd64"},
args: []string{
"--keep-tmp-dir",
"--defaultsh=echo",
"github.com/u-root/u-root/cmds/core/echo",
"github.com/u-root/u-root/cmds/core/init",
},
exitCode: 1,
wantOutput: hasTempDir,
},
{
name: "ARM7 build",
Expand Down Expand Up @@ -191,22 +219,28 @@ func TestUrootCmdline(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
var g errgroup.Group
var f1, f2 *os.File
var out string
var sum1, sum2 []byte

g.Go(func() error {
var err error
f1, sum1, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir)
f1, out, sum1, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir)
return err
})

g.Go(func() error {
var err error
f2, sum2, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir)
f2, _, sum2, err = buildIt(t, execPath, tt.args, tt.env, gocoverdir)
return err
})

err := g.Wait()
if tt.wantOutput != nil {
tt.wantOutput(t, out)
}

var exitErr *exec.ExitError
if err := g.Wait(); errors.As(err, &exitErr) {
if errors.As(err, &exitErr) {
if ec := exitErr.Sys().(syscall.WaitStatus).ExitStatus(); ec != tt.exitCode {
t.Errorf("mkuimage exit code = %d, want %d", ec, tt.exitCode)
}
Expand Down Expand Up @@ -235,11 +269,11 @@ func TestUrootCmdline(t *testing.T) {
}
}

func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir string) (*os.File, []byte, error) {
func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir string) (*os.File, string, []byte, error) {
t.Helper()
initramfs, err := os.CreateTemp(t.TempDir(), "u-root-")
if err != nil {
return nil, nil, err
return nil, "", nil, err
}

// Use the u-root command outside of the $GOPATH tree to make sure it
Expand All @@ -254,14 +288,14 @@ func buildIt(t *testing.T, execPath string, args, env []string, gocoverdir strin
out, err := c.CombinedOutput()
t.Logf("Output:\n%s", out)
if err != nil {
return nil, nil, err
return nil, string(out), nil, err
}

h1 := sha256.New()
if _, err := io.Copy(h1, initramfs); err != nil {
return nil, nil, err
return nil, string(out), nil, err
}
return initramfs, h1.Sum(nil), nil
return initramfs, string(out), h1.Sum(nil), nil
}

func TestCheckArgs(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package mkuimage
import (
_ "github.com/u-root/u-root/cmds/core/dhclient"
_ "github.com/u-root/u-root/cmds/core/echo"
_ "github.com/u-root/u-root/cmds/core/elvish"
_ "github.com/u-root/u-root/cmds/core/init"
_ "github.com/u-root/u-root/cmds/core/ip"
_ "github.com/u-root/u-root/cmds/core/ls"
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ require (
github.com/josharian/native v1.1.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mdlayher/packet v1.1.2 // indirect
github.com/mdlayher/socket v0.5.0 // indirect
github.com/pierrec/lz4/v4 v4.1.14 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vishvananda/netlink v1.2.1-beta.2 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/dl v0.0.0-20240206215513-68cc34b6ca6f // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
src.elv.sh v0.16.0-rc1.0.20220116211855-fda62502ad7f // indirect
)
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/creack/pty v1.1.15/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -25,7 +24,6 @@ github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mdlayher/packet v1.1.2 h1:3Up1NG6LZrsgDVn6X4L9Ge/iyRyxFEFD9o6Pr3Q1nQY=
Expand Down Expand Up @@ -55,10 +53,6 @@ github.com/vishvananda/netlink v1.2.1-beta.2/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhg
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
golang.org/dl v0.0.0-20240206215513-68cc34b6ca6f h1:IWM5GianvyTx750h5TRfWms43pREUt1gQ0ZdVQdoiOM=
golang.org/dl v0.0.0-20240206215513-68cc34b6ca6f/go.mod h1:fwQ+hlTD8I6TIzOGkQqxQNfE2xqR+y7SzGaDkksVFkw=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
Expand All @@ -69,10 +63,6 @@ golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
Expand Down
2 changes: 0 additions & 2 deletions uimage/builder/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestBinaryBuild(t *testing.T) {
Env: golang.Default(golang.DisableCGO()),
Packages: []string{
"../../cmd/mkuimage",
"github.com/u-root/u-root/cmds/core/elvish",
"github.com/u-root/u-root/cmds/core/init",
"cmd/test2json",
},
Expand All @@ -31,7 +30,6 @@ func TestBinaryBuild(t *testing.T) {
}

mustContain := []string{
"bin/elvish",
"bin/mkuimage",
"bin/test2json",
"bin/init",
Expand Down

0 comments on commit 6ad4c77

Please sign in to comment.