github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/docker/logger/formatter_test.go (about) 1 /* 2 Copyright 2021 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 logger 18 19 import ( 20 "bytes" 21 "strings" 22 "sync" 23 "testing" 24 25 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker/tracker" 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph" 27 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/output" 28 "github.com/GoogleContainerTools/skaffold/testutil" 29 ) 30 31 type mockColorPicker struct{} 32 33 func (m *mockColorPicker) Pick(image string) output.Color { 34 return output.Default 35 } 36 37 func (m *mockColorPicker) AddImage(string) {} 38 39 func TestPrintLogLine(t *testing.T) { 40 testutil.Run(t, "verify lines are not intermixed", func(t *testutil.T) { 41 var ( 42 buf bytes.Buffer 43 wg sync.WaitGroup 44 45 linesPerGroup = 100 46 groups = 5 47 ) 48 49 f := NewDockerLogFormatter(&mockColorPicker{}, tracker.NewContainerTracker(), func() bool { return false }, "id") 50 51 for i := 0; i < groups; i++ { 52 wg.Add(1) 53 54 go func() { 55 for i := 0; i < linesPerGroup; i++ { 56 f.PrintLine(&buf, "TEXT\n") 57 } 58 wg.Done() 59 }() 60 } 61 wg.Wait() 62 63 lines := strings.Split(buf.String(), "\n") 64 for i := 0; i < groups*linesPerGroup; i++ { 65 t.CheckDeepEqual("TEXT", lines[i]) 66 } 67 }) 68 } 69 70 func TestPrintLogLineFormatted(t *testing.T) { 71 testutil.Run(t, "verify lines have correct prefix", func(t *testutil.T) { 72 var ( 73 buf bytes.Buffer 74 wg sync.WaitGroup 75 76 linesPerGroup = 100 77 groups = 5 78 ) 79 ct := tracker.NewContainerTracker() 80 ct.Add(graph.Artifact{ImageName: "image", Tag: "image:tag"}, tracker.Container{ID: "id"}) 81 82 f := NewDockerLogFormatter(&mockColorPicker{}, ct, func() bool { return false }, "id") 83 84 for i := 0; i < groups; i++ { 85 wg.Add(1) 86 87 go func() { 88 for i := 0; i < linesPerGroup; i++ { 89 f.PrintLine(&buf, "TEXT\n") 90 } 91 wg.Done() 92 }() 93 } 94 wg.Wait() 95 96 lines := strings.Split(buf.String(), "\n") 97 for i := 0; i < groups*linesPerGroup; i++ { 98 t.CheckDeepEqual("[image] TEXT", lines[i]) 99 } 100 }) 101 }