util/testenv: add new package to hold InTest

Removes duplicated code.

Updates #cleanup

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali
2023-08-08 09:29:35 -07:00
committed by Maisem Ali
parent 3e255d76e1
commit 682fd72f7b
10 changed files with 53 additions and 25 deletions

View File

@@ -13,7 +13,6 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
@@ -49,13 +48,12 @@ import (
"tailscale.com/util/clientmetric"
"tailscale.com/util/must"
"tailscale.com/util/racebuild"
"tailscale.com/util/testenv"
"tailscale.com/util/winutil"
"tailscale.com/version"
"tailscale.com/version/distro"
)
func inTest() bool { return flag.Lookup("test.v") != nil }
var getLogTargetOnce struct {
sync.Once
v string // URL of logs server, or empty for default
@@ -576,7 +574,7 @@ func NewWithConfigPath(collection, dir, cmdName string, netMon *netmon.Monitor,
conf.IncludeProcSequence = true
}
if envknob.NoLogsNoSupport() || inTest() {
if envknob.NoLogsNoSupport() || testenv.InTest() {
logf("You have disabled logging. Tailscale will not be able to provide support.")
conf.HTTPC = &http.Client{Transport: noopPretendSuccessTransport{}}
} else if val := getLogTarget(); val != "" {
@@ -756,7 +754,7 @@ func dialContext(ctx context.Context, netw, addr string, netMon *netmon.Monitor,
// The logf parameter is optional; if non-nil, logs are printed using the
// provided function; if nil, log.Printf will be used instead.
func NewLogtailTransport(host string, netMon *netmon.Monitor, logf logger.Logf) http.RoundTripper {
if inTest() {
if testenv.InTest() {
return noopPretendSuccessTransport{}
}
// Start with a copy of http.DefaultTransport and tweak it a bit.