metrics_du_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // +build linux
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package volume_test
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. "golang.org/x/sys/unix"
  21. utiltesting "k8s.io/client-go/util/testing"
  22. . "k8s.io/kubernetes/pkg/volume"
  23. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  24. )
  25. func getExpectedBlockSize(path string) int64 {
  26. statfs := &unix.Statfs_t{}
  27. err := unix.Statfs(path, statfs)
  28. if err != nil {
  29. return 0
  30. }
  31. return int64(statfs.Bsize)
  32. }
  33. // TestMetricsDuGetCapacity tests that MetricsDu can read disk usage
  34. // for path
  35. func TestMetricsDuGetCapacity(t *testing.T) {
  36. tmpDir, err := utiltesting.MkTmpdir("metrics_du_test")
  37. if err != nil {
  38. t.Fatalf("Can't make a tmp dir: %v", err)
  39. }
  40. defer os.RemoveAll(tmpDir)
  41. metrics := NewMetricsDu(tmpDir)
  42. expectedEmptyDirUsage, err := volumetest.FindEmptyDirectoryUsageOnTmpfs()
  43. if err != nil {
  44. t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
  45. }
  46. actual, err := metrics.GetMetrics()
  47. if err != nil {
  48. t.Errorf("Unexpected error when calling GetMetrics %v", err)
  49. }
  50. if e, a := expectedEmptyDirUsage.Value(), actual.Used.Value(); e != a {
  51. t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
  52. }
  53. // TODO(pwittroc): Figure out a way to test these values for correctness, maybe by formatting and mounting a file
  54. // as a filesystem
  55. if a := actual.Capacity.Value(); a <= 0 {
  56. t.Errorf("Expected Capacity %d to be greater than 0.", a)
  57. }
  58. if a := actual.Available.Value(); a <= 0 {
  59. t.Errorf("Expected Available %d to be greater than 0.", a)
  60. }
  61. // Write a file and expect Used to increase
  62. ioutil.WriteFile(filepath.Join(tmpDir, "f1"), []byte("Hello World"), os.ModeTemporary)
  63. actual, err = metrics.GetMetrics()
  64. if err != nil {
  65. t.Errorf("Unexpected error when calling GetMetrics %v", err)
  66. }
  67. if e, a := (expectedEmptyDirUsage.Value() + getExpectedBlockSize(filepath.Join(tmpDir, "f1"))), actual.Used.Value(); e != a {
  68. t.Errorf("Unexpected Used for directory with file. Expected %v, got %d.", e, a)
  69. }
  70. }
  71. // TestMetricsDuRequireInit tests that if MetricsDu is not initialized with a path, GetMetrics
  72. // returns an error
  73. func TestMetricsDuRequirePath(t *testing.T) {
  74. metrics := NewMetricsDu("")
  75. actual, err := metrics.GetMetrics()
  76. expected := &Metrics{}
  77. if !volumetest.MetricsEqualIgnoreTimestamp(actual, expected) {
  78. t.Errorf("Expected empty Metrics from uninitialized MetricsDu, actual %v", *actual)
  79. }
  80. if err == nil {
  81. t.Errorf("Expected error when calling GetMetrics on uninitialized MetricsDu, actual nil")
  82. }
  83. }
  84. // TestMetricsDuRealDirectory tests that if MetricsDu is initialized to a non-existent path, GetMetrics
  85. // returns an error
  86. func TestMetricsDuRequireRealDirectory(t *testing.T) {
  87. metrics := NewMetricsDu("/not/a/real/directory")
  88. actual, err := metrics.GetMetrics()
  89. expected := &Metrics{}
  90. if !volumetest.MetricsEqualIgnoreTimestamp(actual, expected) {
  91. t.Errorf("Expected empty Metrics from incorrectly initialized MetricsDu, actual %v", *actual)
  92. }
  93. if err == nil {
  94. t.Errorf("Expected error when calling GetMetrics on incorrectly initialized MetricsDu, actual nil")
  95. }
  96. }