Skip to content

Commit 2610aeb

Browse files
committed
Add agentclient unit test
1 parent a43a9e3 commit 2610aeb

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

pkg/agent/agentclient/client_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package agentclient
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
10+
"google.golang.org/grpc"
11+
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
12+
)
13+
14+
func TestServeData_HTTP(t *testing.T) {
15+
client := NewAgentClient("")
16+
client.stream = newStream()
17+
stopCh := make(chan struct{})
18+
19+
// Start agnet
20+
go client.Serve(stopCh)
21+
defer close(stopCh)
22+
23+
// Start test http server as remote service
24+
expectedBody := "Hello, client"
25+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26+
fmt.Fprint(w, expectedBody)
27+
}))
28+
defer ts.Close()
29+
30+
// Stimulate sending KAS DIAL_REQ to AgentClient
31+
dialPacket := &agent.Packet{
32+
Type: agent.PacketType_DIAL_REQ,
33+
Payload: &agent.Packet_DialRequest{
34+
DialRequest: &agent.DialRequest{
35+
Protocol: "tcp",
36+
Address: ts.URL[len("http://"):],
37+
Random: 111,
38+
},
39+
},
40+
}
41+
client.stream.Send(dialPacket)
42+
43+
// Expect receiving DIAL_RSP packet from AgentClient
44+
pkg, _ := client.stream.Recv()
45+
if pkg == nil {
46+
t.Error("unexpected nil packet")
47+
}
48+
if pkg.Type != agent.PacketType_DIAL_RSP {
49+
t.Errorf("expect PacketType_DIAL_RSP; got %v", pkg.Type)
50+
}
51+
dialRsp := pkg.Payload.(*agent.Packet_DialResponse)
52+
connID := dialRsp.DialResponse.ConnectID
53+
if dialRsp.DialResponse.Random != 111 {
54+
t.Errorf("expect random=111; got %v", dialRsp.DialResponse.Random)
55+
}
56+
57+
// Send Data (HTTP Request) via AgentClient to the test http server
58+
dataPacket := &agent.Packet{
59+
Type: agent.PacketType_DATA,
60+
Payload: &agent.Packet_Data{
61+
Data: &agent.Data{
62+
ConnectID: connID,
63+
Data: []byte("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"), // HTTP request
64+
},
65+
},
66+
}
67+
client.stream.Send(dataPacket)
68+
69+
// Expect receiving http response via AgentClient
70+
pkg, _ = client.stream.Recv()
71+
if pkg == nil {
72+
t.Error("unexpected nil packet")
73+
}
74+
if pkg.Type != agent.PacketType_DATA {
75+
t.Errorf("expect PacketType_DIAL_RSP; got %v", pkg.Type)
76+
}
77+
data := pkg.Payload.(*agent.Packet_Data).Data.Data
78+
79+
// Verify response data
80+
//
81+
// HTTP/1.1 200 OK\r\n
82+
// Date: Tue, 07 May 2019 06:44:57 GMT\r\n
83+
// Content-Length: 14\r\n
84+
// Content-Type: text/plain; charset=utf-8\r\n
85+
// \r\n
86+
// Hello, client
87+
headAndBody := strings.Split(string(data), "\r\n")
88+
if body := headAndBody[len(headAndBody)-1]; body != expectedBody {
89+
t.Errorf("expect body %v; got %v", expectedBody, body)
90+
}
91+
}
92+
93+
// fakeStream implements AgentService_ConnectClient
94+
type fakeStream struct {
95+
grpc.ClientStream
96+
ch chan *agent.Packet
97+
}
98+
99+
func newStream() agent.AgentService_ConnectClient {
100+
s := &fakeStream{}
101+
s.ch = make(chan *agent.Packet)
102+
return s
103+
}
104+
105+
func (s *fakeStream) Send(packet *agent.Packet) error {
106+
s.ch <- packet
107+
return nil
108+
}
109+
110+
func (s *fakeStream) Recv() (*agent.Packet, error) {
111+
return <-s.ch, nil
112+
}

0 commit comments

Comments
 (0)