Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/apis/agones/v1/gameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func (gss *GameServerSpec) Validate(apiHooks APIHooks, devAddress string, fldPat

// make sure the container value points to a valid container
if !gss.HasContainer(gss.Container, false) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("container"), gss.Container, "Could not find a container named " + gss.Container))
allErrs = append(allErrs, field.Invalid(fldPath.Child("container"), gss.Container, "Could not find a container named "+gss.Container))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My updated linter was failing on this one, so this got blended in.

}

// no host port when using dynamic PortPolicy
Expand Down
73 changes: 49 additions & 24 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ func (f *Framework) SendGameServerUDP(t *testing.T, gs *agonesv1.GameServer, msg

// SendGameServerUDPToPort sends a message to a gameserver at the named port and returns its reply
// returns error if no Ports were allocated or a port of the specified name doesn't exist
// nolint:dupl
func (f *Framework) SendGameServerUDPToPort(t *testing.T, gs *agonesv1.GameServer, portName string, msg string) (string, error) {
log := TestLogger(t)
if len(gs.Status.Ports) == 0 {
Expand Down Expand Up @@ -610,23 +611,26 @@ func (f *Framework) SendUDP(t *testing.T, address, msg string) (string, error) {
// SendGameServerTCP sends a message to a gameserver and returns its reply
// finds the first tcp port from the spec to send the message to,
// returns error if no Ports were allocated
func SendGameServerTCP(gs *agonesv1.GameServer, msg string) (string, error) {
// nolint:dupl
func (f *Framework) SendGameServerTCP(t *testing.T, gs *agonesv1.GameServer, msg string) (string, error) {
if len(gs.Status.Ports) == 0 {
return "", errors.New("Empty Ports array")
}

// use first tcp port
for _, p := range gs.Spec.Ports {
if p.Protocol == corev1.ProtocolTCP {
return SendGameServerTCPToPort(gs, p.Name, msg)
return f.SendGameServerTCPToPort(t, gs, p.Name, msg)
}
}
return "", errors.New("No TCP ports")
}

// SendGameServerTCPToPort sends a message to a gameserver at the named port and returns its reply
// returns error if no Ports were allocated or a port of the specified name doesn't exist
func SendGameServerTCPToPort(gs *agonesv1.GameServer, portName string, msg string) (string, error) {
// nolint:dupl
func (f *Framework) SendGameServerTCPToPort(t *testing.T, gs *agonesv1.GameServer, portName string, msg string) (string, error) {
log := TestLogger(t)
if len(gs.Status.Ports) == 0 {
return "", errors.New("Empty Ports array")
}
Expand All @@ -637,36 +641,57 @@ func SendGameServerTCPToPort(gs *agonesv1.GameServer, portName string, msg strin
}
}
address := fmt.Sprintf("%s:%d", gs.Status.Address, port.Port)
return SendTCP(address, msg)
}
reply, err := f.SendTCP(t, address, msg)

// SendTCP sends a message to an address, and returns its reply if
// it returns one in 30 seconds
func SendTCP(address, msg string) (string, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
return "", err
log.WithField("gs", gs.ObjectMeta.Name).WithField("status", fmt.Sprintf("%+v", gs.Status)).Info("Failed to send TCP packet to GameServer. Dumping Events!")
f.LogEvents(t, log, gs.ObjectMeta.Namespace, gs)
}

if err := conn.SetReadDeadline(time.Now().Add(30 * time.Second)); err != nil {
return "", err
}
return reply, err
}

defer func() {
if err := conn.Close(); err != nil {
logrus.Warn("Could not close TCP connection")
// SendTCP sends a message to an address, and returns its reply if
// it returns one in 30 seconds. Will retry 5 times, in case TCP packets drop.
func (f *Framework) SendTCP(t *testing.T, address, msg string) (string, error) {
log := TestLogger(t).WithField("address", address)
var response string
// sometimes we get I/O timeout, so let's do a retry
err := wait.PollUntilContextTimeout(context.Background(), 2*time.Second, time.Minute, true, func(_ context.Context) (bool, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
log.WithError(err).Info("could not dial address")
return false, nil
}
}()

// writes to the tcp connection
_, err = fmt.Fprintln(conn, msg)
if err != nil {
return "", err
}
defer func() {
if err := conn.Close(); err != nil {
log.Warn("Could not close TCP connection")
}
}()

if err := conn.SetReadDeadline(time.Now().Add(30 * time.Second)); err != nil {
log.WithError(err).Info("Could not set read deadline")
return false, nil
}

// writes to the tcp connection
_, err = fmt.Fprintln(conn, msg)
if err != nil {
log.WithError(err).Info("could not write message to address")
return false, nil
}

response, err = bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.WithError(err).Info("Could not read from address")
}

return err == nil, nil
})

response, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return "", err
return "", errors.Wrap(err, "timed out attempting to send TCP message to address")
}

return response, nil
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/gameserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func TestGameServerTcpProtocol(t *testing.T) {
readyGs, err := framework.CreateGameServerAndWaitUntilReady(t, framework.Namespace, gs)
require.NoError(t, err)

replyTCP, err := e2eframework.SendGameServerTCP(readyGs, "Hello World !")
replyTCP, err := framework.SendGameServerTCP(t, readyGs, "Hello World !")
require.NoError(t, err)
assert.Equal(t, "ACK TCP: Hello World !\n", replyTCP)
}
Expand Down Expand Up @@ -1088,7 +1088,7 @@ func TestGameServerTcpUdpProtocol(t *testing.T) {

logrus.WithField("name", readyGs.ObjectMeta.Name).Info("UDP ping passed, sending TCP ping")

replyTCP, err := e2eframework.SendGameServerTCPToPort(readyGs, tcpPort.Name, "Hello World !")
replyTCP, err := framework.SendGameServerTCPToPort(t, readyGs, tcpPort.Name, "Hello World !")
if err != nil {
t.Fatalf("Could not ping TCP GameServer: %v", err)
}
Expand Down Expand Up @@ -1137,7 +1137,7 @@ func TestGameServerStaticTcpUdpProtocol(t *testing.T) {

logrus.WithField("name", readyGs.ObjectMeta.Name).Info("UDP ping passed, sending TCP ping")

replyTCP, err := e2eframework.SendGameServerTCPToPort(readyGs, tcpPort.Name, "Hello World !")
replyTCP, err := framework.SendGameServerTCPToPort(t, readyGs, tcpPort.Name, "Hello World !")
if err != nil {
t.Fatalf("Could not ping TCP GameServer: %v", err)
}
Expand All @@ -1164,7 +1164,7 @@ func TestGameServerStaticTcpProtocol(t *testing.T) {

logrus.WithField("name", readyGs.ObjectMeta.Name).Info("sending TCP ping")

replyTCP, err := e2eframework.SendGameServerTCP(readyGs, "Hello World !")
replyTCP, err := framework.SendGameServerTCP(t, readyGs, "Hello World !")
require.NoError(t, err)
assert.Equal(t, "ACK TCP: Hello World !\n", replyTCP)

Expand Down
Loading