github.com/helmwave/helmwave@v0.36.4-0.20240509190856-b35563eba4c6/pkg/hooks/run_test.go (about) 1 package hooks_test 2 3 import ( 4 "context" 5 "errors" 6 "os/exec" 7 "testing" 8 9 "github.com/helmwave/helmwave/pkg/hooks" 10 "github.com/helmwave/helmwave/tests" 11 log "github.com/sirupsen/logrus" 12 "github.com/stretchr/testify/mock" 13 "github.com/stretchr/testify/suite" 14 ) 15 16 var _ hooks.Hook = (*MockHook)(nil) 17 18 type MockHook struct { 19 mock.Mock 20 } 21 22 func (m *MockHook) Run(_ context.Context) error { 23 return m.Called().Error(0) 24 } 25 26 func (m *MockHook) Log() *log.Entry { 27 return m.Called().Get(0).(*log.Entry) 28 } 29 30 type LifecycleRunTestSuite struct { 31 suite.Suite 32 33 ctx context.Context 34 } 35 36 func TestLifecycleRunTestSuite(t *testing.T) { 37 t.Parallel() 38 suite.Run(t, new(LifecycleRunTestSuite)) 39 } 40 41 func (ts *LifecycleRunTestSuite) SetupTest() { 42 ts.ctx = tests.GetContext(ts.T()) 43 } 44 45 func (ts *LifecycleRunTestSuite) TestRunNoError() { 46 hook := &MockHook{} 47 hook.On("Run").Return(nil) 48 49 lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}} 50 51 err := lifecycle.RunPreBuild(ts.ctx) 52 53 ts.Require().NoError(err) 54 hook.AssertExpectations(ts.T()) 55 } 56 57 func (ts *LifecycleRunTestSuite) TestRunError() { 58 hook := &MockHook{} 59 errExpected := errors.New("test 123") 60 hook.On("Run").Return(errExpected) 61 hook.On("Log").Return(log.WithField("test", ts.T().Name())) 62 63 lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}} 64 65 err := lifecycle.RunPreBuild(ts.ctx) 66 67 ts.Require().ErrorIs(err, errExpected) 68 hook.AssertExpectations(ts.T()) 69 } 70 71 func (ts *LifecycleRunTestSuite) TestEmpty() { 72 lifecycle := hooks.Lifecycle{} 73 74 ts.Require().NoError(lifecycle.RunPreBuild(ts.ctx)) 75 ts.Require().NoError(lifecycle.RunPostBuild(ts.ctx)) 76 ts.Require().NoError(lifecycle.RunPreUp(ts.ctx)) 77 ts.Require().NoError(lifecycle.RunPostUp(ts.ctx)) 78 ts.Require().NoError(lifecycle.RunPreDown(ts.ctx)) 79 ts.Require().NoError(lifecycle.RunPostDown(ts.ctx)) 80 ts.Require().NoError(lifecycle.RunPreRollback(ts.ctx)) 81 ts.Require().NoError(lifecycle.RunPostRollback(ts.ctx)) 82 } 83 84 type HookRunTestSuite struct { 85 suite.Suite 86 87 ctx context.Context 88 } 89 90 func TestHookRunTestSuite(t *testing.T) { 91 t.Parallel() 92 suite.Run(t, new(HookRunTestSuite)) 93 } 94 95 func (ts *HookRunTestSuite) SetupTest() { 96 ts.ctx = tests.GetContext(ts.T()) 97 } 98 99 func (ts *HookRunTestSuite) TestRunCanceledContext() { 100 hook := hooks.NewHook() 101 hook.Cmd = "id" 102 103 lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}} 104 ctx, cancel := context.WithCancel(ts.ctx) 105 cancel() 106 107 err := lifecycle.RunPreBuild(ctx) 108 109 ts.Require().ErrorIs(err, context.Canceled) 110 } 111 112 func (ts *HookRunTestSuite) TestRunNoError() { 113 hook := hooks.NewHook() 114 hook.Cmd = "id" 115 116 lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}} 117 118 err := lifecycle.RunPreBuild(ts.ctx) 119 120 ts.Require().NoError(err) 121 } 122 123 func (ts *HookRunTestSuite) TestRunWrongCommand() { 124 hook := hooks.NewHook() 125 hook.Cmd = "id 123" 126 127 lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}} 128 129 err := lifecycle.RunPreBuild(ts.ctx) 130 131 ts.Require().ErrorIs(err, exec.ErrNotFound) 132 }