Fix volume secret_test.go on darwin by not checking volume usage metrics

This commit is contained in:
Phillip Wittrock 2016-02-16 17:16:06 -08:00
parent 6c638b5f3f
commit 1e5175594d
5 changed files with 88 additions and 17 deletions

View File

@ -19,8 +19,6 @@ package volume
import (
"errors"
"fmt"
"os/exec"
"strings"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/volume/util"
@ -64,17 +62,10 @@ func (md *metricsDu) GetMetrics() (*Metrics, error) {
// runDu executes the "du" command and writes the results to metrics.Used
func (md *metricsDu) runDu(metrics *Metrics) error {
// Uses the same niceness level as cadvisor.fs does when running du
// Uses -B 1 to always scale to a blocksize of 1 byte
out, err := exec.Command("nice", "-n", "19", "du", "-s", "-B", "1", md.path).CombinedOutput()
used, err := util.Du(md.path)
if err != nil {
return fmt.Errorf("failed command 'du' on %s with error %v", md.path, err)
return err
}
used, err := resource.ParseQuantity(strings.Fields(string(out))[0])
if err != nil {
return fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err)
}
used.Format = resource.BinarySI
metrics.Used = used
return nil
}

View File

@ -21,10 +21,10 @@ import (
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/api"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/testing/fake"
@ -33,6 +33,8 @@ import (
"k8s.io/kubernetes/pkg/volume"
"k8s.io/kubernetes/pkg/volume/empty_dir"
"k8s.io/kubernetes/pkg/volume/util"
"github.com/stretchr/testify/assert"
)
func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) {
@ -122,12 +124,16 @@ func TestPlugin(t *testing.T) {
}
}
doTestSecretDataInVolume(volumePath, secret, t)
defer doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
// Metrics only supported on linux
metrics, err := builder.GetMetrics()
assert.NotEmpty(t, metrics)
assert.NoError(t, err)
doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
if runtime.GOOS == "linux" {
assert.NotEmpty(t, metrics)
assert.NoError(t, err)
} else {
t.Skipf("Volume metrics not supported on %s", runtime.GOOS)
}
}
// Test the case where the 'ready' file has been created and the pod volume dir

View File

@ -0,0 +1,47 @@
// +build darwin
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"errors"
"fmt"
"os/exec"
"strings"
"k8s.io/kubernetes/pkg/api/resource"
)
// FSInfo linux returns (available bytes, byte capacity, error) for the filesystem that
// path resides upon.
func FsInfo(path string) (int64, int64, error) {
return 0, 0, errors.New("FsInfo not supported for this build.")
}
func Du(path string) (*resource.Quantity, error) {
out, err := exec.Command("nice", "-n", "19", "du", "-s", path).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed command 'du' ($ nice -n 19 du -s) on path %s with error %v", path, 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
}

View File

@ -19,7 +19,12 @@ limitations under the License.
package util
import (
"fmt"
"os/exec"
"strings"
"syscall"
"k8s.io/kubernetes/pkg/api/resource"
)
// FSInfo linux returns (available bytes, byte capacity, error) for the filesystem that
@ -39,3 +44,18 @@ func FsInfo(path string) (int64, int64, error) {
return available, capacity, nil
}
func Du(path string) (*resource.Quantity, error) {
// Uses the same niceness level as cadvisor.fs does when running du
// Uses -B 1 to always scale to a blocksize of 1 byte
out, err := exec.Command("nice", "-n", "19", "du", "-s", "-B", "1", path).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed command 'du' ($ nice -n 19 du -s -B 1) on path %s with error %v", path, 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
}

View File

@ -1,4 +1,4 @@
// +build !linux
// +build !linux,!darwin
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
@ -20,9 +20,16 @@ package util
import (
"errors"
"fmt"
"k8s.io/kubernetes/pkg/api/resource"
)
// FSInfo unsupported returns 0 values for available and capacity and an error.
func FsInfo(path string) (int64, int64, error) {
return 0, 0, errors.New("FsInfo not supported for this build.")
}
func Du(path string) (*resource.Quantity, error) {
return nil, fmt.Errorf("Du not support for this build.")
}