|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/codefly-dev/core/agents/services" |
| 12 | + basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0" |
| 13 | + builderv0 "github.com/codefly-dev/core/generated/go/codefly/services/builder/v0" |
| 14 | + "github.com/codefly-dev/core/resources" |
| 15 | + "github.com/codefly-dev/core/shared" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +// createdBuilder loads and creates a fastapi service in a fresh temp directory |
| 20 | +// and returns the builder together with its service location. |
| 21 | +func createdBuilder(t *testing.T) (*Builder, string) { |
| 22 | + t.Helper() |
| 23 | + ctx := context.Background() |
| 24 | + |
| 25 | + tmpDir, err := os.MkdirTemp("testdata", "recipe") |
| 26 | + require.NoError(t, err) |
| 27 | + tmpDir = shared.MustSolvePath(tmpDir) |
| 28 | + t.Cleanup(func() { _ = os.RemoveAll(tmpDir) }) |
| 29 | + |
| 30 | + service := resources.Service{Name: fmt.Sprintf("svc-%v", time.Now().UnixMilli()), Version: "0.0.0"} |
| 31 | + require.NoError(t, service.SaveAtDir(ctx, path.Join(tmpDir, fmt.Sprintf("mod/%s", service.Name)))) |
| 32 | + service.WithModule("mod") |
| 33 | + |
| 34 | + identity := &basev0.ServiceIdentity{ |
| 35 | + Name: service.Name, |
| 36 | + Version: service.Version, |
| 37 | + Module: "mod", |
| 38 | + Workspace: "test", |
| 39 | + WorkspacePath: tmpDir, |
| 40 | + RelativeToWorkspace: fmt.Sprintf("mod/%s", service.Name), |
| 41 | + } |
| 42 | + |
| 43 | + builder := NewBuilder(NewService()) |
| 44 | + _, err = builder.Load(ctx, &builderv0.LoadRequest{Identity: identity, CreationMode: &builderv0.CreationMode{Communicate: false}}) |
| 45 | + require.NoError(t, err) |
| 46 | + _, err = builder.Create(ctx, &builderv0.CreateRequest{}) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + return builder, builder.Local("builder") |
| 50 | +} |
| 51 | + |
| 52 | +func recipeBuildRequest(outputDir string) *builderv0.BuildRequest { |
| 53 | + return &builderv0.BuildRequest{ |
| 54 | + BuildContext: &builderv0.BuildContext{ |
| 55 | + Kind: &builderv0.BuildContext_DockerBuildContext{ |
| 56 | + DockerBuildContext: &builderv0.DockerBuildContext{DockerRepository: "example.com/test"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + OutputDirectory: outputDir, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestBuildEmitsRecipePlan proves that when the CLI provides an output |
| 64 | +// directory, Build renders the Dockerfile there and returns a DockerBuildPlan |
| 65 | +// (not the legacy DockerBuildResult) whose recipe the CLI executor can consume: |
| 66 | +// the Dockerfile is resolved relative to the output directory, the context to |
| 67 | +// the service directory, and the image targets a multi-arch manifest list. |
| 68 | +func TestBuildEmitsRecipePlan(t *testing.T) { |
| 69 | + builder, outputDir := createdBuilder(t) |
| 70 | + |
| 71 | + resp, err := builder.Build(context.Background(), recipeBuildRequest(outputDir)) |
| 72 | + require.NoError(t, err) |
| 73 | + require.Equal(t, builderv0.BuildStatus_SUCCESS, resp.GetState().GetState(), resp.GetState().GetMessage()) |
| 74 | + |
| 75 | + plan := resp.GetResult().GetDockerBuildPlan() |
| 76 | + require.NotNil(t, plan, "recipe path must emit a DockerBuildPlan") |
| 77 | + require.Nil(t, resp.GetResult().GetDockerBuildResult(), "recipe path must not emit a legacy DockerBuildResult") |
| 78 | + |
| 79 | + require.Len(t, plan.GetRecipes(), 1) |
| 80 | + recipe := plan.GetRecipes()[0] |
| 81 | + require.Equal(t, "Dockerfile", recipe.GetDockerfile()) |
| 82 | + require.Equal(t, ".", recipe.GetContext()) |
| 83 | + require.Equal(t, []string{"linux/amd64", "linux/arm64"}, recipe.GetPlatforms()) |
| 84 | + require.NotEmpty(t, recipe.GetImage()) |
| 85 | + |
| 86 | + // The plan must verify against the on-disk tree exactly as the CLI verifies |
| 87 | + // it before running docker buildx. |
| 88 | + require.NoError(t, services.VerifyDockerBuildPlan(outputDir, plan)) |
| 89 | + |
| 90 | + // The Dockerfile the CLI builds must actually exist in the emitted tree. |
| 91 | + _, err = os.Stat(path.Join(outputDir, recipe.GetDockerfile())) |
| 92 | + require.NoError(t, err) |
| 93 | +} |
0 commit comments