-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtestutils.go
134 lines (126 loc) · 4.2 KB
/
testutils.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
// Copyright 2023 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 tenantcapabilitiestestutils
import (
"strconv"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities/tenantcapabilitiespb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigbounds"
"github.com/cockroachdb/cockroach/pkg/sql/protoreflect"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/datadriven"
"github.com/stretchr/testify/require"
)
// ParseBatchRequests is a helper function to parse datadriven input that
// declares (empty) batch requests of supported types, for a particular tenant.
// The constructed batch request is returned. The cmds are of the following
// form:
//
// cmds=(split, scan, cput)
func ParseBatchRequests(t *testing.T, d *datadriven.TestData) (ba kvpb.BatchRequest) {
for _, cmd := range d.CmdArgs {
if cmd.Key == "cmds" {
for _, z := range cmd.Vals {
method, ok := kvpb.StringToMethodMap[z]
if !ok {
t.Fatalf("unsupported request type: %s", z)
}
request := kvpb.CreateRequest(method)
ba.Add(request)
}
}
}
return ba
}
// ParseTenantCapabilityUpsert parses all args which have a key that is a
// capability key and sets it on top of the default tenant capabilities.
func ParseTenantCapabilityUpsert(
t *testing.T, d *datadriven.TestData,
) (roachpb.TenantID, *tenantcapabilitiespb.TenantCapabilities, error) {
tID := GetTenantID(t, d)
caps := tenantcapabilitiespb.TenantCapabilities{}
for _, arg := range d.CmdArgs {
capability, ok := tenantcapabilities.FromName(arg.Key)
if !ok {
continue
}
switch c := capability.(type) {
case tenantcapabilities.BoolCapability:
b, err := strconv.ParseBool(arg.Vals[0])
if err != nil {
return roachpb.TenantID{}, nil, err
}
c.Value(&caps).Set(b)
case tenantcapabilities.SpanConfigBoundsCapability:
jsonD, err := json.ParseJSON(arg.Vals[0])
if err != nil {
return roachpb.TenantID{}, nil, err
}
var v tenantcapabilitiespb.SpanConfigBounds
if _, err := protoreflect.JSONBMarshalToMessage(jsonD, &v); err != nil {
return roachpb.TenantID{}, nil, err
}
c.Value(&caps).Set(spanconfigbounds.New(&v))
}
}
return tID, &caps, nil
}
func ParseTenantCapabilityDelete(t *testing.T, d *datadriven.TestData) *tenantcapabilities.Update {
tID := GetTenantID(t, d)
update := tenantcapabilities.Update{
Entry: tenantcapabilities.Entry{
TenantID: tID,
},
Deleted: true,
}
return &update
}
func GetTenantID(t *testing.T, d *datadriven.TestData) roachpb.TenantID {
var tenantID string
if d.HasArg("ten") {
d.ScanArgs(t, "ten", &tenantID)
}
if roachpb.IsSystemTenantName(roachpb.TenantName(tenantID)) {
return roachpb.SystemTenantID
}
tID, err := roachpb.TenantIDFromString(tenantID)
require.NoError(t, err)
return tID
}
// AlteredCapabilitiesString prints all altered capability values that no
// longer match DefaultCapabilities. This is different from
// Capabilities.String which only prints non-zero value fields.
func AlteredCapabilitiesString(capabilities *tenantcapabilitiespb.TenantCapabilities) string {
defaultCapabilities := tenantcapabilities.DefaultCapabilities()
var builder strings.Builder
builder.WriteByte('{')
space := ""
for _, capID := range tenantcapabilities.IDs {
value := tenantcapabilities.MustGetValueByID(capabilities, capID)
defaultValue := tenantcapabilities.MustGetValueByID(defaultCapabilities, capID)
if value.String() != defaultValue.String() {
builder.WriteString(space)
builder.WriteString(capID.String())
builder.WriteByte(':')
builder.WriteString(value.String())
space = " "
}
}
// All capabilities have default values.
if space == "" {
builder.WriteString("default")
}
builder.WriteByte('}')
return builder.String()
}