forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tablet.go
143 lines (120 loc) · 4.19 KB
/
tablet.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2017 Google Inc.
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 agreedto 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 test
import (
"testing"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
// checkTablet verifies the topo server API is correct for managing tablets.
func checkTablet(t *testing.T, ts *topo.Server) {
ctx := context.Background()
tablet := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: LocalCellName,
Uid: 1,
},
Hostname: "localhost",
MysqlHostname: "localhost",
PortMap: map[string]int32{
"vt": 3333,
},
Tags: map[string]string{"tag": "value"},
Keyspace: "test_keyspace",
Type: topodatapb.TabletType_MASTER,
KeyRange: newKeyRange("-10"),
}
topoproto.SetMysqlPort(tablet, 3334)
if err := ts.CreateTablet(ctx, tablet); err != nil {
t.Fatalf("CreateTablet: %v", err)
}
if err := ts.CreateTablet(ctx, tablet); !topo.IsErrType(err, topo.NodeExists) {
t.Fatalf("CreateTablet(again): %v", err)
}
if _, err := ts.GetTablet(ctx, &topodatapb.TabletAlias{
Cell: LocalCellName,
Uid: 666,
}); !topo.IsErrType(err, topo.NoNode) {
t.Fatalf("GetTablet(666): %v", err)
}
ti, err := ts.GetTablet(ctx, tablet.Alias)
if err != nil {
t.Fatalf("GetTablet %v: %v", tablet.Alias, err)
}
if !proto.Equal(ti.Tablet, tablet) {
t.Errorf("put and got tablets are not identical:\n%#v\n%#v", tablet, t)
}
if _, err := ts.GetTabletsByCell(ctx, "666"); !topo.IsErrType(err, topo.NoNode) {
t.Errorf("GetTabletsByCell(666): %v", err)
}
inCell, err := ts.GetTabletsByCell(ctx, LocalCellName)
if err != nil {
t.Fatalf("GetTabletsByCell: %v", err)
}
if len(inCell) != 1 || !proto.Equal(inCell[0], tablet.Alias) {
t.Errorf("GetTabletsByCell: want [%v], got %v", tablet.Alias, inCell)
}
ti.Tablet.Hostname = "remotehost"
if err := ts.UpdateTablet(ctx, ti); err != nil {
t.Errorf("UpdateTablet: %v", err)
}
ti, err = ts.GetTablet(ctx, tablet.Alias)
if err != nil {
t.Fatalf("GetTablet %v: %v", tablet.Alias, err)
}
if want := "remotehost"; ti.Tablet.Hostname != want {
t.Errorf("nt.Hostname: want %v, got %v", want, ti.Tablet.Hostname)
}
// test UpdateTabletFields works
updatedTablet, err := ts.UpdateTabletFields(ctx, tablet.Alias, func(t *topodatapb.Tablet) error {
t.Hostname = "anotherhost"
return nil
})
if err != nil {
t.Fatalf("UpdateTabletFields: %v", err)
}
if got, want := updatedTablet.Hostname, "anotherhost"; got != want {
t.Errorf("updatedTablet.Hostname = %q, want %q", got, want)
}
ti, err = ts.GetTablet(ctx, tablet.Alias)
if err != nil {
t.Fatalf("GetTablet %v: %v", tablet.Alias, err)
}
if got, want := ti.Tablet.Hostname, "anotherhost"; got != want {
t.Errorf("nt.Hostname = %q, want %q", got, want)
}
// test UpdateTabletFields that returns ErrNoUpdateNeeded works
if _, err := ts.UpdateTabletFields(ctx, tablet.Alias, func(t *topodatapb.Tablet) error {
return topo.NewError(topo.NoUpdateNeeded, tablet.Alias.String())
}); err != nil {
t.Errorf("UpdateTabletFields: %v", err)
}
if nti, nerr := ts.GetTablet(ctx, tablet.Alias); nti.Version() != ti.Version() {
t.Fatalf("GetTablet %v: %v %v", tablet.Alias, nti, nerr)
}
if want := "anotherhost"; ti.Tablet.Hostname != want {
t.Errorf("nt.Hostname: want %v, got %v", want, ti.Tablet.Hostname)
}
if err := ts.DeleteTablet(ctx, tablet.Alias); err != nil {
t.Errorf("DeleteTablet: %v", err)
}
if err := ts.DeleteTablet(ctx, tablet.Alias); !topo.IsErrType(err, topo.NoNode) {
t.Errorf("DeleteTablet(again): %v", err)
}
if _, err := ts.GetTablet(ctx, tablet.Alias); !topo.IsErrType(err, topo.NoNode) {
t.Errorf("GetTablet: expected error, tablet was deleted: %v", err)
}
}