MCPcopy Index your code
hub / github.com/coder/coder / TestCleanStaleSessions

Function TestCleanStaleSessions

provisionersdk/tfpath/tfpath_test.go:16–77  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestCleanStaleSessions(t *testing.T) {
17 t.Parallel()
18
19 t.Run("NonFatalRemoveFailure", func(t *testing.T) {
20 t.Parallel()
21 const parentDir = "parent"
22 // Verify RemoveAll failure is not fatal
23 ctx := testutil.Context(t, testutil.WaitShort)
24
25 called := false
26 mem := afero.NewMemMapFs()
27 staleSession := tfpath.Session(parentDir, "stale")
28 err := mem.MkdirAll(staleSession.WorkDirectory(), 0o777)
29 require.NoError(t, err)
30
31 failingFs := &removeFailure{
32 Fs: mem,
33 removeAll: func(path string) error {
34 called = true
35 return xerrors.New("constant failure")
36 },
37 }
38
39 future := time.Now().Add(time.Hour * 24 * 120)
40 l := tfpath.Session(parentDir, "sess1")
41 err = l.CleanStaleSessions(ctx, slogtest.Make(t, &slogtest.Options{
42 IgnoreErrors: true,
43 }), failingFs, future)
44 require.NoError(t, err)
45 require.True(t, called)
46 })
47
48 t.Run("FatalRemoveFailure", func(t *testing.T) {
49 // If the stale directory is the same one we plan to use, that is
50 // an issue.
51 t.Parallel()
52 const parentDir = "parent"
53 // Verify RemoveAll failure is not fatal
54 ctx := testutil.Context(t, testutil.WaitShort)
55
56 called := false
57 mem := afero.NewMemMapFs()
58 staleSession := tfpath.Session(parentDir, "stale")
59 err := mem.MkdirAll(staleSession.WorkDirectory(), 0o777)
60 require.NoError(t, err)
61
62 failingFs := &removeFailure{
63 Fs: mem,
64 removeAll: func(path string) error {
65 called = true
66 return xerrors.New("constant failure")
67 },
68 }
69
70 future := time.Now().Add(time.Hour * 24 * 120)
71 err = staleSession.CleanStaleSessions(ctx, slogtest.Make(t, &slogtest.Options{
72 IgnoreErrors: true,
73 }), failingFs, future)

Callers

nothing calls this directly

Calls 8

ContextFunction · 0.92
SessionFunction · 0.92
MkdirAllMethod · 0.80
WorkDirectoryMethod · 0.80
CleanStaleSessionsMethod · 0.80
RunMethod · 0.65
NewMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected