@@ -25,6 +25,8 @@ import (
2525
2626 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727 stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
28+
29+ "github.com/GoogleCloudPlatform/k8s-stackdriver/kubelet-to-gcm/monitor"
2830)
2931
3032const (
@@ -300,6 +302,59 @@ func TestTranslator(t *testing.T) {
300302 }
301303}
302304
305+ func TestTranslator_NilFields (t * testing.T ) {
306+ summary := & stats.Summary {
307+ Node : stats.NodeStats {
308+ StartTime : metav1 .NewTime (time .Now ()),
309+ Memory : & stats.MemoryStats {
310+ Time : metav1 .NewTime (time .Now ().Add (time .Second )),
311+ WorkingSetBytes : monitor .Uint64Ptr (100 ),
312+ PageFaults : monitor .Uint64Ptr (10 ),
313+ MajorPageFaults : nil , // This would cause panic in translateMemory
314+ },
315+ Fs : & stats.FsStats {
316+ CapacityBytes : monitor .Uint64Ptr (1000 ),
317+ UsedBytes : monitor .Uint64Ptr (500 ),
318+ },
319+ CPU : & stats.CPUStats {
320+ Time : metav1 .NewTime (time .Now ().Add (time .Second )),
321+ UsageCoreNanoSeconds : monitor .Uint64Ptr (100 ),
322+ },
323+ },
324+ Pods : []stats.PodStats {
325+ {
326+ PodRef : stats.PodReference {Name : "test-pod" , Namespace : "test-ns" },
327+ Containers : []stats.ContainerStats {
328+ {
329+ Name : "test-container" ,
330+ StartTime : metav1 .NewTime (time .Now ()),
331+ CPU : & stats.CPUStats {
332+ Time : metav1 .NewTime (time .Now ().Add (time .Second )),
333+ UsageCoreNanoSeconds : monitor .Uint64Ptr (100 ),
334+ },
335+ Memory : & stats.MemoryStats {
336+ Time : metav1 .NewTime (time .Now ().Add (time .Second )),
337+ WorkingSetBytes : monitor .Uint64Ptr (100 ),
338+ },
339+ Rootfs : & stats.FsStats {
340+ UsedBytes : nil , // This would cause panic in containerTranslateFS
341+ },
342+ Logs : & stats.FsStats {
343+ UsedBytes : nil , // This would cause panic in containerTranslateFS
344+ },
345+ },
346+ },
347+ },
348+ },
349+ }
350+
351+ translator := NewTranslator ("zone" , "project" , "cluster" , "location" , "instance" , "id" , "k8s_" , map [string ]string {}, time .Minute )
352+ _ , err := translator .Translate (summary )
353+ if err != nil {
354+ t .Errorf ("Translate failed: %v" , err )
355+ }
356+ }
357+
303358func TestTranslateContainers (t * testing.T ) {
304359 aliceContainer := * getContainerStats (false )
305360 bobContainer := * getContainerStats (false )
0 commit comments