Skip to content
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
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,6 @@ func (cmd *BuildCmd) build(
}
}
user := devcconfig.GetRemoteUser(result)
_ = devcconfig.WriteResultJSON(os.Stdout, containerID, user, workdir)
_ = devcconfig.WriteResultJSON(os.Stdout, containerID, user, workdir, nil)
return nil
}
2 changes: 1 addition & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (cmd *ExecCmd) Run(ctx context.Context, args []string) error {
return err
}

_ = devcconfig.WriteResultJSON(os.Stderr, containerDetails.ID, user, workdir)
_ = devcconfig.WriteResultJSON(os.Stderr, containerDetails.ID, user, workdir, nil)
return nil
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ func (cmd *UpCmd) Run(
}

containerID := ""
if wctx.result != nil && wctx.result.ContainerDetails != nil {
containerID = wctx.result.ContainerDetails.ID
var warnings []string
if wctx.result != nil {
if wctx.result.ContainerDetails != nil {
containerID = wctx.result.ContainerDetails.ID
}
warnings = wctx.result.HostWarnings
}
_ = config2.WriteResultJSON(os.Stdout, containerID, wctx.user, wctx.workdir)
_ = config2.WriteResultJSON(os.Stdout, containerID, wctx.user, wctx.workdir, warnings)
return nil
}

Expand Down
67 changes: 67 additions & 0 deletions e2e/tests/up/host_requirements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package up

import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"

"github.com/devsy-org/devsy/e2e/framework"
"github.com/devsy-org/devsy/pkg/devcontainer/config"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

var _ = ginkgo.Describe(
"testing up command host requirements warnings",
ginkgo.Label("up-host-requirements"),
func() {
var dtc *dockerTestContext

ginkgo.BeforeEach(func(ctx context.Context) {
var err error
dtc = &dockerTestContext{}
dtc.initialDir, err = os.Getwd()
framework.ExpectNoError(err)

dtc.f, err = setupDockerProvider(
filepath.Join(dtc.initialDir, "bin"), "docker",
)
framework.ExpectNoError(err)
})

ginkgo.It("surfaces hostRequirements warnings in JSON envelope", func(ctx context.Context) {
tempDir, err := setupWorkspace(
"tests/up/testdata/docker-host-requirements",
dtc.initialDir,
dtc.f,
)
framework.ExpectNoError(err)

stdout, _, err := dtc.f.DevsyUpStreams(ctx, tempDir)
framework.ExpectNoError(err)

lines := strings.Split(strings.TrimSpace(stdout), "\n")
gomega.Expect(lines).NotTo(gomega.BeEmpty())

lastLine := lines[len(lines)-1]
var envelope config.ResultEnvelope
err = json.Unmarshal([]byte(lastLine), &envelope)
framework.ExpectNoError(err)

gomega.Expect(envelope.Outcome).To(gomega.Equal("success"))
gomega.Expect(envelope.Warnings).NotTo(gomega.BeEmpty())

found := false
for _, w := range envelope.Warnings {
if strings.Contains(w, "cpus:") {
found = true
break
}
}
gomega.Expect(found).To(gomega.BeTrue(),
"expected a cpus warning in %v", envelope.Warnings)
}, ginkgo.SpecTimeout(framework.TimeoutShort()))
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "HostRequirements",
"image": "ghcr.io/devsy-org/test-images/go:1",
"hostRequirements": {
"cpus": 128
}
}
13 changes: 8 additions & 5 deletions pkg/devcontainer/config/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ import (
)

type ResultEnvelope struct {
Outcome string `json:"outcome"`
ContainerID string `json:"containerId"`
RemoteUser string `json:"remoteUser"`
RemoteWorkspaceFolder string `json:"remoteWorkspaceFolder"`
Outcome string `json:"outcome"`
ContainerID string `json:"containerId"`
RemoteUser string `json:"remoteUser"`
RemoteWorkspaceFolder string `json:"remoteWorkspaceFolder"`
Warnings []string `json:"warnings,omitempty"`
}

type ErrorEnvelope struct {
Outcome string `json:"outcome"`
Message string `json:"message"`
}

func WriteResultJSON(w io.Writer, containerID, user, workdir string) error {
//nolint:revive
func WriteResultJSON(w io.Writer, containerID, user, workdir string, warnings []string) error {
env := ResultEnvelope{
Outcome: "success",
ContainerID: containerID,
RemoteUser: user,
RemoteWorkspaceFolder: workdir,
Warnings: warnings,
}
data, err := json.Marshal(env)
if err != nil {
Expand Down
38 changes: 37 additions & 1 deletion pkg/devcontainer/config/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,52 @@ func TestWriteResultJSON(t *testing.T) {
containerID string
user string
workdir string
warnings []string
}{
{
name: "typical values",
containerID: "abc123def456",
user: "vscode",
workdir: "/workspaces/project",
warnings: nil,
},
{
name: "root user",
containerID: "sha256:abcdef1234567890",
user: "root",
workdir: "/workspaces/my-app",
warnings: nil,
},
{
name: "empty strings",
containerID: "",
user: "",
workdir: "",
warnings: nil,
},
{
name: "special characters in container ID",
containerID: "container:with/special-chars_123",
user: "dev-user",
workdir: "/workspaces/my project",
warnings: nil,
},
{
name: "with warnings",
containerID: "abc123",
user: "vscode",
workdir: "/workspaces/project",
warnings: []string{
"cpus: required 128, available 4",
"memory: required 256gb (274877906944 bytes), available 17179869184 bytes",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
err := WriteResultJSON(&buf, tt.containerID, tt.user, tt.workdir)
err := WriteResultJSON(&buf, tt.containerID, tt.user, tt.workdir, tt.warnings)
if err != nil {
t.Fatalf("WriteResultJSON returned error: %v", err)
}
Expand Down Expand Up @@ -71,6 +86,27 @@ func TestWriteResultJSON(t *testing.T) {
t.Errorf("remoteWorkspaceFolder = %q, want %q",
envelope.RemoteWorkspaceFolder, tt.workdir)
}
if len(tt.warnings) == 0 {
if envelope.Warnings != nil {
t.Errorf("warnings = %v, want nil (omitempty)", envelope.Warnings)
}
if bytes.Contains(output, []byte(`"warnings"`)) {
t.Error("warnings field must be omitted when empty")
}
}
if len(tt.warnings) > 0 {
if len(envelope.Warnings) != len(tt.warnings) {
t.Fatalf(
"warnings length = %d, want %d",
len(envelope.Warnings), len(tt.warnings),
)
}
for i, w := range tt.warnings {
if envelope.Warnings[i] != w {
t.Errorf("warnings[%d] = %q, want %q", i, envelope.Warnings[i], w)
}
}
}

if bytes.Contains(output[:len(output)-1], []byte("\n")) {
t.Error("JSON must be single-line (no embedded newlines)")
Expand Down
1 change: 1 addition & 0 deletions pkg/devcontainer/config/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Result struct {
MergedConfig *MergedDevContainerConfig `json:"MergedConfig"`
SubstitutionContext *SubstitutionContext `json:"SubstitutionContext"`
ContainerDetails *ContainerDetails `json:"ContainerDetails"`
HostWarnings []string `json:"HostWarnings,omitempty"`
}

type DevContainerConfigWithPath struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/devcontainer/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type setupContainerParams struct {
mergedConfig *config.MergedDevContainerConfig
substitutionContext *config.SubstitutionContext
timeout time.Duration
hostWarnings []string
}

type setupInfo struct {
Expand Down Expand Up @@ -119,6 +120,7 @@ func (r *runner) buildResult(params *setupContainerParams) *config.Result {
MergedConfig: params.mergedConfig,
SubstitutionContext: params.substitutionContext,
ContainerDetails: params.containerDetails,
HostWarnings: params.hostWarnings,
}

if r.WorkspaceConfig.CLIOptions.DefaultUserEnvProbe != "" {
Expand Down
5 changes: 4 additions & 1 deletion pkg/devcontainer/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ exec /usr/local/bin/devsy agent container daemon
type resolvedContainer struct {
details *config.ContainerDetails
mergedConfig *config.MergedDevContainerConfig
hostWarnings []string
}

// resolveParams bundles the arguments shared by the container resolution methods.
Expand Down Expand Up @@ -101,6 +102,7 @@ func (r *runner) runSingleContainer(
mergedConfig: resolved.mergedConfig,
substitutionContext: substitutionContext,
timeout: timeout,
hostWarnings: resolved.hostWarnings,
})
}

Expand Down Expand Up @@ -220,7 +222,7 @@ func (r *runner) resolveNewContainer(
ctx context.Context,
p *resolveParams,
) (*resolvedContainer, error) {
config.ValidateHostRequirements(
hostWarnings := config.ValidateHostRequirements(
p.parsedConfig.Config.HostRequirements,
config.SystemHostInfo{},
p.substitutionContext.LocalWorkspaceFolder,
Expand Down Expand Up @@ -276,6 +278,7 @@ func (r *runner) resolveNewContainer(
return &resolvedContainer{
details: containerDetails,
mergedConfig: mergedConfig,
hostWarnings: hostWarnings,
}, nil
}

Expand Down
Loading