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