...

Source file src/k8s.io/kubernetes/pkg/volume/metrics_du_test.go

Documentation: k8s.io/kubernetes/pkg/volume

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     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  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // TestMetricsDuGetCapacity tests that MetricsDu can read disk usage
    30  // for path
    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  	// TODO(pwittroc): Figure out a way to test these values for correctness, maybe by formatting and mounting a file
    53  	// as a filesystem
    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  	// Write a file and expect Used to increase
    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  	// create a hardlink and expect inodes count to stay the same
    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  // TestMetricsDuRequireInit tests that if MetricsDu is not initialized with a path, GetMetrics
    88  // returns an error
    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  // TestMetricsDuRealDirectory tests that if MetricsDu is initialized to a non-existent path, GetMetrics
   102  // returns an error
   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