vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/keyspace_dao_test.go (about)

     1  /*
     2  Copyright 2022 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 inst
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  	_ "modernc.org/sqlite"
    24  
    25  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    26  	"vitess.io/vitess/go/vt/topo"
    27  	"vitess.io/vitess/go/vt/topotools"
    28  	"vitess.io/vitess/go/vt/vtorc/db"
    29  )
    30  
    31  func TestSaveAndReadKeyspace(t *testing.T) {
    32  	orcDb, err := db.OpenVTOrc()
    33  	require.NoError(t, err)
    34  	defer func() {
    35  		_, err = orcDb.Exec("delete from vitess_keyspace")
    36  		require.NoError(t, err)
    37  	}()
    38  
    39  	tests := []struct {
    40  		name           string
    41  		keyspaceName   string
    42  		keyspace       *topodatapb.Keyspace
    43  		keyspaceWanted *topodatapb.Keyspace
    44  		err            string
    45  	}{
    46  		{
    47  			name:         "Success with keyspaceType and durability",
    48  			keyspaceName: "ks1",
    49  			keyspace: &topodatapb.Keyspace{
    50  				KeyspaceType:     topodatapb.KeyspaceType_NORMAL,
    51  				DurabilityPolicy: "semi_sync",
    52  			},
    53  			keyspaceWanted: nil,
    54  			err:            "",
    55  		}, {
    56  			name:         "Success with keyspaceType and no durability",
    57  			keyspaceName: "ks2",
    58  			keyspace: &topodatapb.Keyspace{
    59  				KeyspaceType: topodatapb.KeyspaceType_NORMAL,
    60  			},
    61  			keyspaceWanted: nil,
    62  			err:            "",
    63  		}, {
    64  			name:         "Success with snapshot keyspaceType",
    65  			keyspaceName: "ks3",
    66  			keyspace: &topodatapb.Keyspace{
    67  				KeyspaceType: topodatapb.KeyspaceType_SNAPSHOT,
    68  			},
    69  			keyspaceWanted: nil,
    70  			err:            "",
    71  		}, {
    72  			name:         "Success with fields that are not stored",
    73  			keyspaceName: "ks4",
    74  			keyspace: &topodatapb.Keyspace{
    75  				KeyspaceType:     topodatapb.KeyspaceType_NORMAL,
    76  				DurabilityPolicy: "none",
    77  				BaseKeyspace:     "baseKeyspace",
    78  			},
    79  			keyspaceWanted: &topodatapb.Keyspace{
    80  				KeyspaceType:     topodatapb.KeyspaceType_NORMAL,
    81  				DurabilityPolicy: "none",
    82  			},
    83  			err: "",
    84  		}, {
    85  			name:           "No keyspace found",
    86  			keyspaceName:   "ks5",
    87  			keyspace:       nil,
    88  			keyspaceWanted: nil,
    89  			err:            ErrKeyspaceNotFound.Error(),
    90  		},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			if tt.keyspaceWanted == nil {
    95  				tt.keyspaceWanted = tt.keyspace
    96  			}
    97  
    98  			if tt.keyspace != nil {
    99  				keyspaceInfo := &topo.KeyspaceInfo{
   100  					Keyspace: tt.keyspace,
   101  				}
   102  				keyspaceInfo.SetKeyspaceName(tt.keyspaceName)
   103  				err := SaveKeyspace(keyspaceInfo)
   104  				require.NoError(t, err)
   105  			}
   106  
   107  			readKeyspaceInfo, err := ReadKeyspace(tt.keyspaceName)
   108  			if tt.err != "" {
   109  				require.EqualError(t, err, tt.err)
   110  			} else {
   111  				require.NoError(t, err)
   112  				require.True(t, topotools.KeyspaceEquality(tt.keyspaceWanted, readKeyspaceInfo.Keyspace))
   113  				require.Equal(t, tt.keyspaceName, readKeyspaceInfo.KeyspaceName())
   114  			}
   115  		})
   116  	}
   117  }