github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/logger/log_test.go (about) 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 logger 18 19 import ( 20 "context" 21 "io/ioutil" 22 "testing" 23 "time" 24 25 v1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 28 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes" 29 "github.com/GoogleContainerTools/skaffold/testutil" 30 ) 31 32 func TestSinceSeconds(t *testing.T) { 33 tests := []struct { 34 description string 35 duration time.Duration 36 expected int64 37 }{ 38 {"0s", 0, 1}, 39 {"1ms", 1 * time.Millisecond, 1}, 40 {"500ms", 500 * time.Millisecond, 1}, 41 {"999ms", 999 * time.Millisecond, 1}, 42 {"1s", 1 * time.Second, 1}, 43 {"1.1s", 1100 * time.Millisecond, 2}, 44 {"1.5s", 1500 * time.Millisecond, 2}, 45 {"1.9s", 1500 * time.Millisecond, 2}, 46 {"2s", 2 * time.Second, 2}, 47 {"10s", 10 * time.Second, 10}, 48 {"60s", 60 * time.Second, 60}, 49 } 50 for _, test := range tests { 51 testutil.Run(t, test.description, func(t *testutil.T) { 52 since := sinceSeconds(test.duration) 53 54 t.CheckDeepEqual(test.expected, since) 55 }) 56 } 57 } 58 59 func TestSelect(t *testing.T) { 60 tests := []struct { 61 description string 62 podSpec v1.PodSpec 63 expectedMatch bool 64 }{ 65 { 66 description: "match container", 67 podSpec: v1.PodSpec{Containers: []v1.Container{{Image: "image1"}}}, 68 expectedMatch: true, 69 }, 70 { 71 description: "match init container", 72 podSpec: v1.PodSpec{InitContainers: []v1.Container{{Image: "image2"}}}, 73 expectedMatch: true, 74 }, 75 { 76 description: "no match", 77 podSpec: v1.PodSpec{Containers: []v1.Container{{Image: "image3"}}}, 78 expectedMatch: false, 79 }, 80 } 81 for _, test := range tests { 82 testutil.Run(t, test.description, func(t *testutil.T) { 83 list := kubernetes.NewImageList() 84 list.Add("image1") 85 list.Add("image2") 86 87 selected := list.Select(&v1.Pod{ 88 Spec: test.podSpec, 89 }) 90 91 t.CheckDeepEqual(test.expectedMatch, selected) 92 }) 93 } 94 } 95 96 func TestLogAggregatorZeroValue(t *testing.T) { 97 var m *LogAggregator 98 99 // Should not raise a nil dereference 100 m.Start(context.Background(), ioutil.Discard) 101 m.Mute() 102 m.Unmute() 103 m.Stop() 104 } 105 106 func podWithName(n string) v1.Pod { 107 return v1.Pod{ 108 ObjectMeta: metav1.ObjectMeta{ 109 Name: n, 110 }, 111 } 112 } 113 114 func containerWithName(n string) v1.ContainerStatus { 115 return v1.ContainerStatus{ 116 Name: n, 117 } 118 }