github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/logfile_test.go (about) 1 /* 2 Copyright 2020 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 build 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "fmt" 24 "io" 25 "os" 26 "path/filepath" 27 "strings" 28 "testing" 29 30 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform" 31 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 32 "github.com/GoogleContainerTools/skaffold/testutil" 33 ) 34 35 func TestWithLogFile(t *testing.T) { 36 logBuildInProgress := "building img with tag img:123" 37 logBuildFailed := "failed to build img with tag img:123" 38 logFilename := " - writing logs to " + filepath.Join(os.TempDir(), "skaffold", "build", "img.log") 39 40 tests := []struct { 41 description string 42 builder ArtifactBuilder 43 muted Muted 44 shouldErr bool 45 expectedDigest string 46 logsFound []string 47 logsNotFound []string 48 }{ 49 { 50 description: "all logs", 51 builder: fakeBuilder, 52 muted: muted(false), 53 shouldErr: false, 54 expectedDigest: "digest", 55 logsFound: []string{logBuildInProgress}, 56 logsNotFound: []string{logFilename}, 57 }, 58 { 59 description: "mute build logs", 60 builder: fakeBuilder, 61 muted: muted(true), 62 shouldErr: false, 63 expectedDigest: "digest", 64 logsFound: []string{logFilename}, 65 logsNotFound: []string{logBuildInProgress}, 66 }, 67 { 68 description: "failed build - all logs", 69 builder: fakeFailingBuilder, 70 muted: muted(false), 71 shouldErr: true, 72 expectedDigest: "", 73 logsFound: []string{logBuildFailed}, 74 logsNotFound: []string{logFilename}, 75 }, 76 { 77 description: "failed build - muted logs", 78 builder: fakeFailingBuilder, 79 muted: muted(true), 80 shouldErr: true, 81 expectedDigest: "", 82 logsFound: []string{logFilename}, 83 logsNotFound: []string{logBuildFailed}, 84 }, 85 } 86 for _, test := range tests { 87 testutil.Run(t, test.description, func(t *testutil.T) { 88 var out bytes.Buffer 89 90 builder := WithLogFile(test.builder, test.muted) 91 digest, err := builder(context.Background(), &out, &latest.Artifact{ImageName: "img"}, "img:123", platform.All) 92 93 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedDigest, digest) 94 for _, found := range test.logsFound { 95 t.CheckContains(found, out.String()) 96 } 97 for _, notFound := range test.logsNotFound { 98 t.CheckFalse(strings.Contains(out.String(), notFound)) 99 } 100 }) 101 } 102 } 103 104 func fakeBuilder(_ context.Context, out io.Writer, a *latest.Artifact, tag string, platforms platform.Matcher) (string, error) { 105 fmt.Fprintln(out, "building", a.ImageName, "with tag", tag) 106 return "digest", nil 107 } 108 109 func fakeFailingBuilder(_ context.Context, out io.Writer, a *latest.Artifact, tag string, platforms platform.Matcher) (string, error) { 110 fmt.Fprintln(out, "failed to build", a.ImageName, "with tag", tag) 111 return "", errors.New("bug") 112 } 113 114 type muted bool 115 116 func (m muted) MuteBuild() bool { 117 return bool(m) 118 }