Merge pull request #14168 from tmrts/refactor/tests

Refactor downward API volume plugin test
This commit is contained in:
Eric Tune 2015-09-22 13:52:31 -07:00
commit f614e7df63

View File

@ -600,7 +600,8 @@ func TestWriteWithUnixPathBadPath(t *testing.T) {
labels := map[string]string{ labels := map[string]string{
"key1": "value1", "key1": "value1",
"key2": "value2"} "key2": "value2",
}
fake := testclient.NewSimpleFake(&api.Pod{ fake := testclient.NewSimpleFake(&api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
@ -613,42 +614,48 @@ func TestWriteWithUnixPathBadPath(t *testing.T) {
pluginMgr := volume.VolumePluginMgr{} pluginMgr := volume.VolumePluginMgr{}
pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake))
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
if err != nil {
t.Errorf("Can't find the plugin by name")
}
volumeSpec := &api.Volume{ volumeSpec := &api.Volume{
Name: testVolumeName, Name: testVolumeName,
VolumeSource: api.VolumeSource{ VolumeSource: api.VolumeSource{
DownwardAPI: &api.DownwardAPIVolumeSource{ DownwardAPI: &api.DownwardAPIVolumeSource{
Items: []api.DownwardAPIVolumeFile{ Items: []api.DownwardAPIVolumeFile{
{Path: "this//labels", FieldRef: api.ObjectFieldSelector{ {
FieldPath: "metadata.labels"}}, Path: "this//labels",
}}}, FieldRef: api.ObjectFieldSelector{
} FieldPath: "metadata.labels",
if err != nil { },
t.Errorf("Can't find the plugin by name") },
},
},
},
} }
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: testPodUID, Labels: labels}} pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: testPodUID, Labels: labels}}
builder, err := plugin.NewBuilder(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{}) builder, err := plugin.NewBuilder(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil { if err != nil {
t.Errorf("Failed to make a new Builder: %v", err) t.Fatalf("Failed to make a new Builder: %v", err)
} } else if builder == nil {
if builder == nil { t.Fatalf("Got a nil Builder")
t.Errorf("Got a nil Builder")
} }
volumePath := builder.GetPath() volumePath := builder.GetPath()
defer CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
err = builder.SetUp() err = builder.SetUp()
if err != nil { if err != nil {
t.Errorf("Failed to setup volume: %v", err) t.Fatalf("Failed to setup volume: %v", err)
} }
var data []byte data, err := ioutil.ReadFile(path.Join(volumePath, "this/labels"))
data, err = ioutil.ReadFile(path.Join(volumePath, "this/labels"))
if err != nil { if err != nil {
t.Errorf(err.Error()) t.Fatalf(err.Error())
} }
if sortLines(string(data)) != sortLines(formatMap(labels)) { if sortLines(string(data)) != sortLines(formatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, formatMap(labels)) t.Errorf("Found `%s` expected %s", data, formatMap(labels))
} }
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
} }