mockapi

package
v0.0.0-...-46d7da7 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2018 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package mockapi supplies a fake Mixer server for use in testing. It should NOT be used outside of testing contexts.

Index

Constants

This section is empty.

Variables

View Source
var DefaultAmount = int64(1)

DefaultAmount is the default quota amount to use in testing (1).

View Source
var DefaultValidDuration = 5 * time.Second

DefaultValidDuration is the default duration to return for quota allocs in testing (1s).

View Source
var DefaultValidUseCount = int32(10000)

DefaultValidUseCount is the default number of valid uses to return for quota allocs for testing (1).

Functions

func ListenerAndPort

func ListenerAndPort() (net.Listener, int, error)

ListenerAndPort starts a listener on an available port and returns both the listener and the port on which it is listening.

func NewMixerServer

func NewMixerServer(impl mixerpb.MixerServer) *grpc.Server

NewMixerServer creates a new grpc.Server with the supplied implementation of the Mixer API.

Types

type AttributesHandler

type AttributesHandler interface {
	// Check will be called once per Mixer API Check() request.
	Check(attribute.Bag) mixerpb.CheckResponse_PreconditionResult

	// Quota will be called once per quota (with params) received in a Check()
	// request.
	Quota(attribute.Bag, QuotaArgs) (QuotaResponse, rpc.Status)

	// Report will be called once per set of attributes received in a Report()
	// request.
	Report(attribute.Bag) rpc.Status
}

AttributesHandler provides an interface for building custom testing behavior. AttributesHandlers are used by a test.AttributesServer to pass attribute bags to testing code for validation of proper transport using the Mixer API.

type AttributesServer

type AttributesServer struct {
	// GlobalDict controls the known global dictionary for attribute processing.
	GlobalDict map[string]int32

	// GenerateGRPCError instructs the server whether or not to fail-fast with
	// an error that will manifest as a GRPC error.
	GenerateGRPCError bool

	// Handler is what the server will call to simulate passing attribute bags
	// and method args within the Mixer server. It allows tests to gain access
	// to the attribute handling pipeline within Mixer and to set the response
	// details.
	Handler AttributesHandler
}

AttributesServer implements the Mixer API to send mutable attributes bags to a channel upon API requests. This can be used for tests that want to exercise the Mixer API and validate server handling of supplied attributes.

func NewAttributesServer

func NewAttributesServer(handler AttributesHandler) *AttributesServer

NewAttributesServer creates an AttributesServer. All channels are set to default length.

func (*AttributesServer) Check

Check sends a copy of the protocol buffers attributes wrapper for the preconditions check as well as for each quotas check to the CheckAttributes channel. It also builds a CheckResponse based on server fields. All channel sends timeout to prevent problematic tests from blocking indefinitely.

func (*AttributesServer) Report

Report iterates through the supplied attributes sets, applying the deltas appropriately, and sending the generated bags to the channel.

type ChannelsHandler

type ChannelsHandler struct {
	AttributesHandler

	// QuotaResponse controls the response returned as part of Quota() calls.
	QuotaResponse QuotaResponse

	// ReturnStatus controls the rpc.Status returned for Check(), Quota(), and
	// Report() calls.
	ReturnStatus rpc.Status

	// CheckAttributes is the channel on which the attribute bag generated by
	// the server for Check() requests is sent.
	CheckAttributes chan attribute.Bag

	// QuotaDispatches is the channel on which the quota information generated by
	// the server for Check() requests is sent.
	QuotaDispatches chan QuotaDispatchInfo

	// ReportAttributes is the channel on which the attribute bag generated by
	// the server for Report() requests is sent.
	ReportAttributes chan attribute.Bag
}

ChannelsHandler sends received attribute.Bags (and other) information on the configured channels (with a timeout).

func NewChannelsHandler

func NewChannelsHandler() *ChannelsHandler

NewChannelsHandler creates a ChannelsHandler with channels of default length and default values for ReturnStatuses and QuotaResponses.

func (*ChannelsHandler) Check

Check implements AttributesHandler interface.

func (*ChannelsHandler) Quota

Quota implements AttributesHandler interface.

func (*ChannelsHandler) Report

func (c *ChannelsHandler) Report(req attribute.Bag) rpc.Status

Report implements AttributesHandler interface.

type QuotaArgs

type QuotaArgs struct {
	// Unique ID for Quota operation.
	DeduplicationID string

	// The quota to allocate from.
	Quota string

	// The amount of quota to allocate.
	Amount int64

	// If true, allows a response to return less quota than requested. When
	// false, the exact requested amount is returned or 0 if not enough quota
	// was available.
	BestEffort bool
}

QuotaArgs mirrors aspect.QuotaArgs. It allows tests to understand how their inputs map into quota calls within Mixer to validate the proper requests are being generated. It is NOT a contract for how a real Mixer would generate or dispatch Quota calls.

type QuotaDispatchInfo

type QuotaDispatchInfo struct {
	Attributes attribute.Bag
	MethodArgs QuotaArgs
}

QuotaDispatchInfo contains both the attribute bag generated by the server for Quota dispatch, as well as the corresponding method arguments.

type QuotaResponse

type QuotaResponse struct {
	// The amount of time until which the returned quota expires, this is 0 for non-expiring quotas.
	Expiration time.Duration

	// The total amount of quota returned, may be less than requested.
	Amount int64

	// Referenced attributes for client
	Referenced *mixerpb.ReferencedAttributes
}

QuotaResponse provides information on the result of a Quota operation. It allows testing to target precise quota responses.

Jump to

Keyboard shortcuts

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