metrics_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 metrics
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. k8stypes "k8s.io/apimachinery/pkg/types"
  19. "k8s.io/kubernetes/pkg/kubelet/volumemanager/cache"
  20. "k8s.io/kubernetes/pkg/volume"
  21. volumetesting "k8s.io/kubernetes/pkg/volume/testing"
  22. "k8s.io/kubernetes/pkg/volume/util"
  23. "k8s.io/kubernetes/pkg/volume/util/operationexecutor"
  24. )
  25. func TestMetricCollection(t *testing.T) {
  26. volumePluginMgr, fakePlugin := volumetesting.GetTestVolumePluginMgr(t)
  27. dsw := cache.NewDesiredStateOfWorld(volumePluginMgr)
  28. asw := cache.NewActualStateOfWorld(k8stypes.NodeName("node-name"), volumePluginMgr)
  29. pod := &v1.Pod{
  30. ObjectMeta: metav1.ObjectMeta{
  31. Name: "pod1",
  32. UID: "pod1uid",
  33. },
  34. Spec: v1.PodSpec{
  35. Volumes: []v1.Volume{
  36. {
  37. Name: "volume-name",
  38. VolumeSource: v1.VolumeSource{
  39. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  40. PDName: "fake-device1",
  41. },
  42. },
  43. },
  44. },
  45. },
  46. }
  47. volumeSpec := &volume.Spec{Volume: &pod.Spec.Volumes[0]}
  48. podName := util.GetUniquePodName(pod)
  49. // Add one volume to DesiredStateOfWorld
  50. generatedVolumeName, err := dsw.AddPodToVolume(podName, pod, volumeSpec, volumeSpec.Name(), "")
  51. if err != nil {
  52. t.Fatalf("AddPodToVolume failed. Expected: <no error> Actual: <%v>", err)
  53. }
  54. mounter, err := fakePlugin.NewMounter(volumeSpec, pod, volume.VolumeOptions{})
  55. if err != nil {
  56. t.Fatalf("NewMounter failed. Expected: <no error> Actual: <%v>", err)
  57. }
  58. mapper, err := fakePlugin.NewBlockVolumeMapper(volumeSpec, pod, volume.VolumeOptions{})
  59. if err != nil {
  60. t.Fatalf("NewBlockVolumeMapper failed. Expected: <no error> Actual: <%v>", err)
  61. }
  62. // Add one volume to ActualStateOfWorld
  63. devicePath := "fake/device/path"
  64. err = asw.MarkVolumeAsAttached("", volumeSpec, "", devicePath)
  65. if err != nil {
  66. t.Fatalf("MarkVolumeAsAttached failed. Expected: <no error> Actual: <%v>", err)
  67. }
  68. markVolumeOpts := operationexecutor.MarkVolumeOpts{
  69. PodName: podName,
  70. PodUID: pod.UID,
  71. VolumeName: generatedVolumeName,
  72. Mounter: mounter,
  73. BlockVolumeMapper: mapper,
  74. OuterVolumeSpecName: volumeSpec.Name(),
  75. VolumeSpec: volumeSpec,
  76. VolumeMountState: operationexecutor.VolumeMounted,
  77. }
  78. err = asw.AddPodToVolume(markVolumeOpts)
  79. if err != nil {
  80. t.Fatalf("AddPodToVolume failed. Expected: <no error> Actual: <%v>", err)
  81. }
  82. metricCollector := &totalVolumesCollector{asw: asw, dsw: dsw, pluginMgr: volumePluginMgr}
  83. // Check if getVolumeCount returns correct data
  84. count := metricCollector.getVolumeCount()
  85. if len(count) != 2 {
  86. t.Errorf("getVolumeCount failed. Expected <2> states, got <%d>", len(count))
  87. }
  88. dswCount, ok := count["desired_state_of_world"]
  89. if !ok {
  90. t.Errorf("getVolumeCount failed. Expected <desired_state_of_world>, got nothing")
  91. }
  92. fakePluginCount := dswCount["fake-plugin"]
  93. if fakePluginCount != 1 {
  94. t.Errorf("getVolumeCount failed. Expected <1> fake-plugin volume in DesiredStateOfWorld, got <%d>",
  95. fakePluginCount)
  96. }
  97. aswCount, ok := count["actual_state_of_world"]
  98. if !ok {
  99. t.Errorf("getVolumeCount failed. Expected <actual_state_of_world>, got nothing")
  100. }
  101. fakePluginCount = aswCount["fake-plugin"]
  102. if fakePluginCount != 1 {
  103. t.Errorf("getVolumeCount failed. Expected <1> fake-plugin volume in ActualStateOfWorld, got <%d>",
  104. fakePluginCount)
  105. }
  106. }