vitess.io/vitess@v0.16.2/go/vt/vtgate/buffer/flags_test.go (about)

     1  /*
     2  Copyright 2019 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 buffer
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/spf13/pflag"
    24  )
    25  
    26  func TestVerifyFlags(t *testing.T) {
    27  	parse := func(args []string) {
    28  		fs := pflag.NewFlagSet("vtgate_buffer_test", pflag.ContinueOnError)
    29  		registerFlags(fs)
    30  
    31  		if err := fs.Parse(args); err != nil {
    32  			t.Errorf("failed to parse args %v: %s", args, err)
    33  		}
    34  	}
    35  	resetFlagsForTesting := func() {
    36  		// Set all flags to their default value.
    37  		parse([]string{})
    38  	}
    39  
    40  	// Verify that the non-allowed (non-trivial) flag combinations are caught.
    41  	defer resetFlagsForTesting()
    42  
    43  	parse([]string{"--buffer_keyspace_shards", "ks1/0"})
    44  	if err := verifyFlags(); err == nil || !strings.Contains(err.Error(), "also requires that") {
    45  		t.Fatalf("List of shards requires --enable_buffer. err: %v", err)
    46  	}
    47  
    48  	resetFlagsForTesting()
    49  
    50  	parse([]string{
    51  		"--enable_buffer",
    52  		"--enable_buffer_dry_run",
    53  	})
    54  	if err := verifyFlags(); err == nil || !strings.Contains(err.Error(), "To avoid ambiguity") {
    55  		t.Fatalf("Dry-run and non-dry-run mode together require an explicit list of shards for actual buffering. err: %v", err)
    56  	}
    57  
    58  	resetFlagsForTesting()
    59  
    60  	parse([]string{
    61  		"--enable_buffer",
    62  		"--buffer_keyspace_shards", "ks1//0",
    63  	})
    64  	if err := verifyFlags(); err == nil || !strings.Contains(err.Error(), "invalid shard path") {
    65  		t.Fatalf("Invalid shard names are not allowed. err: %v", err)
    66  	}
    67  
    68  	resetFlagsForTesting()
    69  
    70  	parse([]string{
    71  		"--enable_buffer",
    72  		"--buffer_keyspace_shards", "ks1,ks1/0",
    73  	})
    74  	if err := verifyFlags(); err == nil || !strings.Contains(err.Error(), "has overlapping entries") {
    75  		t.Fatalf("Listed keyspaces and shards must not overlap. err: %v", err)
    76  	}
    77  }