Skip to content

Commit

Permalink
Reuse Ping function for DERP ping
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont committed Apr 24, 2023
1 parent b465592 commit 96f9680
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 47 deletions.
1 change: 0 additions & 1 deletion integration/tailscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type TailscaleClient interface {
WaitForLogout() error
WaitForPeers(expected int) error
Ping(hostnameOrIP string, opts ...tsic.PingOption) error
PingViaDERP(hostnameOrIP string, opts ...tsic.PingOption) error
Curl(url string, opts ...tsic.CurlOption) (string, error)
ID() string
}
51 changes: 6 additions & 45 deletions integration/tsic/tsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,49 +545,6 @@ func (t *TailscaleInContainer) Ping(hostnameOrIP string, opts ...PingOption) err

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 {
result, _, err := t.Execute(
command,
Expand All @@ -614,8 +571,12 @@ func (t *TailscaleInContainer) PingViaDERP(hostnameOrIP string, opts ...PingOpti
return backoff.Permanent(errTailscalePingFailed)
}

if !strings.Contains(result, "via DERP") {
return backoff.Permanent(errTailscalePingNotDERP)
if !args.direct {
if strings.Contains(result, "via DERP") {
return nil
} else {
return backoff.Permanent(errTailscalePingNotDERP)
}
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ func pingDerpAllHelper(t *testing.T, clients []TailscaleClient, addrs []string)
continue
}

err := client.PingViaDERP(
err := client.Ping(
addr,
tsic.WithPingTimeout(derpPingTimeout),
tsic.WithPingCount(derpPingCount),
tsic.WithPingUntilDirect(false),
)
if err != nil {
t.Errorf("failed to ping %s from %s: %s", addr, client.Hostname(), err)
Expand Down

0 comments on commit 96f9680

Please sign in to comment.