flag

package
v3.15.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2018 License: Apache-2.0, Apache-2.0 Imports: 8 Imported by: 6

Documentation

Overview

Provides unified interface to access needed flags when you are testing.

Property file

"NewTestFlags()" has two way to load property file:

OWL_TEST_PROPS_FILE - Environment Variable
"-owl.test.propfile" - flag of "go test"

Entry Environment Variables

Following environment variables are supported when calling "NewTestFlags()".

OWL_TEST_PROPS - As same as "-owl.test"
OWL_TEST_PROPS_SEP - As same as "-owl.test.sep"

Entry Flags

There are only two flags of Golang needed:

-owl.test=<properties>
-owl.test.sep=<separator for properties>

The format used by "owl.test" is property file:

https://en.wikipedia.org/wiki/.properties

In order to separate properties, "owl.test.sep"(as regular expression) would be used to recognize a record of property file.

See "DEFAULT_SEPARATOR" constant for default separator.

Loading priority

When you execute go test with viable values for both of the environment variables and the flags, the priority is:

  1. Load file from environment variable(OWL_TEST_PROPS_FILE)
  2. Load properties of environment variable(OWL_TEST_PROPS)
  3. Load file from flag("-owl.test.propfile")
  4. Load properties from flag("-owl.test")

Pre-defined Properties - Features

There are some pre-defined properties:

mysql(DEPRECATED) - MySql connection

client.http.host - HTTP client
client.http.port - HTTP client
client.http.ssl - HTTP client
client.http.resource - HTTP client

client.jsonrpc.host - JSONRPC Client
client.jsonrpc.port - JSONRPC Client

it.web.enable - IT to Web

The object of "*TestFlags" provides various functions to check whether or not some configuration for testing are enabled.

For example, "HasMySql()" would let you know whether "mysql=<conn>" is viable.

Pre-defined Properties - Owl Databases of MySql

Following list shows build-in supporting databases of Owl Database:

mysql.owl_portal - MySql connection on OWL-Portal
mysql.owl_graph - MySql connection on OWL-Graph
mysql.owl_uic - MySql connection on OWL-Uic
mysql.owl_links - MySql connection on OWL-Links
mysql.owl_grafana - MySql connection on OWL-Grafana
mysql.owl_dashboard - MySql connection on OWL-Dashboard
mysql.owl_boss - MySql connection on OWL-Boss

You could use "HasMySqlOfOwlDb(int)" or "GetMysqlOfOwlDb(int)" to retrieve value of properties.

Constraint

The empty string of property value would be considered as non-viable.

SkipFactory - Features

"SkipFactory" provides interfaces to build skip functions, which use Ginkgo's "Skip()" function.

The "BuildSkipFactory()" function generate a new "SkipFactory" by refresh "NewTestFlags()" instance.

SkipFactory - Owl Databases of MySql

You could use "BuildSkipFactoryOfOwlDb()" to retrieve the "SkipFactory" by some of build-in databases.

Features

There are various constants, like "F_HttpClient" or "F_MySql", to be used in "BuildSkipFactory()".

Skipping with Ginkgo

You could use "FeatureHelpString()" to generate default message for skipping.

features := F_HttpClient | F_MySql
sf := BuildSkipFactory(features, FeatureHelpString(features))

Context("Sometest", sf.PrependBeforeEach(func() {
	/* Your test... */

	It("Something...", func() {
		/* Your test... */
	})
}))

Skipping with Ginkgo Builder

You could use "FeatureHelpString" to generate default message for skipping.

features := F_HttpClient | F_MySql
sf := BuildSkipFactory(features, FeatureHelpString(features))

NewGinkgoBuilder("Your Context").
	It("Test 1", func() {
		sf.Skip()
		/* Your test... */
	}).
	ToContext()
}))

Compose SkipFactory

Some modules are depend on complex environments. For example, the "query" module is using multiple databases and other modules. You could "Compose()" multiple "SkipFactory"s to perform complex checking of testing environments.

features := F_HttpClient
db := OWL_DB_PORTAL | OWL_DB_UIC

sf := BuildSkipFactory(features, FeatureHelpString(features))
sdb := BuildSkipFactoryOfOwlDb(db, OwlDbHelpString(db)).Compose(sf)

Index

Constants

View Source
const (
	// Feature of HTTP client
	F_HttpClient = 0x01
	// Feature of JSONRPC client
	F_JsonRpcClient = 0x02
	// Deprecated: Feature of MySql
	F_MySql = 0x100
	// Feature of IT web
	F_ItWeb = 0x10000
)

Bit reservation principals:

Bits (0~7): For clients of various protocols
Bits (8~15): For databases
Bits (16~23): For misc(e.x. mocking server)
View Source
const (
	ENV_OWL_TEST_PROPS      = "OWL_TEST_PROPS"
	ENV_OWL_TEST_PROPS_SEP  = "OWL_TEST_PROPS_SEP"
	ENV_OWL_TEST_PROPS_FILE = "OWL_TEST_PROPS_FILE"
)
View Source
const (
	OWL_DB_PORTAL    = 0x01
	OWL_DB_GRAPH     = 0x02
	OWL_DB_UIC       = 0x04
	OWL_DB_LINKS     = 0x8
	OWL_DB_GRAFANA   = 0x10
	OWL_DB_DASHBOARD = 0x20
	OWL_DB_BOSS      = 0x40
)
View Source
const (
	// Default separator
	DEFAULT_SEPARATOR = "\\s+"
)

Variables

This section is empty.

Functions

func FeatureHelp

func FeatureHelp(matchFeatures int) []string

Gets help of features, every feature has a corresponding message.

func FeatureHelpString

func FeatureHelpString(matchFeatures int) string

Gets help of features of string.

This function likes "FeatureHelp(int)" beside joining the messages with a space character.

func MatchFlags

func MatchFlags(sourceFlags *TestFlags, matchFeatures int) bool

Checks the match features on "*TestFlags"

func MatchFlagsOfOwlDb

func MatchFlagsOfOwlDb(sourceFlags *TestFlags, matchDbs int) bool

Checks the match db on "*TestFlags"

func OwlDbHelp

func OwlDbHelp(matchDbs int) []string

Gets help of properties about OWL databases

func OwlDbHelpString

func OwlDbHelpString(matchDbs int) string

Gets help of properties about OWL databases as string

This function likes "OwlDbHelp(int)" beside joining the messages with a space character.

Types

type SkipFactory

type SkipFactory interface {
	// Generates a function with prepending of "BeforeEach()" block
	PrependBeforeEach(func()) func()
	// Generates a "BeforeEach()" function with skipping
	BeforeEachSkip()
	// Skips current execution directly
	Skip()
	// Composes another SkipFactory
	Compose(SkipFactory) SkipFactory
}

Defines the interfaces could be used to skip tests in various situations.

1. Generates a "BeforeEach()":

sf.BeforeEachSkip()

2. Used with "Describe(string, interface{})" or "Context(string, interface{})"

Context("Context", sf.PrependBeforeEach(func() {
	/* Your test... */
}))

3. Used with "It()", "Specify()":

It("Context...", func() {
	sf.Skip()
	/* Your test... */
})

func BuildSkipFactory

func BuildSkipFactory(matchFeatures int, message string, callerSkip ...int) SkipFactory

Builds factory of skipping process.

This function would auto-load "*TestFlags".

func BuildSkipFactoryByBool

func BuildSkipFactoryByBool(shouldSkip bool, message string, callerSkip ...int) SkipFactory

func BuildSkipFactoryOfOwlDb

func BuildSkipFactoryOfOwlDb(matchDb int, message string, callerSkip ...int) SkipFactory

Builds Factory of skipping process

type TestFlags

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

Convenient type used to access specific testing environment of OWL.

func NewTestFlags

func NewTestFlags() *TestFlags

Initializes the object of "*TestFlags" by parsing flag automatically.

This function parses os.Args every time it is get called.

func (*TestFlags) GetHttpClient

func (f *TestFlags) GetHttpClient() (string, uint16, string, bool)

Gets property values of:

client.http.host
client.http.port
client.http.resource
client.http.ssl

func (*TestFlags) GetJsonRpcClient

func (f *TestFlags) GetJsonRpcClient() (string, uint16)

Gets property values of "client.jsonrpc.host" and "client.jsonrpc.port"

func (*TestFlags) GetMySql deprecated

func (f *TestFlags) GetMySql() string

Gets property value of "mysql"

Deprecated: Use "GetMysqlOfOwlDb(int)" instead.

func (*TestFlags) GetMysqlOfOwlDb

func (f *TestFlags) GetMysqlOfOwlDb(owlDb int) string

func (*TestFlags) GetViper

func (f *TestFlags) GetViper() *viper.Viper

func (*TestFlags) HasHttpClient

func (f *TestFlags) HasHttpClient() bool

Gives "true" if and only if following properties are viable:

client.http.host=
client.http.port=

Example:

"-owl.flag=client.http.host=127.0.0.1 client.http.port=3396"

func (*TestFlags) HasItWeb

func (f *TestFlags) HasItWeb() bool

Gives "true" if and only if "it.web.enable" property is true

Example:

"-owl.flag=it.web.enable=true"

func (*TestFlags) HasJsonRpcClient

func (f *TestFlags) HasJsonRpcClient() bool

Gives "true" if and only if following properties are viable:

client.jsonrpc.host=
client.jsonrpc.port=

Example:

"-owl.flag=client.jsonrpc.host=127.0.0.1 client.jsonrpc.port=3396"

func (*TestFlags) HasMySql

func (f *TestFlags) HasMySql() bool

Gives "true" if and only if "mysql" property is non-empty

Example:

"-owl.flag=mysql=root:cepave@tcp(192.168.20.50:3306)/falcon_portal_test?parseTime=True&loc=Local"

Depcrecated: Use "HasMySqlOfOwlDb(int)" instead.

func (*TestFlags) HasMySqlOfOwlDb

func (f *TestFlags) HasMySqlOfOwlDb(owlDb int) bool

Gives "true" if and only if "mysql.<db>" property is non-empty

Example:

"-owl.flag=mysql.portal=root:cepave@tcp(192.168.20.50:3306)/falcon_portal_test?parseTime=True&loc=Local"

Jump to

Keyboard shortcuts

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