mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-10 16:04:04 +00:00
114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
package supervisor
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAllocatedStartInjectsDynamicEndpointAndCallsReadyAfterProbe(t *testing.T) {
|
|
ready := false
|
|
readyCalled := false
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/gameserver":
|
|
_, _ = w.Write([]byte(`{"status":{"address":"203.0.113.9","ports":[{"name":"game","port":31001}]}}`))
|
|
case "/ready-probe":
|
|
if ready {
|
|
w.WriteHeader(http.StatusOK)
|
|
} else {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
}
|
|
case "/ready":
|
|
readyCalled = true
|
|
w.WriteHeader(http.StatusOK)
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
ready = true
|
|
path := filepath.Join(t.TempDir(), "env.txt")
|
|
argsPath := filepath.Join(t.TempDir(), "args.txt")
|
|
command := []string{"/bin/sh", "-c", "env > " + path + "; printf '%s' \"$@\" > " + argsPath, "shell"}
|
|
s, err := New(Config{Command: command, SDKBaseURL: server.URL, ReadyURL: server.URL + "/ready-probe", Transport: "steam_sdr", ReadyTimeout: time.Second, PollInterval: time.Millisecond})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Wait(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
contents, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(contents), "SDR_LISTEN_PORT=31001") || !strings.Contains(string(contents), "SDR_IP=203.0.113.9:31001") {
|
|
t.Fatalf("dynamic endpoint not injected: %s", contents)
|
|
}
|
|
args, err := os.ReadFile(argsPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(args), "--port=31001") {
|
|
t.Fatalf("dynamic port argument not injected: %s", args)
|
|
}
|
|
if !readyCalled {
|
|
t.Fatal("Agones Ready was called before process-ready probe")
|
|
}
|
|
}
|
|
|
|
func TestAllocatedENetDoesNotReceiveSDRVariables(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/gameserver" {
|
|
_, _ = w.Write([]byte(`{"status":{"address":"127.0.0.1","ports":[{"name":"game","port":31002}]}}`))
|
|
return
|
|
}
|
|
if r.URL.Path == "/ready-probe" {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
path := filepath.Join(t.TempDir(), "env.txt")
|
|
s, err := New(Config{Command: []string{"/bin/sh", "-c", "env > " + path}, SDKBaseURL: server.URL, ReadyURL: server.URL + "/ready-probe", Transport: "enet", ReadyTimeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Wait(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
contents, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(contents), "SDR_LISTEN_PORT=") || strings.Contains(string(contents), "SDR_IP=") {
|
|
t.Fatalf("ENet received SDR variables: %s", contents)
|
|
}
|
|
}
|
|
|
|
func TestDirectModeDoesNotRequireAgonesReadiness(t *testing.T) {
|
|
s, err := New(Config{Command: []string{"/bin/sh", "-c", "exit 0"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Start(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Wait(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|