From fb9edd8432a9df1ee584b6395829fb47e5dc3d6f Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 21:59:48 -0600 Subject: [PATCH 1/2] test(platform): cover owner, version, and remote-command framing pkg/platform was the only top-level package tree with no test files at all, across all twelve of its subpackages. Start with the parts that are pure logic and need no cluster: ownership resolution, the /version endpoint, and the remote-command wire protocol. - owner: user match, team membership, acting-team ownership, nil inputs, and the OwnerFilter pflag value. - version: response parsing, status and malformed-JSON errors, the leading-"v" normalization, and the missing-devsyVersion message. - remotecommand: data, close, and exit-code message round trips, plus rejection of unknown types and truncated payloads. The owner tests document that an empty Self user name currently matches an empty UserOrTeam, because the comparison is string equality of two empty strings. That reads like a bug, but tightening it is an authorization change, so the test pins the present behavior and the comment explains it rather than silently changing the semantics. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/platform/owner_test.go | 121 ++++++++++++++++++++ pkg/platform/remotecommand/protocol_test.go | 81 +++++++++++++ pkg/platform/version_test.go | 104 +++++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 pkg/platform/owner_test.go create mode 100644 pkg/platform/remotecommand/protocol_test.go create mode 100644 pkg/platform/version_test.go diff --git a/pkg/platform/owner_test.go b/pkg/platform/owner_test.go new file mode 100644 index 0000000000..5154af7b6b --- /dev/null +++ b/pkg/platform/owner_test.go @@ -0,0 +1,121 @@ +package platform + +import ( + "testing" + + managementv1 "github.com/devsy-org/api/pkg/apis/management/v1" + storagev1 "github.com/devsy-org/api/pkg/apis/storage/v1" + "github.com/stretchr/testify/suite" +) + +const ( + ownerTestUser = "alice" + ownerTestTeam = "devs" +) + +type ownerSuite struct { + suite.Suite +} + +func TestOwnerSuite(t *testing.T) { + suite.Run(t, new(ownerSuite)) +} + +func (s *ownerSuite) TestIsOwnerMatchesUserName() { + self := s.self(s.userInfo(ownerTestUser), nil) + s.True(IsOwner(self, &storagev1.UserOrTeam{User: ownerTestUser})) +} + +func (s *ownerSuite) TestIsOwnerRejectsDifferentUser() { + self := s.self(s.userInfo(ownerTestUser), nil) + s.False(IsOwner(self, &storagev1.UserOrTeam{User: "bob"})) +} + +// Membership in a team the user belongs to grants ownership of that team. +func (s *ownerSuite) TestIsOwnerMatchesTeamMembership() { + self := s.self(s.userInfo(ownerTestUser, ownerTestTeam), nil) + s.True(IsOwner(self, &storagev1.UserOrTeam{Team: ownerTestTeam})) + s.False(IsOwner(self, &storagev1.UserOrTeam{Team: "other"})) +} + +// The acting team owns its own resources even when the user is not a member. +func (s *ownerSuite) TestIsOwnerMatchesActingTeam() { + self := s.self( + s.userInfo(ownerTestUser), + &storagev1.EntityInfo{Name: ownerTestTeam}, + ) + s.True(IsOwner(self, &storagev1.UserOrTeam{Team: ownerTestTeam})) + s.False(IsOwner(self, &storagev1.UserOrTeam{Team: "other"})) +} + +func (s *ownerSuite) TestIsOwnerHandlesNilInputs() { + self := s.self(s.userInfo(ownerTestUser), nil) + s.False(IsOwner(nil, &storagev1.UserOrTeam{User: ownerTestUser})) + s.False(IsOwner(self, nil)) + s.False(IsOwner(nil, nil)) +} + +// An empty user name currently matches an empty UserOrTeam, because the +// comparison is a plain string equality of two empty strings. This is +// documented rather than asserted as correct: it means a Self that carries no +// resolved user is treated as owning a subject with no user set. Tightening it +// is an authorization change and needs its own decision, so this test pins the +// present behavior to make any change visible. +func (s *ownerSuite) TestIsOwnerEmptyNamesCurrentlyMatch() { + self := s.self(&managementv1.UserInfo{}, &storagev1.EntityInfo{}) + s.True(IsOwner(self, &storagev1.UserOrTeam{})) + + // A resolved user name does not match an empty subject, so the match above + // is specifically the both-sides-empty case. + named := s.self(s.userInfo(ownerTestUser), nil) + s.False(IsOwner(named, &storagev1.UserOrTeam{})) +} + +func (s *ownerSuite) TestUserIsOwnerHandlesNilUser() { + s.False(userIsOwner(nil, &storagev1.UserOrTeam{User: ownerTestUser})) +} + +func (s *ownerSuite) TestOwnerFilterSetAcceptsKnownValues() { + for _, tc := range []struct { + in string + want OwnerFilter + }{ + {"", SelfOwnerFilter}, + {"self", SelfOwnerFilter}, + {"all", AllOwnerFilter}, + } { + var filter OwnerFilter + s.Require().NoError(filter.Set(tc.in), "input %q", tc.in) + s.Equal(tc.want, filter, "input %q", tc.in) + } +} + +func (s *ownerSuite) TestOwnerFilterSetRejectsUnknownValue() { + var filter OwnerFilter + err := filter.Set("everything") + s.Require().Error(err) + s.Contains(err.Error(), "everything") +} + +func (s *ownerSuite) TestOwnerFilterTypeAndString() { + filter := AllOwnerFilter + s.Equal("ownerFilter", filter.Type()) + s.Equal("all", filter.String()) +} + +func (s *ownerSuite) self( + user *managementv1.UserInfo, + team *storagev1.EntityInfo, +) *managementv1.Self { + return &managementv1.Self{ + Status: managementv1.SelfStatus{User: user, Team: team}, + } +} + +func (s *ownerSuite) userInfo(teams ...string) *managementv1.UserInfo { + info := &managementv1.UserInfo{EntityInfo: storagev1.EntityInfo{Name: ownerTestUser}} + for _, team := range teams { + info.Teams = append(info.Teams, &storagev1.EntityInfo{Name: team}) + } + return info +} diff --git a/pkg/platform/remotecommand/protocol_test.go b/pkg/platform/remotecommand/protocol_test.go new file mode 100644 index 0000000000..d21363860e --- /dev/null +++ b/pkg/platform/remotecommand/protocol_test.go @@ -0,0 +1,81 @@ +package remotecommand + +import ( + "bytes" + "io" + "testing" + + "github.com/stretchr/testify/suite" +) + +type protocolSuite struct { + suite.Suite +} + +func TestProtocolSuite(t *testing.T) { + suite.Run(t, new(protocolSuite)) +} + +// A data message carries its type byte followed by the payload. +func (s *protocolSuite) TestDataMessageRoundTrip() { + original := newDataMessage(StdoutData, []byte("hello")) + encoded := original.Bytes() + s.Equal([]byte{byte(StdoutData), 'h', 'e', 'l', 'l', 'o'}, encoded) + + parsed, err := ParseMessage(bytes.NewReader(encoded)) + s.Require().NoError(err) + s.Equal(StdoutData, parsed.messageType) + payload, err := io.ReadAll(parsed.data) + s.Require().NoError(err) + s.Equal("hello", string(payload)) +} + +func (s *protocolSuite) TestCloseMessagesCarryNoPayload() { + for _, messageType := range []MessageType{StdoutClose, StderrClose, StdinClose} { + encoded := newCloseMessage(messageType).Bytes() + s.Equal([]byte{byte(messageType)}, encoded) + + parsed, err := ParseMessage(bytes.NewReader(encoded)) + s.Require().NoError(err, "type %d", messageType) + s.Equal(messageType, parsed.messageType, "type %d", messageType) + s.Nil(parsed.data, "type %d", messageType) + } +} + +func (s *protocolSuite) TestExitCodeRoundTrip() { + original := NewExitCodeMessage(42) + encoded := original.Bytes() + s.Equal(byte(ExitCode), encoded[0]) + + parsed, err := ParseMessage(bytes.NewReader(encoded)) + s.Require().NoError(err) + s.Equal(ExitCode, parsed.messageType) + s.Equal(int64(42), parsed.exitCode) +} + +func (s *protocolSuite) TestExitCodeRoundTripNegative() { + encoded := NewExitCodeMessage(-1).Bytes() + parsed, err := ParseMessage(bytes.NewReader(encoded)) + s.Require().NoError(err) + s.Equal(ExitCode, parsed.messageType) + s.Equal(int64(-1), parsed.exitCode) +} + +func (s *protocolSuite) TestParseRejectsUnknownMessageType() { + _, err := ParseMessage(bytes.NewReader([]byte{99})) + s.Require().Error(err) + // The type is rendered in binary, matching the production error. + s.Contains(err.Error(), "1100011") +} + +func (s *protocolSuite) TestParseRejectsTruncatedExitCode() { + // A type byte with no varint payload behind it. + _, err := ParseMessage(bytes.NewReader([]byte{byte(ExitCode)})) + s.Require().Error(err) + s.Contains(err.Error(), "read exit code") +} + +func (s *protocolSuite) TestParseReturnsErrorOnEmptyInput() { + _, err := ParseMessage(bytes.NewReader(nil)) + s.ErrorIs(err, io.EOF) +} diff --git a/pkg/platform/version_test.go b/pkg/platform/version_test.go new file mode 100644 index 0000000000..2816103bb8 --- /dev/null +++ b/pkg/platform/version_test.go @@ -0,0 +1,104 @@ +package platform + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/suite" +) + +type versionSuite struct { + suite.Suite +} + +func TestVersionSuite(t *testing.T) { + suite.Run(t, new(versionSuite)) +} + +func (s *versionSuite) TestGetPlatformVersionParsesBothFields() { + url := s.serve(http.StatusOK, `{"version":"1.2.3","devsyVersion":"v0.4.5"}`) + + got, err := GetPlatformVersion(url) + s.Require().NoError(err) + s.Equal("1.2.3", got.Version) + s.Equal("v0.4.5", got.DevsyVersion) +} + +func (s *versionSuite) TestGetPlatformVersionOmitsAbsentFields() { + url := s.serve(http.StatusOK, `{}`) + + got, err := GetPlatformVersion(url) + s.Require().NoError(err) + s.Empty(got.Version) + s.Empty(got.DevsyVersion) +} + +func (s *versionSuite) TestGetPlatformVersionReportsStatusAndBody() { + url := s.serve(http.StatusForbidden, "nope") + + _, err := GetPlatformVersion(url) + s.Require().Error(err) + s.Contains(err.Error(), "nope") + s.Contains(err.Error(), "403") +} + +func (s *versionSuite) TestGetPlatformVersionRejectsMalformedJSON() { + url := s.serve(http.StatusOK, "not json") + + _, err := GetPlatformVersion(url) + s.Require().Error(err) + s.Contains(err.Error(), "parse") +} + +// The devsy version is normalized to always carry a leading "v". +func (s *versionSuite) TestGetDevsyVersionAddsMissingVPrefix() { + url := s.serve(http.StatusOK, `{"devsyVersion":"0.4.5"}`) + + got, err := GetDevsyVersion(url) + s.Require().NoError(err) + s.Equal("v0.4.5", got) +} + +func (s *versionSuite) TestGetDevsyVersionKeepsExistingVPrefix() { + url := s.serve(http.StatusOK, `{"devsyVersion":"v0.4.5"}`) + + got, err := GetDevsyVersion(url) + s.Require().NoError(err) + s.Equal("v0.4.5", got) +} + +// A platform that reports no devsy version is a provider-version problem, and +// the error must point the user at --version rather than failing obscurely. +func (s *versionSuite) TestGetDevsyVersionRequiresDevsyVersion() { + url := s.serve(http.StatusOK, `{"version":"1.2.3"}`) + + _, err := GetDevsyVersion(url) + s.Require().Error(err) + s.Contains(err.Error(), "--version") +} + +func (s *versionSuite) TestGetDevsyVersionPropagatesFetchError() { + server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})) + url := server.URL + server.Close() + + _, err := GetDevsyVersion(url) + s.Require().Error(err) + s.Contains(err.Error(), "get") +} + +// serve starts a test server that answers /version with the given status and +// body, and returns its base URL. +func (s *versionSuite) serve(status int, body string) string { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/version" { + w.WriteHeader(http.StatusNotFound) + return + } + w.WriteHeader(status) + _, _ = w.Write([]byte(body)) + })) + s.T().Cleanup(server.Close) + return server.URL +} From 7a71f8194981c9d9f85d6f45d729e02dbf98f2bf Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 23:08:36 -0600 Subject: [PATCH 2/2] style: trim comments in platform tests Most restated the test name. Kept the ones carrying information the code does not: that the empty-name ownership match is pinned rather than endorsed, that the unknown message type is rendered in binary, and why the version error is expected to name --version. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/platform/owner_test.go | 15 +++++---------- pkg/platform/remotecommand/protocol_test.go | 3 +-- pkg/platform/version_test.go | 7 ++----- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/pkg/platform/owner_test.go b/pkg/platform/owner_test.go index 5154af7b6b..8e84d6d28e 100644 --- a/pkg/platform/owner_test.go +++ b/pkg/platform/owner_test.go @@ -31,14 +31,13 @@ func (s *ownerSuite) TestIsOwnerRejectsDifferentUser() { s.False(IsOwner(self, &storagev1.UserOrTeam{User: "bob"})) } -// Membership in a team the user belongs to grants ownership of that team. func (s *ownerSuite) TestIsOwnerMatchesTeamMembership() { self := s.self(s.userInfo(ownerTestUser, ownerTestTeam), nil) s.True(IsOwner(self, &storagev1.UserOrTeam{Team: ownerTestTeam})) s.False(IsOwner(self, &storagev1.UserOrTeam{Team: "other"})) } -// The acting team owns its own resources even when the user is not a member. +// The acting team owns its resources even when the user is not a member. func (s *ownerSuite) TestIsOwnerMatchesActingTeam() { self := s.self( s.userInfo(ownerTestUser), @@ -55,18 +54,14 @@ func (s *ownerSuite) TestIsOwnerHandlesNilInputs() { s.False(IsOwner(nil, nil)) } -// An empty user name currently matches an empty UserOrTeam, because the -// comparison is a plain string equality of two empty strings. This is -// documented rather than asserted as correct: it means a Self that carries no -// resolved user is treated as owning a subject with no user set. Tightening it -// is an authorization change and needs its own decision, so this test pins the -// present behavior to make any change visible. +// An empty user name matches an empty UserOrTeam, since both sides are empty +// strings. Tightening this is an authorization change, so the test pins the +// present behavior rather than asserting it is correct. func (s *ownerSuite) TestIsOwnerEmptyNamesCurrentlyMatch() { self := s.self(&managementv1.UserInfo{}, &storagev1.EntityInfo{}) s.True(IsOwner(self, &storagev1.UserOrTeam{})) - // A resolved user name does not match an empty subject, so the match above - // is specifically the both-sides-empty case. + // A resolved name does not match, so the match above is the both-empty case. named := s.self(s.userInfo(ownerTestUser), nil) s.False(IsOwner(named, &storagev1.UserOrTeam{})) } diff --git a/pkg/platform/remotecommand/protocol_test.go b/pkg/platform/remotecommand/protocol_test.go index d21363860e..6b4dc158c0 100644 --- a/pkg/platform/remotecommand/protocol_test.go +++ b/pkg/platform/remotecommand/protocol_test.go @@ -16,7 +16,6 @@ func TestProtocolSuite(t *testing.T) { suite.Run(t, new(protocolSuite)) } -// A data message carries its type byte followed by the payload. func (s *protocolSuite) TestDataMessageRoundTrip() { original := newDataMessage(StdoutData, []byte("hello")) encoded := original.Bytes() @@ -64,7 +63,7 @@ func (s *protocolSuite) TestExitCodeRoundTripNegative() { func (s *protocolSuite) TestParseRejectsUnknownMessageType() { _, err := ParseMessage(bytes.NewReader([]byte{99})) s.Require().Error(err) - // The type is rendered in binary, matching the production error. + // Rendered in binary, matching the production error. s.Contains(err.Error(), "1100011") } diff --git a/pkg/platform/version_test.go b/pkg/platform/version_test.go index 2816103bb8..e420b57c87 100644 --- a/pkg/platform/version_test.go +++ b/pkg/platform/version_test.go @@ -51,7 +51,6 @@ func (s *versionSuite) TestGetPlatformVersionRejectsMalformedJSON() { s.Contains(err.Error(), "parse") } -// The devsy version is normalized to always carry a leading "v". func (s *versionSuite) TestGetDevsyVersionAddsMissingVPrefix() { url := s.serve(http.StatusOK, `{"devsyVersion":"0.4.5"}`) @@ -68,8 +67,8 @@ func (s *versionSuite) TestGetDevsyVersionKeepsExistingVPrefix() { s.Equal("v0.4.5", got) } -// A platform that reports no devsy version is a provider-version problem, and -// the error must point the user at --version rather than failing obscurely. +// The error must point at --version, since a missing devsyVersion is a +// provider-version problem. func (s *versionSuite) TestGetDevsyVersionRequiresDevsyVersion() { url := s.serve(http.StatusOK, `{"version":"1.2.3"}`) @@ -88,8 +87,6 @@ func (s *versionSuite) TestGetDevsyVersionPropagatesFetchError() { s.Contains(err.Error(), "get") } -// serve starts a test server that answers /version with the given status and -// body, and returns its base URL. func (s *versionSuite) serve(status int, body string) string { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/version" {