tailcfg: add IP and Types field to PingRequest

Signed-off-by: Simeng He <simeng@tailscale.com>
This commit is contained in:
Simeng He
2021-07-16 10:51:54 -04:00
committed by simenghe
parent d5e1abd0c4
commit e199e407d2
4 changed files with 126 additions and 1 deletions

View File

@@ -6,9 +6,13 @@ package controlclient
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"inet.af/netaddr"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tailcfg"
"tailscale.com/types/wgkey"
)
@@ -103,3 +107,56 @@ func TestNewHostinfo(t *testing.T) {
}
t.Logf("Got: %s", j)
}
func TestTsmpPing(t *testing.T) {
hi := NewHostinfo()
ni := tailcfg.NetInfo{LinkType: "wired"}
hi.NetInfo = &ni
key, err := wgkey.NewPrivate()
if err != nil {
t.Error(err)
}
opts := Options{
ServerURL: "https://example.com",
Hostinfo: hi,
GetMachinePrivateKey: func() (wgkey.Private, error) {
return key, nil
},
}
c, err := NewDirect(opts)
if err != nil {
t.Fatal(err)
}
pingRes := &ipnstate.PingResult{
IP: "123.456.7890",
Err: "",
NodeName: "testnode",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body := new(ipnstate.PingResult)
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
t.Fatal(err)
}
if pingRes.IP != body.IP {
t.Fatalf("PingResult did not have the correct IP : got %v, expected : %v", body.IP, pingRes.IP)
}
w.WriteHeader(200)
}))
defer ts.Close()
now := time.Now()
pr := &tailcfg.PingRequest{
URL: ts.URL,
}
err = postPingResult(now, t.Logf, c.httpc, pr, pingRes)
if err != nil {
t.Fatal(err)
}
}