From 651c4904229116691f407e6160e1bd40f9c52a34 Mon Sep 17 00:00:00 2001 From: Florent Poinsard Date: Wed, 30 Oct 2024 12:13:23 -0600 Subject: [PATCH] Add unit test Signed-off-by: Florent Poinsard --- go/cmd/vttablet/cli/cli_test.go | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 go/cmd/vttablet/cli/cli_test.go diff --git a/go/cmd/vttablet/cli/cli_test.go b/go/cmd/vttablet/cli/cli_test.go new file mode 100644 index 00000000000..1782aaff99d --- /dev/null +++ b/go/cmd/vttablet/cli/cli_test.go @@ -0,0 +1,55 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cli + +import ( + "context" + "os" + "testing" + + "github.com/stretchr/testify/require" + + "vitess.io/vitess/go/vt/topo" + "vitess.io/vitess/go/vt/topo/memorytopo" +) + +// TestRunFailsToStartTabletManager tests the code path in 'run' where we fail to start the TabletManager +// this is done by starting vttablet without a cnf file but requesting it to restore from backup. +// When starting, the TabletManager checks if it needs to restore, in tm.handleRestore but this step will +// fail if we do not provide a cnf file and if the flag --restore_from_backup is provided. +func TestRunFailsToStartTabletManager(t *testing.T) { + tabletPath = "cell-1" + ts, factory := memorytopo.NewServerAndFactory(context.Background(), "cell") + topo.RegisterFactory("test", factory) + + args := append([]string{}, os.Args...) + t.Cleanup(func() { + ts.Close() + tabletPath = "" + os.Args = append([]string{}, args...) + }) + + os.Args = []string{"vttablet", + "--topo_implementation", "test", "--topo_global_server_address", "localhost", "--topo_global_root", "cell", + "--db_host", "localhost", "--db_port", "3306", + "--tablet-path", "cell-1", "--init_keyspace", "ks", "--init_shard", "0", "--init_tablet_type", "replica", + "--restore_from_backup", + } + + err := Main.Execute() + require.ErrorContains(t, err, "you cannot enable --restore_from_backup without a my.cnf file") +}