forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vschema.go
131 lines (119 loc) · 2.79 KB
/
vschema.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package test
import (
"testing"
"github.com/golang/protobuf/proto"
"github.com/youtube/vitess/go/vt/topo"
"golang.org/x/net/context"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vschemapb "github.com/youtube/vitess/go/vt/proto/vschema"
)
// checkVSchema runs the tests on the VSchema part of the API
func checkVSchema(t *testing.T, ts topo.Impl) {
ctx := context.Background()
if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil {
t.Fatalf("CreateKeyspace: %v", err)
}
shard := &topodatapb.Shard{
KeyRange: newKeyRange("b0-c0"),
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != nil {
t.Fatalf("CreateShard: %v", err)
}
got, err := ts.GetVSchema(ctx, "test_keyspace")
want := &vschemapb.Keyspace{}
if err != topo.ErrNoNode {
t.Error(err)
}
err = ts.SaveVSchema(ctx, "test_keyspace", &vschemapb.Keyspace{
Sharded: true,
Vindexes: map[string]*vschemapb.Vindex{
"stfu1": {
Type: "stfu",
Params: map[string]string{
"stfu1": "1",
},
Owner: "t1",
},
"stln1": {
Type: "stln",
Owner: "t1",
},
},
Tables: map[string]*vschemapb.Table{
"t1": {
ColumnVindexes: []*vschemapb.ColumnVindex{
{
Column: "c1",
Name: "stfu1",
}, {
Column: "c2",
Name: "stln1",
},
},
},
},
})
if err != nil {
t.Error(err)
}
got, err = ts.GetVSchema(ctx, "test_keyspace")
if err != nil {
t.Error(err)
}
want = &vschemapb.Keyspace{
Sharded: true,
Vindexes: map[string]*vschemapb.Vindex{
"stfu1": {
Type: "stfu",
Params: map[string]string{
"stfu1": "1",
},
Owner: "t1",
},
"stln1": {
Type: "stln",
Owner: "t1",
},
},
Tables: map[string]*vschemapb.Table{
"t1": {
ColumnVindexes: []*vschemapb.ColumnVindex{
{
Column: "c1",
Name: "stfu1",
}, {
Column: "c2",
Name: "stln1",
},
},
},
},
}
if !proto.Equal(got, want) {
t.Errorf("GetVSchema: %s, want %s", got, want)
}
err = ts.SaveVSchema(ctx, "test_keyspace", &vschemapb.Keyspace{})
if err != nil {
t.Error(err)
}
got, err = ts.GetVSchema(ctx, "test_keyspace")
if err != nil {
t.Error(err)
}
want = &vschemapb.Keyspace{}
if !proto.Equal(got, want) {
t.Errorf("GetVSchema: %s, want %s", got, want)
}
// Make sure the vschema is not returned as a shard name,
// because they share the same directory location.
shards, err := ts.GetShardNames(ctx, "test_keyspace")
if err != nil {
t.Errorf("GetShardNames: %v", err)
}
if len(shards) != 1 || shards[0] != "b0-c0" {
t.Errorf(`GetShardNames: want [ "b0-c0" ], got %v`, shards)
}
}