Files
nomad/client/allocdir/testing.go
James Rasell 85c30dfd1e test: Remove use of "mitchellh/go-testing-interface" for stdlib. (#25640)
The stdlib testing package now includes this interface, so we can
remove our dependency on the external library.
2025-04-14 07:43:49 +01:00

40 lines
875 B
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package allocdir
import (
"os"
"testing"
hclog "github.com/hashicorp/go-hclog"
)
// TestAllocDir returns a built alloc dir in a temporary directory and cleanup
// func.
func TestAllocDir(t testing.TB, l hclog.Logger, prefix, id string) (*AllocDir, func()) {
dir, err := os.MkdirTemp("", prefix)
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
allocDir := NewAllocDir(l, dir, dir, id)
cleanup := func() {
if err := os.RemoveAll(dir); err != nil {
t.Logf("error cleaning up alloc dir %q: %v", prefix, err)
}
if err := allocDir.Destroy(); err != nil {
t.Logf("error cleaning up alloc dir %q: %v", prefix, err)
}
}
if err := allocDir.Build(); err != nil {
cleanup()
t.Fatalf("error building alloc dir %q: %v", prefix, err)
}
return allocDir, cleanup
}