| 51 | } |
| 52 | |
| 53 | func Test_TestRun(t *testing.T) { |
| 54 | t.Parallel() |
| 55 | |
| 56 | t.Run("OK", func(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | |
| 59 | var ( |
| 60 | name, id = "test", "1" |
| 61 | runCalled atomic.Int64 |
| 62 | cleanupCalled atomic.Int64 |
| 63 | collectableCalled atomic.Int64 |
| 64 | |
| 65 | testFns = testFns{ |
| 66 | RunFn: func(ctx context.Context, id string, logs io.Writer) error { |
| 67 | runCalled.Add(1) |
| 68 | return nil |
| 69 | }, |
| 70 | CleanupFn: func(ctx context.Context, id string, logs io.Writer) error { |
| 71 | cleanupCalled.Add(1) |
| 72 | return nil |
| 73 | }, |
| 74 | GetMetricsFn: func() map[string]any { |
| 75 | collectableCalled.Add(1) |
| 76 | return nil |
| 77 | }, |
| 78 | } |
| 79 | ) |
| 80 | |
| 81 | run := harness.NewTestRun(name, id, testFns) |
| 82 | require.Equal(t, fmt.Sprintf("%s/%s", name, id), run.FullID()) |
| 83 | |
| 84 | err := run.Run(context.Background()) |
| 85 | require.NoError(t, err) |
| 86 | require.EqualValues(t, 1, runCalled.Load()) |
| 87 | require.EqualValues(t, 1, collectableCalled.Load()) |
| 88 | |
| 89 | err = run.Cleanup(context.Background()) |
| 90 | require.NoError(t, err) |
| 91 | require.EqualValues(t, 1, cleanupCalled.Load()) |
| 92 | }) |
| 93 | |
| 94 | t.Run("Cleanup", func(t *testing.T) { |
| 95 | t.Parallel() |
| 96 | |
| 97 | t.Run("NoFn", func(t *testing.T) { |
| 98 | t.Parallel() |
| 99 | |
| 100 | run := harness.NewTestRun("test", "1", testFns{ |
| 101 | RunFn: func(ctx context.Context, id string, logs io.Writer) error { |
| 102 | return nil |
| 103 | }, |
| 104 | CleanupFn: nil, |
| 105 | }) |
| 106 | |
| 107 | err := run.Cleanup(context.Background()) |
| 108 | require.NoError(t, err) |
| 109 | }) |
| 110 | |