-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathclone_test.go
83 lines (76 loc) · 2.38 KB
/
clone_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package protoutil_test
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts/ptpb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/gogo/protobuf/proto"
)
func TestCloneProto(t *testing.T) {
testCases := []struct {
pb protoutil.Message
shouldPanic bool
}{
// Uncloneable types (all contain UUID fields).
{&roachpb.StoreIdent{}, true},
{&enginepb.TxnMeta{}, true},
{&roachpb.Transaction{}, true},
{&kvpb.Error{}, true},
{&protoutil.RecursiveAndUncloneable{}, true},
// Cloneable types. This includes all types for which a
// protoutil.Clone call exists in the codebase as of 2016-11-21.
{&zonepb.ZoneConfig{}, false},
{&gossip.Info{}, false},
{&gossip.BootstrapInfo{}, false},
{&descpb.IndexDescriptor{}, false},
{&roachpb.SplitTrigger{}, false},
{&roachpb.Value{}, false},
{&kvserverpb.ReplicaState{}, false},
{&roachpb.RangeDescriptor{}, false},
{&catpb.PartitioningDescriptor{}, false},
{&ptpb.Record{}, false},
}
for _, tc := range testCases {
var clone protoutil.Message
var panicObj interface{}
func() {
defer func() {
panicObj = recover()
}()
clone = protoutil.Clone(tc.pb)
}()
if tc.shouldPanic {
if panicObj == nil {
t.Errorf("%T: expected panic but didn't get one", tc.pb)
} else {
if panicStr := fmt.Sprint(panicObj); !strings.Contains(panicStr, "attempt to clone") {
t.Errorf("%T: got unexpected panic %s", tc.pb, panicStr)
}
}
} else {
if panicObj != nil {
t.Errorf("%T: got unexpected panic %v", tc.pb, panicObj)
}
}
if panicObj == nil {
realClone := proto.Clone(tc.pb)
if !reflect.DeepEqual(clone, realClone) {
t.Errorf("%T: clone did not equal original. expected:\n%+v\ngot:\n%+v", tc.pb, realClone, clone)
}
}
}
}