Move LoadPodFromFile to volume utils

SavePodToFile is not used anywhere and LoadPodFromFile is used only by PV
recycler.
This commit is contained in:
Jan Safranek
2017-06-26 16:02:43 +02:00
parent ee8b663d22
commit c2dc5b5bf1
8 changed files with 118 additions and 146 deletions

View File

@@ -17,6 +17,8 @@ limitations under the License.
package util
import (
"io/ioutil"
"os"
"testing"
"k8s.io/api/core/v1"
@@ -140,3 +142,87 @@ func testVolumeWithNodeAffinity(t *testing.T, affinity *v1.NodeAffinity) *v1.Per
ObjectMeta: objMeta,
}
}
func TestLoadPodFromFile(t *testing.T) {
tests := []struct {
name string
content string
expectError bool
}{
{
"yaml",
`
apiVersion: v1
kind: Pod
metadata:
name: testpod
spec:
containers:
- image: gcr.io/google_containers/busybox
`,
false,
},
{
"json",
`
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "testpod"
},
"spec": {
"containers": [
{
"image": "gcr.io/google_containers/busybox"
}
]
}
}`,
false,
},
{
"invalid pod",
`
apiVersion: v1
kind: Pod
metadata:
name: testpod
spec:
- image: gcr.io/google_containers/busybox
`,
true,
},
}
for _, test := range tests {
tempFile, err := ioutil.TempFile("", "podfile")
defer os.Remove(tempFile.Name())
if err != nil {
t.Fatalf("cannot create temporary file: %v", err)
}
if _, err = tempFile.Write([]byte(test.content)); err != nil {
t.Fatalf("cannot save temporary file: %v", err)
}
if err = tempFile.Close(); err != nil {
t.Fatalf("cannot close temporary file: %v", err)
}
pod, err := LoadPodFromFile(tempFile.Name())
if test.expectError {
if err == nil {
t.Errorf("test %q expected error, got nil", test.name)
}
} else {
// no error expected
if err != nil {
t.Errorf("error loading pod %q: %v", test.name, err)
}
if pod == nil {
t.Errorf("test %q expected pod, got nil", test.name)
}
}
}
}