|
| 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 | +} |
0 commit comments