Skip to content

Commit

Permalink
Add GetSandbox()
Browse files Browse the repository at this point in the history
Signed-off-by: Raphaël Pinson <[email protected]>
  • Loading branch information
raphink committed Sep 30, 2024
1 parent ddf65e2 commit 156c07b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
25 changes: 25 additions & 0 deletions instruqt/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ type SandboxVar struct {
Value string // The value of the sandbox variable.
}

// sandboxQuery represents the GraphQL query structure for a single sandbox by its ID
type sandboxQuery struct {
Sandbox Sandbox `graphql:"sandbox(ID: $id)"`
}

// sandboxesQuery represents the GraphQL query structure for retrieving all sandboxes
// associated with a specific team.
type sandboxesQuery struct {
Expand All @@ -47,6 +52,7 @@ type Sandbox struct {
State string // The current state of the sandbox (e.g., "running", "stopped").
Track SandboxTrack // The track associated with the sandbox.
Invite TrackInvite // The invite details associated with the sandbox.
User User // The user running the sandbox.
}

// GetSandboxVariable retrieves a specific variable from a sandbox environment
Expand Down Expand Up @@ -80,6 +86,25 @@ func (c *Client) GetSandboxVariable(playID string, key string) (v string, err er
return q.GetSandboxVariable.Value, nil
}

// GetSandbox retrieves a sandbox by its ID.
//
// Returns:
// - Sandbox: The sandbox.
// - error: Any error encountered while retrieving the sandbox.
func (c *Client) GetSandbox(id string) (s Sandbox, err error) {
var q sandboxQuery
variables := map[string]interface{}{
"id": graphql.String(id),
"teamSlug": graphql.String(c.TeamSlug), // Pass teamSlug for User info
}

if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return s, err
}

return q.Sandbox, nil
}

// GetSandboxes retrieves all sandboxes associated with the team slug defined in the client.
//
// Returns:
Expand Down
40 changes: 40 additions & 0 deletions instruqt/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package instruqt

import (
"context"
"errors"
"testing"
"time"
Expand Down Expand Up @@ -71,6 +72,45 @@ func TestGetSandboxVariable_Error(t *testing.T) {
mockClient.AssertExpectations(t)
}

func TestGetSandbox(t *testing.T) {
// Create a mock GraphQL client
mockClient := new(MockGraphQLClient)
client := &Client{
GraphQLClient: mockClient,
Context: context.Background(),
TeamSlug: "isovalent", // Include a teamSlug in the client
}

// Define the expected sandbox response
expectedSandbox := Sandbox{
Last_Activity_At: time.Now(),
State: "active",
Track: SandboxTrack{
Id: "track-123",
Title: "Test Track",
},
Invite: TrackInvite{
Id: "invite-123",
},
}

// Set up the mock to return the expected sandbox data
mockClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
query := args.Get(1).(*sandboxQuery)
query.Sandbox = expectedSandbox
}).Return(nil)

// Call the GetSandbox method
sandbox, err := client.GetSandbox("sandbox-123")

// Validate the results
assert.NoError(t, err)
assert.Equal(t, expectedSandbox, sandbox)

// Ensure the mock expectations are met
mockClient.AssertExpectations(t)
}

func TestGetSandboxes(t *testing.T) {
mockClient := new(MockGraphQLClient)
client := &Client{
Expand Down

0 comments on commit 156c07b

Please sign in to comment.