comproto

package
v0.0.0-...-e93c592 Latest Latest
Warning

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

Go to latest
Published: May 19, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DiscoveredByPath specifies that the command is discovered using environment path
	DiscoveredByPath = "PATH"

	// DiscoveredByHomeBinDir specifies that the command is discovered using
	// an home directory setting in environment.
	DiscoveredByHomeBinDir = "HomeBinDir"
)
View Source
const (
	// ExecTypeExe requires the use of an Executable for the stubbing
	ExecTypeExe = "EXE"

	// ExecTypeBash requires the use of a bash script for the stubbing
	ExecTypeBash = "BASH"
)
View Source
const (
	// StubbingModeStatic requires static stubbing.
	// The stubfunc will be evaludated immediately (before the command execution)
	// with zero request.
	// The outcome is static and cannot  depend on the command arguments
	StubbingModeStatic = "STATIC"

	// StubbingModeDyna reuqires dynamic stubbing.
	// The stub-func will be evaluated online (reacting to the command execution)
	// The outome ca there for depends on command arguments
	StubbingModeDyna = "DYNAMIC"
)

Variables

This section is empty.

Functions

func AdaptFuncsToCmdStub

func AdaptFuncsToCmdStub(
	stubFuncs []StubFunc,
	repeatLast bool,
) func(sreq StubRequest) *ExecOutcome

AdaptFuncsToCmdStub returns a CmdStub which will use a list of function to procudes the execution outcomes.

func AdaptOutcomeToCmdStub

func AdaptOutcomeToCmdStub(
	outcome *ExecOutcome,
) func(sreq StubRequest) *ExecOutcome

AdaptOutcomeToCmdStub returns a StubFunc which will return the given outcome.

func AdaptOutcomesToCmdStub

func AdaptOutcomesToCmdStub(
	outcomes []*ExecOutcome,
	repeatLast bool,
) func(sreq StubRequest) *ExecOutcome

AdaptOutcomesToCmdStub returns a CmdStub which will yield the given outcomes.

func CfgDefaultTimeout

func CfgDefaultTimeout() time.Duration

CfgDefaultTimeout return default value of timeout in not provided.

func CmdConfigTmpl

func CmdConfigTmpl() (script []byte, err error)

CmdConfigTmpl returns the template used to generate stub cmd configuration.

func EffectuateAlternativeExecOutcome

func EffectuateAlternativeExecOutcome(stubFunc StubFunc)

EffectuateAlternativeExecOutcome effectuates an alternative outcome to the configured one for test helper processes.

func EffectuateConfiguredExecOutcome

func EffectuateConfiguredExecOutcome(
	extraJobOnStubRequestForStaticMode func(StubRequest) error,
)

EffectuateConfiguredExecOutcome effectuates the configured static or dynamic outcome for test helper processes.

func EffectuateDynamicOutcome

func EffectuateDynamicOutcome(
	timeout time.Duration,
	pipePathStubber string,
	pipePathTestHelperProc string,
	req StubRequest,
	stderr io.Writer,
	stdout io.Writer,

) (exitCode uint8)

EffectuateDynamicOutcome uses IPC to request execution outcome from running test.

func ExecOutcomeEncoderFunc

func ExecOutcomeEncoderFunc(writer io.Writer) func(outcome *ExecOutcome) error

ExecOutcomeEncoderFunc return an encoder which can write exec outcome representation into the writer.

func StubRequestEncoderFunc

func StubRequestEncoderFunc(writer io.Writer) func(outcome *StubRequest) error

StubRequestEncoderFunc return an encoder which can write stubbing request representation into the writer.

func StubbingOngoing

func StubbingOngoing() bool

StubbingOngoing tells whether the current process is run as stub instead of an actual execution. The check is based of off process environment

func ValueDuration

func ValueDuration(o OptionalDuration) (valueX time.Duration, err error)

ValueDuration returns the duration value if present or an error.

func ValueUint8

func ValueUint8(o OptionalUint8) (value uint8, err error)

ValueUint8 returns the uint8 value if present or an error.

func WriteStubRequestToNamedPipe

func WriteStubRequestToNamedPipe(
	pipePath string, stubReq StubRequest, timeout time.Duration,
) error

WriteStubRequestToNamedPipe write the stubbing request to the named pipe

Types

type CmdConfig

type CmdConfig struct {
	StubKey                 string
	CmdToStub               string
	UnitTestExec            string
	TestHelperProcessMethod string
	TxtStdout               string
	TxtStderr               string
	ExitCode                OptionalUint8
	PipeStubber             string
	PipeTestHelperProcess   string
	Timeout                 OptionalDuration
	DataDir                 string
}

CmdConfig holds configuration data for an executable used for stubbing

func CmdConfigForCommand

func CmdConfigForCommand(cmdPath string) (*CmdConfig, error)

CmdConfigForCommand loads the configuration for the given executable identified by its path.

func CmdConfigLoadedFromFile

func CmdConfigLoadedFromFile(cmdConfigPath string) (*CmdConfig, error)

CmdConfigLoadedFromFile loads the configuration from the given file path.

func (CmdConfig) CreateConfigFile

func (cfg CmdConfig) CreateConfigFile(
	cmdDir string,
) error

CreateConfigFile create a file holding the configuration state at the given directory.

func (CmdConfig) ExitCodeTxt

func (cfg CmdConfig) ExitCodeTxt() string

ExitCodeTxt return the text representation of the exit code.

func (CmdConfig) ExitCodeUint8

func (cfg CmdConfig) ExitCodeUint8() (exitcode uint8, err error)

ExitCodeUint8 returns the configured exit code.

func (CmdConfig) StderrAvail

func (cfg CmdConfig) StderrAvail() bool

StderrAvail return true if stderr data are available; false otherwise.

func (CmdConfig) StdoutAvail

func (cfg CmdConfig) StdoutAvail() bool

StdoutAvail return true if stdout data are available; false otherwise.

func (CmdConfig) StubRequestWith

func (cfg CmdConfig) StubRequestWith(args []string) StubRequest

StubRequestWith returns a stub request that requests execution of the configured cmd with the given arguments.

func (CmdConfig) TimeoutAsDuration

func (cfg CmdConfig) TimeoutAsDuration() (timeout time.Duration, err error)

TimeoutAsDuration returns configured timeout as time.Duration.

func (CmdConfig) TimeoutAsDurationOrDefault

func (cfg CmdConfig) TimeoutAsDurationOrDefault() (timeout time.Duration)

TimeoutAsDurationOrDefault returns configured timeout as time.Duration, or the default timeout in any misconfiguration.

func (CmdConfig) TimeoutAsFormattedNanos

func (cfg CmdConfig) TimeoutAsFormattedNanos() string

TimeoutAsFormattedNanos return a string nanos integer representation of the configured timeout.

func (CmdConfig) UseDynamicOutcome

func (cfg CmdConfig) UseDynamicOutcome() bool

UseDynamicOutcome tells whether the use of dynamic outcome is configured.

func (CmdConfig) UseStaticOutCome

func (cfg CmdConfig) UseStaticOutCome() bool

UseStaticOutCome stells whether the us of static outcome is configured.

func (CmdConfig) UseTestHelperProcess

func (cfg CmdConfig) UseTestHelperProcess() bool

UseTestHelperProcess tells whether the use of test helper process is configured.

type DiscoveredBy

type DiscoveredBy string

DiscoveredBy specifies how executable is discovered.

type DiscoveredByHomeDirBinData

type DiscoveredByHomeDirBinData struct {
	EnvHomeKey string
	// BinDirs bin sub directory paths.
	// using to avoid guessing path separator
	BinDirs []string
}

DiscoveredByHomeDirBinData holds data used to the home environment key and the binary sub-directory structure which contains the executable e.g. For Java: EnvHomeKey=JAVA_HOME and BinDirs=[]string{"bin"}

so that the java binary is located in ${JAVA_HOME}/bin/

type ExecOutcome

type ExecOutcome struct {
	Key string

	// Expected command exec outcome
	// the receiving side is expected forward them as is
	Stdout   string
	Stderr   string
	ExitCode uint8

	InternalErrTxt string
}

ExecOutcome modells what happens when the stubbed command is executed. It basically the content of std-out and std-err and the error code the stubbed command us supposed to yield. The stubber may encounter some internal err. In that case: - InternalErrTxt is provided. - ExitCode does not hold a meaningfull value - The requesting side should not use it as the expected exit code Not using an typed error here in order to ease serialization during ipc and allow usage both as dto and stubbing function return type.

func ExecOutcomeDecoderFunc

func ExecOutcomeDecoderFunc(reader io.Reader) (*ExecOutcome, error)

ExecOutcomeDecoderFunc decodes an exec-outcome from the given reader.

func ReadStubbingResponseFromNamedPipe

func ReadStubbingResponseFromNamedPipe(pipePath string, timeout time.Duration) (*ExecOutcome, error)

ReadStubbingResponseFromNamedPipe reads the stubbing outcome from the named pipe.

func (ExecOutcome) InternalErrTxtAvail

func (o ExecOutcome) InternalErrTxtAvail() bool

InternalErrTxtAvail return true if InternalErrTxt is available (not "") and false otherwise.

func (ExecOutcome) StderrAvail

func (o ExecOutcome) StderrAvail() bool

StderrAvail return true if Stderr is available (not "") and false otherwise.

func (ExecOutcome) StdoutAvail

func (o ExecOutcome) StdoutAvail() bool

StdoutAvail return true if Stdout is available (not "") and false otherwise.

type ExecType

type ExecType string

ExecType specifies the type of exec used for stubbing

type OptionalDuration

type OptionalDuration interface{}

OptionalDuration is an time.Duration or nil to fake optional type for time.Duration. It may legly holds the following type as long as the valze is within the rang of time.Duration: int,int8/16/32/64,uint,uint8/16/32/64

type OptionalUint8

type OptionalUint8 interface{}

OptionalUint8 is an uint8 or nil to fake optional type for unit8. It may legaly holds the following types as long as their value is within the rang of uint8: int,int8/16/32/64,uint,uint8/16/32/64

type Settings

type Settings struct {
	DiscoveredBy               DiscoveredBy
	DiscoveredByHomeDirBinData DiscoveredByHomeDirBinData

	ExecType ExecType

	Mode StubbingMode

	TestHelperProcessMethodName string

	Timeout OptionalDuration
}

Settings holds setting data for an exec stubbing

func SettingsDynaStubCmdDiscoveredByPath

func SettingsDynaStubCmdDiscoveredByPath() *Settings

SettingsDynaStubCmdDiscoveredByPath constructs a new Settings for dynamically stubbing a cmd which is discovered by Path.

func (*Settings) DiscoveredByHomeDirBin

func (s *Settings) DiscoveredByHomeDirBin(envHomeKey string, binDirs ...string)

DiscoveredByHomeDirBin selects discovery by home dir

func (*Settings) DiscoveredByPath

func (s *Settings) DiscoveredByPath()

DiscoveredByPath selects discovery by path

func (*Settings) ExecTypeBash

func (s *Settings) ExecTypeBash()

ExecTypeBash sets exec type to BASH

func (*Settings) ExecTypeExe

func (s *Settings) ExecTypeExe()

ExecTypeExe sets exec type to EXE

func (Settings) InModDyna

func (s Settings) InModDyna() bool

InModDyna returns true if in mode dynamic false otherwise.

func (Settings) InModStatic

func (s Settings) InModStatic() bool

InModStatic returns true if in mode static false otherwise.

func (Settings) IsCmdDiscoveredByHomeDir

func (s Settings) IsCmdDiscoveredByHomeDir() bool

IsCmdDiscoveredByHomeDir true is cmd are set to be discovered by home-dir.

func (Settings) IsUsingExecTypeBash

func (s Settings) IsUsingExecTypeBash() bool

IsUsingExecTypeBash returns true bash is specified as exec type; false otherwise.

func (Settings) IsUsingTestProcessHelper

func (s Settings) IsUsingTestProcessHelper() bool

IsUsingTestProcessHelper returns true if TestProcessHelper is provided and expected to be used; false otherwise.

func (*Settings) ModeDanymic

func (s *Settings) ModeDanymic()

ModeDanymic set stubbing mode to dynamic

func (*Settings) ModeStatic

func (s *Settings) ModeStatic()

ModeStatic set stubbing mode to static

func (*Settings) WithTestProcessHelper

func (s *Settings) WithTestProcessHelper(methodName string)

WithTestProcessHelper requires the use a test process helper.

func (*Settings) WithoutTestProcessHelper

func (s *Settings) WithoutTestProcessHelper()

WithoutTestProcessHelper requires stubbing without the use of a test process helper

type StubFunc

type StubFunc func(sreq StubRequest) *ExecOutcome

StubFunc function use to produce the outcome for a stubbed command execution

func RecordingExecutions

func RecordingExecutions(
	stubFunc StubFunc,
) (recordingStubFunc StubFunc, reqsStore *[]StubRequest)

RecordingExecutions adds request recording feature to the given StubFunc

type StubRequest

type StubRequest struct {
	// Key identifies the stubbing setup
	Key     string
	CmdName string
	Args    []string
}

StubRequest holds data send to a stubber through a ComChannel to request stubbing

func StopOperationRequest

func StopOperationRequest() *StubRequest

func StubRequestDecoderFunc

func StubRequestDecoderFunc(reader io.Reader) (*StubRequest, error)

StubRequestDecoderFunc decodes an stubbing request from the given reader.

func (StubRequest) IsRequestingStop

func (req StubRequest) IsRequestingStop() bool

func (StubRequest) String

func (req StubRequest) String() string

type StubRequestDirRepo

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

StubRequestDirRepo a repository to save and load stubbing requests from a directory.

func NewStubRequestDirRepo

func NewStubRequestDirRepo(dataDir string) StubRequestDirRepo

NewStubRequestDirRepo creates a new stubbing request repo given the data directory.

func (StubRequestDirRepo) DeleteAll

func (repo StubRequestDirRepo) DeleteAll() error

DeleteAll deletes all persisted requests.

func (StubRequestDirRepo) FindAll

func (repo StubRequestDirRepo) FindAll() (requests *[]StubRequest, err error)

FindAll return all persisted requests.

func (StubRequestDirRepo) Save

func (repo StubRequestDirRepo) Save(req StubRequest) error

Save saves the given request.

type StubbingMode

type StubbingMode string

StubbingMode specifies the stubing mode (static or dynamic).

Jump to

Keyboard shortcuts

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