Commit 0c5ea4b5
0c5ea4b540b8Merged
Sviatoslav Dumchenko <[email protected]> ยท 3 years ago ยท 1 file ยท +1 added
Description
chore(tests): add hook run tests
Details
- Commit
- 0c5ea4b540b8
- Parent
- 0f83b9acffad
pkg/hooks/run_test.goaddedView file
1+
package hooks_test2+
3+
import (4+
"context"5+
"errors"6+
"os/exec"7+
"testing"8+
9+
"github.com/helmwave/helmwave/pkg/hooks"10+
log "github.com/sirupsen/logrus"11+
"github.com/stretchr/testify/mock"12+
"github.com/stretchr/testify/suite"13+
)14+
15+
var _ hooks.Hook = (*MockHook)(nil)16+
17+
type MockHook struct {18+
mock.Mock19+
}20+
21+
func (m *MockHook) Run(ctx context.Context) error {22+
return m.Called().Error(0)23+
}24+
25+
func (m *MockHook) Log() *log.Entry {26+
return m.Called().Get(0).(*log.Entry)27+
}28+
29+
type LifecycleRunTestSuite struct {30+
suite.Suite31+
}32+
33+
func TestLifecycleRunTestSuite(t *testing.T) {34+
t.Parallel()35+
suite.Run(t, new(LifecycleRunTestSuite))36+
}37+
38+
func (s *LifecycleRunTestSuite) TestRunNoError() {39+
hook := &MockHook{}40+
hook.On("Run").Return(nil)41+
42+
lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}}43+
ctx := context.Background()44+
45+
err := lifecycle.PreBuilding(ctx)46+
47+
s.Require().NoError(err)48+
hook.AssertExpectations(s.T())49+
}50+
51+
func (s *LifecycleRunTestSuite) TestRunError() {52+
hook := &MockHook{}53+
errExpected := errors.New("test 123")54+
hook.On("Run").Return(errExpected)55+
hook.On("Log").Return(log.WithField("test", s.T().Name()))56+
57+
lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}}58+
ctx := context.Background()59+
60+
err := lifecycle.PreBuilding(ctx)61+
62+
s.Require().ErrorIs(err, errExpected)63+
hook.AssertExpectations(s.T())64+
}65+
66+
type HookRunTestSuite struct {67+
suite.Suite68+
}69+
70+
func TestHookRunTestSuite(t *testing.T) {71+
t.Parallel()72+
suite.Run(t, new(HookRunTestSuite))73+
}74+
75+
func (s *HookRunTestSuite) TestRunCanceledContext() {76+
hook := hooks.NewHook()77+
hook.Cmd = "id"78+
79+
lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}}80+
ctx, cancel := context.WithCancel(context.Background())81+
cancel()82+
83+
err := lifecycle.PreBuilding(ctx)84+
85+
s.Require().ErrorIs(err, context.Canceled)86+
}87+
88+
func (s *HookRunTestSuite) TestRunNoError() {89+
hook := hooks.NewHook()90+
hook.Cmd = "id"91+
92+
lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}}93+
ctx := context.Background()94+
95+
err := lifecycle.PreBuilding(ctx)96+
97+
s.Require().NoError(err)98+
}99+
100+
func (s *HookRunTestSuite) TestRunWrongCommand() {101+
hook := hooks.NewHook()102+
hook.Cmd = "id 123"103+
104+
lifecycle := hooks.Lifecycle{PreBuild: []hooks.Hook{hook}}105+
ctx := context.Background()106+
107+
err := lifecycle.PreBuilding(ctx)108+
109+
s.Require().ErrorIs(err, exec.ErrNotFound)110+
}