log_metrics_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package collectors
  14. import (
  15. "strings"
  16. "testing"
  17. "k8s.io/component-base/metrics/testutil"
  18. statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  19. )
  20. func TestNoMetricsCollected(t *testing.T) {
  21. // Refresh Desc to share with different registry
  22. descLogSize = descLogSize.GetRawDesc()
  23. collector := &logMetricsCollector{
  24. podStats: func() ([]statsapi.PodStats, error) {
  25. return []statsapi.PodStats{}, nil
  26. },
  27. }
  28. if err := testutil.CustomCollectAndCompare(collector, strings.NewReader(""), ""); err != nil {
  29. t.Fatal(err)
  30. }
  31. }
  32. func TestMetricsCollected(t *testing.T) {
  33. // Refresh Desc to share with different registry
  34. descLogSize = descLogSize.GetRawDesc()
  35. size := uint64(18)
  36. collector := &logMetricsCollector{
  37. podStats: func() ([]statsapi.PodStats, error) {
  38. return []statsapi.PodStats{
  39. {
  40. PodRef: statsapi.PodReference{
  41. Namespace: "some-namespace",
  42. Name: "podName1",
  43. UID: "UID_some_id",
  44. },
  45. Containers: []statsapi.ContainerStats{
  46. {
  47. Name: "containerName1",
  48. Logs: &statsapi.FsStats{
  49. UsedBytes: &size,
  50. },
  51. },
  52. },
  53. },
  54. }, nil
  55. },
  56. }
  57. err := testutil.CustomCollectAndCompare(collector, strings.NewReader(`
  58. # HELP kubelet_container_log_filesystem_used_bytes [ALPHA] Bytes used by the container's logs on the filesystem.
  59. # TYPE kubelet_container_log_filesystem_used_bytes gauge
  60. kubelet_container_log_filesystem_used_bytes{container="containerName1",namespace="some-namespace",pod="podName1",uid="UID_some_id"} 18
  61. `), "kubelet_container_log_filesystem_used_bytes")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. }