Modifies tests to use new volume objects.

This commit is contained in:
Danny Jones 2014-07-29 10:51:59 -07:00
parent 6191ffc0de
commit 3f7f6cb2dc
4 changed files with 20 additions and 42 deletions

View File

@ -93,7 +93,7 @@ kubelet:
- system: True - system: True
- gid_from_name: True - gid_from_name: True
- shell: /sbin/nologin - shell: /sbin/nologin
- home: /var/lib/kubelet - home: /var/kubelet
- groups: - groups:
- docker - docker
- require: - require:

View File

@ -487,7 +487,13 @@ func (kl *Kubelet) determineActiveVolumes() map[string]volume.Cleaner {
name := result["volumeName"] name := result["volumeName"]
podID := result["podID"] podID := result["podID"]
identifier := path.Join(podID, name) identifier := path.Join(podID, name)
activeVolumes[identifier], err = volume.CreateVolumeCleaner(kind, fullPath) cleaner, err := volume.CreateVolumeCleaner(kind, fullPath)
if err != nil {
glog.Errorf("Could not create cleaner for volume %v.", identifier)
}
if cleaner != nil {
activeVolumes[identifier] = cleaner
}
} }
return nil return nil
}) })

View File

@ -31,7 +31,6 @@ var ErrUnsupportedVolumeType = errors.New("unsupported volume type")
type Interface interface { type Interface interface {
// GetPath returns the directory path the volume is mounted to. // GetPath returns the directory path the volume is mounted to.
GetPath() string GetPath() string
} }
// The Builder interface provides the method to set up/mount the volume. // The Builder interface provides the method to set up/mount the volume.
@ -39,7 +38,6 @@ type Builder interface {
Interface Interface
// SetUp prepares and mounts/unpacks the volume to a directory path. // SetUp prepares and mounts/unpacks the volume to a directory path.
SetUp() error SetUp() error
} }
// The Cleaner interface provides the method to cleanup/unmount the volumes. // The Cleaner interface provides the method to cleanup/unmount the volumes.
@ -90,7 +88,7 @@ func (emptyDir *EmptyDirectoryBuilder) GetPath() string {
return path.Join(emptyDir.RootDir, emptyDir.PodID, "volumes", "empty", emptyDir.Name) return path.Join(emptyDir.RootDir, emptyDir.PodID, "volumes", "empty", emptyDir.Name)
} }
// EmptyDirectoryCleaners only need to know what path the are cleaning // EmptyDirectoryCleaners only need to know what path they are cleaning
type EmptyDirectoryCleaner struct { type EmptyDirectoryCleaner struct {
Path string Path string
} }
@ -110,10 +108,6 @@ func CreateEmptyDirectoryBuilder(volume *api.Volume, podID string, rootDir strin
return &EmptyDirectoryBuilder{volume.Name, podID, rootDir} return &EmptyDirectoryBuilder{volume.Name, podID, rootDir}
} }
func CreateEmptyDirectoryCleaner(path string) *EmptyDirectoryCleaner {
return &EmptyDirectoryCleaner{path}
}
// CreateVolumeBuilder returns a Builder capable of mounting a volume described by an // CreateVolumeBuilder returns a Builder capable of mounting a volume described by an
// *api.Volume, or an error. // *api.Volume, or an error.
func CreateVolumeBuilder(volume *api.Volume, podID string, rootDir string) (Builder, error) { func CreateVolumeBuilder(volume *api.Volume, podID string, rootDir string) (Builder, error) {
@ -136,10 +130,11 @@ func CreateVolumeBuilder(volume *api.Volume, podID string, rootDir string) (Buil
return vol, nil return vol, nil
} }
// CreateVolumeCleaner returns a Cleaner capable of tearing down a volume.
func CreateVolumeCleaner(kind string, path string) (Cleaner, error) { func CreateVolumeCleaner(kind string, path string) (Cleaner, error) {
switch kind { switch kind {
case "empty": case "empty":
return CreateEmptyDirectoryCleaner(path), nil return &EmptyDirectoryCleaner{path}, nil
default: default:
return nil, ErrUnsupportedVolumeType return nil, ErrUnsupportedVolumeType
} }

View File

@ -46,7 +46,7 @@ func TestCreateVolumeBuilders(t *testing.T) {
}, },
"/dir/path", "/dir/path",
"my-id", "my-id",
"host", "",
}, },
{ {
api.Volume{ api.Volume{
@ -59,7 +59,7 @@ func TestCreateVolumeBuilders(t *testing.T) {
"my-id", "my-id",
"empty", "empty",
}, },
{api.Volume{}, "", ""}, {api.Volume{}, "", "", ""},
{ {
api.Volume{ api.Volume{
Name: "empty-dir", Name: "empty-dir",
@ -72,9 +72,9 @@ func TestCreateVolumeBuilders(t *testing.T) {
} }
for _, createVolumesTest := range createVolumesTests { for _, createVolumesTest := range createVolumesTests {
tt := createVolumesTest tt := createVolumesTest
v, err := CreateVolumeBuilder(&tt.volume, tt.podID, tempDir) vb, err := CreateVolumeBuilder(&tt.volume, tt.podID, tempDir)
if tt.volume.Source == nil { if tt.volume.Source == nil {
if v != nil { if vb != nil {
t.Errorf("Expected volume to be nil") t.Errorf("Expected volume to be nil")
} }
continue continue
@ -88,47 +88,24 @@ func TestCreateVolumeBuilders(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
err = v.SetUp() err = vb.SetUp()
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
path := v.GetPath() path := vb.GetPath()
if path != tt.path { if path != tt.path {
t.Errorf("Unexpected bind path. Expected %v, got %v", tt.path, path) t.Errorf("Unexpected bind path. Expected %v, got %v", tt.path, path)
} }
v, err = CreateVolumeCleaner(tt.kind) vc, err := CreateVolumeCleaner(tt.kind, vb.GetPath())
if tt.kind == "" { if tt.kind == "" {
if err != ErrUnsupportedVolumeType { if err != ErrUnsupportedVolumeType {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
continue
} }
err = v.TearDown() err = vc.TearDown()
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
} }
} }
func TestEmptySetUpAndTearDown(t *testing.T) {
volumes := []api.Volume{
{
Name: "empty-dir",
Source: &api.VolumeSource{
EmptyDirectory: &api.EmptyDirectory{},
},
},
}
expectedPath := "/tmp/kubelet/fakeID/volumes/empty/empty-dir"
for _, volume := range volumes {
volumeBuilder, _ := CreateVolumeBuilder(&volume, "fakeID", "/tmp/kubelet")
volumeBuilder.SetUp()
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
t.Errorf("Mount directory %v does not exist after SetUp", expectedPath)
}
volumeCleaner, _ := CreateVolumeCleaner("empty", expectedPath)
volumeCleaner.TearDown()
if _, err := os.Stat(expectedPath); !os.IsNotExist(err) {
t.Errorf("Mount directory %v still exists after TearDown", expectedPath)
}
}
os.RemoveAll("/tmp/kubelet")
}