Reuse Ping function for DERP ping

This commit is contained in:
Juan Font 2023-04-23 11:41:23 +00:00
parent b465592c07
commit 96f9680afd
3 changed files with 8 additions and 47 deletions

View file

@ -25,7 +25,6 @@ type TailscaleClient interface {
WaitForLogout() error WaitForLogout() error
WaitForPeers(expected int) error WaitForPeers(expected int) error
Ping(hostnameOrIP string, opts ...tsic.PingOption) error Ping(hostnameOrIP string, opts ...tsic.PingOption) error
PingViaDERP(hostnameOrIP string, opts ...tsic.PingOption) error
Curl(url string, opts ...tsic.CurlOption) (string, error) Curl(url string, opts ...tsic.CurlOption) (string, error)
ID() string ID() string
} }

View file

@ -545,49 +545,6 @@ func (t *TailscaleInContainer) Ping(hostnameOrIP string, opts ...PingOption) err
command = append(command, hostnameOrIP) command = append(command, hostnameOrIP)
return t.pool.Retry(func() error {
result, _, err := t.Execute(command)
if err != nil {
log.Printf(
"failed to run ping command from %s to %s, err: %s",
t.Hostname(),
hostnameOrIP,
err,
)
return err
}
if !strings.Contains(result, "pong") && !strings.Contains(result, "is local") {
return backoff.Permanent(errTailscalePingFailed)
}
return nil
})
}
// PingViaDERP executes the Tailscale ping command and pings a hostname
// or IP via the DERP network (i.e., not a direct connection). It accepts a series of DERPPingOption.
// TODO(kradalby): Make multiping, go routine magic.
func (t *TailscaleInContainer) PingViaDERP(hostnameOrIP string, opts ...PingOption) error {
args := pingArgs{
timeout: time.Second,
count: defaultPingCount,
}
for _, opt := range opts {
opt(&args)
}
command := []string{
"tailscale", "ping",
fmt.Sprintf("--timeout=%s", args.timeout),
fmt.Sprintf("--c=%d", args.count),
"--until-direct=false",
}
command = append(command, hostnameOrIP)
return t.pool.Retry(func() error { return t.pool.Retry(func() error {
result, _, err := t.Execute( result, _, err := t.Execute(
command, command,
@ -614,8 +571,12 @@ func (t *TailscaleInContainer) PingViaDERP(hostnameOrIP string, opts ...PingOpti
return backoff.Permanent(errTailscalePingFailed) return backoff.Permanent(errTailscalePingFailed)
} }
if !strings.Contains(result, "via DERP") { if !args.direct {
return backoff.Permanent(errTailscalePingNotDERP) if strings.Contains(result, "via DERP") {
return nil
} else {
return backoff.Permanent(errTailscalePingNotDERP)
}
} }
return nil return nil

View file

@ -40,10 +40,11 @@ func pingDerpAllHelper(t *testing.T, clients []TailscaleClient, addrs []string)
continue continue
} }
err := client.PingViaDERP( err := client.Ping(
addr, addr,
tsic.WithPingTimeout(derpPingTimeout), tsic.WithPingTimeout(derpPingTimeout),
tsic.WithPingCount(derpPingCount), tsic.WithPingCount(derpPingCount),
tsic.WithPingUntilDirect(false),
) )
if err != nil { if err != nil {
t.Errorf("failed to ping %s from %s: %s", addr, client.Hostname(), err) t.Errorf("failed to ping %s from %s: %s", addr, client.Hostname(), err)