volume_stats_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/component-base/metrics/testutil"
  19. statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  20. statstest "k8s.io/kubernetes/pkg/kubelet/server/stats/testing"
  21. )
  22. func newUint64Pointer(i uint64) *uint64 {
  23. return &i
  24. }
  25. func TestVolumeStatsCollector(t *testing.T) {
  26. // Fixed metadata on type and help text. We prepend this to every expected
  27. // output so we only have to modify a single place when doing adjustments.
  28. const metadata = `
  29. # HELP kubelet_volume_stats_available_bytes [ALPHA] Number of available bytes in the volume
  30. # TYPE kubelet_volume_stats_available_bytes gauge
  31. # HELP kubelet_volume_stats_capacity_bytes [ALPHA] Capacity in bytes of the volume
  32. # TYPE kubelet_volume_stats_capacity_bytes gauge
  33. # HELP kubelet_volume_stats_inodes [ALPHA] Maximum number of inodes in the volume
  34. # TYPE kubelet_volume_stats_inodes gauge
  35. # HELP kubelet_volume_stats_inodes_free [ALPHA] Number of free inodes in the volume
  36. # TYPE kubelet_volume_stats_inodes_free gauge
  37. # HELP kubelet_volume_stats_inodes_used [ALPHA] Number of used inodes in the volume
  38. # TYPE kubelet_volume_stats_inodes_used gauge
  39. # HELP kubelet_volume_stats_used_bytes [ALPHA] Number of used bytes in the volume
  40. # TYPE kubelet_volume_stats_used_bytes gauge
  41. `
  42. var (
  43. podStats = []statsapi.PodStats{
  44. {
  45. PodRef: statsapi.PodReference{Name: "test-pod", Namespace: "test-namespace", UID: "UID_test-pod"},
  46. StartTime: metav1.Now(),
  47. VolumeStats: []statsapi.VolumeStats{
  48. {
  49. FsStats: statsapi.FsStats{
  50. Time: metav1.Now(),
  51. AvailableBytes: newUint64Pointer(5.663154176e+09),
  52. CapacityBytes: newUint64Pointer(1.0434699264e+10),
  53. UsedBytes: newUint64Pointer(4.21789696e+09),
  54. InodesFree: newUint64Pointer(655344),
  55. Inodes: newUint64Pointer(655360),
  56. InodesUsed: newUint64Pointer(16),
  57. },
  58. Name: "test",
  59. PVCRef: nil,
  60. },
  61. {
  62. FsStats: statsapi.FsStats{
  63. Time: metav1.Now(),
  64. AvailableBytes: newUint64Pointer(5.663154176e+09),
  65. CapacityBytes: newUint64Pointer(1.0434699264e+10),
  66. UsedBytes: newUint64Pointer(4.21789696e+09),
  67. InodesFree: newUint64Pointer(655344),
  68. Inodes: newUint64Pointer(655360),
  69. InodesUsed: newUint64Pointer(16),
  70. },
  71. Name: "test",
  72. PVCRef: &statsapi.PVCReference{
  73. Name: "testpvc",
  74. Namespace: "testns",
  75. },
  76. },
  77. },
  78. },
  79. {
  80. // Another pod references the same PVC (test-namespace/testpvc).
  81. PodRef: statsapi.PodReference{Name: "test-pod-2", Namespace: "test-namespace", UID: "UID_test-pod"},
  82. StartTime: metav1.Now(),
  83. VolumeStats: []statsapi.VolumeStats{
  84. {
  85. FsStats: statsapi.FsStats{
  86. Time: metav1.Now(),
  87. AvailableBytes: newUint64Pointer(5.663154176e+09),
  88. CapacityBytes: newUint64Pointer(1.0434699264e+10),
  89. UsedBytes: newUint64Pointer(4.21789696e+09),
  90. InodesFree: newUint64Pointer(655344),
  91. Inodes: newUint64Pointer(655360),
  92. InodesUsed: newUint64Pointer(16),
  93. },
  94. Name: "test",
  95. PVCRef: &statsapi.PVCReference{
  96. Name: "testpvc",
  97. Namespace: "testns",
  98. },
  99. },
  100. },
  101. },
  102. }
  103. want = metadata + `
  104. kubelet_volume_stats_available_bytes{namespace="testns",persistentvolumeclaim="testpvc"} 5.663154176e+09
  105. kubelet_volume_stats_capacity_bytes{namespace="testns",persistentvolumeclaim="testpvc"} 1.0434699264e+10
  106. kubelet_volume_stats_inodes{namespace="testns",persistentvolumeclaim="testpvc"} 655360
  107. kubelet_volume_stats_inodes_free{namespace="testns",persistentvolumeclaim="testpvc"} 655344
  108. kubelet_volume_stats_inodes_used{namespace="testns",persistentvolumeclaim="testpvc"} 16
  109. kubelet_volume_stats_used_bytes{namespace="testns",persistentvolumeclaim="testpvc"} 4.21789696e+09
  110. `
  111. metrics = []string{
  112. "kubelet_volume_stats_available_bytes",
  113. "kubelet_volume_stats_capacity_bytes",
  114. "kubelet_volume_stats_inodes",
  115. "kubelet_volume_stats_inodes_free",
  116. "kubelet_volume_stats_inodes_used",
  117. "kubelet_volume_stats_used_bytes",
  118. }
  119. )
  120. mockStatsProvider := new(statstest.StatsProvider)
  121. mockStatsProvider.On("ListPodStats").Return(podStats, nil)
  122. mockStatsProvider.On("ListPodStatsAndUpdateCPUNanoCoreUsage").Return(podStats, nil)
  123. if err := testutil.CustomCollectAndCompare(&volumeStatsCollector{statsProvider: mockStatsProvider}, strings.NewReader(want), metrics...); err != nil {
  124. t.Errorf("unexpected collecting result:\n%s", err)
  125. }
  126. }