Move LoadPodFromFile to volume utils
SavePodToFile is not used anywhere and LoadPodFromFile is used only by PV recycler.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user