sandbox

package
v1.3.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2022 License: Apache-2.0 Imports: 43 Imported by: 0

README

Intro

The main goal of package sandbox is a simplification setup of NSMgr infrastructure for unit testing. For example, if we need to check any chain element with remote NSM use-case then we need to configure and start the next GRPC servers:

NSMgr1
NSMgr2
Forwarder1
Forwarder2
Registry

And after that add needed test steps and asserts. So to reduce test lines was developed sandbox package.

Uses

Let's consider the most common use-cases of package sandbox.

Setup single node NSM

Problem: setup NSMgr and Forwarder to checking new endpoint chain element.
Solution:

	...
	localDomain := sandbox.NewBuilder(ctx, t).
		SetNodesCount(1).
		SetNSMgrProxySupplier(nil).
		SetRegistryProxySupplier(nil).
		Build()
	localDomain.Nodes[0].NewEndpoint(ctx, &registry.Endpoint{...}, sandbox.GenerateTestToken, ...myNewEndpointChain)
	client := localDomain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, ...clientChain)
	...

Problem: setup my external NSMgr and Forwarder to checking my external NSMgr.
Solution:

	...
	localDomain := sandbox.NewBuilder(ctx, t).
		SetNodesCount(1).
		SetNSMgrProxySupplier(nil).
		SetRegistryProxySupplier(nil).
		SetNSMgrSupplier(myExternalNSMgrFunc)
		Build()
	defer localDomain.Cleanup()
	...

Problem: setup my NSMgr and new Forwarder to checking my Forwarder chain.
Solution:

	...
	localDomain := sandbox.NewBuilder(ctx, t).
		SetNodesCount(1).
		SetNSMgrProxySupplier(nil).
		SetRegistryProxySupplier(nil).
		SetNodeSetup(func(ctx context.Context, node *sandbox.Node, _ int) {
			node.NewNSMgr(ctx, sandbox.Name("nsmgr"), nil, sandbox.GenerateTestToken, nsmgr.NewServer)
			node.NewForwarder(ctx, &registry.Endpoint{...}, sandbox.GenerateTestToken, ...myNewForwarderChain)
		}).
		Build()
	defer localDomain.Cleanup()
	...

Setup remote NSM infrastructure for remote use-case

Problem: setup NSMgr and Forwarder to checking remote use-case.
Solution:

	...
	localDomain := sandbox.NewBuilder(ctx, t).
		SetNodesCount(2).
		SetNSMgrProxySupplier(nil).
		SetRegistryProxySupplier(nil).
		Build()
	defer localDomain.Cleanup()
	localDomain.Nodes[1].NewEndpoint(ctx, &registry.Endpoint{...}, sandbox.GenerateTestToken, ...remoteEndpointChain)
	...

Problem: setup NSMgr, set of Forwarders and set of Endpoints on each node to checking complex scenarios.
Solution:

	...
	localDomain := sandbox.NewBuilder(ctx, t).
		SetNodesCount(nodesCount).
		SetNSMgrProxySupplier(nil).
		SetRegistryProxySupplier(nil).
		SetNodeSetup(func(ctx context.Context, node *sandbox.Node) {
			node.NewForwarder(ctx, &registry.Endpoint{...}, sandbox.GenerateTestToken, ...forwarderChain)
			node.NewEndpoint(ctx, &registry.Endpoint{...}, sandbox.GenerateTestToken, ...endpointChain)
		}).
		Build()
	defer localDomain.Cleanup()
	...

Setup only local registry

Problem: setup registry and to check API.
Solution:

	...
	localDomain := sandbox.NewBuilder(ctx, t).
		SetNodesCount(0).
		SetNSMgrProxySupplier(nil).
		SetRegistryProxySupplier(nil).
		Build()
	defer localDomain.Cleanup()
	registryURL := localDomain.Registry.URL
	// Create new registry client by registryURL
	...

Setup remote NSM infrastructure for interdomain use-case

Problem: setup NSMgrs, Forwarders, Registries to checking interdomain use-case via DNS.
Solution:

	...
	fakeServer := new(sandbox.FakeDNSResolver)
	domain1 := sandbox.NewBuilder(ctx, t).
		SetNodesCount(1).
		SetDNSDomainName("domain1").
		SetDNSResolver(fakeServer).
		Build()
	defer domain1.Cleanup()
	fakeServer.Register("domain1", domain1.Registry.URL)
	domain2 := sandbox.NewBuilder(ctx, t).
		SetNodesCount(1).
		SetDNSDomainName("domain2").
		SetDNSResolver(fakeServer).
		Build()
	defer domain1.Cleanup()
	fakeServer.Register("domain2", domain2.Registry.URL)
	...

Documentation

Overview

Package sandbox provides API for testing NSM chains such as Forwarder, NSC, NSMgrs, Registries, NSE.

Index

Constants

View Source
const (
	// DialTimeout is a default dial timeout for the sandbox tests
	DialTimeout = 2 * time.Second
)

Variables

This section is empty.

Functions

func CheckURLFree added in v1.1.0

func CheckURLFree(u *url.URL) bool

CheckURLFree returns is given url is free for Listen

func CloneURL added in v1.1.0

func CloneURL(u *url.URL) *url.URL

CloneURL clones given url

func DialOptions added in v1.1.0

func DialOptions(options ...DialOption) []grpc.DialOption

DialOptions is a helper method for building []grpc.DialOption for testing

func GenerateExpiringToken

func GenerateExpiringToken(duration time.Duration) token.GeneratorFunc

GenerateExpiringToken returns a token generator with the specified expiration duration.

func GenerateTestToken

func GenerateTestToken(_ credentials.AuthInfo) (tokenValue string, expireTime time.Time, err error)

GenerateTestToken generates test token

func SetupDefaultNode added in v1.1.0

func SetupDefaultNode(ctx context.Context, node *Node, supplyNSMgr SupplyNSMgrFunc)

SetupDefaultNode setups NSMgr and default Forwarder on the given node

func UniqueName added in v1.1.0

func UniqueName(prefix string) string

UniqueName creates unique name with the given prefix

func WithInsecureRPCCredentials

func WithInsecureRPCCredentials() grpc.DialOption

WithInsecureRPCCredentials makes passed call option with credentials.PerRPCCredentials insecure.

func WithInsecureStreamRPCCredentials

func WithInsecureStreamRPCCredentials() grpc.DialOption

WithInsecureStreamRPCCredentials makes passed call option with credentials.PerRPCCredentials insecure.

Types

type Builder

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

Builder implements builder pattern for building NSM Domain

func NewBuilder

func NewBuilder(ctx context.Context, t *testing.T) *Builder

NewBuilder creates new SandboxBuilder

func (*Builder) Build

func (b *Builder) Build() *Domain

Build builds Domain and Supplier

func (*Builder) SetDNSDomainName

func (b *Builder) SetDNSDomainName(name string) *Builder

SetDNSDomainName sets DNS domain name for the building NSM domain

func (*Builder) SetDNSResolver

func (b *Builder) SetDNSResolver(d dnsresolve.Resolver) *Builder

SetDNSResolver sets DNS resolver for proxy registries

func (*Builder) SetNSMgrProxySupplier

func (b *Builder) SetNSMgrProxySupplier(f SupplyNSMgrProxyFunc) *Builder

SetNSMgrProxySupplier replaces default nsmgr-proxy supplier to custom function

func (*Builder) SetNSMgrSupplier

func (b *Builder) SetNSMgrSupplier(f SupplyNSMgrFunc) *Builder

SetNSMgrSupplier replaces default nsmgr supplier to custom function

func (*Builder) SetNodeSetup

func (b *Builder) SetNodeSetup(f SetupNodeFunc) *Builder

SetNodeSetup replaces default node setup to custom function

func (*Builder) SetNodesCount

func (b *Builder) SetNodesCount(nodesCount int) *Builder

SetNodesCount sets nodes count

func (*Builder) SetRegistryExpiryDuration

func (b *Builder) SetRegistryExpiryDuration(registryExpiryDuration time.Duration) *Builder

SetRegistryExpiryDuration replaces registry expiry duration to custom

func (*Builder) SetRegistryProxySupplier

func (b *Builder) SetRegistryProxySupplier(f SupplyRegistryProxyFunc) *Builder

SetRegistryProxySupplier replaces default memory registry supplier to custom function

func (*Builder) SetRegistrySupplier

func (b *Builder) SetRegistrySupplier(f SupplyRegistryFunc) *Builder

SetRegistrySupplier replaces default memory registry supplier to custom function

func (*Builder) SetTokenGenerateFunc

func (b *Builder) SetTokenGenerateFunc(f token.GeneratorFunc) *Builder

SetTokenGenerateFunc sets function for the token generation

func (*Builder) UseUnixSockets

func (b *Builder) UseUnixSockets() *Builder

UseUnixSockets sets 1 node and mark it to use unix socket to listen on.

type DialOption added in v1.1.0

type DialOption func(o *dialOpts)

DialOption is an option pattern for DialOptions

func WithTokenGenerator added in v1.1.0

func WithTokenGenerator(tokenGenerator token.GeneratorFunc) DialOption

WithTokenGenerator sets tokenGenerator for DialOptions

type Domain

type Domain struct {
	Nodes         []*Node
	NSMgrProxy    *NSMgrEntry
	Registry      *RegistryEntry
	RegistryProxy *RegistryEntry

	DNSResolver dnsresolve.Resolver
	Name        string
	// contains filtered or unexported fields
}

Domain contains attached to domain nodes, registry

func (*Domain) NewNSRegistryClient added in v1.1.0

func (d *Domain) NewNSRegistryClient(ctx context.Context, generatorFunc token.GeneratorFunc) registryapi.NetworkServiceRegistryClient

NewNSRegistryClient creates new NS registry client for the domain

type EndpointEntry

type EndpointEntry struct {
	Name string
	URL  *url.URL

	endpoint.Endpoint
	registryapi.NetworkServiceEndpointRegistryClient
	// contains filtered or unexported fields
}

EndpointEntry is pair of endpoint.Endpoint and url.URL

func (EndpointEntry) Cancel added in v1.1.0

func (r EndpointEntry) Cancel()

func (EndpointEntry) Restart added in v1.1.0

func (r EndpointEntry) Restart()

type FakeDNSResolver

type FakeDNSResolver struct {
	sync.Mutex
	// contains filtered or unexported fields
}

FakeDNSResolver implements dnsresolve.Resolver interface and can be used for logic DNS testing

func (*FakeDNSResolver) AddSRVEntry

func (f *FakeDNSResolver) AddSRVEntry(name, service string, u *url.URL)

AddSRVEntry adds new DNS record by passed url.URL

func (*FakeDNSResolver) LookupIPAddr

func (f *FakeDNSResolver) LookupIPAddr(_ context.Context, host string) ([]net.IPAddr, error)

LookupIPAddr lookups IP address by host

func (*FakeDNSResolver) LookupSRV

func (f *FakeDNSResolver) LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, error)

LookupSRV lookups DNS SRV record

type NSMgrEntry

type NSMgrEntry struct {
	Name string
	URL  *url.URL

	nsmgr.Nsmgr
	// contains filtered or unexported fields
}

NSMgrEntry is pair of nsmgr.Nsmgr and url.URL

func (NSMgrEntry) Cancel added in v1.1.0

func (r NSMgrEntry) Cancel()

func (NSMgrEntry) Restart added in v1.1.0

func (r NSMgrEntry) Restart()

type Node

type Node struct {
	NSMgr      *NSMgrEntry
	Forwarders map[string]*EndpointEntry
	// contains filtered or unexported fields
}

Node is a NSMgr with Forwarder, NSE registry clients

func (*Node) NewClient

func (n *Node) NewClient(
	ctx context.Context,
	generatorFunc token.GeneratorFunc,
	additionalOpts ...client.Option,
) networkservice.NetworkServiceClient

NewClient starts a new client and connects it to the node NSMgr

func (*Node) NewEndpoint

func (n *Node) NewEndpoint(
	ctx context.Context,
	nse *registryapi.NetworkServiceEndpoint,
	generatorFunc token.GeneratorFunc,
	additionalFunctionality ...networkservice.NetworkServiceServer,
) *EndpointEntry

NewEndpoint starts a new endpoint and registers it on the node NSMgr

func (*Node) NewForwarder

func (n *Node) NewForwarder(
	ctx context.Context,
	nse *registryapi.NetworkServiceEndpoint,
	generatorFunc token.GeneratorFunc,
	additionalFunctionality ...networkservice.NetworkServiceServer,
) *EndpointEntry

NewForwarder starts a new forwarder and registers it on the node NSMgr

func (*Node) NewNSMgr added in v1.1.0

func (n *Node) NewNSMgr(
	ctx context.Context,
	name string,
	serveURL *url.URL,
	generatorFunc token.GeneratorFunc,
	supplyNSMgr SupplyNSMgrFunc,
) *NSMgrEntry

NewNSMgr creates a new NSMgr

type RegistryEntry

type RegistryEntry struct {
	URL *url.URL

	registry.Registry
	// contains filtered or unexported fields
}

RegistryEntry is pair of registry.Registry and url.URL

func (RegistryEntry) Cancel added in v1.1.0

func (r RegistryEntry) Cancel()

func (RegistryEntry) Restart added in v1.1.0

func (r RegistryEntry) Restart()

type SetupNodeFunc

type SetupNodeFunc func(ctx context.Context, node *Node, nodeNum int)

SetupNodeFunc setups each node on Builder.Build() stage

type SupplyNSMgrFunc

type SupplyNSMgrFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, options ...nsmgr.Option) nsmgr.Nsmgr

SupplyNSMgrFunc supplies NSMGR

type SupplyNSMgrProxyFunc

type SupplyNSMgrProxyFunc func(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator token.GeneratorFunc, options ...nsmgrproxy.Option) nsmgr.Nsmgr

SupplyNSMgrProxyFunc nsmgr proxy

type SupplyRegistryFunc

type SupplyRegistryFunc func(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...grpc.DialOption) registry.Registry

SupplyRegistryFunc supplies Registry

type SupplyRegistryProxyFunc

type SupplyRegistryProxyFunc func(ctx context.Context, dnsResolver dnsresolve.Resolver, options ...grpc.DialOption) registry.Registry

SupplyRegistryProxyFunc supplies registry proxy

Jump to

Keyboard shortcuts

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