|
| 1 | +package api_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + kapi "k8s.io/kubernetes/pkg/api" |
| 8 | + "k8s.io/kubernetes/pkg/api/resource" |
| 9 | + |
| 10 | + "github.com/openshift/origin/pkg/quota/api" |
| 11 | + _ "github.com/openshift/origin/pkg/quota/api/install" |
| 12 | +) |
| 13 | + |
| 14 | +func TestDeepCopy(t *testing.T) { |
| 15 | + makeq := func() resource.Quantity { |
| 16 | + q := resource.Quantity{} |
| 17 | + q.Set(100) |
| 18 | + return q |
| 19 | + } |
| 20 | + make := func() *api.ClusterResourceQuota { |
| 21 | + crq := &api.ClusterResourceQuota{} |
| 22 | + crq.Status.Namespaces.Insert("ns1", kapi.ResourceQuotaStatus{Hard: kapi.ResourceList{"a": makeq()}, Used: kapi.ResourceList{"a": makeq()}}) |
| 23 | + crq.Status.Namespaces.Insert("ns2", kapi.ResourceQuotaStatus{Hard: kapi.ResourceList{"b": makeq()}, Used: kapi.ResourceList{"b": makeq()}}) |
| 24 | + return crq |
| 25 | + } |
| 26 | + |
| 27 | + check := make() |
| 28 | + |
| 29 | + original := make() |
| 30 | + if !reflect.DeepEqual(check, original) { |
| 31 | + t.Error("before mutation of copy, check and original should be identical but are not, likely failure in deepequal") |
| 32 | + } |
| 33 | + if !kapi.Semantic.DeepEqual(check, original) { |
| 34 | + t.Error("before mutation of copy, check and original should be identical but are not, likely failure in deepequal") |
| 35 | + } |
| 36 | + |
| 37 | + copiedObj, err := kapi.Scheme.Copy(original) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + copied := copiedObj.(*api.ClusterResourceQuota) |
| 42 | + if !reflect.DeepEqual(copied, original) { |
| 43 | + t.Error("before mutation of copy, copied and original should be identical but are not, likely failure in deepequal") |
| 44 | + } |
| 45 | + if !kapi.Semantic.DeepEqual(copied, original) { |
| 46 | + t.Error("before mutation of copy, copied and original should be identical but are not, likely failure in deepequal") |
| 47 | + } |
| 48 | + |
| 49 | + // Mutate the copy |
| 50 | + for e := copied.Status.Namespaces.OrderedKeys().Front(); e != nil; e = e.Next() { |
| 51 | + k := e.Value.(string) |
| 52 | + ns, _ := copied.Status.Namespaces.Get(k) |
| 53 | + for k2, v2 := range ns.Hard { |
| 54 | + v2.Set(v2.Value() + 2) |
| 55 | + ns.Hard[k2] = v2 |
| 56 | + } |
| 57 | + for k2, v2 := range ns.Used { |
| 58 | + v2.Set(v2.Value() + 1) |
| 59 | + ns.Used[k2] = v2 |
| 60 | + } |
| 61 | + copied.Status.Namespaces.Insert(k, ns) |
| 62 | + } |
| 63 | + |
| 64 | + if !reflect.DeepEqual(check, original) { |
| 65 | + t.Error("after mutation of copy, check and original should be identical but are not, likely failure in deep copy (ensure custom DeepCopy is being used)") |
| 66 | + } |
| 67 | + if !kapi.Semantic.DeepEqual(check, original) { |
| 68 | + t.Error("after mutation of copy, check and original should be identical but are not, likely failure in deep copy (ensure custom DeepCopy is being used)") |
| 69 | + } |
| 70 | + |
| 71 | + if reflect.DeepEqual(original, copied) { |
| 72 | + t.Error("after mutation of copy, original and copied should be different but are not, likely failure in deep copy (ensure custom DeepCopy is being used)") |
| 73 | + } |
| 74 | + if kapi.Semantic.DeepEqual(original, copied) { |
| 75 | + t.Error("after mutation of copy, original and copied should be different but are not, likely failure in deep copy (ensure custom DeepCopy is being used)") |
| 76 | + } |
| 77 | +} |
0 commit comments