Skip to content

Provide testing utilities and mock implementations #59

@coolguy1771

Description

@coolguy1771

Proposal

The library currently lacks built-in testing support, forcing users to implement their own mocks or manage complex infrastructure for integration tests. I propose adding a testutil package to provide a flexible MockClient and a TestServer helper.

Proposed Solution

The testutil package will focus on two areas:

  1. Unit Testing: A function-based MockClient that allows users to inject custom behavior per test case without implementing the entire interface manually.
  2. Integration Testing: A TestServer that wraps httptest.Server to simulate the scaleset API, allowing users to verify their full networking stack.

Mock Implementation

package testutil

import (
    "context"
    "github.com/actions/scaleset"
)

// MockClient provides a flexible mock for the listener.Client interface.
type MockClient struct {
    GetMessageFunc    func(ctx context.Context, lastID, cap int) (*scaleset.RunnerScaleSetMessage, error)
    DeleteMessageFunc func(ctx context.Context, messageID int) error
    SessionFunc       func() scaleset.RunnerScaleSetSession
    CloseFunc         func(ctx context.Context) error
}

func (m *MockClient) GetMessage(ctx context.Context, lastID, cap int) (*scaleset.RunnerScaleSetMessage, error) {
    if m.GetMessageFunc != nil {
        return m.GetMessageFunc(ctx, lastID, cap)
    }
    return nil, nil
}

// ... other interface methods implemented similarly ...

Integration Helper

// TestServer simulates the scaleset API for end-to-end testing.
type TestServer struct {
    URL    string
    Client *scaleset.Client
}

func NewTestServer(t *testing.T, options ...TestServerOption) *TestServer
func (s *TestServer) EnqueueMessage(msg *scaleset.RunnerScaleSetMessage)
func (s *TestServer) GetReceivedDeleteRequests() []int

Benefits

  • Reduced Boilerplate: Users don't need to redefine interfaces or mock logic in every downstream project.
  • Reliable Integration: The TestServer ensures that user-implemented scalers handle real HTTP interactions, including headers and status codes, correctly.
  • Consistency: Provides a standard way to test scaleset logic across different implementations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions