Skip to content

0chain_unit_tests

hm90121 edited this page Jul 15, 2021 · 1 revision

Unit tests

0chain unit tests verify the behavior of individual parts of the program. Config for the base docker image can be provided on run to execute general unit tests.

unit testing uml

Getting started

Prerequisites

Docker and Git must be installed to run the tests.

Install Git using the following command:

sudo apt install git

Docker installation instructions can be found here.

Cloning the repository and Building Base Image

Clone the 0chain repository:

git clone https://github.com/0chain/0chain.git

Navigate to the 0chain folder and run the script to build a base docker image for unit testing :

cd 0chain
./docker.local/bin/build.base.sh

The base image includes all the dependencies required to test the 0chain code.

Running Tests

Now run the script containing unit tests.

./docker.local/bin/unit_test.sh 

The list of packages is optional, and if provided runs only the tests from those packages. Command for running unit tests with specific packages.

./docker.local/bin/unit_test.sh [<packages>]

Testing Steps

Unit testing happens over a series of steps one after the other.

Step 1: FROM zchain_build_base

This FROMstep does the required preparation and specifies the underlying OS architecture to use the build image. Here we are using the base image created in the build phase.

Step 2: ENV SRC_DIR=/0chain

The SRC_DIR variable is a reference to a file path that contains the code from your pull request. Here /0chain directory is specified as it is the one that was cloned.

Step 3: Setting the GO111Module variable to ON

GO111MODULE is an environment variable that can be set when using go for changing how Go imports packages. It was introduced to help ensure a smooth transition to the module system.

GO111MODULE=on will force using Go modules even if the project is in your GOPATH. Requires go.mod to work.

Note: The default behavior in Go 1.16 is now GO111MODULE=on

Step 4: COPY ./code/go/0chain.net $SRC_DIR/go/0chain.net

This step copies the code from the source path to the destination path.

Step 5: RUN cd $SRC_DIR/go/0chain.net && go mod download

The RUN command is an image build step that allows installing of applications and packages requited for testing while thego mod download downloads the specific module versions you've specified in the go.mod file.

Step 6: RUN cd $GOPATH/pkg/mod/github.com/valyala/gozstd@v1.5. && chmod -R +w . && make clean libzstd.a

This step runs the gozstd package and provides write permissions to the directory. gozstd which is a go wrapper for zstd (library) provides Go bindings for the libzstd C library. The make clean is ran in the last to clean up the code and remove all the compiled object files from the source code

Step 7: WORKDIR $SRC_DIR/go

This step defines the working directory for running unit tests which is (0chain/code/go/0chain.net/).For all the running general unit tests their code coverage will be defined in the terminal like this

ok      0chain.net/chaincore/block      0.128s  coverage: 98.9% of statements

The above output shows 98.9% of code statements was covered with tests.

Here is a sample output for all the unit test cases:

?       0chain.net/chaincore    [no test files]
ok      0chain.net/chaincore/block      0.128s  coverage: 98.9% of statements
?       0chain.net/chaincore/block/magicBlock   [no test files]
ok      0chain.net/chaincore/chain      0.254s  coverage: 6.0% of statements
?       0chain.net/chaincore/chain/state        [no test files]
ok      0chain.net/chaincore/client     0.328s  coverage: 30.8% of statements
?       0chain.net/chaincore/config     [no test files]
?       0chain.net/chaincore/diagnostics        [no test files]
ok      0chain.net/chaincore/httpclientutil     2.048s  coverage: 91.7% of statements
ok      0chain.net/chaincore/node       0.011s  coverage: 8.9% of statements
ok      0chain.net/chaincore/round      0.048s  coverage: 97.1% of statements
ok      0chain.net/chaincore/smartcontract      0.032s  coverage: 9.1% of statements                                                                             
ok      0chain.net/chaincore/smartcontractinterface     0.032s  coverage: 97.3%                                                                               
?       0chain.net/chaincore/state      [no test files]
ok      0chain.net/chaincore/threshold/bls      9.912s  coverage: 1.1% of statem                                                                             
ok      0chain.net/chaincore/tokenpool  10.034s coverage: 100.0% of statements
ok      0chain.net/chaincore/transaction        0.029s  coverage: 0.4% of statements [no tests to run]
ok      0chain.net/chaincore/wallet     6.600s  coverage: 40.0% of statements
?       0chain.net/conductor    [no test files]
?       0chain.net/conductor/conductor  [no test files]
?       0chain.net/conductor/conductrpc [no test files]
?       0chain.net/conductor/config     [no test files]
?       0chain.net/conductor/sdkproxy   [no test files]
?       0chain.net/conductor/utils      [no test files]
?       0chain.net/core [no test files]
?       0chain.net/core/build   [no test files]
ok      0chain.net/core/cache   0.004s  coverage: 100.0% of statements
ok      0chain.net/core/common  0.238s  coverage: 87.4% of statements
ok      0chain.net/core/datastore       0.033s  coverage: 92.0% of statements
ok      0chain.net/core/ememorystore    1.018s  coverage: 91.7% of statements
ok      0chain.net/core/encryption      1.290s  coverage: 95.3% of statements
?       0chain.net/core/encryption/keys [no test files]
ok      0chain.net/core/logging 0.069s  coverage: 96.5% of statements
ok      0chain.net/core/memorystore     0.281s  coverage: 93.8% of statements
?       0chain.net/core/metric  [no test files]
ok      0chain.net/core/persistencestore        0.036s  coverage: 73.5% of statements
ok      0chain.net/core/util    22.237s coverage: 76.5% of statements
ok      0chain.net/miner        0.303s  coverage: 8.0% of statements
?       0chain.net/miner/miner  [no test files]
?       0chain.net/miner/mocks  [no test files]
?       0chain.net/mocks        [no test files]
?       0chain.net/mocks/core/datastore [no test files]
?       0chain.net/mocks/core/encryption        [no test files]
ok      0chain.net/sharder      0.168s  coverage: 20.8% of statements
ok      0chain.net/sharder/blockdb      0.004s  coverage: 79.3% of statements
ok      0chain.net/sharder/blockstore   0.045s  coverage: 79.7% of statements
?       0chain.net/sharder/sharder      [no test files]
?       0chain.net/smartcontract        [no test files]
?       0chain.net/smartcontract/faucetsc       [no test files]
ok      0chain.net/smartcontract/interestpoolsc 0.030s  coverage: 45.0% of statements
ok      0chain.net/smartcontract/minersc        0.104s  coverage: 30.9% of statements
?       0chain.net/smartcontract/multisigsc     [no test files]
?       0chain.net/smartcontract/multisigsc/test        [no test files]
?       0chain.net/smartcontract/setupsc        [no test files]
ok      0chain.net/smartcontract/storagesc      1.877s  coverage: 58.8% of statements
ok      0chain.net/smartcontract/vestingsc      0.034s  coverage: 81.8% of statements
ok      0chain.net/smartcontract/zrc20sc        0.030s  coverage: 23.3% of statements