Commit ff9296fc authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #35055 from ivan4th/make-downward-api-test-table-driven

Automatic merge from submit-queue (batch tested with PRs 42365, 42429, 41770, 42018, 35055) Make Downward API test table-driven
parents 4728a052 e80ae630
......@@ -35,7 +35,12 @@ import (
volumetest "k8s.io/kubernetes/pkg/volume/testing"
)
const downwardAPIDir = "..data"
const (
downwardAPIDir = "..data"
testPodUID = types.UID("test_pod_uid")
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) {
tempDir, err := utiltesting.MkTmpdir("downwardApi_volume_test.")
......@@ -60,431 +65,178 @@ func TestCanSupport(t *testing.T) {
}
}
func CleanEverything(plugin volume.VolumePlugin, testVolumeName, volumePath string, testPodUID types.UID, t *testing.T) {
unmounter, err := plugin.NewUnmounter(testVolumeName, testPodUID)
if err != nil {
t.Errorf("Failed to make a new Unmounter: %v", err)
}
if unmounter == nil {
t.Errorf("Got a nil Unmounter")
}
if err := unmounter.TearDown(); err != nil {
t.Errorf("Expected success, got: %v", err)
}
if _, err := os.Stat(volumePath); err == nil {
t.Errorf("TearDown() failed, volume path still exists: %s", volumePath)
} else if !os.IsNotExist(err) {
t.Errorf("SetUp() failed: %v", err)
}
}
func TestLabels(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_labels"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
labels := map[string]string{
func TestDownwardAPI(t *testing.T) {
labels1 := map[string]string{
"key1": "value1",
"key2": "value2"}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Labels: labels,
},
})
pluginMgr := volume.VolumePluginMgr{}
rootDir, host := newTestHost(t, clientset)
defer os.RemoveAll(rootDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "labels", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.labels"}}}},
},
}
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Labels: labels}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
// downwardAPI volume should create its own empty wrapper path
podWrapperMetadataDir := fmt.Sprintf("%v/pods/%v/plugins/kubernetes.io~empty-dir/wrapped_%v", rootDir, testPodUID, testVolumeName)
if _, err := os.Stat(podWrapperMetadataDir); err != nil {
if os.IsNotExist(err) {
t.Errorf("SetUp() failed, empty-dir wrapper path was not created: %s", podWrapperMetadataDir)
} else {
t.Errorf("SetUp() failed: %v", err)
}
}
var data []byte
data, err = ioutil.ReadFile(path.Join(volumePath, "labels"))
if err != nil {
t.Errorf(err.Error())
"key2": "value2",
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
labels2 := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
func TestAnnotations(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_annotations"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
annotations := map[string]string{
"a1": "value1",
"a2": "value2"}
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "annotations", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.annotations"}}}},
"a2": "value2",
}
testCases := []struct {
name string
files map[string]string
modes map[string]int32
podLabels map[string]string
podAnnotations map[string]string
steps []testStep
}{
{
name: "test_labels",
files: map[string]string{"labels": "metadata.labels"},
podLabels: labels1,
steps: []testStep{
// for steps that involve files, stepName is also
// used as the name of the file to verify
verifyMapInFile{stepName{"labels"}, labels1},
},
},
}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Annotations: annotations,
{
name: "test_annotations",
files: map[string]string{"annotations": "metadata.annotations"},
podAnnotations: annotations,
steps: []testStep{
verifyMapInFile{stepName{"annotations"}, annotations},
},
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Annotations: annotations}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
var data []byte
data, err = ioutil.ReadFile(path.Join(volumePath, "annotations"))
if err != nil {
t.Errorf(err.Error())
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(annotations))
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
func TestName(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_name"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "name_file_name", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.name"}}}},
{
name: "test_name",
files: map[string]string{"name_file_name": "metadata.name"},
steps: []testStep{
verifyLinesInFile{stepName{"name_file_name"}, testName},
},
},
}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
{
name: "test_namespace",
files: map[string]string{"namespace_file_name": "metadata.namespace"},
steps: []testStep{
verifyLinesInFile{stepName{"namespace_file_name"}, testNamespace},
},
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Name: testName}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
var data []byte
data, err = ioutil.ReadFile(path.Join(volumePath, "name_file_name"))
if err != nil {
t.Errorf(err.Error())
}
if string(data) != testName {
t.Errorf("Found `%s` expected %s", string(data), testName)
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
func TestNamespace(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_namespace"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "namespace_file_name", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.namespace"}}}},
{
name: "test_write_twice_no_update",
files: map[string]string{"labels": "metadata.labels"},
podLabels: labels1,
steps: []testStep{
reSetUp{stepName{"resetup"}, false, nil},
verifyMapInFile{stepName{"labels"}, labels1},
},
},
}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
{
name: "test_write_twice_with_update",
files: map[string]string{"labels": "metadata.labels"},
podLabels: labels1,
steps: []testStep{
reSetUp{stepName{"resetup"}, true, labels2},
verifyMapInFile{stepName{"labels"}, labels2},
},
},
{
name: "test_write_with_unix_path",
files: map[string]string{
"these/are/my/labels": "metadata.labels",
"these/are/your/annotations": "metadata.annotations",
},
podLabels: labels1,
podAnnotations: annotations,
steps: []testStep{
verifyMapInFile{stepName{"these/are/my/labels"}, labels1},
verifyMapInFile{stepName{"these/are/your/annotations"}, annotations},
},
},
{
name: "test_write_with_two_consecutive_slashes_in_the_path",
files: map[string]string{"this//labels": "metadata.labels"},
podLabels: labels1,
steps: []testStep{
verifyMapInFile{stepName{"this/labels"}, labels1},
},
},
{
name: "test_default_mode",
files: map[string]string{"name_file_name": "metadata.name"},
steps: []testStep{
verifyMode{stepName{"name_file_name"}, 0644},
},
},
{
name: "test_item_mode",
files: map[string]string{"name_file_name": "metadata.name"},
modes: map[string]int32{"name_file_name": 0400},
steps: []testStep{
verifyMode{stepName{"name_file_name"}, 0400},
},
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Namespace: testNamespace}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
for _, testCase := range testCases {
test := newDownwardAPITest(t, testCase.name, testCase.files, testCase.podLabels, testCase.podAnnotations, testCase.modes)
for _, step := range testCase.steps {
test.t.Logf("Test case: %q Step: %q", testCase.name, step.getName())
step.run(test)
}
test.tearDown()
}
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
type downwardAPITest struct {
t *testing.T
name string
plugin volume.VolumePlugin
pod *v1.Pod
mounter volume.Mounter
volumePath string
rootDir string
}
var data []byte
data, err = ioutil.ReadFile(path.Join(volumePath, "namespace_file_name"))
if err != nil {
t.Errorf(err.Error())
func newDownwardAPITest(t *testing.T, name string, volumeFiles, podLabels, podAnnotations map[string]string, modes map[string]int32) *downwardAPITest {
defaultMode := int32(0644)
var files []v1.DownwardAPIVolumeFile
for path, fieldPath := range volumeFiles {
file := v1.DownwardAPIVolumeFile{
Path: path,
FieldRef: &v1.ObjectFieldSelector{
FieldPath: fieldPath,
},
}
if mode, found := modes[path]; found {
file.Mode = &mode
}
files = append(files, file)
}
if string(data) != testNamespace {
t.Errorf("Found `%s` expected %s", string(data), testNamespace)
podMeta := metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Labels: podLabels,
Annotations: podAnnotations,
}
clientset := fake.NewSimpleClientset(&v1.Pod{ObjectMeta: podMeta})
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
func TestWriteTwiceNoUpdate(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_write_twice_no_update"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
labels := map[string]string{
"key1": "value1",
"key2": "value2"}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Labels: labels,
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
rootDir, host := newTestHost(t, clientset)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "labels", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.labels"}}}},
},
}
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Labels: labels}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
// get the link of the link
var currentTarget string
if currentTarget, err = os.Readlink(path.Join(volumePath, downwardAPIDir)); err != nil {
t.Errorf(".data should be a link... %s\n", err.Error())
}
err = mounter.SetUp(nil) // now re-run Setup
if err != nil {
t.Errorf("Failed to re-setup volume: %v", err)
}
// get the link of the link
var currentTarget2 string
if currentTarget2, err = os.Readlink(path.Join(volumePath, downwardAPIDir)); err != nil {
t.Errorf(".data should be a link... %s\n", err.Error())
}
if currentTarget2 != currentTarget {
t.Errorf("No update between the two Setup... Target link should be the same %s %s\n", currentTarget, currentTarget2)
}
var data []byte
data, err = ioutil.ReadFile(path.Join(volumePath, "labels"))
if err != nil {
t.Errorf(err.Error())
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
func TestWriteTwiceWithUpdate(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_write_twice_with_update"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
labels := map[string]string{
"key1": "value1",
"key2": "value2"}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Labels: labels,
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
Name: name,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "labels", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.labels"}}}},
Items: files,
},
},
}
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Labels: labels}}
podMeta.UID = testPodUID
pod := &v1.Pod{ObjectMeta: podMeta}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
......@@ -493,346 +245,149 @@ func TestWriteTwiceWithUpdate(t *testing.T) {
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
var currentTarget string
if currentTarget, err = os.Readlink(path.Join(volumePath, downwardAPIDir)); err != nil {
t.Errorf("labels file should be a link... %s\n", err.Error())
}
// downwardAPI volume should create its own empty wrapper path
podWrapperMetadataDir := fmt.Sprintf("%v/pods/%v/plugins/kubernetes.io~empty-dir/wrapped_%v", rootDir, testPodUID, name)
var data []byte
data, err = ioutil.ReadFile(path.Join(volumePath, "labels"))
if err != nil {
t.Errorf(err.Error())
if _, err := os.Stat(podWrapperMetadataDir); err != nil {
if os.IsNotExist(err) {
t.Errorf("SetUp() failed, empty-dir wrapper path was not created: %s", podWrapperMetadataDir)
} else {
t.Errorf("SetUp() failed: %v", err)
}
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
return &downwardAPITest{
t: t,
plugin: plugin,
pod: pod,
mounter: mounter,
volumePath: volumePath,
rootDir: rootDir,
}
}
newLabels := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3"}
// Now update the labels
pod.ObjectMeta.Labels = newLabels
err = mounter.SetUp(nil) // now re-run Setup
func (test *downwardAPITest) tearDown() {
unmounter, err := test.plugin.NewUnmounter(test.name, testPodUID)
if err != nil {
t.Errorf("Failed to re-setup volume: %v", err)
}
// get the link of the link
var currentTarget2 string
if currentTarget2, err = os.Readlink(path.Join(volumePath, downwardAPIDir)); err != nil {
t.Errorf(".current should be a link... %s\n", err.Error())
test.t.Errorf("Failed to make a new Unmounter: %v", err)
}
if currentTarget2 == currentTarget {
t.Errorf("Got and update between the two Setup... Target link should NOT be the same\n")
if unmounter == nil {
test.t.Errorf("Got a nil Unmounter")
}
data, err = ioutil.ReadFile(path.Join(volumePath, "labels"))
if err != nil {
t.Errorf(err.Error())
if err := unmounter.TearDown(); err != nil {
test.t.Errorf("Expected success, got: %v", err)
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(newLabels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(newLabels))
if _, err := os.Stat(test.volumePath); err == nil {
test.t.Errorf("TearDown() failed, volume path still exists: %s", test.volumePath)
} else if !os.IsNotExist(err) {
test.t.Errorf("SetUp() failed: %v", err)
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
os.RemoveAll(test.rootDir)
}
func TestWriteWithUnixPath(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_write_with_unix_path"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
labels := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3\n"}
annotations := map[string]string{
"a1": "value1",
"a2": "value2"}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Labels: labels,
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "this/is/mine/labels", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.labels"}},
{Path: "this/is/yours/annotations", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.annotations"}},
}}},
}
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Labels: labels, Annotations: annotations}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
// testStep represents a named step of downwardAPITest.
// For steps that deal with files, step name also serves
// as the name of the file that's used by the step.
type testStep interface {
getName() string
run(*downwardAPITest)
}
var data []byte
data, err = ioutil.ReadFile(path.Join(volumePath, "this/is/mine/labels"))
if err != nil {
t.Errorf(err.Error())
}
type stepName struct {
name string
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}
func (step stepName) getName() string { return step.name }
data, err = ioutil.ReadFile(path.Join(volumePath, "this/is/yours/annotations"))
func doVerifyLinesInFile(t *testing.T, volumePath, filename string, expected string) {
data, err := ioutil.ReadFile(path.Join(volumePath, filename))
if err != nil {
t.Errorf(err.Error())
return
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(annotations)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(annotations))
actualStr := sortLines(string(data))
expectedStr := sortLines(expected)
if actualStr != expectedStr {
t.Errorf("Found `%s`, expected `%s`", actualStr, expectedStr)
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
func TestWriteWithUnixPathBadPath(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_write_with_unix_path"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
labels := map[string]string{
"key1": "value1",
"key2": "value2",
}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Labels: labels,
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
if err != nil {
t.Errorf("Can't find the plugin by name")
}
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{
Path: "this//labels",
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.labels",
},
},
},
},
},
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Labels: labels}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Fatalf("Failed to make a new Mounter: %v", err)
} else if mounter == nil {
t.Fatalf("Got a nil Mounter")
}
volumePath := mounter.GetPath()
defer CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
err = mounter.SetUp(nil)
if err != nil {
t.Fatalf("Failed to setup volume: %v", err)
}
data, err := ioutil.ReadFile(path.Join(volumePath, "this/labels"))
if err != nil {
t.Fatalf(err.Error())
}
if sortLines(string(data)) != sortLines(fieldpath.FormatMap(labels)) {
t.Errorf("Found `%s` expected %s", data, fieldpath.FormatMap(labels))
}
type verifyLinesInFile struct {
stepName
expected string
}
func TestDefaultMode(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_name"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
defaultMode := int32(0644)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{Path: "name_file_name", FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.name"}}}},
},
}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
},
})
func (step verifyLinesInFile) run(test *downwardAPITest) {
doVerifyLinesInFile(test.t, test.volumePath, step.name, step.expected)
}
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Name: testName}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
}
type verifyMapInFile struct {
stepName
expected map[string]string
}
volumePath := mounter.GetPath()
func (step verifyMapInFile) run(test *downwardAPITest) {
doVerifyLinesInFile(test.t, test.volumePath, step.name, fieldpath.FormatMap(step.expected))
}
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
}
type verifyMode struct {
stepName
expectedMode int32
}
fileInfo, err := os.Stat(path.Join(volumePath, "name_file_name"))
func (step verifyMode) run(test *downwardAPITest) {
fileInfo, err := os.Stat(path.Join(test.volumePath, step.name))
if err != nil {
t.Errorf(err.Error())
test.t.Errorf(err.Error())
return
}
actualMode := fileInfo.Mode()
expectedMode := os.FileMode(defaultMode)
expectedMode := os.FileMode(step.expectedMode)
if actualMode != expectedMode {
t.Errorf("Found mode `%v` expected %v", actualMode, expectedMode)
test.t.Errorf("Found mode `%v` expected %v", actualMode, expectedMode)
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
func TestItemMode(t *testing.T) {
var (
testPodUID = types.UID("test_pod_uid")
testVolumeName = "test_name"
testNamespace = "test_metadata_namespace"
testName = "test_metadata_name"
)
type reSetUp struct {
stepName
linkShouldChange bool
newLabels map[string]string
}
defaultMode := int32(0644)
itemMode := int32(0400)
volumeSpec := &v1.Volume{
Name: testVolumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
DefaultMode: &defaultMode,
Items: []v1.DownwardAPIVolumeFile{
{
Path: "name_file_name", FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.name"},
Mode: &itemMode,
},
},
},
},
func (step reSetUp) run(test *downwardAPITest) {
if step.newLabels != nil {
test.pod.ObjectMeta.Labels = step.newLabels
}
clientset := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
},
})
pluginMgr := volume.VolumePluginMgr{}
tmpDir, host := newTestHost(t, clientset)
defer os.RemoveAll(tmpDir)
pluginMgr.InitPlugins(ProbeVolumePlugins(), host)
plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName)
if err != nil {
t.Errorf("Can't find the plugin by name")
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: testPodUID, Name: testName}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
currentTarget, err := os.Readlink(path.Join(test.volumePath, downwardAPIDir))
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
}
if mounter == nil {
t.Errorf("Got a nil Mounter")
test.t.Errorf("labels file should be a link... %s\n", err.Error())
}
volumePath := mounter.GetPath()
err = mounter.SetUp(nil)
if err != nil {
t.Errorf("Failed to setup volume: %v", err)
// now re-run Setup
if err = test.mounter.SetUp(nil); err != nil {
test.t.Errorf("Failed to re-setup volume: %v", err)
}
fileInfo, err := os.Stat(path.Join(volumePath, "name_file_name"))
// get the link of the link
currentTarget2, err := os.Readlink(path.Join(test.volumePath, downwardAPIDir))
if err != nil {
t.Errorf(err.Error())
test.t.Errorf(".current should be a link... %s\n", err.Error())
}
actualMode := fileInfo.Mode()
expectedMode := os.FileMode(itemMode)
if actualMode != expectedMode {
t.Errorf("Found mode `%v` expected %v", actualMode, expectedMode)
switch {
case step.linkShouldChange && currentTarget2 == currentTarget:
test.t.Errorf("Got and update between the two Setup... Target link should NOT be the same\n")
case !step.linkShouldChange && currentTarget2 != currentTarget:
test.t.Errorf("No update between the two Setup... Target link should be the same %s %s\n",
currentTarget, currentTarget2)
}
CleanEverything(plugin, testVolumeName, volumePath, testPodUID, t)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment