tstest: add method to Replace values for tests

We have many function pointers that we replace for the duration of test and
restore it on test completion, add method to do that.

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali
2023-03-03 16:18:59 -08:00
committed by Maisem Ali
parent 12100320d2
commit b9ebf7cf14
5 changed files with 48 additions and 12 deletions

View File

@@ -1075,16 +1075,12 @@ func TestUpdatePrefs(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.sshOverTailscale {
old := getSSHClientEnvVar
getSSHClientEnvVar = func() string { return "100.100.100.100 1 1" }
t.Cleanup(func() { getSSHClientEnvVar = old })
tstest.Replace(t, &getSSHClientEnvVar, func() string { return "100.100.100.100 1 1" })
} else if isSSHOverTailscale() {
// The test is being executed over a "real" tailscale SSH
// session, but sshOverTailscale is unset. Make the test appear
// as if it's not over tailscale SSH.
old := getSSHClientEnvVar
getSSHClientEnvVar = func() string { return "" }
t.Cleanup(func() { getSSHClientEnvVar = old })
tstest.Replace(t, &getSSHClientEnvVar, func() string { return "" })
}
if tt.env.goos == "" {
tt.env.goos = "linux"