Skip to content

Commit

Permalink
internal/gomote: add swarming list instances
Browse files Browse the repository at this point in the history
This change adds the ability to list instances for the swarming gomote
implementation.

Fixes golang/go#63775

Change-Id: Id765d7415076e28b1f1ba60d32136d976522f434
Reviewed-on: https://go-review.googlesource.com/c/build/+/537898
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Auto-Submit: Carlos Amedee <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
cagedmantis authored and gopherbot committed Oct 30, 2023
1 parent 08f08c6 commit cabf17f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/gomote/swarming.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,28 @@ func (ss *SwarmingServer) ListSwarmingBuilders(ctx context.Context, req *protos.
return &protos.ListSwarmingBuildersResponse{Builders: builders}, nil
}

// ListInstances will list the gomote instances owned by the requester. The requester must be authenticated.
func (ss *SwarmingServer) ListInstances(ctx context.Context, req *protos.ListInstancesRequest) (*protos.ListInstancesResponse, error) {
creds, err := access.IAPFromContext(ctx)
if err != nil {
log.Printf("ListInstances access.IAPFromContext(ctx) = nil, %s", err)
return nil, status.Errorf(codes.Unauthenticated, "request does not contain the required authentication")
}
res := &protos.ListInstancesResponse{}
for _, s := range ss.buildlets.List() {
if s.OwnerID != creds.ID {
continue
}
res.Instances = append(res.Instances, &protos.Instance{
GomoteId: s.ID,
BuilderType: s.BuilderType,
HostType: s.HostType,
Expires: s.Expires.Unix(),
})
}
return res, nil
}

// session is a helper function that retrieves a session associated with the gomoteID and ownerID.
func (ss *SwarmingServer) session(gomoteID, ownerID string) (*remote.Session, error) {
session, err := ss.buildlets.Session(gomoteID)
Expand Down
22 changes: 22 additions & 0 deletions internal/gomote/swarming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/testing/protocmp"
)

const testSwarmingBucketName = "unit-testing-bucket-swarming"
Expand Down Expand Up @@ -290,6 +291,27 @@ func TestSwarmingDestroyInstanceError(t *testing.T) {
}
}

func TestSwarmingListInstance(t *testing.T) {
client := setupGomoteSwarmingTest(t, context.Background(), mockSwarmClientSimple())
ctx := access.FakeContextWithOutgoingIAPAuth(context.Background(), fakeIAP())
var want []*protos.Instance
for i := 0; i < 3; i++ {
want = append(want, &protos.Instance{
GomoteId: mustCreateSwarmingInstance(t, client, fakeIAP()),
BuilderType: "gotip-linux-amd64-boringcrypto",
})
}
mustCreateSwarmingInstance(t, client, fakeIAPWithUser("user-x", "uuid-user-x"))
response, err := client.ListInstances(ctx, &protos.ListInstancesRequest{})
if err != nil {
t.Fatalf("client.ListInstances = nil, %s; want no error", err)
}
got := response.GetInstances()
if diff := cmp.Diff(want, got, protocmp.Transform(), protocmp.IgnoreFields(&protos.Instance{}, "expires", "host_type")); diff != "" {
t.Errorf("ListInstances() mismatch (-want, +got):\n%s", diff)
}
}

func TestStartNewSwarmingTask(t *testing.T) {
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stdout)
Expand Down

0 comments on commit cabf17f

Please sign in to comment.