vitess.io/vitess@v0.16.2/go/vt/vtctl/reparentutil/reparenttestutil/util.go (about)

     1  /*
     2  Copyright 2021 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 reparenttestutil contains utility functions for writing tests
    18  package reparenttestutil
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/vt/topo"
    27  )
    28  
    29  // SetKeyspaceDurability sets the durability policy of a given keyspace. This function should only be used in test code
    30  // To make it explicit and prevent any future errors, we are taking a testing.T argument, even though we could have returned the error
    31  func SetKeyspaceDurability(ctx context.Context, t *testing.T, ts *topo.Server, keyspace, durability string) {
    32  	ctx, unlock, lockErr := ts.LockKeyspace(ctx, keyspace, "testutil.SetKeyspaceDurability")
    33  	if lockErr != nil {
    34  		return
    35  	}
    36  
    37  	var err error
    38  	defer unlock(&err)
    39  
    40  	ki, err := ts.GetKeyspace(ctx, keyspace)
    41  	require.NoError(t, err)
    42  
    43  	ki.DurabilityPolicy = durability
    44  
    45  	err = ts.UpdateKeyspace(ctx, ki)
    46  	require.NoError(t, err)
    47  }