Fix assumptions about tmpfs in metrics du tests

This commit is contained in:
Paul Morie 2015-12-11 12:55:17 -05:00
parent 775369a8f1
commit eb89c2519a
4 changed files with 52 additions and 13 deletions

View File

@ -298,13 +298,18 @@ func TestMetrics(t *testing.T) {
// Need to create the subdirectory
os.MkdirAll(builder.GetPath(), 0755)
expectedEmptyDirUsage, err := volume.FindEmptyDirectoryUsageOnTmpfs()
if err != nil {
t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
}
// TODO(pwittroc): Move this into a reusable testing utility
metrics, err := builder.GetMetrics()
if err != nil {
t.Errorf("Unexpected error when calling GetMetrics %v", err)
}
if metrics.Used.Value() != 4096 {
t.Errorf("Expected Used %d to be 4096", metrics.Used.Value())
if e, a := expectedEmptyDirUsage.Value(), metrics.Used.Value(); e != a {
t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
}
if metrics.Capacity.Value() <= 0 {
t.Errorf("Expected Capacity to be greater than 0")

View File

@ -300,13 +300,17 @@ func TestMetrics(t *testing.T) {
t.Errorf("Failed to make a new Builder: %v", err)
}
// TODO(pwittroc): Move this into a reusable testing utility
expectedEmptyDirUsage, err := volume.FindEmptyDirectoryUsageOnTmpfs()
if err != nil {
t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
}
metrics, err := builder.GetMetrics()
if err != nil {
t.Errorf("Unexpected error when calling GetMetrics %v", err)
}
if metrics.Used.Value() != 4096 {
t.Errorf("Expected Used %d to be 4096", metrics.Used)
if e, a := expectedEmptyDirUsage.Value(), metrics.Used.Value(); e != a {
t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
}
if metrics.Capacity.Value() <= 0 {
t.Errorf("Expected Capacity to be greater than 0")

View File

@ -23,6 +23,8 @@ import (
"testing"
)
const expectedBlockSize = 4096
// TestMetricsDuGetCapacity tests that MetricsDu can read disk usage
// for path
func TestMetricsDuGetCapacity(t *testing.T) {
@ -33,21 +35,26 @@ func TestMetricsDuGetCapacity(t *testing.T) {
defer os.RemoveAll(tmpDir)
metrics := NewMetricsDu(tmpDir)
expectedEmptyDirUsage, err := FindEmptyDirectoryUsageOnTmpfs()
if err != nil {
t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
}
actual, err := metrics.GetMetrics()
if err != nil {
t.Errorf("Unexpected error when calling GetMetrics %v", err)
}
if actual.Used.Value() != 4096 {
t.Errorf("Expected Used %d for empty directory to be 4096.", actual.Used.Value())
if e, a := expectedEmptyDirUsage.Value(), actual.Used.Value(); e != a {
t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
}
// TODO(pwittroc): Figure out a way to test these values for correctness, maybe by formatting and mounting a file
// as a filesystem
if actual.Capacity.Value() <= 0 {
t.Errorf("Expected Capacity %d to be greater than 0.", actual.Capacity.Value())
if a := actual.Capacity.Value(); a <= 0 {
t.Errorf("Expected Capacity %d to be greater than 0.", a)
}
if actual.Available.Value() <= 0 {
t.Errorf("Expected Available %d to be greater than 0.", actual.Available.Value())
if a := actual.Available.Value(); a <= 0 {
t.Errorf("Expected Available %d to be greater than 0.", a)
}
// Write a file and expect Used to increase
@ -56,8 +63,8 @@ func TestMetricsDuGetCapacity(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error when calling GetMetrics %v", err)
}
if actual.Used.Value() != 8192 {
t.Errorf("Unexpected Used for directory with file. Expected 8192, was %d.", actual.Used.Value())
if e, a := (expectedEmptyDirUsage.Value() + expectedBlockSize), actual.Used.Value(); e != a {
t.Errorf("Unexpected Used for directory with file. Expected %v, got %d.", e, a)
}
}

View File

@ -18,10 +18,14 @@ package volume
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/types"
@ -267,3 +271,22 @@ func (fc *FakeProvisioner) NewPersistentVolumeTemplate() (*api.PersistentVolume,
func (fc *FakeProvisioner) Provision(pv *api.PersistentVolume) error {
return nil
}
// FindEmptyDirectoryUsageOnTmpfs finds the expected usage of an empty directory existing on
// a tmpfs filesystem on this system.
func FindEmptyDirectoryUsageOnTmpfs() (*resource.Quantity, error) {
tmpDir, err := ioutil.TempDir(os.TempDir(), "metrics_du_test")
if err != nil {
return nil, err
}
out, err := exec.Command("nice", "-n", "19", "du", "-s", "-B", "1", tmpDir).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed command 'du' on %s with error %v", tmpDir, err)
}
used, err := resource.ParseQuantity(strings.Fields(string(out))[0])
if err != nil {
return nil, fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err)
}
used.Format = resource.BinarySI
return used, nil
}