Files
coder/agent/proto/script_test.go
T
Bobby Ho ee5e3a4029 feat: send script resource addresses and dependencies in the agent manifest (#30627)
feat: include script dependencies in agent manifests

Send persisted script Terraform addresses and dependency lists on every
manifest request, preserving them across agent restarts, reconnects, and
shutdown without rebuilding the workspace.

- Add `resource_address` and `dependencies` to the agent proto, with
  `SUCCESS` and `COMPLETION` requirements.
- Reuse shared database-to-SDK validation and add bidirectional SDK/proto
  conversion. Reject empty prerequisite addresses and invalid requirements
  rather than accepting partial lists.
- Bump the agent API to v2.13 and update the agent's RPC connection.
  Older agents ignore the new fields; older servers return empty fields.
- Test manifest delivery, subagent isolation, round trips, invalid
  dependencies, and unchanged encoding when the new fields are unset.

Script execution is unchanged; ordering enforcement follows in PLAT-549.
Production dependency lists remain empty until #30556 merges.

Stacked on #30625. Refs PLAT-548.
2026-10-14 17:26:31 +00:00

79 lines
2.3 KiB
Go

package proto_test
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
agentproto "github.com/coder/coder/v2/agent/proto"
)
func TestScriptDependenciesRoundTrip(t *testing.T) {
t.Parallel()
id, logSourceID := uuid.New(), uuid.New()
want := &agentproto.Manifest{
Scripts: []*agentproto.WorkspaceAgentScript{{
Id: id[:],
LogSourceId: logSourceID[:],
DisplayName: "Install tools",
RunOnStart: true,
ResourceAddress: "coder_script.install_tools",
Dependencies: []*agentproto.WorkspaceAgentScriptDependency{
{
PrerequisiteResourceAddress: "coder_script.clone_repo",
Requirement: agentproto.WorkspaceAgentScriptDependency_REQUIREMENT_SUCCESS,
},
{
PrerequisiteResourceAddress: "coder_script.warm_cache",
Requirement: agentproto.WorkspaceAgentScriptDependency_REQUIREMENT_COMPLETION,
},
},
}},
}
data, err := proto.Marshal(want)
require.NoError(t, err)
got := &agentproto.Manifest{}
require.NoError(t, proto.Unmarshal(data, got))
require.True(t, proto.Equal(want, got))
}
// A script with neither new field set must encode to the same bytes it did
// before fields 11 and 12 existed, so an older agent sees nothing new.
// Walking the tags instead of comparing against a recorded byte string
// keeps the test stable when unrelated fields are added later.
func TestUnorderedScriptEncodesWithoutNewFields(t *testing.T) {
t.Parallel()
id, logSourceID := uuid.New(), uuid.New()
script := &agentproto.WorkspaceAgentScript{
Id: id[:],
LogSourceId: logSourceID[:],
Script: "git clone ...",
RunOnStart: true,
Timeout: durationpb.New(2 * time.Minute),
DisplayName: "Clone repo",
}
data, err := proto.Marshal(script)
require.NoError(t, err)
require.NotEmpty(t, data)
for len(data) > 0 {
num, typ, n := protowire.ConsumeTag(data)
require.Positive(t, n)
require.LessOrEqual(t, int(num), 10, "field %d must not be encoded when it is not set", num)
data = data[n:]
n = protowire.ConsumeFieldValue(num, typ, data)
require.Positive(t, n)
data = data[n:]
}
}