Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add test for buildengine deploy #1056

Merged
merged 1 commit into from
Mar 8, 2024
Merged
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
12 changes: 10 additions & 2 deletions buildengine/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ type deploymentArtefact struct {
localPath string
}

type DeployClient interface {
GetArtefactDiffs(context.Context, *connect.Request[ftlv1.GetArtefactDiffsRequest]) (*connect.Response[ftlv1.GetArtefactDiffsResponse], error)
UploadArtefact(context.Context, *connect.Request[ftlv1.UploadArtefactRequest]) (*connect.Response[ftlv1.UploadArtefactResponse], error)
CreateDeployment(context.Context, *connect.Request[ftlv1.CreateDeploymentRequest]) (*connect.Response[ftlv1.CreateDeploymentResponse], error)
ReplaceDeploy(context.Context, *connect.Request[ftlv1.ReplaceDeployRequest]) (*connect.Response[ftlv1.ReplaceDeployResponse], error)
Status(context.Context, *connect.Request[ftlv1.StatusRequest]) (*connect.Response[ftlv1.StatusResponse], error)
}

// Deploy a module to the FTL controller with the given number of replicas. Optionally wait for the deployment to become ready.
func Deploy(ctx context.Context, module Module, replicas int32, waitForDeployOnline bool, client ftlv1connect.ControllerServiceClient) error {
func Deploy(ctx context.Context, module Module, replicas int32, waitForDeployOnline bool, client DeployClient) error {
logger := log.FromContext(ctx).Scope(module.Module)
ctx = log.ContextWithLogger(ctx, logger)
logger.Infof("Deploying module")
Expand Down Expand Up @@ -221,7 +229,7 @@ func relToCWD(path string) string {
return rel
}

func checkReadiness(ctx context.Context, client ftlv1connect.ControllerServiceClient, deploymentName string, replicas int32) error {
func checkReadiness(ctx context.Context, client DeployClient, deploymentName string, replicas int32) error {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()

Expand Down
84 changes: 84 additions & 0 deletions buildengine/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package buildengine

import (
"context"
"os"
"testing"

"connectrpc.com/connect"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/sha256"
"github.com/alecthomas/assert/v2"
)

type mockDeployClient struct {
MissingDigests []string
DeploymentName string
}

func (m *mockDeployClient) GetArtefactDiffs(context.Context, *connect.Request[ftlv1.GetArtefactDiffsRequest]) (*connect.Response[ftlv1.GetArtefactDiffsResponse], error) {
return connect.NewResponse(&ftlv1.GetArtefactDiffsResponse{
MissingDigests: m.MissingDigests,
}), nil
}

func (m *mockDeployClient) UploadArtefact(ctx context.Context, req *connect.Request[ftlv1.UploadArtefactRequest]) (*connect.Response[ftlv1.UploadArtefactResponse], error) {
sha256digest := sha256.Sum(req.Msg.Content)
return connect.NewResponse(&ftlv1.UploadArtefactResponse{Digest: sha256digest[:]}), nil
}

func (m *mockDeployClient) CreateDeployment(context.Context, *connect.Request[ftlv1.CreateDeploymentRequest]) (*connect.Response[ftlv1.CreateDeploymentResponse], error) {
return connect.NewResponse(&ftlv1.CreateDeploymentResponse{DeploymentName: m.DeploymentName}), nil
}

func (m *mockDeployClient) ReplaceDeploy(context.Context, *connect.Request[ftlv1.ReplaceDeployRequest]) (*connect.Response[ftlv1.ReplaceDeployResponse], error) {
return nil, nil
}

func (m *mockDeployClient) Status(context.Context, *connect.Request[ftlv1.StatusRequest]) (*connect.Response[ftlv1.StatusResponse], error) {
resp := &ftlv1.StatusResponse{
Deployments: []*ftlv1.StatusResponse_Deployment{
{Key: m.DeploymentName, Replicas: 1},
},
}
return connect.NewResponse(resp), nil
}

func TestDeploy(t *testing.T) {
sch := &schema.Schema{
Modules: []*schema.Module{
schema.Builtins(),
{Name: "another", Decls: []schema.Decl{
&schema.Data{Name: "EchoRequest"},
&schema.Data{Name: "EchoResponse"},
&schema.Verb{
Name: "echo",
Request: &schema.DataRef{Name: "EchoRequest"},
Response: &schema.DataRef{Name: "EchoResponse"},
},
}},
},
}
ctx := log.ContextWithLogger(context.Background(), log.Configure(os.Stderr, log.Config{}))

modulePath := "testdata/modules/another"
module, err := LoadModule(ctx, modulePath)
assert.NoError(t, err)

// Build first to make sure the files are there.
err = Build(ctx, sch, module)
assert.NoError(t, err)

sum, err := sha256.SumFile(modulePath + "/_ftl/main")
assert.NoError(t, err)

client := &mockDeployClient{
MissingDigests: []string{sum.String()},
DeploymentName: "test-deployment",
}

err = Deploy(ctx, module, int32(1), true, client)
assert.NoError(t, err)
}