go.dedis.ch/onet/v4@v4.0.0-pre1/app/README.md (about)

     1  Navigation: [DEDIS](https://github.com/dedis/doc/tree/master/README.md) ::
     2  [Onet](../README.md) ::
     3  App support
     4  
     5  # App support
     6  
     7  This is a collection of libraries to support CLI-apps that work with the onet
     8  framework. The go-library simplifies loading and saving of group-configurations
     9  and supports some very basic input/output functions.
    10  
    11  For integration support to test the CLI-apps, `libtest.sh` supports a range
    12  of tests to make sure that the binary behaves as needed.
    13  
    14  # LibTest.sh
    15  
    16  This is a specialized bash-library to handle the following parts of the test:
    17  
    18  * compiling of a conode with the required services
    19  * setting up and removing the directory structure to run the tests
    20  * different test primitives to test failure or success of running the binary
    21  
    22  One goal of `libtest.sh` is to test the binary in a way as close to what a user
    23  would do when running the binary. This leads to a better understanding whether
    24  the binary has a logical setup of the commands and command line options.
    25  
    26  ## Command Line Options
    27  
    28  The default for `libtest.sh`-enabled test is to:
    29  
    30  * compile the binary
    31  * run all the tests in a temporary directory
    32  * delete the temporary directory after the test.
    33  
    34  Two command line options are available to infulence how the test is run:
    35  
    36  * `-nt` - NoTemp - sets up the test-environment in `./build` and doesn't delete
    37  it after the test is done. This is useful if you want to inspect the environment
    38  after the test run. *CAREFUL* because it will *NOT* recompile the binary. You
    39  will use this flag mostly while developing the `test.sh` file, so that you can
    40  quickly test if your new tests are correct or not.
    41  * `-b` - Build - only useful with `-nt` to force a build of the binary. While
    42  developing the tests, in a first time you might want to use `-nt` until your
    43  `test.sh` file is OK, then you will work on your go-code. To recompile the go-code,
    44  use this flag. Once the go-code is OK, you can remove this flag again and work
    45  on the `test.sh` file.
    46  
    47  ## Test.sh setup
    48  
    49  The common name for using `libtest.sh` is a file called `test.sh` in the
    50  same directory as the CLI-binary. The usual structure for this file is:
    51  
    52  * Global variables and inclusion of `libtest.sh`
    53  * main-method setting up the environment and starting the tests
    54  * a list of test-methods
    55  * helper methods
    56  * a call to main, so that the main method is at the top and all necessary
    57  methods are defined before main is called.
    58  
    59  ### Global Variables and inclusion of `libtest.sh`
    60  
    61  | Variable | Default | Explanation |
    62  | -------- | ------- | ----------- |
    63  | `DBG_TEST` | `0` | Show the output of the commands: 0=none, 1=test-names, 2=all |
    64  | `DBG_SRV` | `0` | DBG-level for server, passed as `-debug` to the `conode` command |
    65  | `NBR` | `3` | Highest number of servers and clients |
    66  | `NBR_SERVERS` | `NBR` | How many servers to configure |
    67  | `NBR_SERVERS_GROUP` | `NBR_SERVERS - 1` | How many servers to write to the
    68  group-file - per default, keep one server out of the group, to test what
    69  happens if a server gets added later |
    70  | `APPDIR` | `pwd` | where the `test.sh`-script is located |
    71  | `APP` | `$(basename $APPDIR)` | The name of the builddir |
    72  
    73  All variables can be overriden by defining them _before_ the inclusion of
    74  `libtest.sh`. The most common setup of a `test.sh` only has the first two
    75  global variables, `DBG_TEST` and `DBG_SRV`:
    76  
    77  ```bash
    78  # Show the output of the commands: 0=none, 1=test-names, 2=all
    79  DBG_TEST=1
    80  # DBG-level for call to the app - this is not handled by libtest.sh, but will
    81  # be used in the helper methods.
    82  DBG_APP=2
    83  # DBG-level for server
    84  DBG_SRV=0
    85  
    86  . $(go env GOPATH)/src/github.com/dedis/onet/app/libtest.sh
    87  ```
    88  
    89  If a test fails, it is common to change those variables to:
    90  
    91  ```bash
    92  DBG_TEST=2
    93  DBG_SRV=2
    94  ```
    95  
    96  So that `libtest.sh` shows the output of the CLI command and the `conode` prints
    97  at a debug-level of 2.
    98  
    99  ### `main`
   100  
   101  To have the most important method at the top (for easier editing), `main`
   102  is defined at the beginning. So any edits to tests can be done there.
   103  
   104  A typical main looks like this:
   105  
   106  ```bash
   107  main(){
   108      startTest
   109      buildConode
   110      test Build
   111      test Network
   112      stopTest
   113  }
   114  ```
   115  
   116  * `startTest` cleans the test-directory and builds the CLI binary
   117  * `buildConode` creates a new conode and automatically includes the
   118  service defined in the `./service` or `../service` directory
   119  * `test Build` makes sure all conodes are stopped and removes all databases
   120  from the previous test, then calls `testBuild`
   121  * `test Network` makes sure all conodes are stopped and removes all databases
   122  from the previous test, then calls `testNetwork`
   123  * `stopTest` stops all remaining conodes and cleans up the test-directory, if it
   124  is the temporary directory
   125  
   126  One common use-case of the `main` method in the case of a failing test is to
   127  comment out all tests that run successfully up to the failing test, so that
   128  a subsequent `./test.sh` run only runs the failing test. Calling `./test.sh -nt`
   129  allows for changing the failing `testName` method to find out what is wrong.
   130  
   131  ### Test-methods
   132  
   133  Each test-method usually starts the required conodes before running the tests
   134  necessary to verify the correct running of the service.
   135  
   136  ```bash
   137  testNetwork(){
   138      runCoBG 1 2
   139      testOut "Running network"
   140      testGrep "Available_Services" runCl -g public.toml
   141      testGrep "Available_Services" runCl -g public.toml
   142  }
   143  ```
   144  
   145  * `runCoBG 1 2` starts conode number `1` and `2` (the index starts at 1) in
   146  the background. There is some code to make sure the conodes are actually up
   147  and running and listening on the websocket-ports.
   148  * `testOut` prints the first argument if `DBG_TEST` is greater or equal to `1`
   149  * `testGrep` searches the first argument in the output of the given command
   150  
   151  The whole list of all `test`-commands can be found [here](#test-commands).
   152  
   153  ### Helper methods
   154  
   155  Each `test.sh` will have its own helper methods, but the most common one is
   156  to write something to run your CLI-app:
   157  
   158  ```bash
   159  runCl(){
   160      dbgRun ./$APP -d $DBG_APP $@
   161  }
   162  ```
   163  
   164  This will call your app with the given debugging-option, referenced from the top
   165  of your `test.sh` for easy change in case you need to debug your test.
   166  
   167  Sometimes you might want to give more option, most often the configuration-directory
   168  to be used:
   169  
   170  ```bash
   171  runCl(){
   172  	local CFG=cl$1
   173  	shift
   174  	dbgRun ./$APP -d $DBG_APP -c $CFG $@
   175  }
   176  ```
   177  
   178  This passes the configuration-directory to the app, supposing the app has a
   179  `-c` argument for passing it.
   180  
   181  ### A call to main
   182  
   183  This is very simple:
   184  
   185  ```bash
   186  main
   187  ```
   188  
   189  As bash is an interpreter, it needs to run through all your methods before
   190  being able to call them. And because it is nice to have all the important
   191  methods at the top, we propose this setup.
   192  
   193  ## Test-commands
   194  
   195  Here is an overview of the currently supported test-commands in `libtest.sh`:
   196  
   197  | Command | Arguments | Description |
   198  | ------- | --------- | ----------- |
   199  | `test` | `Name` | Prints `Name`, cleans up build-directory, deletes all databases from services in previous run, and calls `testName` |
   200  | `testOut` | `$@` | Outputs all arguments if `DBT_TEST -ge 1` |
   201  | `dbgOut` | `$@` | Outputs all arguments if `DBT_TEST -ge 2` |
   202  | `dbgRun` | `$@` | Runs `$@` and outputs the result of `$@` if `DBG_TEST -ge 2`. Redirects the output in all cases if `OUTFILE` is set. |
   203  | `testOK` | `$@` | Asserts that the exit-code of  running `$@` using `dbgRun` is `0`. |
   204  | `testFail` | `$@` | Asserts that the exit-code of  running `$@` using `dbgRun` is NOT `0`. |
   205  | `testFile` | `File` | Asserts `File` exists and is a file. |
   206  | `testNFile` | `File` | Asserts that `File` DOES NOT exist. |
   207  | `testFileGrep` | `String` `File` | Asserts that `String` exists in `File`. |
   208  | `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. |
   209  | `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. |
   210  | `testReGrep` | `String` | Asserts `String` is part of the last command being run by `testGrep` or `testNGrep`. |
   211  | `testReNGrep` | `String` | Asserts `String` is NOT part of the last command being run by `testGrep` or `testNGrep`. |
   212  | `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. |