k3s/pkg/util/io/io_test.go

57 lines
1.7 KiB
Go
Raw Normal View History

2015-09-03 03:14:26 +00:00
/*
Copyright 2015 The Kubernetes Authors.
2015-09-03 03:14:26 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io_test
2015-09-03 03:14:26 +00:00
import (
"fmt"
"os"
"testing"
2015-09-03 03:14:26 +00:00
"github.com/pborman/uuid"
apiequality "k8s.io/apimachinery/pkg/api/equality"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/runtime"
utiltesting "k8s.io/client-go/util/testing"
2015-09-03 03:14:26 +00:00
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/io"
2015-09-03 03:14:26 +00:00
"k8s.io/kubernetes/pkg/volume"
)
func TestSavePodToFile(t *testing.T) {
pod := volume.NewPersistentVolumeRecyclerPodTemplate()
// sets all default values on a pod for equality comparison after decoding from file
2017-01-12 18:17:43 +00:00
codec := api.Codecs.LegacyCodec(api.Registry.GroupOrDie(api.GroupName).GroupVersion)
2016-01-22 05:11:30 +00:00
encoded, err := runtime.Encode(codec, pod)
runtime.DecodeInto(codec, encoded, pod)
2015-09-03 03:14:26 +00:00
tmpDir := utiltesting.MkTmpdirOrDie("kube-io-test")
defer os.RemoveAll(tmpDir)
path := fmt.Sprintf("/%s/kube-io-test-%s", tmpDir, uuid.New())
2015-09-03 03:14:26 +00:00
if err := io.SavePodToFile(pod, path, 777); err != nil {
2015-09-03 03:14:26 +00:00
t.Fatalf("failed to save pod to file: %v", err)
}
podFromFile, err := io.LoadPodFromFile(path)
2015-09-03 03:14:26 +00:00
if err != nil {
t.Fatalf("failed to load pod from file: %v", err)
}
if !apiequality.Semantic.DeepEqual(pod, podFromFile) {
2015-09-03 03:14:26 +00:00
t.Errorf("\nexpected %#v\ngot %#v\n", pod, podFromFile)
}
}