@@ -17,14 +17,24 @@ limitations under the License.
17
17
package log
18
18
19
19
import (
20
- "fmt"
20
+ "bytes"
21
+ "encoding/json"
21
22
"io/ioutil"
22
23
23
24
"github.com/go-logr/logr"
24
25
. "github.com/onsi/ginkgo"
25
26
. "github.com/onsi/gomega"
27
+ kapi "k8s.io/api/core/v1"
28
+ "k8s.io/apimachinery/pkg/types"
26
29
)
27
30
31
+ // testStringer is a fmt.Stringer
32
+ type testStringer struct {}
33
+
34
+ func (testStringer ) String () string {
35
+ return "value"
36
+ }
37
+
28
38
// fakeSyncWriter is a fake zap.SyncerWriter that lets us test if sync was called
29
39
type fakeSyncWriter bool
30
40
@@ -249,26 +259,101 @@ var _ = Describe("runtime log", func() {
249
259
Expect (ZapLoggerTo (ioutil .Discard , true )).NotTo (BeNil ())
250
260
})
251
261
})
252
- })
253
262
254
- Describe ("fataliferr" , func () {
255
- It ("should not call the fn if there is not an error" , func () {
256
- called := false
257
- fn := func (format string , v ... interface {}) {
258
- called = true
259
- }
260
- fatalIfErr (nil , fn )
261
- Expect (called ).To (BeFalse ())
262
- })
263
+ Context ("when logging kubernetes objects" , func () {
264
+ var logOut * bytes.Buffer
265
+ var logger logr.Logger
266
+
267
+ BeforeEach (func () {
268
+ logOut = new (bytes.Buffer )
269
+ By ("setting up the logger" )
270
+ // use production settings (false) to get just json output
271
+ logger = ZapLoggerTo (logOut , false )
272
+ })
273
+
274
+ It ("should log a standard namespaced Kubernetes object name and namespace" , func () {
275
+ pod := & kapi.Pod {}
276
+ pod .Name = "some-pod"
277
+ pod .Namespace = "some-ns"
278
+ logger .Info ("here's a kubernetes object" , "thing" , pod )
279
+
280
+ outRaw := logOut .Bytes ()
281
+ res := map [string ]interface {}{}
282
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
283
+
284
+ Expect (res ).To (HaveKeyWithValue ("thing" , map [string ]interface {}{
285
+ "name" : pod .Name ,
286
+ "namespace" : pod .Namespace ,
287
+ }))
288
+ })
289
+
290
+ It ("should work fine with normal stringers" , func () {
291
+ logger .Info ("here's a non-kubernetes stringer" , "thing" , testStringer {})
292
+ outRaw := logOut .Bytes ()
293
+ res := map [string ]interface {}{}
294
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
295
+
296
+ Expect (res ).To (HaveKeyWithValue ("thing" , "value" ))
297
+ })
263
298
264
- It ("should call the fn if there is an error" , func () {
265
- called := false
266
- fn := func (format string , v ... interface {}) {
267
- called = true
268
- }
269
- fatalIfErr (fmt .Errorf ("error" ), fn )
270
- Expect (called ).To (BeTrue ())
299
+ It ("should log a standard non-namespaced Kubernetes object name" , func () {
300
+ node := & kapi.Node {}
301
+ node .Name = "some-node"
302
+ logger .Info ("here's a kubernetes object" , "thing" , node )
303
+
304
+ outRaw := logOut .Bytes ()
305
+ res := map [string ]interface {}{}
306
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
307
+
308
+ Expect (res ).To (HaveKeyWithValue ("thing" , map [string ]interface {}{
309
+ "name" : node .Name ,
310
+ }))
311
+ })
312
+
313
+ It ("should log a standard Kubernetes object's kind, if set" , func () {
314
+ node := & kapi.Node {}
315
+ node .Name = "some-node"
316
+ node .APIVersion = "v1"
317
+ node .Kind = "Node"
318
+ logger .Info ("here's a kubernetes object" , "thing" , node )
319
+
320
+ outRaw := logOut .Bytes ()
321
+ res := map [string ]interface {}{}
322
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
323
+
324
+ Expect (res ).To (HaveKeyWithValue ("thing" , map [string ]interface {}{
325
+ "name" : node .Name ,
326
+ "apiVersion" : "v1" ,
327
+ "kind" : "Node" ,
328
+ }))
329
+ })
330
+
331
+ It ("should log a standard non-namespaced NamespacedName name" , func () {
332
+ name := types.NamespacedName {Name : "some-node" }
333
+ logger .Info ("here's a kubernetes object" , "thing" , name )
334
+
335
+ outRaw := logOut .Bytes ()
336
+ res := map [string ]interface {}{}
337
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
338
+
339
+ Expect (res ).To (HaveKeyWithValue ("thing" , map [string ]interface {}{
340
+ "name" : name .Name ,
341
+ }))
342
+ })
343
+
344
+ It ("should log a standard namespaced NamespacedName name and namespace" , func () {
345
+ name := types.NamespacedName {Name : "some-pod" , Namespace : "some-ns" }
346
+ logger .Info ("here's a kubernetes object" , "thing" , name )
347
+
348
+ outRaw := logOut .Bytes ()
349
+ res := map [string ]interface {}{}
350
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
351
+
352
+ Expect (res ).To (HaveKeyWithValue ("thing" , map [string ]interface {}{
353
+ "name" : name .Name ,
354
+ "namespace" : name .Namespace ,
355
+ }))
356
+ })
271
357
})
272
358
})
273
-
274
359
})
0 commit comments