@@ -17,14 +17,20 @@ limitations under the License.
1717package rest
1818
1919import (
20+ "fmt"
21+ "strings"
2022 "testing"
2123
2224 "k8s.io/apimachinery/pkg/api/errors"
2325 genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
2426 "k8s.io/apiserver/pkg/registry/generic"
2527 genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
28+ utilfeature "k8s.io/apiserver/pkg/util/feature"
29+ featuregatetesting "k8s.io/component-base/featuregate/testing"
2630 api "k8s.io/kubernetes/pkg/apis/core"
31+ "k8s.io/kubernetes/pkg/features"
2732 "k8s.io/kubernetes/pkg/registry/registrytest"
33+ "k8s.io/utils/ptr"
2834)
2935
3036func TestPodLogValidates (t * testing.T ) {
@@ -40,16 +46,86 @@ func TestPodLogValidates(t *testing.T) {
4046 }
4147 logRest := & LogREST {Store : store , KubeletConn : nil }
4248
49+ // This test will panic if you don't have a validation failure.
4350 negativeOne := int64 (- 1 )
44- testCases := []* api.PodLogOptions {
45- {SinceSeconds : & negativeOne },
46- {TailLines : & negativeOne },
51+ testCases := []struct {
52+ name string
53+ podOptions api.PodLogOptions
54+ podQueryLogOptions bool
55+ invalidStreamMatch string
56+ }{
57+ {
58+ name : "SinceSeconds" ,
59+ podOptions : api.PodLogOptions {
60+ SinceSeconds : & negativeOne ,
61+ },
62+ },
63+ {
64+ name : "TailLines" ,
65+ podOptions : api.PodLogOptions {
66+ TailLines : & negativeOne ,
67+ },
68+ },
69+ {
70+ name : "StreamWithGateOff" ,
71+ podOptions : api.PodLogOptions {
72+ SinceSeconds : & negativeOne ,
73+ },
74+ podQueryLogOptions : false ,
75+ },
76+ {
77+ name : "StreamWithGateOnDefault" ,
78+ podOptions : api.PodLogOptions {
79+ SinceSeconds : & negativeOne ,
80+ },
81+ podQueryLogOptions : true ,
82+ },
83+ {
84+ name : "StreamWithGateOnAll" ,
85+ podOptions : api.PodLogOptions {
86+ SinceSeconds : & negativeOne ,
87+ Stream : ptr .To (api .LogStreamAll ),
88+ },
89+ podQueryLogOptions : true ,
90+ },
91+ {
92+ name : "StreamWithGateOnStdErr" ,
93+ podOptions : api.PodLogOptions {
94+ SinceSeconds : & negativeOne ,
95+ Stream : ptr .To (api .LogStreamStderr ),
96+ },
97+ podQueryLogOptions : true ,
98+ },
99+ {
100+ name : "StreamWithGateOnStdOut" ,
101+ podOptions : api.PodLogOptions {
102+ SinceSeconds : & negativeOne ,
103+ Stream : ptr .To (api .LogStreamStdout ),
104+ },
105+ podQueryLogOptions : true ,
106+ },
107+ {
108+ name : "StreamWithGateOnAndBadValue" ,
109+ podOptions : api.PodLogOptions {
110+ Stream : ptr .To ("nostream" ),
111+ },
112+ podQueryLogOptions : true ,
113+ invalidStreamMatch : "nostream" ,
114+ },
47115 }
48116
49117 for _ , tc := range testCases {
50- _ , err := logRest .Get (genericapirequest .NewDefaultContext (), "test" , tc )
51- if ! errors .IsInvalid (err ) {
52- t .Fatalf ("Unexpected error: %v" , err )
53- }
118+ t .Run (tc .name , func (t * testing.T ) {
119+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .PodLogsQuerySplitStreams , tc .podQueryLogOptions )
120+ _ , err := logRest .Get (genericapirequest .NewDefaultContext (), "test" , & tc .podOptions )
121+ if ! errors .IsInvalid (err ) {
122+ t .Fatalf ("Unexpected error: %v" , err )
123+ }
124+ if tc .invalidStreamMatch != "" {
125+ if ! strings .Contains (err .Error (), "nostream" ) {
126+ t .Error (fmt .Printf ("Expected %s got %s" , err .Error (), "nostream" ))
127+ }
128+ }
129+ })
54130 }
55131}
0 commit comments