Added Pinger interface, plumbed down to Direct, Trying some integration tests

This commit is contained in:
Simeng He
2021-05-17 11:17:31 -04:00
parent a14bf1fdc2
commit d52cf5e99b
5 changed files with 100 additions and 1 deletions

View File

@@ -1234,6 +1234,7 @@ func (c *Direct) CustomPing(mr *tailcfg.MapResponse) bool {
log.Printf("Custom Ping Triggered with %d number of peers\n", len(mr.Peers))
log.Println("Ping Request: ", mr.PingRequest)
ip := mr.PingRequest.TestIP
log.Println("TestIP : ", ip)
start := time.Now()
// Run the ping
c.pinger.Ping(ip, true, func(res *ipnstate.PingResult) {

View File

@@ -104,6 +104,7 @@ func TestNewHostinfo(t *testing.T) {
t.Logf("Got: %s", j)
}
// Currently not working properly
func TestPingFromMapResponse(t *testing.T) {
hi := NewHostinfo()
ni := tailcfg.NetInfo{LinkType: "wired"}
@@ -131,7 +132,7 @@ func TestPingFromMapResponse(t *testing.T) {
}
pingRequest := tailcfg.PingRequest{URL: "localhost:3040", Log: true, PayloadSize: 10}
mr := &tailcfg.MapResponse{Peers: peers, Domain: "DumbTest", PingRequest: &pingRequest}
if !CustomPing(mr) {
if !c.CustomPing(mr) {
t.Errorf("Custom ping failed!\n")
}
t.Log("Successful ping")

View File

@@ -652,3 +652,80 @@ func (w *authURLParserWriter) Write(p []byte) (n int, err error) {
}
return n, err
}
type panicOnUseTransport struct{}
func (panicOnUseTransport) RoundTrip(*http.Request) (*http.Response, error) {
panic("unexpected HTTP request")
}
func TestTwoNodePing(t *testing.T) {
// < --->
t.Parallel()
bins := buildTestBinaries(t)
env := newTestEnv(t, bins)
t.Log("Env :", env.ControlServer.URL)
res, err := http.Get(env.ControlServer.URL + "/ping")
t.Log("RESPONSE", res)
if err != nil {
t.Error(err)
}
defer env.Close()
// Create two nodes:
n1 := newTestNode(t, env)
d1 := n1.StartDaemon(t)
defer d1.Kill()
n2 := newTestNode(t, env)
d2 := n2.StartDaemon(t)
defer d2.Kill()
n1.AwaitListening(t)
n2.AwaitListening(t)
n1.MustUp()
n2.MustUp()
n1.AwaitRunning(t)
n2.AwaitRunning(t)
// Spinup a ping so we can run that between the two nodes
peers := []*tailcfg.Node{
{ID: 1},
}
// tsTun := tstun.Wrap(t.Logf, tstun.NewFake())
// err = tsTun.InjectOutbound([]byte("random"))
// if err != nil {
// t.Error(err)
// }
if err := tstest.WaitFor(2*time.Second, func() error {
st := n1.MustStatus(t)
st2 := n2.MustStatus(t)
pingRequest := &tailcfg.PingRequest{URL: env.ControlServer.URL, Log: true, PayloadSize: 10, TestIP: st.TailscaleIPs[0]}
mr := &tailcfg.MapResponse{Peers: peers, Domain: "DumbTest", PingRequest: pingRequest}
// t.Log(c.CustomPing(mr))
t.Log(mr)
t.Log("Peers of Node 1 : ", st.Peers())
t.Log("Length of map : ", len(st.Peer))
t.Log("Tailscale IPs of n1 : ", st.TailscaleIPs)
t.Log("Tailscale IPs of n1 : ", st2.TailscaleIPs)
if len(st.Peer) == 0 {
return errors.New("no peers")
}
if len(st.Peer) > 1 {
return fmt.Errorf("got %d peers; want 1", len(st.Peer))
}
peer := st.Peer[st.Peers()[0]]
if peer.ID == st.Self.ID {
return errors.New("peer is self")
}
return nil
}); err != nil {
t.Error(err)
}
d1.MustCleanShutdown(t)
d2.MustCleanShutdown(t)
}

View File

@@ -84,10 +84,12 @@ func (s *Server) logf(format string, a ...interface{}) {
}
func (s *Server) initMux() {
log.Println("Mux inited")
s.mux = http.NewServeMux()
s.mux.HandleFunc("/", s.serveUnhandled)
s.mux.HandleFunc("/key", s.serveKey)
s.mux.HandleFunc("/machine/", s.serveMachine)
s.mux.HandleFunc("/ping", s.receivePingInfo)
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -510,6 +512,7 @@ func (s *Server) serveMap(w http.ResponseWriter, r *http.Request, mkey tailcfg.M
//
// No updates to s are done here.
func (s *Server) MapResponse(req *tailcfg.MapRequest) (res *tailcfg.MapResponse, err error) {
log.Println(*req)
node := s.Node(req.NodeKey)
if node == nil {
// node key rotated away (once test server supports that)
@@ -537,6 +540,9 @@ func (s *Server) MapResponse(req *tailcfg.MapRequest) (res *tailcfg.MapResponse,
netaddr.MustParseIPPrefix(fmt.Sprintf("100.64.%d.%d/32", uint8(node.ID>>8), uint8(node.ID))),
}
res.Node.AllowedIPs = res.Node.Addresses
// Optional Ping Request, hardcode address for now, in the two nodes example we are accessing node4.
res.PingRequest = &tailcfg.PingRequest{TestIP: netaddr.IPv4(100, 64, 0, 2), Types: "tsmp"}
return res, nil
}
@@ -687,3 +693,15 @@ func breakSameNodeMapResponseStreams(req *tailcfg.MapRequest) bool {
}
return true
}
// This is where the PUT requests will go
func (s *Server) receivePingInfo(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
log.Println("Received NON PUT request, should panic if this happens after")
// panic("Only PUT requests are supported currently")
}
w.Header().Set("Content-Type", "text/plain")
log.Println("Ping Info Received", r.Body)
w.WriteHeader(200)
io.WriteString(w, "Ping Streamed Back")
}

View File

@@ -1128,12 +1128,14 @@ func (e *userspaceEngine) Ping(ip netaddr.IP, useTSMP bool, cb func(*ipnstate.Pi
res := &ipnstate.PingResult{IP: ip.String()}
peer, err := e.peerForIP(ip)
if err != nil {
log.Println(err)
e.logf("ping(%v): %v", ip, err)
res.Err = err.Error()
cb(res)
return
}
if peer == nil {
log.Println("No peer moment")
e.logf("ping(%v): no matching peer", ip)
res.Err = "no matching peer"
cb(res)