Files
teleport/lib/tbot/cli/globals_test.go
T
Tim Buckley b717b5174f MWI: Fix certain --no- flags failing to override config file (#54402)
* MWI: Fix certain `--no-` flags failing to override config file

This fixes a few instances where `--no-` flags were unable to
override config file parameters as we didn't check if the flag was
actually set by the user. This now uses the value parsed by kingpin
directly if an associated `*SetByUser` flag is also set, making it
possible to use, e.g., `--no-oneshot` to disable oneshot mode if
also loading a `tbot.yaml` with the flag enabled.

Most `BoolVar()` uses in tbot don't apply - only those that can
plausibly be overridden by a config file. That means only `start`
type commands, and then only flags which produce configs that can
actually be overridden, which excludes most generated service
definitions.

* Fix tests

Adds a new test for inverted global flags, and fixes a broken test.

* Fix lints
2025-05-09 02:32:09 +00:00

96 lines
2.5 KiB
Go

/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cli
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/gravitational/teleport/lib/tbot/config"
)
// TestGlobals tests that GlobalArgs initialize and parse properly, and mutate
// BotConfig as expected.
func TestGlobalArgs(t *testing.T) {
app, _ := buildMinimalKingpinApp("start")
globals := NewGlobalArgs(app)
// Note: various flags here are already tested as part of sharedStartArgs.
_, err := app.Parse([]string{
"start",
"--debug",
"--config=foo.yaml",
"--fips",
"--trace",
"--trace-exporter=foo",
"--insecure",
"--log-format=json",
})
require.NoError(t, err)
require.True(t, globals.Debug)
require.True(t, globals.FIPS)
require.True(t, globals.Insecure)
require.True(t, globals.Trace)
require.Equal(t, "foo", globals.TraceExporter)
require.Equal(t, "foo.yaml", globals.ConfigPath)
// Clear the config path, otherwise LoadConfigWithMutators will try to load
// it.
globals.ConfigPath = ""
// Convert these args to a BotConfig and check it. Globals don't set many
// config flags, so not much to check here.
cfg, err := LoadConfigWithMutators(globals)
require.NoError(t, err)
require.True(t, cfg.Debug)
require.True(t, cfg.FIPS)
require.True(t, cfg.Insecure)
}
func TestGlobalInvertedFlags(t *testing.T) {
app, _ := buildMinimalKingpinApp("start")
globals := NewGlobalArgs(app)
_, err := app.Parse([]string{
"start",
"--no-debug",
"--no-fips",
"--no-insecure",
"--config=foo.yaml",
"--trace",
"--trace-exporter=foo",
"--log-format=json",
})
require.NoError(t, err)
cfg, err := TestConfigWithMutators(&config.BotConfig{
Debug: true,
FIPS: true,
Insecure: true,
}, globals)
require.NoError(t, err)
require.False(t, cfg.Debug)
require.False(t, cfg.FIPS)
require.False(t, cfg.Insecure)
}