Skip to content

Commit acc50a5

Browse files
committed
Add udp test
Signed-off-by: Ulysses Souza <[email protected]>
1 parent ed182f4 commit acc50a5

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

compliance_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package main
22

33
import (
44
"fmt"
5+
"net"
56
"testing"
67
"time"
8+
9+
"gotest.tools/v3/assert"
710
)
811

912
const (
@@ -14,6 +17,8 @@ const (
1417

1518
volumefileEntrypoint = "http://" + localhost + ":8080/volumefile"
1619
volumeUrl = volumefileEntrypoint + "?filename="
20+
21+
udpEntrypoint = "http://" + localhost + ":8080/udp"
1722
)
1823

1924
func TestSimpleLifecycle(t *testing.T) {
@@ -106,6 +111,28 @@ func TestConfigFile(t *testing.T) {
106111
})
107112
}
108113

109-
func jsonResponse(content string) string {
110-
return fmt.Sprintf("{\"response\":\"%s\"}\n", content)
114+
func TestUdpPort(t *testing.T) {
115+
h := TestHelper{
116+
T: t,
117+
testDir: "udp_port",
118+
specRef: "Networks-top-level-element",
119+
}
120+
h.TestUpDown(func() {
121+
udpValue := "myUdpvalue"
122+
123+
ServerAddr, err := net.ResolveUDPAddr("udp", localhost+":10001")
124+
assert.NilError(h.T, err)
125+
LocalAddr, err := net.ResolveUDPAddr("udp", localhost+":0")
126+
assert.NilError(h.T, err)
127+
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
128+
assert.NilError(h.T, err)
129+
defer Conn.Close()
130+
buf := []byte(fmt.Sprintf("{\"request\":%q}", udpValue))
131+
_, err = Conn.Write(buf)
132+
assert.NilError(h.T, err)
133+
134+
actual := h.getHttpBody(udpEntrypoint)
135+
expected := jsonResponse(udpValue)
136+
h.Check(expected, actual)
137+
})
111138
}

server/main.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io/ioutil"
7+
"net"
68
"net/http"
79
"os"
810
"path/filepath"
@@ -12,7 +14,10 @@ import (
1214
"github.com/labstack/echo"
1315
)
1416

15-
const defaultPort = 8080
17+
const (
18+
defaultHttpPort = 8080
19+
defaultUdpPort = 10001
20+
)
1621

1722
func getMapResponse(response string) map[string]string {
1823
return map[string]string{
@@ -75,8 +80,37 @@ func fileHandler(c echo.Context) error {
7580
)
7681
}
7782

83+
var udpValue string
84+
85+
func udpServer() {
86+
fmt.Println("Running UDP server...")
87+
ServerAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", defaultUdpPort))
88+
ServerConn, err := net.ListenUDP("udp", ServerAddr)
89+
checkError(err, true)
90+
defer ServerConn.Close()
91+
92+
var objmap map[string]string
93+
buf := make([]byte, 1024)
94+
for {
95+
n, _, err := ServerConn.ReadFromUDP(buf)
96+
checkError(err, true)
97+
err = json.Unmarshal(buf[:n], &objmap)
98+
checkError(err, true)
99+
udpValue = objmap["request"]
100+
}
101+
}
102+
103+
func udpHandler(c echo.Context) error {
104+
return c.JSON(
105+
http.StatusOK,
106+
getMapResponse(udpValue),
107+
)
108+
}
109+
78110
func main() {
79-
port := defaultPort
111+
go udpServer()
112+
113+
port := defaultHttpPort
80114
httpPort := os.Getenv("HTTP_PORT")
81115
if httpPort != "" {
82116
port, _ = strconv.Atoi(httpPort)
@@ -90,5 +124,15 @@ func main() {
90124
}
91125
e.GET("/ping", pingHandler)
92126
e.GET("/volumefile", fileHandler)
127+
e.GET("/udp", udpHandler)
93128
e.Logger.Fatal(e.StartServer(s))
94129
}
130+
131+
func checkError(err error, exitOnError bool) {
132+
if err != nil {
133+
fmt.Println("Error: ", err)
134+
if exitOnError {
135+
os.Exit(0)
136+
}
137+
}
138+
}

tests/udp_port/compose.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3.7'
2+
3+
services:
4+
entry:
5+
image: test-server
6+
ports:
7+
- 8080:8080/tcp
8+
- target: 10001
9+
published: 10001
10+
protocol: udp
11+
mode: host

tests_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,7 @@ func (h TestHelper) getHttpBody(address string) string {
175175
assert.NilError(h.T, err)
176176
return string(body)
177177
}
178+
179+
func jsonResponse(content string) string {
180+
return fmt.Sprintf("{\"response\":\"%s\"}\n", content)
181+
}

0 commit comments

Comments
 (0)