Skip to content

Commit

Permalink
fix: missing match allocation configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldooli committed Dec 12, 2024
1 parent 7b1f7db commit 27b460b
Show file tree
Hide file tree
Showing 15 changed files with 912 additions and 1,386 deletions.
23 changes: 9 additions & 14 deletions e2e/framework/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func NewAPIClient(baseAddr string) *APIClient {

func (c *APIClient) Do(verb, path string, request proto.Message, response proto.Message) error {
var buf []byte
_, err := c.marshaler.MarshalAppend(buf, request)
marshalled, err := c.marshaler.MarshalAppend(buf, request)
if err != nil {
return fmt.Errorf("failed to encode request: %w", err)
}

req, err := http.NewRequest(verb, fmt.Sprintf("%s%s", c.baseAddr, path), bytes.NewBuffer(buf))
req, err := http.NewRequest(verb, fmt.Sprintf("%s%s", c.baseAddr, path), bytes.NewBuffer(marshalled))
if err != nil {
return fmt.Errorf("failed to build request: %w", err)
}
Expand All @@ -67,23 +67,18 @@ func (c *APIClient) Do(verb, path string, request proto.Message, response proto.

defer resp.Body.Close()

if resp.StatusCode >= 400 {
if body, err := io.ReadAll(resp.Body); err == nil {
return fmt.Errorf("failed with status %d, response body: %s", resp.StatusCode, string(body))
}

return fmt.Errorf("failed with status %d", resp.StatusCode)
var body []byte
if body, err = io.ReadAll(resp.Body); err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}

var out []byte
err = c.unmarshaler.Unmarshal(out, response)
if err != nil {
return fmt.Errorf("failed to decode response: %w", err)
if resp.StatusCode >= 400 {
return fmt.Errorf("failed with status: %d, body: %s", resp.StatusCode, string(body))
}

err = resp.Write(bytes.NewBuffer(out))
err = c.unmarshaler.Unmarshal(body, response)
if err != nil {
return fmt.Errorf("failed to write response: %w", err)
return fmt.Errorf("failed to decode response: %w", err)
}

return nil
Expand Down
32 changes: 11 additions & 21 deletions e2e/framework/maestro/components/management_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,33 @@
package components

import (
"context"
"fmt"
"net/http"
"strings"
"time"

tc "github.com/testcontainers/testcontainers-go"

"github.com/docker/compose/v2/pkg/api"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/topfreegames/maestro/e2e/framework/maestro/helpers"
)

type ManagementApiServer struct {
Address string
compose tc.DockerCompose
}

func ProvideManagementApi(maestroPath string) (*ManagementApiServer, error) {
func ProvideManagementApi(ctx context.Context, compose tc.ComposeStack) (*ManagementApiServer, error) {
address := "http://localhost:8080"
client := &http.Client{}

composeFilePaths := []string{fmt.Sprintf("%s/e2e/framework/maestro/docker-compose.yml", maestroPath)}
identifier := strings.ToLower("e2e-test")

compose := tc.NewLocalDockerCompose(composeFilePaths, identifier)
composeErr := compose.WithCommand([]string{"up", "-d", "--build", "management-api"}).Invoke()
services := compose.Services()
services = append(services, "management-api")

if composeErr.Error != nil {
return nil, fmt.Errorf("failed to start management API: %s", composeErr.Error)
err := compose.Up(ctx, tc.RunServices(services...), tc.Recreate(api.RecreateNever), tc.RecreateDependencies(api.RecreateNever), tc.Wait(true))
if err != nil {
return nil, fmt.Errorf("failed to start management-api: %w", err)
}

err := helpers.TimedRetry(func() error {
err = helpers.TimedRetry(func() error {
res, err := client.Get("http://localhost:8081/healthz")
if err != nil {
return err
Expand All @@ -69,12 +66,5 @@ func ProvideManagementApi(maestroPath string) (*ManagementApiServer, error) {
return nil, fmt.Errorf("unable to reach management API: %s", err)
}

return &ManagementApiServer{
Address: address,
compose: compose,
}, nil
}

func (ms *ManagementApiServer) Teardown() {
ms.compose.WithCommand([]string{"rm", "-s", "-v", "-f", "management-api"}).Invoke()
return &ManagementApiServer{Address: address}, nil
}
57 changes: 28 additions & 29 deletions e2e/framework/maestro/components/rooms_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,37 @@
package components

import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/topfreegames/maestro/e2e/framework/maestro/exec"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/docker/api/types/network"
docker "github.com/docker/docker/client"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/topfreegames/maestro/e2e/framework/maestro/helpers"

"strings"

tc "github.com/testcontainers/testcontainers-go"
)

type RoomsApiServer struct {
Address string
ContainerInternalAddress string
compose tc.DockerCompose
}

func ProvideRoomsApi(maestroPath string) (*RoomsApiServer, error) {
func ProvideRoomsApi(ctx context.Context, compose tc.ComposeStack) (*RoomsApiServer, error) {
address := "http://localhost:8070"
client := &http.Client{}

composeFilePaths := []string{fmt.Sprintf("%s/e2e/framework/maestro/docker-compose.yml", maestroPath)}
identifier := strings.ToLower("e2e-test")

compose := tc.NewLocalDockerCompose(composeFilePaths, identifier)
composeErr := compose.WithCommand([]string{"up", "-d", "--build", "rooms-api"}).Invoke()
services := compose.Services()
services = append(services, "rooms-api")

if composeErr.Error != nil {
return nil, fmt.Errorf("failed to start rooms API: %s", composeErr.Error)
err := compose.Up(ctx, tc.RunServices(services...), tc.Recreate(api.RecreateNever), tc.RecreateDependencies(api.RecreateNever), tc.Wait(true))
if err != nil {
return nil, fmt.Errorf("failed to start rooms-api: %w", err)
}

err := helpers.TimedRetry(func() error {
err = helpers.TimedRetry(func() error {
res, err := client.Get("http://localhost:8071/healthz")
if err != nil {
return err
Expand All @@ -72,28 +70,29 @@ func ProvideRoomsApi(maestroPath string) (*RoomsApiServer, error) {
return nil, fmt.Errorf("unable to reach rooms API: %s", err)
}

cmd, err := exec.ExecSysCmd(
maestroPath,
"docker",
"inspect", "-f", "'{{range .IPAM.Config}}{{.Gateway}}{{end}}'", "e2e-test_default",
)
container, err := compose.ServiceContainer(ctx, "rooms-api")
if err != nil {
return nil, fmt.Errorf("unable to get rooms api container: %s", err)
}

networks, err := container.Networks(ctx)
if err != nil {
return nil, fmt.Errorf("unable to run docker inspect command: %s", err)
return nil, fmt.Errorf("failed to get rooms-api container networks: %w", err)
}

output, err := cmd.ReadOutput()
cli, err := docker.NewClientWithOpts()
if err != nil {
return nil, fmt.Errorf("failed to create docker client: %w", err)
}

networkInspect, err := cli.NetworkInspect(ctx, networks[0], network.InspectOptions{Verbose: true, Scope: ""})
if err != nil {
return nil, fmt.Errorf("unable to get rooms api container internal address: %s", err)
return nil, fmt.Errorf("failed to inspect rooms-api network: %w", err)
}

roomsApiIP := strings.Trim(strings.TrimSuffix(string(output), "\n"), "'")
output := networkInspect.IPAM.Config[0].Gateway
roomsApiIP := strings.Trim(strings.TrimSuffix(output, "\n"), "'")
internalAddress := fmt.Sprintf("%s:8070", roomsApiIP)

return &RoomsApiServer{compose: compose, Address: address, ContainerInternalAddress: internalAddress}, nil
}

func (ms *RoomsApiServer) Teardown() {
ms.compose.WithCommand([]string{"rm", "-s", "-v", "-f", "rooms-api"}).Invoke()
return &RoomsApiServer{Address: address, ContainerInternalAddress: internalAddress}, nil
}
32 changes: 12 additions & 20 deletions e2e/framework/maestro/components/runtime_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,30 @@
package components

import (
"context"
"fmt"
"net/http"
"strings"
"time"

tc "github.com/testcontainers/testcontainers-go"

"github.com/docker/compose/v2/pkg/api"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/topfreegames/maestro/e2e/framework/maestro/helpers"
)

type RuntimeWatcherServer struct {
compose tc.DockerCompose
}
type RuntimeWatcherServer struct{}

func ProvideRuntimeWatcher(maestroPath string) (*RuntimeWatcherServer, error) {
func ProvideRuntimeWatcher(ctx context.Context, compose tc.ComposeStack) (*RuntimeWatcherServer, error) {
client := &http.Client{}

composeFilePaths := []string{fmt.Sprintf("%s/e2e/framework/maestro/docker-compose.yml", maestroPath)}
identifier := strings.ToLower("e2e-test")

compose := tc.NewLocalDockerCompose(composeFilePaths, identifier)
composeErr := compose.WithCommand([]string{"up", "-d", "--build", "runtime-watcher"}).Invoke()
services := compose.Services()
services = append(services, "runtime-watcher")

if composeErr.Error != nil {
return nil, fmt.Errorf("failed to start runtime watcher API: %s", composeErr.Error)
err := compose.Up(ctx, tc.RunServices(services...), tc.Recreate(api.RecreateNever), tc.RecreateDependencies(api.RecreateNever), tc.Wait(true))
if err != nil {
return nil, fmt.Errorf("failed to start runtime-watcher: %w", err)
}

err := helpers.TimedRetry(func() error {
err = helpers.TimedRetry(func() error {
res, err := client.Get("http://localhost:8061/healthz")
if err != nil {
return err
Expand All @@ -67,9 +63,5 @@ func ProvideRuntimeWatcher(maestroPath string) (*RuntimeWatcherServer, error) {
return nil, fmt.Errorf("unable to reach runtime watcher API: %s", err)
}

return &RuntimeWatcherServer{compose: compose}, nil
}

func (ws *RuntimeWatcherServer) Teardown() {
ws.compose.WithCommand([]string{"rm", "-s", "-v", "-f", "runtime-watcher"}).Invoke()
return &RuntimeWatcherServer{}, nil
}
32 changes: 12 additions & 20 deletions e2e/framework/maestro/components/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,30 @@
package components

import (
"context"
"fmt"
"net/http"
"strings"
"time"

tc "github.com/testcontainers/testcontainers-go"

"github.com/docker/compose/v2/pkg/api"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/topfreegames/maestro/e2e/framework/maestro/helpers"
)

type WorkerServer struct {
compose tc.DockerCompose
}
type WorkerServer struct{}

func ProvideWorker(maestroPath string) (*WorkerServer, error) {
func ProvideWorker(ctx context.Context, compose tc.ComposeStack) (*WorkerServer, error) {
client := &http.Client{}

composeFilePaths := []string{fmt.Sprintf("%s/e2e/framework/maestro/docker-compose.yml", maestroPath)}
identifier := strings.ToLower("e2e-test")

compose := tc.NewLocalDockerCompose(composeFilePaths, identifier)
composeErr := compose.WithCommand([]string{"up", "-d", "--build", "worker"}).Invoke()
services := compose.Services()
services = append(services, "worker")

if composeErr.Error != nil {
return nil, fmt.Errorf("failed to start worker %s", composeErr.Error)
err := compose.Up(ctx, tc.RunServices(services...), tc.Recreate(api.RecreateNever), tc.RecreateDependencies(api.RecreateNever), tc.Wait(true))
if err != nil {
return nil, fmt.Errorf("failed to start worker: %w", err)
}

err := helpers.TimedRetry(func() error {
err = helpers.TimedRetry(func() error {
res, err := client.Get("http://127.0.0.1:8051/healthz")
if err != nil {
return err
Expand All @@ -67,9 +63,5 @@ func ProvideWorker(maestroPath string) (*WorkerServer, error) {
return nil, fmt.Errorf("unable to reach worker %s", err)
}

return &WorkerServer{compose: compose}, nil
}

func (ws *WorkerServer) Teardown() {
ws.compose.WithCommand([]string{"rm", "-s", "-v", "-f", "runtime-watcher"}).Invoke()
return &WorkerServer{}, nil
}
34 changes: 11 additions & 23 deletions e2e/framework/maestro/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
package maestro

import (
"context"
"errors"
"fmt"
"strings"
"time"

tc "github.com/testcontainers/testcontainers-go"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/topfreegames/maestro/e2e/framework/maestro/exec"
"github.com/topfreegames/maestro/e2e/framework/maestro/helpers"
)
Expand All @@ -37,22 +38,20 @@ type dependencies struct {
KubeconfigPath string
RedisAddress string
KubeAddress string
compose tc.DockerCompose
}

// TODO(gabrielcorado): spin up the dependencies using different ports from dev
// environment to avoid conflicts.
func provideDependencies(maestroPath string) (*dependencies, error) {
composeFilePaths := []string{fmt.Sprintf("%s/e2e/framework/maestro/docker-compose.yml", maestroPath)}
identifier := strings.ToLower("e2e-test")

compose := tc.NewLocalDockerCompose(composeFilePaths, identifier)
_ = compose.WithCommand([]string{"down", "--remove-orphans", "--volumes"}).Invoke()

composeErr := compose.WithCommand([]string{"up", "-d", "postgres", "redis", "k3s_agent", "k3s_server"}).Invoke()
func provideDependencies(ctx context.Context, maestroPath string, compose tc.ComposeStack) (*dependencies, error) {
err := compose.Down(ctx, tc.RemoveOrphans(true), tc.RemoveVolumes(true), tc.RemoveImagesAll)
if err != nil {
return nil, fmt.Errorf("failed to tear down running containers: %w", err)
}

if composeErr.Error != nil {
return nil, fmt.Errorf("failed to start dependencies: %s", composeErr.Error)
services := []string{"postgres", "redis", "k3s_agent", "k3s_server"}
err = compose.Up(context.Background(), tc.RunServices(services...), tc.RemoveOrphans(true), tc.Wait(true))
if err != nil {
return nil, fmt.Errorf("failed to start dependencies: %w", err)
}

migrateErr := helpers.TimedRetry(func() error {
Expand All @@ -79,23 +78,12 @@ func provideDependencies(maestroPath string) (*dependencies, error) {
}, time.Second, 2*time.Minute)

if migrateErr != nil {
compose.WithCommand([]string{"down", "--remove-orphans", "--volumes"}).Invoke()
return nil, fmt.Errorf("failed to migrate database: %s", migrateErr)
}

return &dependencies{
KubeconfigPath: fmt.Sprintf("%s/kubeconfig/kubeconfig.yaml", maestroPath),
KubeAddress: "https://127.0.0.1:6443",
RedisAddress: "redis://127.0.0.1:6379/0",
compose: compose,
}, nil
}

func (d *dependencies) Teardown() {
d.compose.WithCommand([]string{"rm", "-s", "-v", "-f", "postgres", "redis", "k3s_agent", "k3s_server"}).Invoke()
exec.ExecSysCmd(
maestroPath,
"docker",
"volume ", "rm", "e2e-test_eventsproto", "e2e-test_kubeconfig",
)
}
Loading

0 comments on commit 27b460b

Please sign in to comment.