volume_stat_calculator_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2017 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 stats
  14. import (
  15. "testing"
  16. "time"
  17. "github.com/stretchr/testify/assert"
  18. k8sv1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. kubestats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  22. statstest "k8s.io/kubernetes/pkg/kubelet/server/stats/testing"
  23. "k8s.io/kubernetes/pkg/volume"
  24. )
  25. const (
  26. namespace0 = "test0"
  27. pName0 = "pod0"
  28. capacity = int64(10000000)
  29. available = int64(5000000)
  30. inodesTotal = int64(2000)
  31. inodesFree = int64(1000)
  32. vol0 = "vol0"
  33. vol1 = "vol1"
  34. pvcClaimName = "pvc-fake"
  35. )
  36. func TestPVCRef(t *testing.T) {
  37. // Create pod spec to test against
  38. podVolumes := []k8sv1.Volume{
  39. {
  40. Name: vol0,
  41. VolumeSource: k8sv1.VolumeSource{
  42. GCEPersistentDisk: &k8sv1.GCEPersistentDiskVolumeSource{
  43. PDName: "fake-device1",
  44. },
  45. },
  46. },
  47. {
  48. Name: vol1,
  49. VolumeSource: k8sv1.VolumeSource{
  50. PersistentVolumeClaim: &k8sv1.PersistentVolumeClaimVolumeSource{
  51. ClaimName: pvcClaimName,
  52. },
  53. },
  54. },
  55. }
  56. fakePod := &k8sv1.Pod{
  57. ObjectMeta: metav1.ObjectMeta{
  58. Name: pName0,
  59. Namespace: namespace0,
  60. UID: "UID" + pName0,
  61. },
  62. Spec: k8sv1.PodSpec{
  63. Volumes: podVolumes,
  64. },
  65. }
  66. // Setup mock stats provider
  67. mockStats := new(statstest.StatsProvider)
  68. volumes := map[string]volume.Volume{vol0: &fakeVolume{}, vol1: &fakeVolume{}}
  69. mockStats.On("ListVolumesForPod", fakePod.UID).Return(volumes, true)
  70. // Calculate stats for pod
  71. statsCalculator := newVolumeStatCalculator(mockStats, time.Minute, fakePod)
  72. statsCalculator.calcAndStoreStats()
  73. vs, _ := statsCalculator.GetLatest()
  74. assert.Len(t, append(vs.EphemeralVolumes, vs.PersistentVolumes...), 2)
  75. // Verify 'vol0' doesn't have a PVC reference
  76. assert.Contains(t, append(vs.EphemeralVolumes, vs.PersistentVolumes...), kubestats.VolumeStats{
  77. Name: vol0,
  78. FsStats: expectedFSStats(),
  79. })
  80. // Verify 'vol1' has a PVC reference
  81. assert.Contains(t, append(vs.EphemeralVolumes, vs.PersistentVolumes...), kubestats.VolumeStats{
  82. Name: vol1,
  83. PVCRef: &kubestats.PVCReference{
  84. Name: pvcClaimName,
  85. Namespace: namespace0,
  86. },
  87. FsStats: expectedFSStats(),
  88. })
  89. }
  90. // Fake volume/metrics provider
  91. var _ volume.Volume = &fakeVolume{}
  92. type fakeVolume struct{}
  93. func (v *fakeVolume) GetPath() string { return "" }
  94. func (v *fakeVolume) GetMetrics() (*volume.Metrics, error) {
  95. return expectedMetrics(), nil
  96. }
  97. func expectedMetrics() *volume.Metrics {
  98. return &volume.Metrics{
  99. Available: resource.NewQuantity(available, resource.BinarySI),
  100. Capacity: resource.NewQuantity(capacity, resource.BinarySI),
  101. Used: resource.NewQuantity(available-capacity, resource.BinarySI),
  102. Inodes: resource.NewQuantity(inodesTotal, resource.BinarySI),
  103. InodesFree: resource.NewQuantity(inodesFree, resource.BinarySI),
  104. InodesUsed: resource.NewQuantity(inodesTotal-inodesFree, resource.BinarySI),
  105. }
  106. }
  107. func expectedFSStats() kubestats.FsStats {
  108. metric := expectedMetrics()
  109. available := uint64(metric.Available.Value())
  110. capacity := uint64(metric.Capacity.Value())
  111. used := uint64(metric.Used.Value())
  112. inodes := uint64(metric.Inodes.Value())
  113. inodesFree := uint64(metric.InodesFree.Value())
  114. inodesUsed := uint64(metric.InodesUsed.Value())
  115. return kubestats.FsStats{
  116. AvailableBytes: &available,
  117. CapacityBytes: &capacity,
  118. UsedBytes: &used,
  119. Inodes: &inodes,
  120. InodesFree: &inodesFree,
  121. InodesUsed: &inodesUsed,
  122. }
  123. }