diff --git a/pkg/apis/agones/v1/gameserver.go b/pkg/apis/agones/v1/gameserver.go index e8a00696fa..40fb7c9298 100644 --- a/pkg/apis/agones/v1/gameserver.go +++ b/pkg/apis/agones/v1/gameserver.go @@ -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)) } // no host port when using dynamic PortPolicy diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 218c04dbf4..a8ea56a267 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -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 { @@ -610,7 +611,8 @@ 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") } @@ -618,7 +620,7 @@ func SendGameServerTCP(gs *agonesv1.GameServer, msg string) (string, error) { // 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") @@ -626,7 +628,9 @@ func SendGameServerTCP(gs *agonesv1.GameServer, msg string) (string, error) { // 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") } @@ -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 diff --git a/test/e2e/gameserver_test.go b/test/e2e/gameserver_test.go index 8d1c271f1c..8a7a711e51 100644 --- a/test/e2e/gameserver_test.go +++ b/test/e2e/gameserver_test.go @@ -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) } @@ -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) } @@ -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) } @@ -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)