-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtfpath_test.go
More file actions
89 lines (75 loc) · 2.17 KB
/
tfpath_test.go
File metadata and controls
89 lines (75 loc) · 2.17 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tfpath_test
import (
"testing"
"time"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/provisionersdk/tfpath"
"github.com/coder/coder/v2/testutil"
)
func TestCleanStaleSessions(t *testing.T) {
t.Parallel()
t.Run("NonFatalRemoveFailure", func(t *testing.T) {
t.Parallel()
const parentDir = "parent"
// Verify RemoveAll failure is not fatal
ctx := testutil.Context(t, testutil.WaitShort)
called := false
mem := afero.NewMemMapFs()
staleSession := tfpath.Session(parentDir, "stale")
err := mem.MkdirAll(staleSession.WorkDirectory(), 0o777)
require.NoError(t, err)
failingFs := &removeFailure{
Fs: mem,
removeAll: func(path string) error {
called = true
return xerrors.New("constant failure")
},
}
future := time.Now().Add(time.Hour * 24 * 120)
l := tfpath.Session(parentDir, "sess1")
err = l.CleanStaleSessions(ctx, slogtest.Make(t, &slogtest.Options{
IgnoreErrors: true,
}), failingFs, future)
require.NoError(t, err)
require.True(t, called)
})
t.Run("FatalRemoveFailure", func(t *testing.T) {
// If the stale directory is the same one we plan to use, that is
// an issue.
t.Parallel()
const parentDir = "parent"
// Verify RemoveAll failure is not fatal
ctx := testutil.Context(t, testutil.WaitShort)
called := false
mem := afero.NewMemMapFs()
staleSession := tfpath.Session(parentDir, "stale")
err := mem.MkdirAll(staleSession.WorkDirectory(), 0o777)
require.NoError(t, err)
failingFs := &removeFailure{
Fs: mem,
removeAll: func(path string) error {
called = true
return xerrors.New("constant failure")
},
}
future := time.Now().Add(time.Hour * 24 * 120)
err = staleSession.CleanStaleSessions(ctx, slogtest.Make(t, &slogtest.Options{
IgnoreErrors: true,
}), failingFs, future)
require.ErrorContains(t, err, "constant failure")
require.True(t, called)
})
}
type removeFailure struct {
afero.Fs
removeAll func(path string) error
}
func (rf *removeFailure) RemoveAll(path string) error {
if rf.removeAll != nil {
return rf.removeAll(path)
}
return rf.Fs.RemoveAll(path)
}