github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/kubeyaml_test.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tools 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/Mirantis/virtlet/tests/gm" 24 "github.com/davecgh/go-spew/spew" 25 v1 "k8s.io/api/core/v1" 26 ) 27 28 func TestLoadStoreYaml(t *testing.T) { 29 tstYaml := `--- 30 apiVersion: v1 31 kind: ServiceAccount 32 metadata: 33 name: virtlet 34 namespace: kube-system 35 --- 36 apiVersion: v1 37 kind: Pod 38 metadata: 39 name: test 40 spec: 41 containers: 42 - name: tst 43 image: docker.io/busybox` 44 objs, err := LoadYaml([]byte(tstYaml)) 45 if err != nil { 46 t.Fatalf("LoadYaml: %v", err) 47 } 48 if len(objs) != 2 { 49 t.Errorf("expected 2 objects but got %d", len(objs)) 50 } else { 51 if _, ok := objs[0].(*v1.ServiceAccount); !ok { 52 t.Errorf("expected *v1.ServiceAccount as the 1st object but got:\n%s", spew.Sdump(objs[0])) 53 } 54 if _, ok := objs[1].(*v1.Pod); !ok { 55 t.Errorf("expected *v1.Pod as the 2nd object but got:\n%s", spew.Sdump(objs[1])) 56 } 57 } 58 out, err := ToYaml(objs) 59 if err != nil { 60 t.Fatalf("ToYaml: %v", err) 61 } 62 reloaded, err := LoadYaml(out) 63 if err != nil { 64 t.Fatalf("reloading yaml: %v", err) 65 } 66 if !reflect.DeepEqual(objs, reloaded) { 67 t.Errorf("object changed after reloading. Was:\n%s\nReloaded:\n%s", spew.Sdump(objs), spew.Sdump(reloaded)) 68 } 69 gm.Verify(t, gm.NewYamlVerifier(out)) 70 }