// Watch returns a watch, which will be pre-populated with all changes // after resourceVersion. func (f *FakeControllerSource) Watch(resourceVersion string) (watch.Interface, error) { f.lock.RLock() defer f.lock.RUnlock() rc, err := strconv.Atoi(resourceVersion) if err != nil { return nil, err } rc++ // Don't re-send them a change they already have. if rc < len(f.changes) { changes := []watch.Event{} for _, c := range f.changes[rc:] { // Must make a copy to allow clients to modify the // object. Otherwise, if they make a change and write // it back, they will inadvertently change the our // canonical copy (in addition to racing with other // clients). objCopy, err := conversion.DeepCopy(c.Object) if err != nil { return nil, err } changes = append(changes, watch.Event{c.Type, objCopy.(runtime.Object)}) } return f.broadcaster.WatchWithPrefix(changes), nil } else if rc > len(f.changes) { return nil, errors.New("resource version in the future not supported by this fake") } return f.broadcaster.Watch(), nil }
// TestDeepCopyOfEmbeddedObject checks to make sure that EmbeddedObject's can be passed through DeepCopy with fidelity func TestDeepCopyOfEmbeddedObject(t *testing.T) { s := runtime.NewScheme() s.AddKnownTypes("", &EmbeddedTest{}) s.AddKnownTypeWithName("v1test", "EmbeddedTest", &EmbeddedTestExternal{}) original := &EmbeddedTest{ ID: "outer", Object: runtime.EmbeddedObject{ &EmbeddedTest{ ID: "inner", }, }, } originalData, err := s.EncodeToVersion(original, "v1test") if err != nil { t.Errorf("unexpected error: %v", err) } t.Logf("originalRole = %v\n", string(originalData)) copyOfOriginal, err := conversion.DeepCopy(original) if err != nil { t.Fatalf("unexpected error: %v", err) } copiedData, err := s.EncodeToVersion(copyOfOriginal.(runtime.Object), "v1test") if err != nil { t.Errorf("unexpected error: %v", err) } t.Logf("copyOfRole = %v\n", string(copiedData)) if !reflect.DeepEqual(original, copyOfOriginal) { t.Errorf("expected \n%v\n, got \n%v", string(originalData), string(copiedData)) } }
func deepCloneService(svc *api.Service) *api.Service { value, err := conversion.DeepCopy(svc) if err != nil { panic("couldn't copy service") } return value.(*api.Service) }
// List returns a list object, with its resource version set. func (f *FakeControllerSource) List() (runtime.Object, error) { f.lock.RLock() defer f.lock.RUnlock() list := make([]runtime.Object, 0, len(f.items)) for _, obj := range f.items { // Must make a copy to allow clients to modify the object. // Otherwise, if they make a change and write it back, they // will inadvertently change the our canonical copy (in // addition to racing with other clients). objCopy, err := conversion.DeepCopy(obj) if err != nil { return nil, err } list = append(list, objCopy.(runtime.Object)) } listObj := &api.List{} if err := runtime.SetList(listObj, list); err != nil { return nil, err } objMeta, err := api.ListMetaFor(listObj) if err != nil { return nil, err } resourceVersion := len(f.changes) objMeta.ResourceVersion = strconv.Itoa(resourceVersion) return listObj, nil }
func roundTrip(t *testing.T, codec runtime.Codec, originalItem runtime.Object) { // Make a copy of the originalItem to give to conversion functions // This lets us know if conversion messed with the input object deepCopy, err := conversion.DeepCopy(originalItem) if err != nil { t.Errorf("Could not copy object: %v", err) return } item := deepCopy.(runtime.Object) name := reflect.TypeOf(item).Elem().Name() data, err := codec.Encode(item) if err != nil { if conversion.IsNotRegisteredError(err) { t.Logf("%v is not registered", name) return } t.Errorf("%v: %v (%#v)", name, err, item) return } obj2, err := codec.Decode(data) if err != nil { t.Errorf("0: %v: %v\nCodec: %v\nData: %s\nSource: %#v", name, err, codec, string(data), originalItem) return } if reflect.TypeOf(item) != reflect.TypeOf(obj2) { obj2conv := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object) if err := api.Scheme.Convert(obj2, obj2conv); err != nil { t.Errorf("0X: no conversion from %v to %v: %v", reflect.TypeOf(item), reflect.TypeOf(obj2), err) return } obj2 = obj2conv } if !api.Semantic.DeepEqual(originalItem, obj2) { t.Errorf("1: %v: diff: %v\nCodec: %v\nData: %s\nSource: %s", name, util.ObjectDiff(originalItem, obj2), codec, string(data), util.ObjectGoPrintSideBySide(originalItem, obj2)) return } obj3 := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object) err = codec.DecodeInto(data, obj3) if err != nil { t.Errorf("2: %v: %v", name, err) return } if !api.Semantic.DeepEqual(originalItem, obj3) { t.Errorf("3: %v: diff: %v\nCodec: %v", name, util.ObjectDiff(originalItem, obj3), codec) return } }
func BenchmarkReplicationControllerCopy(b *testing.B) { data, err := ioutil.ReadFile("replication_controller_example.json") if err != nil { b.Fatalf("Unexpected error while reading file: %v", err) } var replicationController api.ReplicationController if err := api.Scheme.DecodeInto(data, &replicationController); err != nil { b.Fatalf("Unexpected error decoding node: %v", err) } var result *api.ReplicationController for i := 0; i < b.N; i++ { obj, err := conversion.DeepCopy(&replicationController) if err != nil { b.Fatalf("Unexpected error copying replication controller: %v", err) } result = obj.(*api.ReplicationController) } if !api.Semantic.DeepEqual(replicationController, *result) { b.Fatalf("Incorrect copy: expected %v, got %v", replicationController, *result) } }
func BenchmarkNodeCopy(b *testing.B) { data, err := ioutil.ReadFile("node_example.json") if err != nil { b.Fatalf("Unexpected error while reading file: %v", err) } var node api.Node if err := api.Scheme.DecodeInto(data, &node); err != nil { b.Fatalf("Unexpected error decoding node: %v", err) } var result *api.Node for i := 0; i < b.N; i++ { obj, err := conversion.DeepCopy(&node) if err != nil { b.Fatalf("Unexpected error copying node: %v", err) } result = obj.(*api.Node) } if !api.Semantic.DeepEqual(node, *result) { b.Fatalf("Incorrect copy: expected %v, got %v", node, *result) } }
func BenchmarkPodCopy(b *testing.B) { data, err := ioutil.ReadFile("pod_example.json") if err != nil { b.Fatalf("Unexpected error while reading file: %v", err) } var pod api.Pod if err := api.Scheme.DecodeInto(data, &pod); err != nil { b.Fatalf("Unexpected error decoding pod: %v", err) } var result *api.Pod for i := 0; i < b.N; i++ { obj, err := conversion.DeepCopy(&pod) if err != nil { b.Fatalf("Unexpected error copying pod: %v", err) } result = obj.(*api.Pod) } if !api.Semantic.DeepEqual(pod, *result) { b.Fatalf("Incorrect copy: expected %v, got %v", pod, *result) } }
func TestDeepCopyApiObjects(t *testing.T) { for i := 0; i < *fuzzIters; i++ { for _, version := range []string{"", testapi.Version()} { f := apitesting.FuzzerFor(t, version, rand.NewSource(rand.Int63())) for kind := range api.Scheme.KnownTypes(version) { item, err := api.Scheme.New(version, kind) if err != nil { t.Fatalf("Could not create a %s: %s", kind, err) } f.Fuzz(item) itemCopy, err := conversion.DeepCopy(item) if err != nil { t.Errorf("Could not deep copy a %s: %s", kind, err) continue } if !reflect.DeepEqual(item, itemCopy) { t.Errorf("expected %#v\ngot %#v", item, itemCopy) } } } } }