Skip to content

Commit 9e23d32

Browse files
authored
Merge pull request #1841 from tejal29/deploy_build
Fix skaffold build templating output and add tests
2 parents d77c062 + 6bd28dc commit 9e23d32

File tree

3 files changed

+155
-20
lines changed

3 files changed

+155
-20
lines changed

cmd/skaffold/app/cmd/build.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ import (
3434

3535
var (
3636
quietFlag bool
37-
buildFormatFlag = flags.NewTemplateFlag("{{range .Builds}}{{.ImageName}} -> {{.Tag}}\n{{end}}", BuildOutput{})
37+
buildFormatFlag = flags.NewTemplateFlag("{{.}}", BuildOutput{})
38+
)
39+
40+
// For testing
41+
var (
42+
createRunnerAndBuildFunc = createRunnerAndBuild
3843
)
3944

4045
// NewCmdBuild describes the CLI command to build artifacts.
@@ -50,8 +55,8 @@ func NewCmdBuild(out io.Writer) *cobra.Command {
5055
}
5156
AddRunDevFlags(cmd)
5257
cmd.Flags().StringArrayVarP(&opts.TargetImages, "build-image", "b", nil, "Choose which artifacts to build. Artifacts with image names that contain the expression will be built only. Default is to build sources for all artifacts")
53-
cmd.Flags().BoolVarP(&quietFlag, "quiet", "q", false, "Suppress the build output and print image built on success")
54-
cmd.Flags().VarP(buildFormatFlag, "output", "o", buildFormatFlag.Usage())
58+
cmd.Flags().BoolVarP(&quietFlag, "quiet", "q", false, "Suppress the build output and print image built on success. See --output to format output. ")
59+
cmd.Flags().VarP(buildFormatFlag, "output", "o", "Used in conjuction with --quiet flag. "+buildFormatFlag.Usage())
5560
return cmd
5661
}
5762

@@ -63,33 +68,23 @@ type BuildOutput struct {
6368
func runBuild(out io.Writer) error {
6469
start := time.Now()
6570
defer func() {
66-
color.Default.Fprintln(out, "Complete in", time.Since(start))
71+
if !quietFlag {
72+
color.Default.Fprintln(out, "Complete in", time.Since(start))
73+
}
6774
}()
6875

6976
ctx, cancel := context.WithCancel(context.Background())
7077
defer cancel()
7178
defer plugin.CleanupClients()
7279
catchCtrlC(cancel)
7380

74-
runner, config, err := newRunner(opts)
75-
if err != nil {
76-
return errors.Wrap(err, "creating runner")
77-
}
78-
defer runner.RPCServerShutdown()
79-
8081
buildOut := out
8182
if quietFlag {
8283
buildOut = ioutil.Discard
8384
}
8485

85-
var targetArtifacts []*latest.Artifact
86-
for _, artifact := range config.Build.Artifacts {
87-
if runner.IsTargetImage(artifact) {
88-
targetArtifacts = append(targetArtifacts, artifact)
89-
}
90-
}
86+
bRes, err := createRunnerAndBuildFunc(ctx, buildOut)
9187

92-
bRes, err := runner.BuildAndTest(ctx, buildOut, targetArtifacts)
9388
if err != nil {
9489
return err
9590
}
@@ -103,3 +98,18 @@ func runBuild(out io.Writer) error {
10398

10499
return nil
105100
}
101+
102+
func createRunnerAndBuild(ctx context.Context, buildOut io.Writer) ([]build.Artifact, error) {
103+
runner, config, err := newRunner(opts)
104+
if err != nil {
105+
return nil, errors.Wrap(err, "creating runner")
106+
}
107+
defer runner.RPCServerShutdown()
108+
var targetArtifacts []*latest.Artifact
109+
for _, artifact := range config.Build.Artifacts {
110+
if runner.IsTargetImage(artifact) {
111+
targetArtifacts = append(targetArtifacts, artifact)
112+
}
113+
}
114+
return runner.BuildAndTest(ctx, buildOut, targetArtifacts)
115+
}

cmd/skaffold/app/cmd/build_test.go

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright 2019 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"bytes"
21+
"context"
22+
"errors"
23+
"io"
24+
"io/ioutil"
25+
"testing"
26+
27+
"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags"
28+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
29+
"github.com/GoogleContainerTools/skaffold/testutil"
30+
)
31+
32+
func TestQuietFlag(t *testing.T) {
33+
mockCreateRunner := func(context.Context, io.Writer) ([]build.Artifact, error) {
34+
return []build.Artifact{{
35+
ImageName: "gcr.io/skaffold/example",
36+
Tag: "test",
37+
}}, nil
38+
}
39+
40+
defer func(f func(context.Context, io.Writer) ([]build.Artifact, error)) {
41+
createRunnerAndBuildFunc = f
42+
}(createRunnerAndBuildFunc)
43+
44+
var tests = []struct {
45+
description string
46+
template string
47+
expectedOutput []byte
48+
mock func(context.Context, io.Writer) ([]build.Artifact, error)
49+
shouldErr bool
50+
}{
51+
{
52+
description: "quiet flag print build images with no template",
53+
expectedOutput: []byte("{[{gcr.io/skaffold/example test}]}"),
54+
shouldErr: false,
55+
mock: mockCreateRunner,
56+
},
57+
{
58+
description: "quiet flag print build images applies pattern specified in template ",
59+
template: "{{range .Builds}}{{.ImageName}} -> {{.Tag}}\n{{end}}",
60+
expectedOutput: []byte("gcr.io/skaffold/example -> test\n"),
61+
shouldErr: false,
62+
mock: mockCreateRunner,
63+
},
64+
{
65+
description: "build errors out when incorrect template specified",
66+
template: "{{.Incorrect}}",
67+
expectedOutput: nil,
68+
shouldErr: true,
69+
mock: mockCreateRunner,
70+
},
71+
}
72+
73+
for _, test := range tests {
74+
t.Run(test.description, func(t *testing.T) {
75+
quietFlag = true
76+
defer func() { quietFlag = false }()
77+
if test.template != "" {
78+
buildFormatFlag = flags.NewTemplateFlag(test.template, BuildOutput{})
79+
}
80+
defer func() { buildFormatFlag = nil }()
81+
createRunnerAndBuildFunc = test.mock
82+
var output bytes.Buffer
83+
err := runBuild(&output)
84+
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, string(test.expectedOutput), output.String())
85+
})
86+
}
87+
}
88+
89+
func TestRunBuild(t *testing.T) {
90+
errRunner := func(context.Context, io.Writer) ([]build.Artifact, error) {
91+
return nil, errors.New("some error")
92+
}
93+
mockCreateRunner := func(context.Context, io.Writer) ([]build.Artifact, error) {
94+
return []build.Artifact{{
95+
ImageName: "gcr.io/skaffold/example",
96+
Tag: "test",
97+
}}, nil
98+
}
99+
defer func(f func(context.Context, io.Writer) ([]build.Artifact, error)) {
100+
createRunnerAndBuildFunc = f
101+
}(createRunnerAndBuildFunc)
102+
103+
var tests = []struct {
104+
description string
105+
mock func(context.Context, io.Writer) ([]build.Artifact, error)
106+
shouldErr bool
107+
}{
108+
{
109+
description: "build should return successfully when runner is successful.",
110+
shouldErr: false,
111+
mock: mockCreateRunner,
112+
},
113+
{
114+
description: "build errors out when there was runner error.",
115+
shouldErr: true,
116+
mock: errRunner,
117+
},
118+
}
119+
for _, test := range tests {
120+
t.Run(test.description, func(t *testing.T) {
121+
createRunnerAndBuildFunc = test.mock
122+
err := runBuild(ioutil.Discard)
123+
testutil.CheckError(t, test.shouldErr, err)
124+
})
125+
}
126+
}

docs/content/en/docs/references/cli/_index.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ Flags:
7070
--enable-rpc skaffold dev Enable gRPC for exposing Skaffold events (true by default for skaffold dev)
7171
-f, --filename string Filename or URL to the pipeline file (default "skaffold.yaml")
7272
-n, --namespace string Run deployments in the specified namespace
73-
-o, --output *flags.TemplateFlag Format output with go-template. For full struct documentation, see https://godoc.org/github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd#BuildOutput (default {{range .Builds}}{{.ImageName}} -> {{.Tag}}
74-
{{end}})
73+
-o, --output *flags.TemplateFlag Used in conjuction with --quiet flag. Format output with go-template. For full struct documentation, see https://godoc.org/github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd#BuildOutput (default {{.}})
7574
-p, --profile stringArray Activate profiles by name
76-
-q, --quiet Suppress the build output and print image built on success
75+
-q, --quiet Suppress the build output and print image built on success. See --output to format output.
7776
--rpc-http-port int tcp port to expose event REST API over HTTP (default 50052)
7877
--rpc-port int tcp port to expose event API (default 50051)
7978
--skip-tests Whether to skip the tests after building

0 commit comments

Comments
 (0)