vitess.io/vitess@v0.16.2/go/vt/topo/topotests/keyspace_test.go (about) 1 /* 2 Copyright 2023 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package topotests 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 26 "vitess.io/vitess/go/vt/topo/memorytopo" 27 "vitess.io/vitess/go/vt/vterrors" 28 29 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 30 "vitess.io/vitess/go/vt/proto/vtrpc" 31 ) 32 33 func TestCreateKeyspace(t *testing.T) { 34 ts := memorytopo.NewServer("zone1") 35 ctx := context.Background() 36 37 t.Run("valid name", func(t *testing.T) { 38 err := ts.CreateKeyspace(ctx, "ks", &topodatapb.Keyspace{}) 39 require.NoError(t, err) 40 }) 41 t.Run("invalid name", func(t *testing.T) { 42 err := ts.CreateKeyspace(ctx, "no/slashes/allowed", &topodatapb.Keyspace{}) 43 assert.Error(t, err) 44 assert.Equal(t, vtrpc.Code_INVALID_ARGUMENT, vterrors.Code(err), "%+v", err) 45 }) 46 } 47 48 func TestGetKeyspace(t *testing.T) { 49 ts := memorytopo.NewServer("zone1") 50 ctx := context.Background() 51 52 t.Run("valid name", func(t *testing.T) { 53 // First, create the keyspace. 54 err := ts.CreateKeyspace(ctx, "ks", &topodatapb.Keyspace{}) 55 require.NoError(t, err) 56 57 // Now, get it. 58 ks, err := ts.GetKeyspace(ctx, "ks") 59 require.NoError(t, err) 60 assert.NotNil(t, ks) 61 }) 62 63 t.Run("invalid name", func(t *testing.T) { 64 // We can't create the keyspace (because we can't create a keyspace 65 // with an invalid name), so we'll validate the error we get is *not* 66 // NOT_FOUND. 67 ks, err := ts.GetKeyspace(ctx, "no/slashes/allowed") 68 assert.Error(t, err) 69 assert.Equal(t, vtrpc.Code_INVALID_ARGUMENT, vterrors.Code(err), "%+v", err) 70 assert.Nil(t, ks) 71 }) 72 }