app

package
v3.2.10 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: LGPL-3.0 Imports: 24 Imported by: 34

README

App support

This is a collection of libraries to support CLI-apps that work with the onet framework. The go-library simplifies loading and saving of group-configurations and supports some very basic input/output functions.

For integration support to test the CLI-apps, libtest.sh supports a range of tests to make sure that the binary behaves as needed.

LibTest.sh

This is a specialized bash-library to handle the following parts of the test:

  • compiling of a conode with the required services
  • setting up and removing the directory structure to run the tests
  • different test primitives to test failure or success of running the binary

One goal of libtest.sh is to test the binary in a way as close to what a user would do when running the binary. This leads to a better understanding whether the binary has a logical setup of the commands and command line options.

Command Line Options

The default for libtest.sh-enabled test is to:

  • compile the binary
  • run all the tests in a temporary directory
  • delete the temporary directory after the test.

Two command line options are available to infulence how the test is run:

  • -nt - NoTemp - sets up the test-environment in ./build and doesn't delete it after the test is done. This is useful if you want to inspect the environment after the test run. CAREFUL because it will NOT recompile the binary. You will use this flag mostly while developing the test.sh file, so that you can quickly test if your new tests are correct or not.
  • -b - Build - only useful with -nt to force a build of the binary. While developing the tests, in a first time you might want to use -nt until your test.sh file is OK, then you will work on your go-code. To recompile the go-code, use this flag. Once the go-code is OK, you can remove this flag again and work on the test.sh file.

Test.sh setup

The common name for using libtest.sh is a file called test.sh in the same directory as the CLI-binary. The usual structure for this file is:

  • Global variables and inclusion of libtest.sh
  • main-method setting up the environment and starting the tests
  • a list of test-methods
  • helper methods
  • a call to main, so that the main method is at the top and all necessary methods are defined before main is called.
Global Variables and inclusion of libtest.sh
Variable Default Explanation
DBG_TEST 0 Show the output of the commands: 0=none, 1=test-names, 2=all
DBG_SRV 0 DBG-level for server, passed as -debug to the conode command
NBR 3 Highest number of servers and clients
NBR_SERVERS NBR How many servers to configure
NBR_SERVERS_GROUP NBR_SERVERS - 1 How many servers to write to the
group-file - per default, keep one server out of the group, to test what
happens if a server gets added later
APPDIR pwd where the test.sh-script is located
APP $(basename $APPDIR) The name of the builddir

All variables can be overriden by defining them before the inclusion of libtest.sh. The most common setup of a test.sh only has the first two global variables, DBG_TEST and DBG_SRV:

# Show the output of the commands: 0=none, 1=test-names, 2=all
DBG_TEST=1
# DBG-level for call to the app - this is not handled by libtest.sh, but will
# be used in the helper methods.
DBG_APP=2
# DBG-level for server
DBG_SRV=0

. $(go env GOPATH)/src/github.com/dedis/onet/app/libtest.sh

If a test fails, it is common to change those variables to:

DBG_TEST=2
DBG_SRV=2

So that libtest.sh shows the output of the CLI command and the conode prints at a debug-level of 2.

main

To have the most important method at the top (for easier editing), main is defined at the beginning. So any edits to tests can be done there.

A typical main looks like this:

main(){
    startTest
    buildConode
    test Build
    test Network
    stopTest
}
  • startTest cleans the test-directory and builds the CLI binary
  • buildConode creates a new conode and automatically includes the service defined in the ./service or ../service directory
  • test Build makes sure all conodes are stopped and removes all databases from the previous test, then calls testBuild
  • test Network makes sure all conodes are stopped and removes all databases from the previous test, then calls testNetwork
  • stopTest stops all remaining conodes and cleans up the test-directory, if it is the temporary directory

One common use-case of the main method in the case of a failing test is to comment out all tests that run successfully up to the failing test, so that a subsequent ./test.sh run only runs the failing test. Calling ./test.sh -nt allows for changing the failing testName method to find out what is wrong.

Test-methods

Each test-method usually starts the required conodes before running the tests necessary to verify the correct running of the service.

testNetwork(){
    runCoBG 1 2
    testOut "Running network"
    testGrep "Available_Services" runCl -g public.toml
    testGrep "Available_Services" runCl -g public.toml
}
  • runCoBG 1 2 starts conode number 1 and 2 (the index starts at 1) in the background. There is some code to make sure the conodes are actually up and running and listening on the websocket-ports.
  • testOut prints the first argument if DBG_TEST is greater or equal to 1
  • testGrep searches the first argument in the output of the given command

The whole list of all test-commands can be found here.

Helper methods

Each test.sh will have its own helper methods, but the most common one is to write something to run your CLI-app:

runCl(){
    dbgRun ./$APP -d $DBG_APP $@
}

This will call your app with the given debugging-option, referenced from the top of your test.sh for easy change in case you need to debug your test.

Sometimes you might want to give more option, most often the configuration-directory to be used:

runCl(){
	local CFG=cl$1
	shift
	dbgRun ./$APP -d $DBG_APP -c $CFG $@
}

This passes the configuration-directory to the app, supposing the app has a -c argument for passing it.

A call to main

This is very simple:

main

As bash is an interpreter, it needs to run through all your methods before being able to call them. And because it is nice to have all the important methods at the top, we propose this setup.

Test-commands

Here is an overview of the currently supported test-commands in libtest.sh:

Command Arguments Description
test Name Prints Name, cleans up build-directory, deletes all databases from services in previous run, and calls testName
testOut $@ Outputs all arguments if DBT_TEST -ge 1
dbgOut $@ Outputs all arguments if DBT_TEST -ge 2
dbgRun $@ Runs $@ and outputs the result of $@ if DBG_TEST -ge 2. Redirects the output in all cases if OUTFILE is set.
testOK $@ Asserts that the exit-code of running $@ using dbgRun is 0.
testFail $@ Asserts that the exit-code of running $@ using dbgRun is NOT 0.
testFile File Asserts File exists and is a file.
testNFile File Asserts that File DOES NOT exist.
testFileGrep String File Asserts that String exists in File.
testGrep String $@[1..] Asserts that String is in the output of the command being run by dbgRun and all but the first input argument. Ignores the exit-code of the command.
testNGrep String $@[1..] Asserts that String is NOT in the output of the command being run by dbgRun and all but the first input argument. Ignores the exit-code of the command.
testReGrep String Asserts String is part of the last command being run by testGrep or testNGrep.
testReNGrep String Asserts String is NOT part of the last command being run by testGrep or testNGrep.
testCount Count String $@[2..] Asserts that String exists exactly Count times in the output of the command being run by dbgRun and all but the first two arguments.

Documentation

Index

Constants

View Source
const (
	// String is a CertificateURL type containing a certificate.
	String CertificateURLType = "string"
	// File is a CertificateURL type that contains the path to a file
	// containing a certificate.
	File = "file"
	// InvalidCertificateURLType is an invalid CertificateURL type.
	InvalidCertificateURLType = "wrong"
	// DefaultCertificateURLType is the default type when no type is specified
	DefaultCertificateURLType = File
)
View Source
const DefaultAddress = "127.0.0.1"

DefaultAddress where to be contacted by other servers.

View Source
const DefaultGroupFile = "public.toml"

DefaultGroupFile is the default group definition file-name.

View Source
const DefaultPort = 7770

DefaultPort to listen and connect to. As of this writing, this port is not listed in /etc/services

View Source
const DefaultServerConfig = "private.toml"

DefaultServerConfig is the default server configuration file-name.

Variables

This section is empty.

Functions

func Copy

func Copy(dst, src string) error

Copy makes a copy of a local file with the same file-mode-bits set.

func GenerateServiceKeyPairs

func GenerateServiceKeyPairs() map[string]ServiceConfig

GenerateServiceKeyPairs generates a map of the service with their key pairs. It can be used to generate server configuration.

func Input

func Input(def string, args ...interface{}) string

Input prints the arguments given with an 'input'-format and proposes the 'def' string as default. If the user presses 'enter', the 'dev' will be returned. In the case of an error it will Fatal.

func InputYN

func InputYN(def bool, args ...interface{}) bool

InputYN asks a Yes/No question. Anything else than upper/lower-case 'y' will be interpreted as no.

func Inputf

func Inputf(def string, f string, args ...interface{}) string

Inputf takes a format string and arguments and calls Input.

func InteractiveConfig

func InteractiveConfig(suite network.Suite, binaryName string)

InteractiveConfig uses stdin to get the [address:]PORT of the server. If no address is given, portscan is used to find the public IP. In case no public IP can be configured, localhost will be used. If everything is OK, the configuration-files will be written. In case of an error this method Fatals.

func RunServer

func RunServer(configFilename string)

RunServer starts a conode with the given config file name. It can be used by different apps (like CoSi, for example)

func TildeToHome

func TildeToHome(path string) string

TildeToHome takes a path and replaces an eventual "~" with the home-directory. If the user-directory is not defined it will return a path relative to the root-directory "/".

Types

type CertificateURL

type CertificateURL string

CertificateURL contains the CertificateURLType and the actual URL certificate, which can be a path leading to a file containing a certificate or a string directly being a certificate.

func (CertificateURL) CertificateURLType

func (cu CertificateURL) CertificateURLType() CertificateURLType

CertificateURLType returns the CertificateURL type from the CertificateURL. It returns InvalidCertificateURLType if the CertificateURL is not valid or if the CertificateURL type is not known.

func (CertificateURL) Content

func (cu CertificateURL) Content() ([]byte, error)

Content returns the bytes representing the certificate.

func (CertificateURL) String

func (cu CertificateURL) String() string

String returns the CertificateURL as a string.

func (CertificateURL) Valid

func (cu CertificateURL) Valid() bool

Valid returns true if the CertificateURL is well formed or false otherwise.

type CertificateURLType

type CertificateURLType string

CertificateURLType represents the type of a CertificateURL. The supported types are defined as constants of type CertificateURLType.

type CothorityConfig

type CothorityConfig struct {
	Suite                      string
	Public                     string
	Services                   map[string]ServiceConfig
	Private                    string
	Address                    network.Address
	ListenAddress              string
	Description                string
	URL                        string
	WebSocketTLSCertificate    CertificateURL
	WebSocketTLSCertificateKey CertificateURL
}

CothorityConfig is the configuration structure of the cothority daemon. - Suite: The cryptographic suite - Public: The public key - Private: The Private key - Address: The external address of the conode, used by others to connect to this one - ListenAddress: The address this conode is listening on - Description: The description - URL: The URL where this server can be contacted externally. - WebSocketTLSCertificate: TLS certificate for the WebSocket - WebSocketTLSCertificateKey: TLS certificate key for the WebSocket

func LoadCothority

func LoadCothority(file string) (*CothorityConfig, error)

LoadCothority loads a conode config from the given file.

func ParseCothority

func ParseCothority(file string) (*CothorityConfig, *onet.Server, error)

ParseCothority parses the config file into a CothorityConfig. It returns the CothorityConfig, the Host so we can already use it, and an error if the file is inaccessible or has wrong values in it.

func (*CothorityConfig) GetServerIdentity

func (hc *CothorityConfig) GetServerIdentity() (*network.ServerIdentity, error)

GetServerIdentity will convert a CothorityConfig into a *network.ServerIdentity. It can give an error if there is a problem parsing the strings from the CothorityConfig.

func (*CothorityConfig) Save

func (hc *CothorityConfig) Save(file string) error

Save will save this CothorityConfig to the given file name. It will return an error if the file couldn't be created or if there is an error in the encoding.

type Group

type Group struct {
	Roster      *onet.Roster
	Description map[*network.ServerIdentity]string
}

Group holds the Roster and the server-description.

func ReadGroupDescToml

func ReadGroupDescToml(f io.Reader) (*Group, error)

ReadGroupDescToml reads a group.toml file and returns the list of ServerIdentities and descriptions in the file. If the file couldn't be decoded or doesn't hold valid ServerIdentities, an error is returned.

func (*Group) GetDescription

func (g *Group) GetDescription(e *network.ServerIdentity) string

GetDescription returns the description of a ServerIdentity.

func (*Group) Save

func (g *Group) Save(suite suites.Suite, filename string) error

Save converts the group into a toml structure and save it to the file

func (*Group) Toml

func (g *Group) Toml(suite suites.Suite) (*GroupToml, error)

Toml returns the GroupToml instance of this Group

type GroupToml

type GroupToml struct {
	Servers []*ServerToml `toml:"servers"`
}

GroupToml holds the data of the group.toml file.

func NewGroupToml

func NewGroupToml(servers ...*ServerToml) *GroupToml

NewGroupToml creates a new GroupToml struct from the given ServerTomls. Currently used together with calling String() on the GroupToml to output a snippet which can be used to create a Cothority.

func (*GroupToml) Save

func (gt *GroupToml) Save(fname string) error

Save writes the GroupToml definition into the file given by its name. It will return an error if the file couldn't be created or if writing to it failed.

func (*GroupToml) String

func (gt *GroupToml) String() string

String returns the TOML representation of this GroupToml.

type ServerServiceConfig

type ServerServiceConfig struct {
	Public string
	Suite  string
}

ServerServiceConfig is a public configuration for a server (i.e. private key is missing)

type ServerToml

type ServerToml struct {
	Address     network.Address
	Suite       string
	Public      string
	Description string
	Services    map[string]ServerServiceConfig
	URL         string `toml:"URL,omitempty"`
}

ServerToml is one entry in the group.toml file describing one server to use for the cothority.

func NewServerToml

func NewServerToml(suite network.Suite, public kyber.Point, addr network.Address,
	desc string, services map[string]ServiceConfig) *ServerToml

NewServerToml takes a public key and an address and returns the corresponding ServerToml. If an error occurs, it will be printed to StdErr and nil is returned.

func (*ServerToml) String

func (s *ServerToml) String() string

String returns the TOML representation of the ServerToml.

func (*ServerToml) ToServerIdentity

func (s *ServerToml) ToServerIdentity() (*network.ServerIdentity, error)

ToServerIdentity converts this ServerToml struct to a ServerIdentity.

type ServiceConfig

type ServiceConfig struct {
	Suite   string
	Public  string
	Private string
}

ServiceConfig is the configuration of a specific service to override default parameters as the key pair

Jump to

Keyboard shortcuts

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