Skip to content

Commit

Permalink
Make tests less flaky
Browse files Browse the repository at this point in the history
Signed-off-by: Maksym Pavlenko <[email protected]>
  • Loading branch information
mxpv authored and fahedouch committed Aug 7, 2020
1 parent 021ba62 commit 613523b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 9 additions & 2 deletions snapshots/devmapper/losetup/losetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

// FindAssociatedLoopDevices returns a list of loop devices attached to a given image
Expand Down Expand Up @@ -65,7 +66,7 @@ func RemoveLoopDevicesAssociatedWithImage(imagePath string) error {
}

for _, loopDevice := range loopDevices {
if err = DetachLoopDevice(loopDevice); err != nil {
if err = DetachLoopDevice(loopDevice); err != nil && err != unix.ENOENT {
return err
}
}
Expand All @@ -75,9 +76,15 @@ func RemoveLoopDevicesAssociatedWithImage(imagePath string) error {

// losetup is a wrapper around losetup command line tool
func losetup(args ...string) (string, error) {
data, err := exec.Command("losetup", args...).CombinedOutput()
cmd := exec.Command("losetup", args...)
cmd.Env = append(cmd.Env, "LANG=C")
data, err := cmd.CombinedOutput()
output := string(data)
if err != nil {
if strings.Contains(output, "No such file or directory") || strings.Contains(output, "No such device") {
return "", unix.ENOENT
}

return "", errors.Wrapf(err, "losetup %s\nerror: %s\n", strings.Join(args, " "), output)
}

Expand Down
9 changes: 8 additions & 1 deletion snapshots/devmapper/losetup/losetup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"os"
"testing"

"github.com/containerd/containerd/pkg/testutil"
"github.com/docker/go-units"
"golang.org/x/sys/unix"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"

"github.com/containerd/containerd/pkg/testutil"
)

func TestLosetup(t *testing.T) {
Expand Down Expand Up @@ -98,6 +100,11 @@ func TestLosetup(t *testing.T) {
err := RemoveLoopDevicesAssociatedWithImage("")
assert.NilError(t, err)
})

t.Run("DetachInvalidDevice", func(t *testing.T) {
err := DetachLoopDevice("/dev/loop_invalid_idx")
assert.Equal(t, unix.ENOENT, err)
})
}

func createSparseImage(t *testing.T) string {
Expand Down

0 comments on commit 613523b

Please sign in to comment.