Skip to content

Commit 82baee6

Browse files
committed
add tests
1 parent f685f05 commit 82baee6

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

pkg/skaffold/deploy/helm/helm_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ package helm
1919
import (
2020
"context"
2121
"fmt"
22+
"io"
2223
"io/ioutil"
2324
"path/filepath"
2425
"strings"
2526
"testing"
2627

2728
"github.com/mitchellh/go-homedir"
29+
"github.com/pkg/errors"
2830

2931
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl"
3032
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/label"
3133
deployutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/util"
3234
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
35+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/hooks"
36+
ctl "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
3337
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/client"
38+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/logger"
3439
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
3540
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
3641
schemautil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
@@ -1601,6 +1606,52 @@ func TestGenerateSkaffoldDebugFilter(t *testing.T) {
16011606
}
16021607
}
16031608

1609+
func TestHelmHooks(t *testing.T) {
1610+
tests := []struct {
1611+
description string
1612+
runner hooks.Runner
1613+
shouldErr bool
1614+
}{
1615+
{
1616+
description: "hooks run successfully",
1617+
runner: hooks.MockRunner{
1618+
PreHooks: func(context.Context, io.Writer) error {
1619+
return nil
1620+
},
1621+
PostHooks: func(context.Context, io.Writer) error {
1622+
return nil
1623+
},
1624+
},
1625+
},
1626+
{
1627+
description: "hooks fails",
1628+
runner: hooks.MockRunner{
1629+
PreHooks: func(context.Context, io.Writer) error {
1630+
return errors.New("failed to execute hooks")
1631+
},
1632+
PostHooks: func(context.Context, io.Writer) error {
1633+
return errors.New("failed to execute hooks")
1634+
},
1635+
},
1636+
shouldErr: true,
1637+
},
1638+
}
1639+
for _, test := range tests {
1640+
testutil.Run(t, test.description, func(t *testutil.T) {
1641+
t.Override(&hooks.NewDeployRunner, func(*ctl.CLI, latestV1.DeployHooks, *[]string, logger.Formatter, hooks.DeployEnvOpts) hooks.Runner {
1642+
return test.runner
1643+
})
1644+
1645+
k, err := NewDeployer(&helmConfig{}, &label.DefaultLabeller{}, &testDeployConfig)
1646+
t.RequireNoError(err)
1647+
err = k.PreDeployHooks(context.Background(), ioutil.Discard)
1648+
t.CheckError(test.shouldErr, err)
1649+
err = k.PostDeployHooks(context.Background(), ioutil.Discard)
1650+
t.CheckError(test.shouldErr, err)
1651+
})
1652+
}
1653+
}
1654+
16041655
type helmConfig struct {
16051656
runcontext.RunContext // Embedded to provide the default values.
16061657
namespace string

pkg/skaffold/deploy/kustomize/kustomize_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"errors"
23+
"io"
2324
"io/ioutil"
2425
"path/filepath"
2526
"testing"
@@ -30,7 +31,10 @@ import (
3031
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/label"
3132
deployutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/util"
3233
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
34+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/hooks"
35+
ctl "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
3336
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/client"
37+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/logger"
3438
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
3539
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
3640
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
@@ -265,6 +269,57 @@ func TestKustomizeCleanup(t *testing.T) {
265269
}
266270
}
267271

272+
func TestKustomizeHooks(t *testing.T) {
273+
tests := []struct {
274+
description string
275+
runner hooks.Runner
276+
shouldErr bool
277+
}{
278+
{
279+
description: "hooks run successfully",
280+
runner: hooks.MockRunner{
281+
PreHooks: func(context.Context, io.Writer) error {
282+
return nil
283+
},
284+
PostHooks: func(context.Context, io.Writer) error {
285+
return nil
286+
},
287+
},
288+
},
289+
{
290+
description: "hooks fails",
291+
runner: hooks.MockRunner{
292+
PreHooks: func(context.Context, io.Writer) error {
293+
return errors.New("failed to execute hooks")
294+
},
295+
PostHooks: func(context.Context, io.Writer) error {
296+
return errors.New("failed to execute hooks")
297+
},
298+
},
299+
shouldErr: true,
300+
},
301+
}
302+
for _, test := range tests {
303+
testutil.Run(t, test.description, func(t *testutil.T) {
304+
t.Override(&KustomizeBinaryCheck, func() bool { return true })
305+
t.Override(&hooks.NewDeployRunner, func(*ctl.CLI, latestV1.DeployHooks, *[]string, logger.Formatter, hooks.DeployEnvOpts) hooks.Runner {
306+
return test.runner
307+
})
308+
309+
k, err := NewDeployer(&kustomizeConfig{
310+
workingDir: ".",
311+
RunContext: runcontext.RunContext{Opts: config.SkaffoldOptions{
312+
Namespace: kubectl.TestNamespace}},
313+
}, &label.DefaultLabeller{}, &latestV1.KustomizeDeploy{})
314+
t.RequireNoError(err)
315+
err = k.PreDeployHooks(context.Background(), ioutil.Discard)
316+
t.CheckError(test.shouldErr, err)
317+
err = k.PostDeployHooks(context.Background(), ioutil.Discard)
318+
t.CheckError(test.shouldErr, err)
319+
})
320+
}
321+
}
322+
268323
func TestDependenciesForKustomization(t *testing.T) {
269324
tests := []struct {
270325
description string

pkg/skaffold/hooks/deploy.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import (
3131
v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
3232
)
3333

34-
func NewDeployRunner(cli *kubectl.CLI, d v1.DeployHooks, namespaces *[]string, formatter logger.Formatter, opts DeployEnvOpts) Runner {
34+
// for testing
35+
var (
36+
NewDeployRunner = newDeployRunner
37+
)
38+
39+
func newDeployRunner(cli *kubectl.CLI, d v1.DeployHooks, namespaces *[]string, formatter logger.Formatter, opts DeployEnvOpts) Runner {
3540
return deployRunner{d, cli, namespaces, formatter, opts, new(sync.Map)}
3641
}
3742

pkg/skaffold/hooks/types.go

+20
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,23 @@ var phases = struct {
4646
PreDeploy: "pre-deploy",
4747
PostDeploy: "post-deploy",
4848
}
49+
50+
// MockRunner implements the Runner interface, to be used in unit tests
51+
type MockRunner struct {
52+
PreHooks func(ctx context.Context, out io.Writer) error
53+
PostHooks func(ctx context.Context, out io.Writer) error
54+
}
55+
56+
func (m MockRunner) RunPreHooks(ctx context.Context, out io.Writer) error {
57+
if m.PreHooks != nil {
58+
return m.PreHooks(ctx, out)
59+
}
60+
return nil
61+
}
62+
63+
func (m MockRunner) RunPostHooks(ctx context.Context, out io.Writer) error {
64+
if m.PostHooks != nil {
65+
return m.PostHooks(ctx, out)
66+
}
67+
return nil
68+
}

0 commit comments

Comments
 (0)