Skip to content

Commit

Permalink
test: add mount command unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Hughes committed Apr 4, 2022
1 parent a31c3cd commit 389f08f
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pkg/siftool/add_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -46,7 +46,7 @@ func Test_command_getAdd(t *testing.T) {
}
args = append(args, tt.flags...)

runCommand(t, cmd, args)
runCommand(t, cmd, args, nil)
})
}
}
4 changes: 2 additions & 2 deletions pkg/siftool/del_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -24,7 +24,7 @@ func Test_command_getDel(t *testing.T) {

cmd := c.getDel()

runCommand(t, cmd, []string{"1", makeTestSIF(t, true)})
runCommand(t, cmd, []string{"1", makeTestSIF(t, true)}, nil)
})
}
}
4 changes: 2 additions & 2 deletions pkg/siftool/dump_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -39,7 +39,7 @@ func Test_command_getDump(t *testing.T) {

cmd := c.getDump()

runCommand(t, cmd, []string{tt.id, tt.path})
runCommand(t, cmd, []string{tt.id, tt.path}, nil)
})
}
}
4 changes: 2 additions & 2 deletions pkg/siftool/header_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -68,7 +68,7 @@ func Test_command_getHeader(t *testing.T) {

cmd := c.getHeader()

runCommand(t, cmd, []string{tt.path})
runCommand(t, cmd, []string{tt.path}, nil)
})
}
}
4 changes: 2 additions & 2 deletions pkg/siftool/info_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -39,7 +39,7 @@ func Test_command_getInfo(t *testing.T) {

cmd := c.getInfo()

runCommand(t, cmd, []string{tt.id, tt.path})
runCommand(t, cmd, []string{tt.id, tt.path}, nil)
})
}
}
4 changes: 2 additions & 2 deletions pkg/siftool/list_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -68,7 +68,7 @@ func Test_command_getList(t *testing.T) {

cmd := c.getList()

runCommand(t, cmd, []string{tt.path})
runCommand(t, cmd, []string{tt.path}, nil)
})
}
}
61 changes: 61 additions & 0 deletions pkg/siftool/mount_test.go
@@ -0,0 +1,61 @@
// Copyright (c) 2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package siftool

import (
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/sylabs/sif/v2/pkg/sif"
)

func Test_command_getMount(t *testing.T) {
if _, err := exec.LookPath("squashfuse"); err != nil {
t.Skip("squashfuse not found, skipping mount tests")
}

tests := []struct {
name string
opts commandOpts
path string
wantErr error
}{
{
name: "Empty",
path: filepath.Join(corpus, "empty.sif"),
wantErr: sif.ErrNoObjects,
},
{
name: "OneGroup",
path: filepath.Join(corpus, "one-group.sif"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path, err := os.MkdirTemp("", "siftool-mount-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
cmd := exec.Command("fusermount", "-u", path)

if err := cmd.Run(); err != nil {
t.Log(err)
}

os.RemoveAll(path)
})

c := &command{opts: tt.opts}

cmd := c.getMount()

runCommand(t, cmd, []string{tt.path, path}, tt.wantErr)
})
}
}
4 changes: 2 additions & 2 deletions pkg/siftool/new_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -32,7 +32,7 @@ func Test_command_getNew(t *testing.T) {

cmd := c.getNew()

runCommand(t, cmd, []string{tf.Name()})
runCommand(t, cmd, []string{tf.Name()}, nil)
})
}
}
4 changes: 2 additions & 2 deletions pkg/siftool/setprim_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -24,7 +24,7 @@ func Test_command_getSetPrim(t *testing.T) {

cmd := c.getSetPrim()

runCommand(t, cmd, []string{"1", makeTestSIF(t, true)})
runCommand(t, cmd, []string{"1", makeTestSIF(t, true)}, nil)
})
}
}
9 changes: 5 additions & 4 deletions pkg/siftool/siftool_test.go
Expand Up @@ -6,6 +6,7 @@ package siftool

import (
"bytes"
"errors"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -47,7 +48,7 @@ func makeTestSIF(t *testing.T, withDataObject bool) string {
return tf.Name()
}

func runCommand(t *testing.T, cmd *cobra.Command, args []string) {
func runCommand(t *testing.T, cmd *cobra.Command, args []string, wantErr error) {
t.Helper()

var out, err bytes.Buffer
Expand All @@ -56,8 +57,8 @@ func runCommand(t *testing.T, cmd *cobra.Command, args []string) {

cmd.SetArgs(args)

if err := cmd.Execute(); err != nil {
t.Fatal(err)
if got, want := cmd.Execute(), wantErr; !errors.Is(got, want) {
t.Fatalf("got error %v, want %v", got, want)
}

g := goldie.New(t,
Expand Down Expand Up @@ -131,7 +132,7 @@ func TestAddCommands(t *testing.T) {
t.Fatal(err)
}

runCommand(t, cmd, tt.args)
runCommand(t, cmd, tt.args, nil)
})
}
}
@@ -0,0 +1 @@
Error: failed to get partition descriptor: no objects in image
9 changes: 9 additions & 0 deletions pkg/siftool/testdata/Test_command_getMount/Empty/out.golden
@@ -0,0 +1,9 @@
Usage:
mount <sif_path> <mount_path>

Examples:
mount image.sif path/

Flags:
-h, --help help for mount

Empty file.
Empty file.

0 comments on commit 389f08f

Please sign in to comment.