1
16
17 package volume_test
18
19 import (
20 "os"
21 "path/filepath"
22 "testing"
23
24 utiltesting "k8s.io/client-go/util/testing"
25 . "k8s.io/kubernetes/pkg/volume"
26 volumetest "k8s.io/kubernetes/pkg/volume/testing"
27 )
28
29
30
31 func TestMetricsDuGetCapacity(t *testing.T) {
32 tmpDir, err := utiltesting.MkTmpdir("metrics_du_test")
33 if err != nil {
34 t.Fatalf("Can't make a tmp dir: %v", err)
35 }
36 defer os.RemoveAll(tmpDir)
37 metrics := NewMetricsDu(tmpDir)
38
39 expectedEmptyDirUsage, err := volumetest.FindEmptyDirectoryUsageOnTmpfs()
40 if err != nil {
41 t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
42 }
43
44 actual, err := metrics.GetMetrics()
45 if err != nil {
46 t.Errorf("Unexpected error when calling GetMetrics %v", err)
47 }
48 if e, a := expectedEmptyDirUsage.Value(), actual.Used.Value(); e != a {
49 t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
50 }
51
52
53
54 if a := actual.Capacity.Value(); a <= 0 {
55 t.Errorf("Expected Capacity %d to be greater than 0.", a)
56 }
57 if a := actual.Available.Value(); a <= 0 {
58 t.Errorf("Expected Available %d to be greater than 0.", a)
59 }
60
61
62 os.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
72 previousInodes := actual.InodesUsed.Value()
73 err = os.Link(filepath.Join(tmpDir, "f1"), filepath.Join(tmpDir, "f2"))
74 if err != nil {
75 t.Errorf("Unexpected error when creating hard link %v", err)
76 }
77 actual, err = metrics.GetMetrics()
78 if err != nil {
79 t.Errorf("Unexpected error when calling GetMetrics %v", err)
80 }
81 if e, a := previousInodes, actual.InodesUsed.Value(); e != a {
82 t.Errorf("Unexpected Used for directory with file. Expected %v, got %d.", e, a)
83 }
84
85 }
86
87
88
89 func TestMetricsDuRequirePath(t *testing.T) {
90 metrics := NewMetricsDu("")
91 actual, err := metrics.GetMetrics()
92 expected := &Metrics{}
93 if !volumetest.MetricsEqualIgnoreTimestamp(actual, expected) {
94 t.Errorf("Expected empty Metrics from uninitialized MetricsDu, actual %v", *actual)
95 }
96 if err == nil {
97 t.Errorf("Expected error when calling GetMetrics on uninitialized MetricsDu, actual nil")
98 }
99 }
100
101
102
103 func TestMetricsDuRequireRealDirectory(t *testing.T) {
104 metrics := NewMetricsDu("/not/a/real/directory")
105 actual, err := metrics.GetMetrics()
106 expected := &Metrics{}
107 if !volumetest.MetricsEqualIgnoreTimestamp(actual, expected) {
108 t.Errorf("Expected empty Metrics from incorrectly initialized MetricsDu, actual %v", *actual)
109 }
110 if err == nil {
111 t.Errorf("Expected error when calling GetMetrics on incorrectly initialized MetricsDu, actual nil")
112 }
113 }
114
View as plain text