-
Notifications
You must be signed in to change notification settings - Fork 2k
/
testing.go
39 lines (30 loc) · 910 Bytes
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package allocdir
import (
"os"
hclog "github.com/hashicorp/go-hclog"
testing "github.com/mitchellh/go-testing-interface"
)
// TestAllocDir returns a built alloc dir in a temporary directory and cleanup
// func.
func TestAllocDir(t testing.T, 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, 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
}