mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 13:55:41 +00:00
Merge pull request #79978 from cwdsuzhou/July/hostpath_cleanup
Fix host path test clean up
This commit is contained in:
commit
37647eb88a
@ -188,7 +188,7 @@ func newDeleter(spec *volume.Spec, host volume.VolumeHost) (volume.Deleter, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newProvisioner(options volume.VolumeOptions, host volume.VolumeHost, plugin *hostPathPlugin) (volume.Provisioner, error) {
|
func newProvisioner(options volume.VolumeOptions, host volume.VolumeHost, plugin *hostPathPlugin) (volume.Provisioner, error) {
|
||||||
return &hostPathProvisioner{options: options, host: host, plugin: plugin}, nil
|
return &hostPathProvisioner{options: options, host: host, plugin: plugin, basePath: "hostpath_pv"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HostPath volumes represent a bare host file or directory mount.
|
// HostPath volumes represent a bare host file or directory mount.
|
||||||
@ -268,19 +268,20 @@ func (c *hostPathUnmounter) TearDownAt(dir string) error {
|
|||||||
// hostPathProvisioner implements a Provisioner for the HostPath plugin
|
// hostPathProvisioner implements a Provisioner for the HostPath plugin
|
||||||
// This implementation is meant for testing only and only works in a single node cluster.
|
// This implementation is meant for testing only and only works in a single node cluster.
|
||||||
type hostPathProvisioner struct {
|
type hostPathProvisioner struct {
|
||||||
host volume.VolumeHost
|
host volume.VolumeHost
|
||||||
options volume.VolumeOptions
|
options volume.VolumeOptions
|
||||||
plugin *hostPathPlugin
|
plugin *hostPathPlugin
|
||||||
|
basePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create for hostPath simply creates a local /tmp/hostpath_pv/%s directory as a new PersistentVolume.
|
// Create for hostPath simply creates a local /tmp/%/%s directory as a new PersistentVolume, default /tmp/hostpath_pv/%s.
|
||||||
// This Provisioner is meant for development and testing only and WILL NOT WORK in a multi-node cluster.
|
// This Provisioner is meant for development and testing only and WILL NOT WORK in a multi-node cluster.
|
||||||
func (r *hostPathProvisioner) Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error) {
|
func (r *hostPathProvisioner) Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error) {
|
||||||
if util.CheckPersistentVolumeClaimModeBlock(r.options.PVC) {
|
if util.CheckPersistentVolumeClaimModeBlock(r.options.PVC) {
|
||||||
return nil, fmt.Errorf("%s does not support block volume provisioning", r.plugin.GetPluginName())
|
return nil, fmt.Errorf("%s does not support block volume provisioning", r.plugin.GetPluginName())
|
||||||
}
|
}
|
||||||
|
|
||||||
fullpath := fmt.Sprintf("/tmp/hostpath_pv/%s", uuid.NewUUID())
|
fullpath := fmt.Sprintf("/tmp/%s/%s", r.basePath, uuid.NewUUID())
|
||||||
|
|
||||||
capacity := r.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
|
capacity := r.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
|
||||||
pv := &v1.PersistentVolume{
|
pv := &v1.PersistentVolume{
|
||||||
|
@ -97,13 +97,12 @@ func TestRecycler(t *testing.T) {
|
|||||||
|
|
||||||
func TestDeleter(t *testing.T) {
|
func TestDeleter(t *testing.T) {
|
||||||
// Deleter has a hard-coded regex for "/tmp".
|
// Deleter has a hard-coded regex for "/tmp".
|
||||||
tempPath := fmt.Sprintf("/tmp/hostpath/%s", uuid.NewUUID())
|
tempPath := fmt.Sprintf("/tmp/hostpath.%s", uuid.NewUUID())
|
||||||
defer os.RemoveAll(tempPath)
|
|
||||||
err := os.MkdirAll(tempPath, 0750)
|
err := os.MkdirAll(tempPath, 0750)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create tmp directory for deleter: %v", err)
|
t.Fatalf("Failed to create tmp directory for deleter: %v", err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(tempPath)
|
||||||
plugMgr := volume.VolumePluginMgr{}
|
plugMgr := volume.VolumePluginMgr{}
|
||||||
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
||||||
|
|
||||||
@ -154,30 +153,32 @@ func TestDeleterTempDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProvisioner(t *testing.T) {
|
func TestProvisioner(t *testing.T) {
|
||||||
tempPath := fmt.Sprintf("/tmp/hostpath/%s", uuid.NewUUID())
|
|
||||||
defer os.RemoveAll(tempPath)
|
|
||||||
err := os.MkdirAll(tempPath, 0750)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Failed to create tempPath %s error:%v", tempPath, err)
|
|
||||||
}
|
|
||||||
plugMgr := volume.VolumePluginMgr{}
|
plugMgr := volume.VolumePluginMgr{}
|
||||||
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{ProvisioningEnabled: true}),
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{ProvisioningEnabled: true}),
|
||||||
nil,
|
nil,
|
||||||
volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
||||||
spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: tempPath}}}}}
|
spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{
|
||||||
|
PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: fmt.Sprintf("/tmp/hostpath.%s", uuid.NewUUID())}}}}}
|
||||||
plug, err := plugMgr.FindCreatablePluginBySpec(spec)
|
plug, err := plugMgr.FindCreatablePluginBySpec(spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Can't find the plugin by name")
|
t.Fatalf("Can't find the plugin by name")
|
||||||
}
|
}
|
||||||
options := volume.VolumeOptions{
|
options := volume.VolumeOptions{
|
||||||
PVC: volumetest.CreateTestPVC("1Gi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
|
PVC: volumetest.CreateTestPVC("1Gi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
|
||||||
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
|
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
|
||||||
}
|
}
|
||||||
creater, err := plug.NewProvisioner(options)
|
creator, err := plug.NewProvisioner(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to make a new Provisioner: %v", err)
|
t.Fatalf("Failed to make a new Provisioner: %v", err)
|
||||||
}
|
}
|
||||||
pv, err := creater.Provision(nil, nil)
|
|
||||||
|
hostPathCreator, ok := creator.(*hostPathProvisioner)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("Not a hostPathProvisioner")
|
||||||
|
}
|
||||||
|
hostPathCreator.basePath = fmt.Sprintf("%s.%s", "hostPath_pv", uuid.NewUUID())
|
||||||
|
|
||||||
|
pv, err := hostPathCreator.Provision(nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error creating volume: %v", err)
|
t.Errorf("Unexpected error creating volume: %v", err)
|
||||||
}
|
}
|
||||||
@ -196,7 +197,8 @@ func TestProvisioner(t *testing.T) {
|
|||||||
t.Errorf("Expected reclaim policy %+v but got %+v", v1.PersistentVolumeReclaimDelete, pv.Spec.PersistentVolumeReclaimPolicy)
|
t.Errorf("Expected reclaim policy %+v but got %+v", v1.PersistentVolumeReclaimDelete, pv.Spec.PersistentVolumeReclaimPolicy)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.RemoveAll(pv.Spec.HostPath.Path)
|
os.RemoveAll(hostPathCreator.basePath)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidHostPath(t *testing.T) {
|
func TestInvalidHostPath(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user