...

Source file src/k8s.io/kubernetes/pkg/kubelet/metrics/collectors/log_metrics_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/metrics/collectors

     1  /*
     2  Copyright 2018 The Kubernetes 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 collectors
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  	"testing"
    23  
    24  	"k8s.io/component-base/metrics/testutil"
    25  	statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
    26  )
    27  
    28  func TestNoMetricsCollected(t *testing.T) {
    29  	// Refresh Desc to share with different registry
    30  	descLogSize = descLogSize.GetRawDesc()
    31  
    32  	collector := &logMetricsCollector{
    33  		podStats: func(_ context.Context) ([]statsapi.PodStats, error) {
    34  			return []statsapi.PodStats{}, nil
    35  		},
    36  	}
    37  
    38  	if err := testutil.CustomCollectAndCompare(collector, strings.NewReader(""), ""); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  }
    42  
    43  func TestMetricsCollected(t *testing.T) {
    44  	// Refresh Desc to share with different registry
    45  	descLogSize = descLogSize.GetRawDesc()
    46  
    47  	size := uint64(18)
    48  	collector := &logMetricsCollector{
    49  		podStats: func(_ context.Context) ([]statsapi.PodStats, error) {
    50  			return []statsapi.PodStats{
    51  				{
    52  					PodRef: statsapi.PodReference{
    53  						Namespace: "some-namespace",
    54  						Name:      "podName1",
    55  						UID:       "UID_some_id",
    56  					},
    57  					Containers: []statsapi.ContainerStats{
    58  						{
    59  							Name: "containerName1",
    60  							Logs: &statsapi.FsStats{
    61  								UsedBytes: &size,
    62  							},
    63  						},
    64  					},
    65  				},
    66  			}, nil
    67  		},
    68  	}
    69  
    70  	err := testutil.CustomCollectAndCompare(collector, strings.NewReader(`
    71  		# HELP kubelet_container_log_filesystem_used_bytes [ALPHA] Bytes used by the container's logs on the filesystem.
    72  		# TYPE kubelet_container_log_filesystem_used_bytes gauge
    73  		kubelet_container_log_filesystem_used_bytes{container="containerName1",namespace="some-namespace",pod="podName1",uid="UID_some_id"} 18
    74  `), "kubelet_container_log_filesystem_used_bytes")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  }
    79  

View as plain text