Skip to content

Commit

Permalink
context: Fix directory leakage for snapshots contexts
Browse files Browse the repository at this point in the history
Fix an error preventing the auto-unpacked snapshot files to be deleted.
Add a unit-test to expose the issue.

Signed-off-by: Marcel Apfelbaum <marcel@redhat.com>
  • Loading branch information
marcel-apf committed Apr 11, 2021
1 parent 1a1bc99 commit 9840479
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (ctx *Context) Teardown() error {
// then it is also in charge of the cleanup.
return nil
}
return snapshot.Cleanup(ctx.SnapshotRoot)
return snapshot.Cleanup(ctx.Chroot)
}

func (ctx *Context) Warn(msg string, args ...interface{}) {
Expand Down
42 changes: 42 additions & 0 deletions pkg/context/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//

package context_test

import (
"os"
"testing"

"github.com/jaypipes/ghw/pkg/context"
"github.com/jaypipes/ghw/pkg/option"
)

const (
testDataSnapshot = "../snapshot/testdata.tar.gz"
)

// nolint: gocyclo
func TestSnapshotContext(t *testing.T) {
ctx := context.New(option.WithSnapshot(option.SnapshotOptions{
Path: testDataSnapshot,
}))

var uncompressedDir string
err := ctx.Do(func() error {
uncompressedDir = ctx.Chroot
return nil
})

if len(uncompressedDir) == 0 {
t.Fatalf("Expected the uncompressed dir path to not be empty")
}
if err != nil {
t.Fatalf("Expected nil err, but got %v", err)
}
if _, err = os.Stat(uncompressedDir); !os.IsNotExist(err) {
t.Fatalf("Expected the uncompressed dir to be deleted: %s", uncompressedDir)
}
}

0 comments on commit 9840479

Please sign in to comment.