Skip to content

Commit d9fa777

Browse files
Handle empty cell names
1 parent f9a007a commit d9fa777

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

pkg/vitess/api_client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ type Tablet struct {
2424

2525
// HasValidCell returns a bool reflecting if a tablet type is in a valid cell
2626
func (t Tablet) HasValidCell(validCells []string) bool {
27-
if len(validCells) == 0 {
27+
cells := make([]string, 0)
28+
for _, cell := range validCells {
29+
cell = strings.TrimSpace(cell)
30+
if cell != "" {
31+
cells = append(cells, cell)
32+
}
33+
}
34+
if len(cells) == 0 {
2835
return true
2936
}
30-
for _, cell := range validCells {
31-
if t.Alias.GetCell() == strings.TrimSpace(cell) {
37+
for _, cell := range cells {
38+
if cell == "" || t.Alias.GetCell() == cell {
3239
return true
3340
}
3441
}

pkg/vitess/api_client_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ func TestParseTablets(t *testing.T) {
8989
})
9090

9191
t.Run("with-cell", func(t *testing.T) {
92-
tablets, err := ParseTablets(config.VitessConfigurationSettings{
92+
settings := config.VitessConfigurationSettings{
9393
API: vitessApi.URL,
9494
Cells: []string{"cell2"},
9595
Keyspace: "test",
9696
Shard: "00",
97-
})
97+
}
98+
tablets, err := ParseTablets(settings)
9899
if err != nil {
99100
t.Fatalf("Expected no error, got %q", err)
100101
}
@@ -106,8 +107,15 @@ func TestParseTablets(t *testing.T) {
106107
if tablets[0].MysqlHostname != "replica1" {
107108
t.Fatalf("Expected hostname %q, got %q", "replica1", tablets[0].MysqlHostname)
108109
}
109-
if tablets[0].Alias.Cell != "cell2" {
110-
t.Fatalf("Expected vitess cell %s, got %s", "cell2", tablets[0].Alias.Cell)
110+
if tablets[0].Alias.GetCell() != "cell2" {
111+
t.Fatalf("Expected vitess cell %s, got %s", "cell2", tablets[0].Alias.GetCell())
112+
}
113+
114+
// empty cell names should cause no filtering
115+
settings.Cells = []string{"", ""}
116+
tablets, _ = ParseTablets(settings)
117+
if len(tablets) != 2 {
118+
t.Fatalf("Expected 2 tablet, got %d", len(tablets))
111119
}
112120
})
113121

0 commit comments

Comments
 (0)