From 4d3713f631a096fdff4bec6a25681af4ad53e50f Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 23 Nov 2022 15:40:47 -0800 Subject: [PATCH] envknob: add GOOS func Centralize the fake GOOS stuff, start to use it more. To be used more in the future. Change-Id: Iabacfbeaf5fca0b53bf4d5dbcdc0367f05a205f9 Signed-off-by: Brad Fitzpatrick --- cmd/tailscaled/tailscaled.go | 7 +------ envknob/envknob.go | 2 -- envknob/envknob_nottest.go | 17 +++++++++++++++++ envknob/envknob_testable.go | 24 ++++++++++++++++++++++++ ipn/ipnlocal/profiles.go | 3 ++- tstest/integration/integration_test.go | 2 +- 6 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 envknob/envknob_nottest.go create mode 100644 envknob/envknob_testable.go diff --git a/cmd/tailscaled/tailscaled.go b/cmd/tailscaled/tailscaled.go index 1279f2448..9fecaad13 100644 --- a/cmd/tailscaled/tailscaled.go +++ b/cmd/tailscaled/tailscaled.go @@ -274,12 +274,7 @@ func statePathOrDefault() string { } func ipnServerOpts() (o ipnserver.Options) { - // Allow changing the OS-specific IPN behavior for tests - // so we can e.g. test Windows-specific behaviors on Linux. - goos := envknob.String("TS_DEBUG_TAILSCALED_IPN_GOOS") - if goos == "" { - goos = runtime.GOOS - } + goos := envknob.GOOS() o.VarRoot = args.statedir diff --git a/envknob/envknob.go b/envknob/envknob.go index 969cf7eac..18665f7f5 100644 --- a/envknob/envknob.go +++ b/envknob/envknob.go @@ -277,11 +277,9 @@ func SSHPolicyFile() string { return String("TS_DEBUG_SSH_POLICY_FILE") } // SSHIgnoreTailnetPolicy is whether to ignore the Tailnet SSH policy for development. func SSHIgnoreTailnetPolicy() bool { return Bool("TS_DEBUG_SSH_IGNORE_TAILNET_POLICY") } - // TKASkipSignatureCheck is whether to skip node-key signature checking for development. func TKASkipSignatureCheck() bool { return Bool("TS_UNSAFE_SKIP_NKS_VERIFICATION") } - // NoLogsNoSupport reports whether the client's opted out of log uploads and // technical support. func NoLogsNoSupport() bool { diff --git a/envknob/envknob_nottest.go b/envknob/envknob_nottest.go new file mode 100644 index 000000000..b49d82e2c --- /dev/null +++ b/envknob/envknob_nottest.go @@ -0,0 +1,17 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build ts_not_in_tests + +package envknob + +import "runtime" + +func GOOS() string { + // When the "ts_not_in_tests" build tag is used, we define this func to just + // return a simple constant so callers optimize just as if the knob were not + // present. We can then build production/optimized builds with the + // "ts_not_in_tests" build tag. + return runtime.GOOS +} diff --git a/envknob/envknob_testable.go b/envknob/envknob_testable.go new file mode 100644 index 000000000..2b126d240 --- /dev/null +++ b/envknob/envknob_testable.go @@ -0,0 +1,24 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build !ts_not_in_tests + +package envknob + +import "runtime" + +// GOOS reports the effective runtime.GOOS to run as. +// +// In practice this returns just runtime.GOOS, unless overridden by +// test TS_DEBUG_FAKE_GOOS. +// +// This allows changing OS-specific stuff like the IPN server behavior +// for tests so we can e.g. test Windows-specific behaviors on Linux. +// This isn't universally used. +func GOOS() string { + if v := String("TS_DEBUG_FAKE_GOOS"); v != "" { + return v + } + return runtime.GOOS +} diff --git a/ipn/ipnlocal/profiles.go b/ipn/ipnlocal/profiles.go index f67526788..e8107e16b 100644 --- a/ipn/ipnlocal/profiles.go +++ b/ipn/ipnlocal/profiles.go @@ -13,6 +13,7 @@ import ( "time" "golang.org/x/exp/slices" + "tailscale.com/envknob" "tailscale.com/ipn" "tailscale.com/tailcfg" "tailscale.com/types/logger" @@ -423,7 +424,7 @@ func ReadStartupPrefsForTest(logf logger.Logf, store ipn.StateStore) (ipn.PrefsV // It also loads the list of known profiles from the StateStore. // If a state key is provided, it will be used to load the current profile. func newProfileManager(store ipn.StateStore, logf logger.Logf, stateKey ipn.StateKey) (*profileManager, error) { - return newProfileManagerWithGOOS(store, logf, stateKey, runtime.GOOS) + return newProfileManagerWithGOOS(store, logf, stateKey, envknob.GOOS()) } func readAutoStartKey(store ipn.StateStore, goos string) (ipn.StateKey, error) { diff --git a/tstest/integration/integration_test.go b/tstest/integration/integration_test.go index e59a29441..7e616a22b 100644 --- a/tstest/integration/integration_test.go +++ b/tstest/integration/integration_test.go @@ -809,7 +809,7 @@ func (n *testNode) StartDaemonAsIPNGOOS(ipnGOOS string) *Daemon { "TS_LOG_TARGET="+n.env.LogCatcherServer.URL, "HTTP_PROXY="+n.env.TrafficTrapServer.URL, "HTTPS_PROXY="+n.env.TrafficTrapServer.URL, - "TS_DEBUG_TAILSCALED_IPN_GOOS="+ipnGOOS, + "TS_DEBUG_FAKE_GOOS="+ipnGOOS, "TS_LOGS_DIR="+t.TempDir(), "TS_NETCHECK_GENERATE_204_URL="+n.env.ControlServer.URL+"/generate_204", )