test

package
v0.0.0-...-516ef81 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultLiquidServiceInfo

func DefaultLiquidServiceInfo(displayName string) liquid.ServiceInfo

DefaultLiquidServiceInfo builds the default ServiceInfo that most mock liquids use. It defines two resources:

  • "capacity" is measured in bytes, AZ-aware and reports capacity.
  • "things" is counted, not AZ-aware and does not report capacity.

func GenerateDummyCommitmentUUID

func GenerateDummyCommitmentUUID(idx uint64) liquid.CommitmentUUID

GenerateDummyCommitmentUUID generates a deterministic UUID from the given ID.

func GenerateDummyTransferToken

func GenerateDummyTransferToken(idx uint64) string

GenerateDummyTransferToken generates a token string from an ID for testing.

func NoJitter

func NoJitter(d time.Duration) time.Duration

NoJitter replaces test.AddJitter in unit tests, to provide deterministic behavior.

func RemoveCommentsFromJSON

func RemoveCommentsFromJSON(jsonStr string) string

RemoveCommentsFromJSON removes C-style comments from JSON literals. It is intended only for use with JSON literals that appear in test code. Its implementation is very simple and not intended for use with untrusted inputs.

func WithEmptyRateRecordsAsNeeded

func WithEmptyRateRecordsAsNeeded(params *setupParams)

WithEmptyRateRecordsAsNeeded is a SetupOption that populates the DB with empty records for project_rates.

It relies on the services and rates to exist! (e.g. use WithPersistedServiceInfo)

It also relies on domains and projects to exist! (e.g. use WithInitialDiscovery)

func WithEmptyResourceRecordsAsNeeded

func WithEmptyResourceRecordsAsNeeded(params *setupParams)

WithEmptyResourceRecordsAsNeeded is a SetupOption that populates the DB with empty records for project_resources and project_az_resources.

It relies on the services, resources and az_resources to exist! (e.g. use WithPersistedServiceInfo)

It also relies on domains and projects to exist! (e.g. use WithInitialDiscovery)

func WithInitialDiscovery

func WithInitialDiscovery(params *setupParams)

WithInitialDiscovery is a SetupOption that populates the DB with records for domains, projects and project_services using the data in the config section "discovery.static_config", by running the ScanDomainsAndProjectsJob.

func WithLiquidConnections

func WithLiquidConnections(params *setupParams)

WithLiquidConnections is a SetupOption that sets up the Cluster the same way as the limes-collect task would do. This means a) the LiquidConnections are filled and b) the respective `services`, `resources` and `az_resources` records are persisted to the database.

Types

type MockLiquidClient

type MockLiquidClient struct {
	ServiceInfo              MockLiquidSlot[liquid.ServiceInfo]
	CapacityReport           MockLiquidSlot[liquid.ServiceCapacityReport]
	UsageReport              MockLiquidSlot[liquid.ServiceUsageReport]
	CommitmentChangeResponse MockLiquidSlot[liquid.CommitmentChangeResponse]

	LastCommitmentChangeRequest liquid.CommitmentChangeRequest
	// contains filtered or unexported fields
}

MockLiquidClient implements the LiquidClient interface.

func (*MockLiquidClient) ChangeCommitments

ChangeCommitments implements the core.LiquidClient interface.

func (*MockLiquidClient) GetCapacityReport

func (l *MockLiquidClient) GetCapacityReport(ctx context.Context, req liquid.ServiceCapacityRequest) (result liquid.ServiceCapacityReport, err error)

GetCapacityReport implements the core.LiquidClient interface.

func (*MockLiquidClient) GetInfo

func (l *MockLiquidClient) GetInfo(ctx context.Context) (result liquid.ServiceInfo, err error)

GetInfo implements the core.LiquidClient interface.

func (*MockLiquidClient) GetUsageReport

func (l *MockLiquidClient) GetUsageReport(ctx context.Context, projectUUID string, req liquid.ServiceUsageRequest) (result liquid.ServiceUsageReport, err error)

GetUsageReport implements the core.LiquidClient interface.

func (*MockLiquidClient) IncrementCapacityReportInfoVersion

func (l *MockLiquidClient) IncrementCapacityReportInfoVersion()

IncrementCapacityReportInfoVersion increments the CapacityReport InfoVersion by 1.

func (*MockLiquidClient) IncrementServiceInfoVersion

func (l *MockLiquidClient) IncrementServiceInfoVersion()

IncrementServiceInfoVersion increments the ServiceInfo version number by 1.

func (*MockLiquidClient) IncrementUsageReportInfoVersion

func (l *MockLiquidClient) IncrementUsageReportInfoVersion()

IncrementUsageReportInfoVersion increments the UsageReport InfoVersion by 1.

func (*MockLiquidClient) PutQuota

func (l *MockLiquidClient) PutQuota(ctx context.Context, projectUUID string, req liquid.ServiceQuotaRequest) (err error)

PutQuota implements the core.LiquidClient interface.

func (*MockLiquidClient) SetQuotaError

func (l *MockLiquidClient) SetQuotaError(err error)

SetQuotaError sets an error to be returned by PutQuota, or clears it if nil is given.

type MockLiquidSlot

type MockLiquidSlot[T cloneableLiquidData[T]] struct {
	// contains filtered or unexported fields
}

MockLiquidSlot contains a prepared response (and/or error) to a LIQUID function call.

func (*MockLiquidSlot[T]) Modify

func (s *MockLiquidSlot[T]) Modify(action func(*T))

Modify changes the held data in-place by executing the provided callback. The data instance will be cloned after the callback to ensure that the test function cannot smuggle live references to the data.

func (*MockLiquidSlot[T]) Set

func (s *MockLiquidSlot[T]) Set(data T)

Set replaces the held data.

func (*MockLiquidSlot[T]) SetError

func (s *MockLiquidSlot[T]) SetError(err error)

SetError sets an error to return instead of the data, or clears it if nil is given.

type PolicyEnforcer

type PolicyEnforcer struct {
	// flags by scope
	AllowCluster bool
	AllowDomain  bool
	AllowProject bool
	// flags by action
	AllowView         bool
	AllowEdit         bool
	AllowEditMaxQuota bool
	AllowUncommit     bool
	// match by request attribute
	RejectServiceType string
}

PolicyEnforcer is a gopherpolicy.Enforcer implementation for API tests.

func (*PolicyEnforcer) Enforce

func (e *PolicyEnforcer) Enforce(rule string, ctx policy.Context) bool

Enforce implements the gopherpolicy.Enforcer interface.

type Setup

type Setup struct {
	// fields that are always set
	Ctx      context.Context //nolint:containedctx // only used in tests
	DB       *gorp.DbMap
	Cluster  *core.Cluster
	Clock    *mock.Clock
	Registry *prometheus.Registry

	TokenValidator             *mock.Validator[*PolicyEnforcer]
	Auditor                    *audittools.MockAuditor
	LiquidClients              map[db.ServiceType]*MockLiquidClient
	Handler                    httptest.Handler
	CurrentProjectCommitmentID *uint64
	CurrentTransferTokenNumber *uint64
	Collector                  *collector.Collector
	// contains filtered or unexported fields
}

Setup contains all the pieces that are needed for most tests.

func NewSetup

func NewSetup(t *testing.T, opts ...SetupOption) Setup

NewSetup prepares most or all pieces of Limes for a test.

func (Setup) GetAZResourceID

func (s Setup) GetAZResourceID(srvType db.ServiceType, resName liquid.ResourceName, az limes.AvailabilityZone) (result db.AZResourceID)

GetAZResourceID is a helper function for finding the ID of a db.AZResource record.

func (Setup) GetProjectID

func (s Setup) GetProjectID(name string) (result db.ProjectID)

GetProjectID is a helper function for finding the ID of a db.Project record.

func (Setup) GetProjectUUID

func (s Setup) GetProjectUUID(name string) (result liquid.ProjectUUID)

GetProjectUUID is a helper function for finding the UUID of a db.Project record.

func (Setup) GetRateID

func (s Setup) GetRateID(srvType db.ServiceType, rateName liquid.RateName) (result db.RateID)

GetRateID is a helper function for finding the ID of a db.Rate record.

func (Setup) GetResourceID

func (s Setup) GetResourceID(srvType db.ServiceType, resName liquid.ResourceName) (result db.ResourceID)

GetResourceID is a helper function for finding the ID of a db.Resource record.

func (Setup) GetServiceID

func (s Setup) GetServiceID(srvType db.ServiceType) (result db.ServiceID)

GetServiceID is a helper function for finding the ID of a db.Service record.

func (Setup) MustDBExec

func (s Setup) MustDBExec(query string, args ...any)

MustDBExec is a shorthand for s.DB.Exec() + t.Fatal() on error.

func (Setup) MustDBInsert

func (s Setup) MustDBInsert(pointerToRecord any)

MustDBInsert is a shorthand for s.DB.Insert() + t.Fatal() on error.

func (Setup) UpdateMockUserIdentity

func (s Setup) UpdateMockUserIdentity(updates map[string]string)

UpdateMockUserIdentity updates the given key-value pairs in the mock user identity, which are then handed to the TokenValidator.

type SetupOption

type SetupOption func(*setupParams)

SetupOption is an option that can be given to NewSetup().

func WithAPIMiddleware

func WithAPIMiddleware(mw func(http.Handler) http.Handler) SetupOption

WithAPIMiddleware is a SetupOption that attaches a custom middleware to the HTTP handler providing the Limes API within the test.

func WithConfig

func WithConfig(jsonStr string) SetupOption

WithConfig is a SetupOption that initializes the test cluster from a configuration provided as JSON. This option is effectively required, as an empty cluster configuration is not allowed.

func WithMockLiquidClient

func WithMockLiquidClient(serviceType db.ServiceType, serviceInfo liquid.ServiceInfo) SetupOption

WithMockLiquidClient is a SetupOption that adds a MockLiquidClient to this test. This option must be provided once for every service type in the config.

func WithPersistedServiceInfo

func WithPersistedServiceInfo(st db.ServiceType, si liquid.ServiceInfo) SetupOption

WithPersistedServiceInfo is a SetupOption that fills ServiceInfo into the DB before setting up the Cluster instance. This is used to test how Cluster.Connect() reacts to preexisting metadata in the DB.

Most tests will want to use EITHER this OR test.WithLiquidConnections(). Either method will fill the `services`, `resources` and `az_resources` tables.

Jump to

Keyboard shortcuts

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