-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
system_table_test.go
146 lines (133 loc) · 5.31 KB
/
system_table_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
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
144
145
146
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package tests_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/gogo/protobuf/proto"
"github.com/kr/pretty"
)
func TestInitialKeys(t *testing.T) {
defer leaktest.AfterTest(t)()
// keysPerDesc before 20.1 was 2. This changes to 3, because during the initial
// value construction, we populate both the deprecated system.namespace table
// and the new system.namespace table. This is done because during bootstrap,
// cluster version isn't known.
const keysPerDesc = 3
const nonDescKeys = 10
ms := sqlbase.MakeMetadataSchema(config.DefaultZoneConfigRef(), config.DefaultSystemZoneConfigRef())
kv, _ /* splits */ := ms.GetInitialValues()
expected := nonDescKeys + keysPerDesc*ms.SystemDescriptorCount()
if actual := len(kv); actual != expected {
t.Fatalf("Wrong number of initial sql kv pairs: %d, wanted %d", actual, expected)
}
// Add an additional table.
sqlbase.SystemAllowedPrivileges[keys.MaxReservedDescID] = privilege.List{privilege.ALL}
desc, err := sql.CreateTestTableDescriptor(
context.TODO(),
keys.SystemDatabaseID,
keys.MaxReservedDescID,
"CREATE TABLE system.x (val INTEGER PRIMARY KEY)",
sqlbase.NewDefaultPrivilegeDescriptor(),
)
if err != nil {
t.Fatal(err)
}
ms.AddDescriptor(keys.SystemDatabaseID, &desc)
kv, _ /* splits */ = ms.GetInitialValues()
expected = nonDescKeys + keysPerDesc*ms.SystemDescriptorCount()
if actual := len(kv); actual != expected {
t.Fatalf("Wrong number of initial sql kv pairs: %d, wanted %d", actual, expected)
}
// Verify that IDGenerator value is correct.
found := false
var idgenkv roachpb.KeyValue
for _, v := range kv {
if v.Key.Equal(keys.DescIDGenerator) {
idgenkv = v
found = true
break
}
}
if !found {
t.Fatal("Could not find descriptor ID generator in initial key set")
}
// Expect 2 non-reserved IDs to have been allocated.
i, err := idgenkv.Value.GetInt()
if err != nil {
t.Fatal(err)
}
if a, e := i, int64(keys.MinUserDescID); a != e {
t.Fatalf("Expected next descriptor ID to be %d, was %d", e, a)
}
}
// TestSystemTableLiterals compares the result of evaluating the `CREATE TABLE`
// statement strings that describe each system table with the TableDescriptor
// literals that are actually used at runtime. This ensures we can use the hand-
// written literals instead of having to evaluate the `CREATE TABLE` statements
// before initialization and with limited SQL machinery bootstraped, while still
// confident that the result is the same as if `CREATE TABLE` had been run.
//
// This test may also be useful when writing a new system table:
// adding the new schema along with a trivial, empty TableDescriptor literal
// will print the expected proto which can then be used to replace the empty
// one (though pruning the explicit zero values may make it more readable).
func TestSystemTableLiterals(t *testing.T) {
defer leaktest.AfterTest(t)()
type testcase struct {
id sqlbase.ID
schema string
pkg sqlbase.TableDescriptor
}
for _, test := range []testcase{
{keys.NamespaceTableID, sqlbase.NamespaceTableSchema, sqlbase.NamespaceTable},
{keys.DescriptorTableID, sqlbase.DescriptorTableSchema, sqlbase.DescriptorTable},
{keys.UsersTableID, sqlbase.UsersTableSchema, sqlbase.UsersTable},
{keys.ZonesTableID, sqlbase.ZonesTableSchema, sqlbase.ZonesTable},
{keys.LeaseTableID, sqlbase.LeaseTableSchema, sqlbase.LeaseTable},
{keys.EventLogTableID, sqlbase.EventLogTableSchema, sqlbase.EventLogTable},
{keys.RangeEventTableID, sqlbase.RangeEventTableSchema, sqlbase.RangeEventTable},
{keys.UITableID, sqlbase.UITableSchema, sqlbase.UITable},
{keys.JobsTableID, sqlbase.JobsTableSchema, sqlbase.JobsTable},
{keys.SettingsTableID, sqlbase.SettingsTableSchema, sqlbase.SettingsTable},
{keys.WebSessionsTableID, sqlbase.WebSessionsTableSchema, sqlbase.WebSessionsTable},
{keys.TableStatisticsTableID, sqlbase.TableStatisticsTableSchema, sqlbase.TableStatisticsTable},
{keys.LocationsTableID, sqlbase.LocationsTableSchema, sqlbase.LocationsTable},
{keys.RoleMembersTableID, sqlbase.RoleMembersTableSchema, sqlbase.RoleMembersTable},
{keys.CommentsTableID, sqlbase.CommentsTableSchema, sqlbase.CommentsTable},
} {
privs := *test.pkg.Privileges
gen, err := sql.CreateTestTableDescriptor(
context.TODO(),
keys.SystemDatabaseID,
test.id,
test.schema,
&privs,
)
if err != nil {
t.Fatalf("test: %+v, err: %v", test, err)
}
fmt.Println(gen)
if !proto.Equal(&test.pkg, &gen) {
diff := strings.Join(pretty.Diff(&test.pkg, &gen), "\n")
t.Errorf("%s table descriptor generated from CREATE TABLE statement does not match "+
"hardcoded table descriptor:\n%s", test.pkg.Name, diff)
}
}
}