api

package
v1.12.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package api provides integration test utilities for the Compute API.

Separate Client Implementation

This package intentionally maintains a separate HTTP client implementation (APIClient) instead of using the auto-generated OpenAPI client. This design choice provides several benefits:

1. **API Contract Validation**: Having an independent client implementation serves as a form of triangulation on API correctness. Any legitimate change to the OpenAPI specification must have a compensating change in this client, making API evolution more explicit and reviewable. Conversely, if a change to the API doesn't require updates here, it may indicate a problem with the change.

2. **Test-Specific Features**: The custom client includes features tailored for integration testing:

  • W3C trace context propagation for request correlation
  • Detailed error logging with trace IDs for debugging
  • Flexible authentication token management
  • Custom timeout and retry logic
  • Direct access to HTTP status codes and response bodies

Future Improvements

* The test scaffolding in this package could be factored out into a central location and reused across multiple integration and sub-integration test suites. This would reduce the cost of maintaining this code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateClusterWithCleanup

func CreateClusterWithCleanup(client *APIClient, ctx context.Context, config *TestConfig, payload openapi.ComputeClusterWrite) (openapi.ComputeClusterRead, string)

CreateClusterWithCleanup creates a cluster, waits for provisioning, and schedules automatic cleanup. Accepts a typed struct for type safety (or use BuildTyped() from the builder).

func ExtractMachineID

func ExtractMachineID(cluster openapi.ComputeClusterRead) string

ExtractMachineID extracts the first machine ID from a cluster's first workload pool.

func ExtractMachineIDsFromPool

func ExtractMachineIDsFromPool(cluster openapi.ComputeClusterRead, poolName string) []string

ExtractMachineIDsFromPool extracts machine IDs from a specific workload pool by name.

func FindPoolByName added in v1.12.0

FindPoolByName finds a workload pool by name in the cluster spec.

func GenerateTestID

func GenerateTestID() string

func GetMachineStatus

func GetMachineStatus(cluster openapi.ComputeClusterRead, machineID string) string

GetMachineStatus retrieves the current status of a machine by ID from a cluster. Searches all workload pools for the machine.

func VerifyClusterPresence

func VerifyClusterPresence(clusters []openapi.ComputeClusterRead, expectedClusterIDs []string)

VerifyClusterPresence verifies that clusters are present in the list.

func VerifyDefaultFirewallRule added in v1.12.0

func VerifyDefaultFirewallRule(cluster *openapi.ComputeClusterRead, poolName string)

VerifyDefaultFirewallRule verifies a pool has the default SSH firewall rule.

func VerifyMachineEvicted

func VerifyMachineEvicted(cluster openapi.ComputeClusterRead, poolName, evictedMachineID string, expectedReplicas int) bool

VerifyMachineEvicted checks that a machine ID is not present in a specific pool and the pool has the expected replica count.

func VerifyMultiplePoolsReplicas added in v1.12.0

func VerifyMultiplePoolsReplicas(cluster *openapi.ComputeClusterRead, poolReplicas map[string]int)

VerifyMultiplePoolsReplicas verifies multiple pools have their expected replicas.

func VerifyPoolFirewallRules added in v1.12.0

func VerifyPoolFirewallRules(cluster *openapi.ComputeClusterRead, poolName string, expectedRuleCount int)

VerifyPoolFirewallRules verifies a pool has the expected firewall configuration.

func VerifyPoolReplicas added in v1.12.0

func VerifyPoolReplicas(cluster *openapi.ComputeClusterRead, poolName string, expectedReplicas int)

VerifyPoolReplicas verifies a pool has the expected number of replicas in spec.

func VerifyProjectPresence

func VerifyProjectPresence(clusters []openapi.ComputeClusterRead, expectedProjectIDs []string)

VerifyProjectPresence verifies that projects are present in the cluster list.

func VerifyWorkloadPoolUpdate

func VerifyWorkloadPoolUpdate(cluster openapi.ComputeClusterRead, expectedMinPools int)

VerifyWorkloadPoolUpdate verifies that a cluster's workload pools were updated correctly.

func WaitForMachineStatus

func WaitForMachineStatus(client *APIClient, ctx context.Context, config *TestConfig, clusterID, machineID, expectedStatus string)

WaitForMachineStatus waits for a specific machine to reach the expected status.

func WaitForMachinesAvailable

func WaitForMachinesAvailable(client *APIClient, ctx context.Context, config *TestConfig, clusterID string) string

WaitForMachinesAvailable waits for machines to be available in the cluster and returns the first machine ID.

func WaitForPoolMachinesActive added in v1.12.0

func WaitForPoolMachinesActive(client *APIClient, ctx context.Context, config *TestConfig, clusterID, poolName string, expectedCount int)

WaitForPoolMachinesActive waits for all machines in a pool to become active.

func WaitForPoolReplicas added in v1.12.0

func WaitForPoolReplicas(client *APIClient, ctx context.Context, config *TestConfig, clusterID, poolName string, expectedReplicas int)

WaitForPoolReplicas waits for a workload pool to have the expected number of replicas.

Types

type APIClient

type APIClient struct {
	// contains filtered or unexported fields
}

func NewAPIClient

func NewAPIClient(baseURL string) (*APIClient, error)

func NewAPIClientWithConfig

func NewAPIClientWithConfig(config *TestConfig) *APIClient

func (*APIClient) CheckClusterQuota

func (c *APIClient) CheckClusterQuota(ctx context.Context, orgID string) error

CheckClusterQuota checks if there is sufficient cluster quota available.

func (*APIClient) CreateCluster

func (c *APIClient) CreateCluster(ctx context.Context, orgID, projectID string, body openapi.ComputeClusterWrite) (openapi.ComputeClusterRead, error)

CreateCluster creates a new compute cluster. Accepts a typed struct for type safety, then converts to JSON for the request.

func (*APIClient) DeleteCluster

func (c *APIClient) DeleteCluster(ctx context.Context, orgID, projectID, clusterID string) error

func (*APIClient) EvictMachines

func (c *APIClient) EvictMachines(ctx context.Context, orgID, projectID, clusterID string, machineIDs []string) error

EvictMachines evicts specified machines from a cluster. Uses typed struct for type safety, then converts to JSON for the request.

func (*APIClient) GetCluster

func (c *APIClient) GetCluster(ctx context.Context, orgID, projectID, clusterID string) (openapi.ComputeClusterRead, error)

GetCluster retrieves a specific cluster. Im using this to poll with eventually to wait for the cluster to be provisioned.

func (*APIClient) HardRebootMachine

func (c *APIClient) HardRebootMachine(ctx context.Context, orgID, projectID, clusterID, machineID string) error

HardRebootMachine performs a hard reboot of a machine.

func (*APIClient) ListClusters

func (c *APIClient) ListClusters(ctx context.Context, orgID, projectID string) ([]openapi.ComputeClusterRead, error)

ListClusters lists all clusters for a project.

func (*APIClient) ListFlavors

func (c *APIClient) ListFlavors(ctx context.Context, orgID, regionID string) ([]map[string]interface{}, error)

func (*APIClient) ListImages

func (c *APIClient) ListImages(ctx context.Context, orgID, regionID string) ([]map[string]interface{}, error)

func (*APIClient) ListOrganizationClusters

func (c *APIClient) ListOrganizationClusters(ctx context.Context, orgID string) ([]openapi.ComputeClusterRead, error)

ListOrganizationClusters lists all clusters for an organization across all projects.

func (*APIClient) ListRegions

func (c *APIClient) ListRegions(ctx context.Context, orgID string) ([]map[string]interface{}, error)

func (*APIClient) SetAuthToken

func (c *APIClient) SetAuthToken(token string)

func (*APIClient) SoftRebootMachine

func (c *APIClient) SoftRebootMachine(ctx context.Context, orgID, projectID, clusterID, machineID string) error

SoftRebootMachine performs a graceful reboot of a machine.

func (*APIClient) StartMachine

func (c *APIClient) StartMachine(ctx context.Context, orgID, projectID, clusterID, machineID string) error

StartMachine starts a stopped machine.

func (*APIClient) StopMachine

func (c *APIClient) StopMachine(ctx context.Context, orgID, projectID, clusterID, machineID string) error

StopMachine stops a running machine.

func (*APIClient) UpdateCluster

func (c *APIClient) UpdateCluster(ctx context.Context, orgID, projectID, clusterID string, body openapi.ComputeClusterWrite) error

UpdateCluster updates an existing cluster. Accepts a typed struct for type safety, then converts to JSON for the request.

type ClusterInfo

type ClusterInfo struct {
	Cluster   openapi.ComputeClusterRead
	ClusterID string
	ProjectID string
}

ClusterInfo holds cluster metadata and project information.

type ClusterPayloadBuilder

type ClusterPayloadBuilder struct {
	// contains filtered or unexported fields
}

ClusterPayloadBuilder builds cluster payloads for testing using type-safe OpenAPI structs.

func NewClusterPayload

func NewClusterPayload() *ClusterPayloadBuilder

NewClusterPayload creates a new cluster payload builder with defaults from config.

func (*ClusterPayloadBuilder) Build

func (b *ClusterPayloadBuilder) Build() map[string]interface{}

Build returns the cluster as a map for JSON marshaling (required for API client).

func (*ClusterPayloadBuilder) BuildTyped

BuildTyped returns the typed cluster struct directly.

func (*ClusterPayloadBuilder) ClearWorkloadPools added in v1.12.0

func (b *ClusterPayloadBuilder) ClearWorkloadPools() *ClusterPayloadBuilder

ClearWorkloadPools removes all default workload pools to start fresh.

func (*ClusterPayloadBuilder) WithDescription

func (b *ClusterPayloadBuilder) WithDescription(desc string) *ClusterPayloadBuilder

WithDescription sets the cluster description.

func (*ClusterPayloadBuilder) WithFlavorID

func (b *ClusterPayloadBuilder) WithFlavorID(flavorID string) *ClusterPayloadBuilder

WithFlavorID sets the flavor ID for all workload pools.

func (*ClusterPayloadBuilder) WithImageID

func (b *ClusterPayloadBuilder) WithImageID(imageID string) *ClusterPayloadBuilder

WithImageID sets the image ID for all workload pools.

func (*ClusterPayloadBuilder) WithName

WithName sets the cluster name.

func (*ClusterPayloadBuilder) WithProjectID

func (b *ClusterPayloadBuilder) WithProjectID(projectID string) *ClusterPayloadBuilder

WithProjectID overrides the default project ID for multi-project testing.

func (*ClusterPayloadBuilder) WithRegionID

func (b *ClusterPayloadBuilder) WithRegionID(regionID string) *ClusterPayloadBuilder

WithRegionID sets the region ID.

func (*ClusterPayloadBuilder) WithWorkloadPool

func (b *ClusterPayloadBuilder) WithWorkloadPool(name, flavorID, imageID string, replicas int) *ClusterPayloadBuilder

WithWorkloadPool adds a workload pool configuration.

type ClusterUpdateFixture

type ClusterUpdateFixture struct {
	Cluster          openapi.ComputeClusterRead
	ClusterID        string
	OriginalReplicas int
}

ClusterUpdateFixture represents a cluster setup for update testing.

func CreateClusterUpdateFixture

func CreateClusterUpdateFixture(client *APIClient, ctx context.Context, config *TestConfig, clusterName string) *ClusterUpdateFixture

CreateClusterUpdateFixture creates a cluster specifically for update testing.

func (*ClusterUpdateFixture) CreateUpdatePayload

func (f *ClusterUpdateFixture) CreateUpdatePayload(config *TestConfig, newReplicas int) openapi.ComputeClusterWrite

CreateUpdatePayload creates a cluster update payload with modified workload pools. Returns typed struct for type safety.

type Endpoints

type Endpoints struct{}

Endpoints contains all API endpoint patterns.

func NewEndpoints

func NewEndpoints() *Endpoints

NewEndpoints creates a new Endpoints instance.

func (*Endpoints) CreateCluster

func (e *Endpoints) CreateCluster(orgID, projectID string) string

func (*Endpoints) DeleteCluster

func (e *Endpoints) DeleteCluster(orgID, projectID, clusterID string) string

func (*Endpoints) EvictMachines

func (e *Endpoints) EvictMachines(orgID, projectID, clusterID string) string

func (*Endpoints) GetCluster

func (e *Endpoints) GetCluster(orgID, projectID, clusterID string) string

func (*Endpoints) HardRebootMachine

func (e *Endpoints) HardRebootMachine(orgID, projectID, clusterID, machineID string) string

func (*Endpoints) HealthCheck

func (e *Endpoints) HealthCheck() string

Health and metadata endpoints.

func (*Endpoints) ListClusters

func (e *Endpoints) ListClusters(orgID, projectID string) string

Cluster management endpoints.

func (*Endpoints) ListFlavors

func (e *Endpoints) ListFlavors(orgID, regionID string) string

func (*Endpoints) ListImages

func (e *Endpoints) ListImages(orgID, regionID string) string

func (*Endpoints) ListOrganizationClusters

func (e *Endpoints) ListOrganizationClusters(orgID string) string

func (*Endpoints) ListRegions

func (e *Endpoints) ListRegions(orgID string) string

Discovery endpoints.

func (*Endpoints) OpenAPISpec

func (e *Endpoints) OpenAPISpec() string

func (*Endpoints) SoftRebootMachine

func (e *Endpoints) SoftRebootMachine(orgID, projectID, clusterID, machineID string) string

func (*Endpoints) StartMachine

func (e *Endpoints) StartMachine(orgID, projectID, clusterID, machineID string) string

Machine operation endpoints.

func (*Endpoints) StopMachine

func (e *Endpoints) StopMachine(orgID, projectID, clusterID, machineID string) string

func (*Endpoints) UpdateCluster

func (e *Endpoints) UpdateCluster(orgID, projectID, clusterID string) string

func (*Endpoints) Version

func (e *Endpoints) Version() string

type MultiProjectClusterFixture

type MultiProjectClusterFixture struct {
	Clusters []ClusterInfo
	Projects []string
}

MultiProjectClusterFixture represents clusters across multiple projects for testing.

func CreateMultiProjectClusterFixture

func CreateMultiProjectClusterFixture(client *APIClient, ctx context.Context, config *TestConfig, projectIDs []string) *MultiProjectClusterFixture

CreateMultiProjectClusterFixture creates clusters in the specified projects for testing.

type QuotaInfo

type QuotaInfo struct {
	Kind        string `json:"kind"`
	DisplayName string `json:"displayName"`
	Description string `json:"description"`
	Quantity    int    `json:"quantity"`
	Used        int    `json:"used"`
	Committed   int    `json:"committed"`
	Reserved    int    `json:"reserved"`
	Default     int    `json:"default"`
	Free        int    `json:"free"`
}

QuotaInfo represents quota information for a resource.

type QuotaResponse

type QuotaResponse struct {
	Quotas []QuotaInfo `json:"quotas"`
}

QuotaResponse represents the quota API response structure.

type ResponseHandlerConfig

type ResponseHandlerConfig struct {
	ResourceType   string
	ResourceID     string
	ResourceIDType string
	AllowForbidden bool
	AllowNotFound  bool
}

ResponseHandlerConfig configures how different status codes should be handled.

type TestConfig

type TestConfig struct {
	BaseURL            string
	IdentityBaseURL    string
	AuthToken          string
	RequestTimeout     time.Duration
	TestTimeout        time.Duration
	OrgID              string
	ProjectID          string
	SecondaryProjectID string
	RegionID           string
	SecondaryRegionID  string
	FlavorID           string
	ImageID            string
	SkipIntegration    bool
	DebugLogging       bool
	LogRequests        bool
	LogResponses       bool
}

func LoadTestConfig

func LoadTestConfig() (*TestConfig, error)

LoadTestConfig loads configuration from environment variables and .env files using viper. Returns an error if required configuration values are missing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL