framework

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: Apache-2.0 Imports: 41 Imported by: 0

Documentation

Overview

Package framework provides integration test functionality for Hornet with a Docker network. It effectively abstracts away all complexity with creating a custom Docker network per test, discovering peers, waiting for them to autopeer and offers easy access to the peers' web API and logs.

Index

Constants

View Source
const (
	// The seed of the genesis wallet.
	GenesisSeed = "BCPCHOJNIRZDMNDFSEEBIBGFQRGZL9PRXJQBSGHQAKCTYXBSDQEQIJ9STDFHFKMVSAZOHKSVIUBSZYBUC"
	// The first address of the genesis wallet.
	GenesisAddress = "9QJKPJPYTNPF9AFCLGLMAGXOR9ZIPYTRISKOGJPM9ZKKDXGRXWFJZMQTETDJJOGYEVRMLAOECBPWTUZ9B"

	// The default web API port of every node.
	WebAPIPort = 14265
)

Variables

View Source
var (
	// ErrBadRequest defines the "bad request" error.
	ErrBadRequest = errors.New("bad request")
	// ErrInternalServerError defines the "internal server error" error.
	ErrInternalServerError = errors.New("internal server error")
	// ErrNotFound defines the "not found" error.
	ErrNotFound = errors.New("not found")
	// ErrUnauthorized defines the "unauthorized" error.
	ErrUnauthorized = errors.New("unauthorized")
	// ErrUnknownError defines the "unknown error" error.
	ErrUnknownError = errors.New("unknown error")
	// ErrNotImplemented defines the "operation not implemented/supported/available" error.
	ErrNotImplemented = errors.New("operation not implemented/supported/available")
)
View Source
var (
	ErrNodeMissingInLayout   = errors.New("node is missing in layout")
	ErrSelfPeering           = errors.New("a node can not peer to itself")
	ErrNoStaticPeers         = errors.New("nodes must have static nodes")
	ErrLayoutEmpty           = errors.New("layout must not be empty")
	ErrNodesDidNotPeerInTime = errors.New("nodes did not peer in time")
	ErrNodesDidNotSyncInTime = errors.New("nodes did not sync in time")
	ErrNodesNotOnlineInTime  = errors.New("nodes did not become online in time")
)

Functions

func ShutdownNetwork

func ShutdownNetwork(t *testing.T, n Shutdowner)

ShutdownNetwork shuts down the network and reports errors.

Types

type AutopeeredNetwork

type AutopeeredNetwork struct {
	*Network
	// contains filtered or unexported fields
}

AutopeeredNetwork is a network consisting out of autopeered nodes. It contains additionally an entry node.

func (*AutopeeredNetwork) AwaitPeering

func (n *AutopeeredNetwork) AwaitPeering(minimumNeighbors int) error

AwaitPeering waits until all peers have reached the minimum amount of neighbors. Returns error if this minimum is not reached after autopeeringMaxTries.

func (*AutopeeredNetwork) CreatePeer

func (n *AutopeeredNetwork) CreatePeer(cfg *NodeConfig) (*Node, error)

CreatePeer creates a new Hornet node initialized with the right entry node.

func (*AutopeeredNetwork) DeletePartitions

func (n *AutopeeredNetwork) DeletePartitions() error

DeletePartitions deletes all partitions of the network. All nodes can communicate with the full network again.

func (*AutopeeredNetwork) Partitions

func (n *AutopeeredNetwork) Partitions() []*Partition

Partitions returns the network's partitions.

func (*AutopeeredNetwork) Shutdown

func (n *AutopeeredNetwork) Shutdown() error

Shutdown shuts down the network.

func (*AutopeeredNetwork) Split

func (n *AutopeeredNetwork) Split(partitions ...[]*Node) error

Split splits the existing network in given partitions.

type CfgOverrideFunc

type CfgOverrideFunc func(index int, cfg *NodeConfig)

CfgOverrideFunc is a function which overrides configuration values.

type CoordinatorConfig

type CoordinatorConfig struct {
	// Whether to let the node run as the coordinator.
	RunAsCoo bool
	// Whether to run the coordinator in bootstrap node.
	Bootstrap bool
	// The coo Merkle root address.
	Address string
	// The coo seed.
	Seed string
	// The MWM/PoW difficulty to use.
	MWM int
	// The security level used for milestones.
	SecurityLevel int
	// The interval in which to issue new milestones.
	IssuanceIntervalSeconds int
	// The depth of the coo merkle tree.
	MerkleTreeDepth int
	// The path to the Merkle tree file.
	MerkleTreeFilePath string
}

CoordinatorConfig defines coordinator specific configuration.

func DefaultCoordinatorConfig

func DefaultCoordinatorConfig() CoordinatorConfig

DefaultCoordinatorConfig returns the default coordinator config.

func (*CoordinatorConfig) CLIFlags

func (cooConfig *CoordinatorConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

type DashboardConfig

type DashboardConfig struct {
	// The bind address of the dashboard
	BindAddress string
}

DashboardConfig holds the dashboard specific configuration.

func DefaultDashboardConfig

func DefaultDashboardConfig() DashboardConfig

DefaultDashboardConfig returns the default profiling config.

func (*DashboardConfig) CLIFlags

func (dashboardConfig *DashboardConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

type DockerContainer

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

DockerContainer is a wrapper object for a Docker container.

func NewDockerContainer

func NewDockerContainer(c *client.Client) *DockerContainer

NewDockerContainer creates a new DockerContainer.

func NewDockerContainerFromExisting

func NewDockerContainerFromExisting(c *client.Client, name string) (*DockerContainer, error)

NewDockerContainerFromExisting creates a new DockerContainer from an already existing Docker container by name.

func (*DockerContainer) ConnectToNetwork

func (d *DockerContainer) ConnectToNetwork(networkID string) error

ConnectToNetwork connects a container to an existent network in the docker host.

func (*DockerContainer) CreateContainer

func (d *DockerContainer) CreateContainer(name string, containerConfig *container.Config, hostConfigs ...*container.HostConfig) error

CreateContainer creates a new container with the given configuration.

func (*DockerContainer) CreateNodeContainer

func (d *DockerContainer) CreateNodeContainer(cfg *NodeConfig) error

CreateNodeContainer creates a new node container.

func (*DockerContainer) CreatePumbaContainer

func (d *DockerContainer) CreatePumbaContainer(name string, containerName string, targetIPs []string) error

CreatePumbaContainer creates a new container with Pumba configuration.

func (*DockerContainer) DisconnectFromNetwork

func (d *DockerContainer) DisconnectFromNetwork(networkID string) error

DisconnectFromNetwork disconnects a container from an existent network in the docker host.

func (*DockerContainer) ExitStatus

func (d *DockerContainer) ExitStatus() (int, error)

ExitStatus returns the exit status according to the container information.

func (*DockerContainer) IP

func (d *DockerContainer) IP(network string) (string, error)

IP returns the IP address according to the container information for the given network.

func (*DockerContainer) Logs

func (d *DockerContainer) Logs() (io.ReadCloser, error)

Logs returns the logs of the container as io.ReadCloser.

func (*DockerContainer) Remove

func (d *DockerContainer) Remove() error

Remove kills and removes a container from the docker host.

func (*DockerContainer) Start

func (d *DockerContainer) Start() error

Start sends a request to the docker daemon to start a container.

func (*DockerContainer) Stop

func (d *DockerContainer) Stop(optionalTimeout ...time.Duration) error

Stop stops a container without terminating the process. The process is blocked until the container stops or the timeout expires.

type Framework

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

Framework is a wrapper that provides the integration testing functionality.

func Instance

func Instance() (f *Framework, err error)

Instance returns the singleton Framework instance.

func (*Framework) CreateAutopeeredNetwork

func (f *Framework) CreateAutopeeredNetwork(name string, peerCount int, minimumNeighbors int, cfgOverrideF ...CfgOverrideFunc) (*AutopeeredNetwork, error)

CreateAutopeeredNetwork creates a network consisting out of peersCount nodes. It waits for the nodes to autopeer until the minimum neighbors criteria is met for every node.

func (*Framework) CreateNetworkWithPartitions

func (f *Framework) CreateNetworkWithPartitions(name string, peerCount, partitions, minimumNeighbors int, cfgOverrideF ...CfgOverrideFunc) (*AutopeeredNetwork, error)

CreateNetworkWithPartitions creates a network consisting out of partitions that contain peerCount nodes per partition. It waits for the peers to autopeer until the minimum neighbors criteria is met for every peer. The entry node is reachable by all nodes at all times.

func (*Framework) CreateStaticNetwork

func (f *Framework) CreateStaticNetwork(name string, layout StaticPeeringLayout, cfgOverrideF ...CfgOverrideFunc) (*StaticNetwork, error)

CreateStaticNetwork creates a network made out of statically peered nodes by the given layout. The first node is initialized with the Coordinator plugin enabled.

type Network

type Network struct {
	// The ID of the network.
	ID string
	// the type of the network.
	NetworkType NetworkType
	// The name of the network.
	Name string
	// The nodes within the network in the order in which they were spawned.
	Nodes []*Node
	// contains filtered or unexported fields
}

Network is a network consisting out of Hornet nodes.

func (*Network) AwaitAllSync

func (n *Network) AwaitAllSync(ctx context.Context) error

AwaitAllSync awaits until all nodes see themselves as synced.

func (*Network) AwaitOnline

func (n *Network) AwaitOnline(ctx context.Context) error

AwaitOnline awaits until all nodes are online or the given context is canceled.

func (*Network) Coordinator

func (n *Network) Coordinator() *Node

Coordinator returns the node with the coordinator plugin enabled.

func (*Network) CreateNode

func (n *Network) CreateNode(cfg *NodeConfig) (*Node, error)

CreateNode creates a new Hornet node in the network and returns it.

func (*Network) PrefixName

func (n *Network) PrefixName(suffix string) string

PrefixName returns the suffix prefixed with the name.

func (*Network) RandomNode

func (n *Network) RandomNode() *Node

RandomNode returns a random peer out of the list of peers.

func (*Network) Shutdown

func (n *Network) Shutdown() error

Shutdown stops all nodes, persists their container logs and removes them from Docker.

func (*Network) SpamZeroVal

func (n *Network) SpamZeroVal(dur time.Duration, parallelism int, batchSize ...int) error

SpamZeroVal starts spamming zero value transactions on all nodes for the given duration.

func (*Network) TakeCPUProfiles

func (n *Network) TakeCPUProfiles(dur time.Duration) error

TakeCPUProfile takes a CPU profile on all nodes within the network.

func (*Network) TakeHeapSnapshots

func (n *Network) TakeHeapSnapshots() error

TakeHeapSnapshot takes a heap snapshot on all nodes within the network.

type NetworkConfig

type NetworkConfig struct {
	// The seed for the autopeering identity.
	AutopeeringSeed string
	// The list of entry nodes.
	EntryNodes []string
	// Whether to run the node as entry node.
	RunAsEntryNode bool
	// The static peers for this node.
	StaticPeers []string
	// Whether to accept any connection.
	AcceptAnyConnection bool
	// Max. amount of connected peers via AcceptAnyConnection.
	MaxPeers int
}

NetworkConfig defines the network specific configuration.

func DefaultNetworkConfig

func DefaultNetworkConfig() NetworkConfig

DefaultNetworkConfig returns the default network config.

func (*NetworkConfig) CLIFlags

func (netConfig *NetworkConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

type NetworkType

type NetworkType byte
const (
	// Defines a network which consists out of autopeered nodes.
	NetworkTypeAutopeered NetworkType = iota
	// Defines a network which consists out of statically peered nodes.
	NetworkTypeStatic
)

type Node

type Node struct {
	// Name of the node derived from the container and hostname.
	Name string
	// the IP address of this node within the network.
	IP string
	// The configuration with which the node was started.
	Config *NodeConfig
	// The autopeering identity of the peer.
	*identity.Identity
	// The iota.go web API instance used to communicate with the node.
	WebAPI *api.API
	// The more specific web API providing more information for debugging purposes.
	DebugWebAPI *WebAPI
	// The profiler instance.
	Profiler
	// The DockerContainer that this peer is running in
	*DockerContainer
	// contains filtered or unexported fields
}

Node represents a Hornet node inside the Docker network.

func (*Node) APIURI

func (p *Node) APIURI() string

APIURI returns the URL under which this node's web API is accessible.

func (*Node) SetNeighbors

func (p *Node) SetNeighbors(peers []*peer.Info)

SetNeighbors sets the neighbors of the peer accordingly.

func (*Node) Spam

func (p *Node) Spam(dur time.Duration, depth int, parallelism int, batchSize ...int) (int32, error)

Spam spams zero value transactions on the node. Returns the number of spammed transactions.

func (*Node) String

func (p *Node) String() string

func (*Node) TotalNeighbors

func (p *Node) TotalNeighbors() int

TotalNeighbors returns the total number of neighbors the peer has.

type NodeConfig

type NodeConfig struct {
	// The name of this node.
	Name string
	// Environment variables.
	Envs []string
	// Binds for the container.
	Binds []string
	// Exposed ports of this container.
	ExposedPorts nat.PortSet
	// Network config.
	Network NetworkConfig
	// Web API config.
	WebAPI WebAPIConfig
	// Snapshot config.
	Snapshot SnapshotConfig
	// Coordinator config.
	Coordinator CoordinatorConfig
	// Plugin config.
	Plugins PluginConfig
	// Profiling config.
	Profiling ProfilingConfig
	// Dashboard config.
	Dashboard DashboardConfig
}

NodeConfig defines the config of a Hornet node.

func DefaultConfig

func DefaultConfig() *NodeConfig

DefaultConfig returns the default NodeConfig.

func (*NodeConfig) AsCoo

func (cfg *NodeConfig) AsCoo()

AsCoo adjusts the config to make it usable as the Coordinator's config.

func (*NodeConfig) CLIFlags

func (cfg *NodeConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

type Partition

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

Partition represents a network partition. It contains its peers and the corresponding Pumba instances that block all traffic to peers in other partitions.

func (*Partition) Peers

func (p *Partition) Peers() []*Node

Nodes returns the partition's peers.

func (*Partition) PeersMap

func (p *Partition) PeersMap() map[string]*Node

PeersMap returns the partition's peers map.

func (*Partition) String

func (p *Partition) String() string

type PluginConfig

type PluginConfig struct {
	// Holds explicitly enabled plugins.
	Enabled []string
	// Holds explicitly disabled plugins.
	Disabled []string
}

PluginConfig defines plugin specific configuration.

func DefaultPluginConfig

func DefaultPluginConfig() PluginConfig

DefaultPluginConfig returns the default plugin config.

func (*PluginConfig) CLIFlags

func (pluginConfig *PluginConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

type Profiler

type Profiler struct {
	http.Client
	// contains filtered or unexported fields
}

Profiler profiles a node for metrics.

func (*Profiler) GraphMetrics

func (n *Profiler) GraphMetrics(dur time.Duration) error

GraphMetrics graphs metrics about TPS, memory consumption, confirmation rate of the node and saves it into the log dir.

func (*Profiler) TakeCPUProfile

func (n *Profiler) TakeCPUProfile(dur time.Duration) error

TakeCPUProfile takes a CPU profile for the given duration and then saves it to the log directory.

func (*Profiler) TakeHeapSnapshot

func (n *Profiler) TakeHeapSnapshot() error

TakeHeapSnapshot takes a snapshot of the heap memory and then saves it to the log directory.

type ProfilingConfig

type ProfilingConfig struct {
	// The bind address of the pprof server.
	BindAddress string
}

ProfilingConfig defines the profiling specific configuration.

func DefaultProfilingConfig

func DefaultProfilingConfig() ProfilingConfig

DefaultProfilingConfig returns the default profiling config.

func (*ProfilingConfig) CLIFlags

func (profilingConfig *ProfilingConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

type Shutdowner

type Shutdowner interface {
	Shutdown() error
}

Shutdowner can shutdown itself.

type SnapshotConfig

type SnapshotConfig struct {
	// The load type of the snapshot.
	LoadType string
	// The path to the global snapshot file.
	GlobalSnapshotFilePath string
	// The index of the global snapshot.
	GlobalSnapshotIndex int
	// The path to the local snapshot file.
	LocalSnapshotFilePath string
	// The file paths to the epoch spent address files.
	EpochSpentAddressesFilePath []string
}

SnapshotConfig defines snapshot specific configuration.

func DefaultSnapshotConfig

func DefaultSnapshotConfig() SnapshotConfig

DefaultSnapshotConfig returns the default snapshot config.

func (*SnapshotConfig) CLIFlags

func (snapshotConfig *SnapshotConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

type StaticNetwork

type StaticNetwork struct {
	*Network
	// contains filtered or unexported fields
}

StaticNetwork defines a network made out of statically peered nodes.

func (*StaticNetwork) AwaitPeering

func (n *StaticNetwork) AwaitPeering(ctx context.Context) error

AwaitPeering awaits until all nodes are peered according to the peering layout.

func (*StaticNetwork) ConnectNodes

func (n *StaticNetwork) ConnectNodes() error

ConnectNodes peers the nodes of the network according to the given layout with each other.

type StaticPeeringLayout

type StaticPeeringLayout map[int]map[int]connected

StaticPeeringLayout defines how in a statically peered network nodes are peered to each other.

var DefaultStaticPeeringLayout StaticPeeringLayout = map[int]map[int]connected{
	0: {1: false, 2: false, 3: false},
	1: {0: false, 2: false, 3: false},
	2: {0: false, 1: false, 3: false},
	3: {0: false, 1: false, 2: false},
}

DefaultStaticPeeringLayout defines a static peering layout with 4 nodes which are all statically peered to each other.

func (StaticPeeringLayout) Validate

func (spl StaticPeeringLayout) Validate() error

Validate validates whether the static peering layout is valid by checking:

  • the layout isn't empty
  • keys must be continuous numbers reflecting the ID of the node
  • a node must hold nodes to peer to and they must exist in the map
  • a node doesn't peer to itself

type WebAPI

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

WebAPI is an API wrapper over the web API.

func NewWebAPI

func NewWebAPI(baseURL string, httpClient ...http.Client) *WebAPI

NewWebAPI returns a new web API instance.

func (*WebAPI) BaseURL

func (api *WebAPI) BaseURL() string

BaseURL returns the baseURL of the API.

func (*WebAPI) Info

func (api *WebAPI) Info() (*webapi.GetNodeInfoReturn, error)

func (*WebAPI) Neighbors

func (api *WebAPI) Neighbors() ([]*peer.Info, error)

Neighbors returns the neighbors to which the node is connected to.

type WebAPIConfig

type WebAPIConfig struct {
	// The bind address for the web API.
	BindAddress string
	// Explicit permitted API calls.
	PermittedAPICalls []string
}

WebAPIConfig defines the web API specific configuration.

func DefaultWebAPIConfig

func DefaultWebAPIConfig() WebAPIConfig

DefaultWebAPIConfig returns the default web API config.

func (*WebAPIConfig) CLIFlags

func (webAPIConfig *WebAPIConfig) CLIFlags() []string

CLIFlags returns the config as CLI flags.

Jump to

Keyboard shortcuts

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