k3s/pkg/volume/testing/testing.go

469 lines
12 KiB
Go
Raw Normal View History

/*
Copyright 2014 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 testing
import (
2015-09-07 18:44:49 +00:00
"fmt"
"os"
"os/exec"
"path"
"strings"
"sync"
"time"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
2015-04-10 16:54:01 +00:00
"k8s.io/kubernetes/pkg/cloudprovider"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/io"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/util/mount"
utilstrings "k8s.io/kubernetes/pkg/util/strings"
utiltesting "k8s.io/kubernetes/pkg/util/testing"
. "k8s.io/kubernetes/pkg/volume"
)
// fakeVolumeHost is useful for testing volume plugins.
type fakeVolumeHost struct {
rootDir string
kubeClient clientset.Interface
pluginMgr VolumePluginMgr
2015-04-10 16:54:01 +00:00
cloud cloudprovider.Interface
mounter mount.Interface
writer io.Writer
}
func NewFakeVolumeHost(rootDir string, kubeClient clientset.Interface, plugins []VolumePlugin) *fakeVolumeHost {
2015-04-10 16:54:01 +00:00
host := &fakeVolumeHost{rootDir: rootDir, kubeClient: kubeClient, cloud: nil}
host.mounter = &mount.FakeMounter{}
host.writer = &io.StdWriter{}
host.pluginMgr.InitPlugins(plugins, host)
return host
}
func (f *fakeVolumeHost) GetPluginDir(podUID string) string {
return path.Join(f.rootDir, "plugins", podUID)
}
func (f *fakeVolumeHost) GetPodVolumeDir(podUID types.UID, pluginName, volumeName string) string {
return path.Join(f.rootDir, "pods", string(podUID), "volumes", pluginName, volumeName)
}
func (f *fakeVolumeHost) GetPodPluginDir(podUID types.UID, pluginName string) string {
return path.Join(f.rootDir, "pods", string(podUID), "plugins", pluginName)
}
func (f *fakeVolumeHost) GetKubeClient() clientset.Interface {
return f.kubeClient
}
2015-04-10 16:54:01 +00:00
func (f *fakeVolumeHost) GetCloudProvider() cloudprovider.Interface {
return f.cloud
}
func (f *fakeVolumeHost) GetMounter() mount.Interface {
return f.mounter
}
func (f *fakeVolumeHost) GetWriter() io.Writer {
return f.writer
}
func (f *fakeVolumeHost) NewWrapperMounter(volName string, spec Spec, pod *api.Pod, opts VolumeOptions) (Mounter, error) {
// The name of wrapper volume is set to "wrapped_{wrapped_volume_name}"
wrapperVolumeName := "wrapped_" + volName
if spec.Volume != nil {
spec.Volume.Name = wrapperVolumeName
}
plug, err := f.pluginMgr.FindPluginBySpec(&spec)
if err != nil {
return nil, err
}
return plug.NewMounter(&spec, pod, opts)
}
func (f *fakeVolumeHost) NewWrapperUnmounter(volName string, spec Spec, podUID types.UID) (Unmounter, error) {
// The name of wrapper volume is set to "wrapped_{wrapped_volume_name}"
wrapperVolumeName := "wrapped_" + volName
if spec.Volume != nil {
spec.Volume.Name = wrapperVolumeName
}
plug, err := f.pluginMgr.FindPluginBySpec(&spec)
if err != nil {
return nil, err
}
return plug.NewUnmounter(spec.Name(), podUID)
2015-02-18 01:26:41 +00:00
}
// Returns the hostname of the host kubelet is running on
func (f *fakeVolumeHost) GetHostName() string {
return "fakeHostName"
}
2015-08-31 13:44:37 +00:00
func ProbeVolumePlugins(config VolumeConfig) []VolumePlugin {
if _, ok := config.OtherAttributes["fake-property"]; ok {
return []VolumePlugin{
&FakeVolumePlugin{
PluginName: "fake-plugin",
Host: nil,
// SomeFakeProperty: config.OtherAttributes["fake-property"] -- string, may require parsing by plugin
},
}
}
return []VolumePlugin{&FakeVolumePlugin{PluginName: "fake-plugin"}}
}
// FakeVolumePlugin is useful for testing. It tries to be a fully compliant
// plugin, but all it does is make empty directories.
// Use as:
// volume.RegisterPlugin(&FakePlugin{"fake-name"})
type FakeVolumePlugin struct {
sync.RWMutex
PluginName string
Host VolumeHost
Config VolumeConfig
LastProvisionerOptions VolumeOptions
NewAttacherCallCount int
NewDetacherCallCount int
Mounters []*FakeVolume
Unmounters []*FakeVolume
Attachers []*FakeVolume
Detachers []*FakeVolume
}
var _ VolumePlugin = &FakeVolumePlugin{}
2015-05-29 20:32:44 +00:00
var _ RecyclableVolumePlugin = &FakeVolumePlugin{}
2015-10-12 18:27:49 +00:00
var _ DeletableVolumePlugin = &FakeVolumePlugin{}
var _ ProvisionableVolumePlugin = &FakeVolumePlugin{}
var _ AttachableVolumePlugin = &FakeVolumePlugin{}
func (plugin *FakeVolumePlugin) getFakeVolume(list *[]*FakeVolume) *FakeVolume {
volume := &FakeVolume{}
*list = append(*list, volume)
return volume
}
func (plugin *FakeVolumePlugin) Init(host VolumeHost) error {
plugin.Lock()
defer plugin.Unlock()
plugin.Host = host
return nil
}
func (plugin *FakeVolumePlugin) Name() string {
plugin.RLock()
defer plugin.RUnlock()
return plugin.PluginName
}
func (plugin *FakeVolumePlugin) CanSupport(spec *Spec) bool {
// TODO: maybe pattern-match on spec.Name() to decide?
return true
}
func (plugin *FakeVolumePlugin) NewMounter(spec *Spec, pod *api.Pod, opts VolumeOptions) (Mounter, error) {
plugin.Lock()
defer plugin.Unlock()
volume := plugin.getFakeVolume(&plugin.Mounters)
volume.PodUID = pod.UID
volume.VolName = spec.Name()
volume.Plugin = plugin
volume.MetricsNil = MetricsNil{}
return volume, nil
}
func (plugin *FakeVolumePlugin) NewUnmounter(volName string, podUID types.UID) (Unmounter, error) {
plugin.Lock()
defer plugin.Unlock()
volume := plugin.getFakeVolume(&plugin.Unmounters)
volume.PodUID = podUID
volume.VolName = volName
volume.Plugin = plugin
volume.MetricsNil = MetricsNil{}
return volume, nil
}
func (plugin *FakeVolumePlugin) NewAttacher() (Attacher, error) {
plugin.Lock()
defer plugin.Unlock()
plugin.NewAttacherCallCount = plugin.NewAttacherCallCount + 1
return plugin.getFakeVolume(&plugin.Attachers), nil
}
func (plugin *FakeVolumePlugin) GetAttachers() (Attachers []*FakeVolume) {
plugin.RLock()
defer plugin.RUnlock()
return plugin.Attachers
}
func (plugin *FakeVolumePlugin) GetNewAttacherCallCount() int {
plugin.RLock()
defer plugin.RUnlock()
return plugin.NewAttacherCallCount
}
func (plugin *FakeVolumePlugin) NewDetacher() (Detacher, error) {
plugin.Lock()
defer plugin.Unlock()
plugin.NewDetacherCallCount = plugin.NewDetacherCallCount + 1
return plugin.getFakeVolume(&plugin.Detachers), nil
}
func (plugin *FakeVolumePlugin) GetDetachers() (Detachers []*FakeVolume) {
plugin.RLock()
defer plugin.RUnlock()
return plugin.Detachers
}
func (plugin *FakeVolumePlugin) GetNewDetacherCallCount() int {
plugin.RLock()
defer plugin.RUnlock()
return plugin.NewDetacherCallCount
}
func (plugin *FakeVolumePlugin) GetDeviceName(spec *Spec) (string, error) {
return spec.Name(), nil
}
func (plugin *FakeVolumePlugin) NewRecycler(pvName string, spec *Spec) (Recycler, error) {
return &fakeRecycler{"/attributesTransferredFromSpec", MetricsNil{}}, nil
2015-05-29 20:32:44 +00:00
}
func (plugin *FakeVolumePlugin) NewDeleter(spec *Spec) (Deleter, error) {
return &FakeDeleter{"/attributesTransferredFromSpec", MetricsNil{}}, nil
}
2015-10-12 18:27:49 +00:00
func (plugin *FakeVolumePlugin) NewProvisioner(options VolumeOptions) (Provisioner, error) {
plugin.Lock()
defer plugin.Unlock()
plugin.LastProvisionerOptions = options
2015-10-12 18:27:49 +00:00
return &FakeProvisioner{options, plugin.Host}, nil
}
func (plugin *FakeVolumePlugin) GetAccessModes() []api.PersistentVolumeAccessMode {
return []api.PersistentVolumeAccessMode{}
}
type FakeVolume struct {
sync.RWMutex
PodUID types.UID
VolName string
Plugin *FakeVolumePlugin
MetricsNil
SetUpCallCount int
TearDownCallCount int
AttachCallCount int
DetachCallCount int
WaitForAttachCallCount int
WaitForDetachCallCount int
MountDeviceCallCount int
UnmountDeviceCallCount int
GetDeviceMountPathCallCount int
}
func (_ *FakeVolume) GetAttributes() Attributes {
return Attributes{
ReadOnly: false,
Managed: true,
SupportsSELinux: true,
}
2015-10-20 18:49:39 +00:00
}
2015-12-18 15:55:11 +00:00
func (fv *FakeVolume) SetUp(fsGroup *int64) error {
fv.Lock()
defer fv.Unlock()
fv.SetUpCallCount++
return fv.SetUpAt(fv.getPath(), fsGroup)
}
2015-12-18 15:55:11 +00:00
func (fv *FakeVolume) SetUpAt(dir string, fsGroup *int64) error {
return os.MkdirAll(dir, 0750)
}
func (fv *FakeVolume) GetPath() string {
fv.RLock()
defer fv.RUnlock()
return fv.getPath()
}
func (fv *FakeVolume) getPath() string {
return path.Join(fv.Plugin.Host.GetPodVolumeDir(fv.PodUID, utilstrings.EscapeQualifiedNameForDisk(fv.Plugin.PluginName), fv.VolName))
}
func (fv *FakeVolume) TearDown() error {
fv.Lock()
defer fv.Unlock()
fv.TearDownCallCount++
return fv.TearDownAt(fv.getPath())
}
func (fv *FakeVolume) TearDownAt(dir string) error {
return os.RemoveAll(dir)
}
2015-05-29 20:32:44 +00:00
func (fv *FakeVolume) Attach(spec *Spec, hostName string) error {
fv.Lock()
defer fv.Unlock()
fv.AttachCallCount++
return nil
}
func (fv *FakeVolume) GetAttachCallCount() int {
fv.RLock()
defer fv.RUnlock()
return fv.AttachCallCount
}
func (fv *FakeVolume) WaitForAttach(spec *Spec, spectimeout time.Duration) (string, error) {
fv.Lock()
defer fv.Unlock()
fv.WaitForAttachCallCount++
return "", nil
}
func (fv *FakeVolume) GetDeviceMountPath(spec *Spec) string {
fv.Lock()
defer fv.Unlock()
fv.GetDeviceMountPathCallCount++
return ""
}
func (fv *FakeVolume) MountDevice(spec *Spec, devicePath string, deviceMountPath string, mounter mount.Interface) error {
fv.Lock()
defer fv.Unlock()
fv.MountDeviceCallCount++
return nil
}
func (fv *FakeVolume) Detach(deviceMountPath string, hostName string) error {
fv.Lock()
defer fv.Unlock()
fv.DetachCallCount++
return nil
}
func (fv *FakeVolume) GetDetachCallCount() int {
fv.RLock()
defer fv.RUnlock()
return fv.DetachCallCount
}
func (fv *FakeVolume) WaitForDetach(devicePath string, timeout time.Duration) error {
fv.Lock()
defer fv.Unlock()
fv.WaitForDetachCallCount++
return nil
}
func (fv *FakeVolume) UnmountDevice(globalMountPath string, mounter mount.Interface) error {
fv.Lock()
defer fv.Unlock()
fv.UnmountDeviceCallCount++
return nil
}
2015-09-07 18:44:49 +00:00
type fakeRecycler struct {
2015-05-29 20:32:44 +00:00
path string
MetricsNil
2015-05-29 20:32:44 +00:00
}
2015-09-07 18:44:49 +00:00
func (fr *fakeRecycler) Recycle() error {
2015-05-29 20:32:44 +00:00
// nil is success, else error
return nil
}
2015-09-07 18:44:49 +00:00
func (fr *fakeRecycler) GetPath() string {
2015-05-29 20:32:44 +00:00
return fr.path
}
2015-09-07 18:44:49 +00:00
func NewFakeRecycler(pvName string, spec *Spec, host VolumeHost, config VolumeConfig) (Recycler, error) {
2015-09-07 18:44:49 +00:00
if spec.PersistentVolume == nil || spec.PersistentVolume.Spec.HostPath == nil {
return nil, fmt.Errorf("fakeRecycler only supports spec.PersistentVolume.Spec.HostPath")
}
return &fakeRecycler{
path: spec.PersistentVolume.Spec.HostPath.Path,
}, nil
}
type FakeDeleter struct {
path string
MetricsNil
}
func (fd *FakeDeleter) Delete() error {
// nil is success, else error
return nil
}
func (fd *FakeDeleter) GetPath() string {
return fd.path
}
2015-10-12 18:27:49 +00:00
type FakeProvisioner struct {
Options VolumeOptions
Host VolumeHost
}
func (fc *FakeProvisioner) Provision() (*api.PersistentVolume, error) {
2015-10-12 18:27:49 +00:00
fullpath := fmt.Sprintf("/tmp/hostpath_pv/%s", util.NewUUID())
pv := &api.PersistentVolume{
2015-10-12 18:27:49 +00:00
ObjectMeta: api.ObjectMeta{
Name: fc.Options.PVName,
2015-10-12 18:27:49 +00:00
Annotations: map[string]string{
"kubernetes.io/createdby": "fakeplugin-provisioner",
},
},
Spec: api.PersistentVolumeSpec{
PersistentVolumeReclaimPolicy: fc.Options.PersistentVolumeReclaimPolicy,
AccessModes: fc.Options.AccessModes,
Capacity: api.ResourceList{
api.ResourceName(api.ResourceStorage): fc.Options.Capacity,
},
PersistentVolumeSource: api.PersistentVolumeSource{
HostPath: &api.HostPathVolumeSource{
Path: fullpath,
},
},
},
}
2015-10-12 18:27:49 +00:00
return pv, nil
2015-10-12 18:27:49 +00:00
}
// FindEmptyDirectoryUsageOnTmpfs finds the expected usage of an empty directory existing on
// a tmpfs filesystem on this system.
func FindEmptyDirectoryUsageOnTmpfs() (*resource.Quantity, error) {
tmpDir, err := utiltesting.MkTmpdir("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
}