Refactor testing into docker file
This means a decent scale refactor but ends with our testing being much simpler Key changes: * single Dockerfile for all services * tests moved into docker up so don't need to be run locally * configurations moved to environment
This commit is contained in:
parent
99da6c5d67
commit
14424c16ca
13 changed files with 171 additions and 107 deletions
|
@ -1,11 +0,0 @@
|
|||
FROM golang:latest
|
||||
LABEL maintainer="Marc Di Luzio <marc.diluzio@gmail.com>"
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN go mod download
|
||||
|
||||
RUN go build -o rove-accountant -ldflags="-X 'github.com/mdiluz/rove/pkg/version.Version=$(git describe --always --long --dirty --tags)'" cmd/rove-accountant/main.go
|
||||
|
||||
CMD [ "./rove-accountant" ]
|
||||
|
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
@ -17,8 +16,8 @@ import (
|
|||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var address = flag.String("address", "", "port to server the accountant on")
|
||||
var data = flag.String("data", os.TempDir(), "path to persistent data")
|
||||
var address = os.Getenv("HOST_ADDRESS")
|
||||
var data = os.Getenv("DATA_PATH")
|
||||
|
||||
// accountantServer is the internal object to manage the requests
|
||||
type accountantServer struct {
|
||||
|
@ -83,14 +82,12 @@ func (a *accountantServer) GetValue(_ context.Context, in *accounts.DataKey) (*a
|
|||
|
||||
// main
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
// Verify the input
|
||||
if len(*address) == 0 {
|
||||
log.Fatal("No address set with --address")
|
||||
if len(address) == 0 {
|
||||
log.Fatal("No address set with $HOST_ADDRESS")
|
||||
}
|
||||
|
||||
persistence.SetPath(*data)
|
||||
persistence.SetPath(data)
|
||||
|
||||
// Initialise and load the accountant
|
||||
accountant := &internal.Accountant{}
|
||||
|
@ -99,7 +96,7 @@ func main() {
|
|||
}
|
||||
|
||||
// Set up the RPC server and register
|
||||
lis, err := net.Listen("tcp", *address)
|
||||
lis, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
@ -118,7 +115,7 @@ func main() {
|
|||
}()
|
||||
|
||||
// Serve the RPC server
|
||||
fmt.Printf("Serving accountant on %s\n", *address)
|
||||
fmt.Printf("Serving accountant on %s\n", address)
|
||||
if err := grpcServer.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to server gRPC: %s", err)
|
||||
}
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
FROM golang:latest
|
||||
LABEL maintainer="Marc Di Luzio <marc.diluzio@gmail.com>"
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN go mod download
|
||||
|
||||
# For /usr/share/dict/words
|
||||
RUN apt-get update && apt-get install -y wamerican
|
||||
|
||||
RUN go build -o rove-server -ldflags="-X 'github.com/mdiluz/rove/pkg/version.Version=$(git describe --always --long --dirty --tags)'" cmd/rove-server/main.go
|
||||
|
||||
CMD [ "./rove-server" ]
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
// +build integration
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
|
@ -11,30 +12,17 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// To be set by the main function
|
||||
var serv rove.Server
|
||||
const (
|
||||
defaultAddress = "localhost:80"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
s := NewServer()
|
||||
if err := s.Initialise(true); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
var serv = func() rove.Server {
|
||||
var address = os.Getenv("ROVE_SERVER_ADDRESS")
|
||||
if len(address) == 0 {
|
||||
address = defaultAddress
|
||||
}
|
||||
|
||||
serv = rove.Server(s.Addr())
|
||||
|
||||
go s.Run()
|
||||
|
||||
fmt.Printf("Test server hosted on %s", serv)
|
||||
code := m.Run()
|
||||
|
||||
if err := s.StopAndClose(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
os.Exit(code)
|
||||
}
|
||||
return rove.Server(address)
|
||||
}()
|
||||
|
||||
func TestServer_Status(t *testing.T) {
|
||||
status, err := serv.Status()
|
||||
|
|
|
@ -6,11 +6,13 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/version"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Handler describes a function that handles any incoming request and can respond
|
||||
|
@ -87,8 +89,10 @@ func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Wri
|
|||
response.Error = "Cannot register empty name"
|
||||
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
reg := accounts.RegisterInfo{Name: data.Name}
|
||||
if acc, err := s.accountant.Register(context.Background(), ®); err != nil {
|
||||
if acc, err := s.accountant.Register(ctx, ®, grpc.WaitForReady(true)); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if !acc.Success {
|
||||
|
@ -125,11 +129,13 @@ func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writ
|
|||
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
key := accounts.DataKey{Account: id, Key: "rover"}
|
||||
if len(id) == 0 {
|
||||
response.Error = "No account ID provided"
|
||||
|
||||
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
|
||||
} else if resp, err := s.accountant.GetValue(ctx, &key); err != nil {
|
||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||
|
||||
} else if !resp.Success {
|
||||
|
@ -155,12 +161,14 @@ func HandleRadar(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
|||
Success: false,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
id := vars["account"]
|
||||
key := accounts.DataKey{Account: id, Key: "rover"}
|
||||
if len(id) == 0 {
|
||||
response.Error = "No account ID provided"
|
||||
|
||||
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
|
||||
} else if resp, err := s.accountant.GetValue(ctx, &key); err != nil {
|
||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||
|
||||
} else if !resp.Success {
|
||||
|
@ -191,12 +199,14 @@ func HandleRover(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
|||
Success: false,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
id := vars["account"]
|
||||
key := accounts.DataKey{Account: id, Key: "rover"}
|
||||
if len(id) == 0 {
|
||||
response.Error = "No account ID provided"
|
||||
|
||||
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
|
||||
} else if resp, err := s.accountant.GetValue(ctx, &key); err != nil {
|
||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||
|
||||
} else if !resp.Success {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// +build integration
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
|
@ -9,6 +11,7 @@ import (
|
|||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/game"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
|
@ -39,7 +42,7 @@ func TestHandleStatus(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHandleRegister(t *testing.T) {
|
||||
data := rove.RegisterData{Name: "one"}
|
||||
data := rove.RegisterData{Name: uuid.New().String()}
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
|
@ -57,22 +60,24 @@ func TestHandleRegister(t *testing.T) {
|
|||
json.NewDecoder(response.Body).Decode(&status)
|
||||
|
||||
if status.Success != true {
|
||||
t.Errorf("got false for /register")
|
||||
t.Errorf("got false for /register: %s", status.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleCommand(t *testing.T) {
|
||||
name := uuid.New().String()
|
||||
s := NewServer()
|
||||
s.Initialise(false) // Leave the world empty with no obstacles
|
||||
reg := accounts.RegisterInfo{Name: "test"}
|
||||
reg := accounts.RegisterInfo{Name: name}
|
||||
acc, err := s.accountant.Register(context.Background(), ®)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, acc.Success)
|
||||
assert.NotNil(t, acc)
|
||||
assert.True(t, acc.Success, acc.Error)
|
||||
|
||||
assert.NoError(t, err, "Error registering account")
|
||||
|
||||
// Spawn the rover rover for the account
|
||||
_, inst, err := s.SpawnRoverForAccount("test")
|
||||
_, inst, err := s.SpawnRoverForAccount(name)
|
||||
assert.NoError(t, s.world.WarpRover(inst, vector.Vector{}))
|
||||
|
||||
attribs, err := s.world.RoverAttributes(inst)
|
||||
|
@ -91,7 +96,7 @@ func TestHandleCommand(t *testing.T) {
|
|||
b, err := json.Marshal(data)
|
||||
assert.NoError(t, err, "Error marshalling data")
|
||||
|
||||
request, _ := http.NewRequest(http.MethodPost, path.Join("/", "test", "/command"), bytes.NewReader(b))
|
||||
request, _ := http.NewRequest(http.MethodPost, path.Join("/", name, "/command"), bytes.NewReader(b))
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s.router.ServeHTTP(response, request)
|
||||
|
@ -118,16 +123,17 @@ func TestHandleCommand(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHandleRadar(t *testing.T) {
|
||||
name := uuid.New().String()
|
||||
s := NewServer()
|
||||
s.Initialise(false) // Spawn a clean world
|
||||
reg := accounts.RegisterInfo{Name: "test"}
|
||||
reg := accounts.RegisterInfo{Name: name}
|
||||
acc, err := s.accountant.Register(context.Background(), ®)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, acc.Success)
|
||||
assert.True(t, acc.Success, acc.Error)
|
||||
assert.NoError(t, err, "Error registering account")
|
||||
|
||||
// Spawn the rover rover for the account
|
||||
attrib, id, err := s.SpawnRoverForAccount("test")
|
||||
attrib, id, err := s.SpawnRoverForAccount(name)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Warp this rover to 0,0
|
||||
|
@ -143,7 +149,7 @@ func TestHandleRadar(t *testing.T) {
|
|||
assert.NoError(t, s.world.Atlas.SetTile(rockPos, game.TileRock))
|
||||
assert.NoError(t, s.world.Atlas.SetTile(emptyPos, game.TileEmpty))
|
||||
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", "test", "/radar"), nil)
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", name, "/radar"), nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s.router.ServeHTTP(response, request)
|
||||
|
@ -175,18 +181,19 @@ func TestHandleRadar(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHandleRover(t *testing.T) {
|
||||
name := uuid.New().String()
|
||||
s := NewServer()
|
||||
s.Initialise(true)
|
||||
reg := accounts.RegisterInfo{Name: "test"}
|
||||
reg := accounts.RegisterInfo{Name: name}
|
||||
acc, err := s.accountant.Register(context.Background(), ®)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, acc.Success)
|
||||
assert.True(t, acc.Success, acc.Error)
|
||||
|
||||
// Spawn one rover for the account
|
||||
attribs, _, err := s.SpawnRoverForAccount("test")
|
||||
attribs, _, err := s.SpawnRoverForAccount(name)
|
||||
assert.NoError(t, err)
|
||||
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", "test", "/rover"), nil)
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", name, "/rover"), nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s.router.ServeHTTP(response, request)
|
||||
|
|
|
@ -3,11 +3,11 @@ package internal
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -20,7 +20,7 @@ import (
|
|||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var accountantAddress = flag.String("accountant", "", "address of the accountant to connect to")
|
||||
var accountantAddress = os.Getenv("ACCOUNTANT_ADDRESS")
|
||||
|
||||
const (
|
||||
// PersistentData will allow the server to load and save it's state
|
||||
|
@ -116,12 +116,12 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
|
|||
s.sync.Add(1)
|
||||
|
||||
// Connect to the accountant
|
||||
fmt.Printf("Dialing accountant on %s\n", *accountantAddress)
|
||||
clientConn, err := grpc.Dial(*accountantAddress, grpc.WithInsecure())
|
||||
fmt.Printf("Dialing accountant on %s\n", accountantAddress)
|
||||
s.clientConn, err = grpc.Dial(accountantAddress, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.accountant = accounts.NewAccountantClient(clientConn)
|
||||
s.accountant = accounts.NewAccountantClient(s.clientConn)
|
||||
|
||||
// Spawn a border on the default world
|
||||
if err := s.world.SpawnWorld(fillWorld); err != nil {
|
||||
|
@ -139,6 +139,7 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
|
|||
}
|
||||
|
||||
// Start the listen
|
||||
fmt.Printf("Listening on %s\n", s.server.Addr)
|
||||
if s.listener, err = net.Listen("tcp", s.server.Addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -3,8 +3,10 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
|
@ -15,9 +17,15 @@ import (
|
|||
|
||||
var ver = flag.Bool("version", false, "Display version number")
|
||||
var quit = flag.Int("quit", 0, "Quit after n seconds, useful for testing")
|
||||
var address = flag.String("address", "", "The address to host on, automatically selected if empty")
|
||||
var data = flag.String("data", "", "Directory to store persistant data, no storage if empty")
|
||||
var tick = flag.Int("tick", 5, "Number of minutes per server tick (0 for no tick)")
|
||||
|
||||
// Address to host the server on, automatically selected if empty
|
||||
var address = os.Getenv("HOST_ADDRESS")
|
||||
|
||||
// Path for persistent storage
|
||||
var data = os.Getenv("DATA_PATH")
|
||||
|
||||
// The tick rate of the server in seconds
|
||||
var tick = os.Getenv("TICK_RATE")
|
||||
|
||||
func InnerMain() {
|
||||
flag.Parse()
|
||||
|
@ -31,13 +39,23 @@ func InnerMain() {
|
|||
fmt.Printf("Initialising version %s...\n", version.Version)
|
||||
|
||||
// Set the persistence path
|
||||
persistence.SetPath(*data)
|
||||
persistence.SetPath(data)
|
||||
|
||||
// Convert the tick rate
|
||||
tickRate := 5
|
||||
if len(tick) > 0 {
|
||||
var err error
|
||||
tickRate, err = strconv.Atoi(tick)
|
||||
if err != nil {
|
||||
log.Fatalf("TICK_RATE not set to valid int: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Create the server data
|
||||
s := internal.NewServer(
|
||||
internal.OptionAddress(*address),
|
||||
internal.OptionAddress(address),
|
||||
internal.OptionPersistentData(),
|
||||
internal.OptionTick(*tick))
|
||||
internal.OptionTick(tickRate))
|
||||
|
||||
// Initialise the server
|
||||
if err := s.Initialise(true); err != nil {
|
||||
|
@ -74,6 +92,5 @@ func InnerMain() {
|
|||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
InnerMain()
|
||||
}
|
||||
|
|
|
@ -12,9 +12,17 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var address = "localhost:80"
|
||||
const (
|
||||
defaultAddress = "localhost:80"
|
||||
)
|
||||
|
||||
func Test_InnerMain(t *testing.T) {
|
||||
|
||||
var address = os.Getenv("ROVE_SERVER_ADDRESS")
|
||||
if len(address) == 0 {
|
||||
address = defaultAddress
|
||||
}
|
||||
|
||||
// Set up the flags to act locally and use a temporary file
|
||||
flag.Set("data", path.Join(os.TempDir(), uuid.New().String()))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue