Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Merge branch 'cudos-dev' into CUDOS-505
Browse files Browse the repository at this point in the history
  • Loading branch information
aemil145 committed Jan 25, 2022
2 parents 8eaa3aa + 58fc4da commit 7c25db8
Show file tree
Hide file tree
Showing 22 changed files with 615 additions and 24 deletions.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,80 @@ npm install -g

---

### Running tests

```bash
# Run all tests
npm run test
```
```bash
# Run specific test
npm run test -- init.test.sh
```

---

### Writing integration tests

```bash
# it is recommended to execute npm run test before reading this to get a general idea of the behavior

# lets take a look at init.test.sh
# the command should initialize a project inside the current directory
# the test follows the classic Arrange-Act-Assert pattern

# our files start with #!/bin/bash, which tells your terminal it should use bash to execute the file
#!/bin/bash

# source lets you use the content of a file
# ex. INIT_FOLDER, TEMPLATE_FILES
source ./packages/cudos-tests/integration-tests/vars.sh

# echo prints out the strings that are passed to it
# -n flag tells your terminal to stay on the same line after printing out the message
echo -n 'cudos init...'

# ARRANGE
# mkdir creates a folder at the path specified in INIT_FOLDER variable
# cd moves to the specified directory
# && lets you execute the command that follows the && only if the first command is successful
mkdir $INIT_FOLDER && cd $INIT_FOLDER

# ACT
# &> /dev/null hides the output of the command
cudos init &> /dev/null

# ASSERT
# ls -R lists directory content.
# `` executes the command placed inside and provides its output
# we compare the output with the expected folder structure defined in TEMPLATE_FILES
if [[ ! `ls -R` == $TEMPLATE_FILES ]]; then
# if the output doesn't match we print out a fail message
# FAILED variable defines a red colored message
# -e flag escapes special characters, we need it in order to have colored messages
# 1>&2 redirects the output to stderr
echo -e "$FAILED\nGenerated folder is invalid!" 1>&2

# we define a variable with status 1
# in bash status code 1 means the script was not successful
exit_status=1
else
# otherwise we print pass message
echo -e $PASSED
fi

# we cleanup the files generated by the test
# rm -r removes the specified directory
rm -r ../$INIT_FOLDER &> /dev/null

# we exit the script
# if the test fails and exit_status is assigned on line40 the program will exit with status 1
# otherwise the exit_status will be undefined and the program will exit without a status, which means it was successful
exit $exit_status
```

---

### Network selection

todo
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.8.1",
"description": "",
"main": "./packages/blast-cmd/blast-cmd.js",
"scripts": {
"test": "./packages/cudos-tests/integration-tests/run-tests.sh"
},
"bin": {
"blast": "./packages/blast-cmd/blast-cmd.js"
},
Expand Down
21 changes: 20 additions & 1 deletion packages/blast-cmd/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ const testInfo = {
const unitTestInfo = {
command: 'unittest',
describe: 'Runs the unit tests of the smart contracts',
builder: (yargs) => {},
builder: (yargs) => {
yargs.option('quiet', {
alias: 'q',
type: 'boolean',
default: false,
description: 'Do not print cargo log messages.'
})
},
handler: unitTestCmd
}

Expand All @@ -56,6 +63,18 @@ const runInfo = {
type: 'string',
describe: 'The path to the script to run'
})
yargs.option('network', {
alias: 'n',
type: 'string',
default: false,
description: 'Option to set a custom network.'
})
yargs.option('account', {
alias: 'a',
type: 'string',
default: false,
description: 'Option to set a custom account for signer. Account name is expected.'
})
},
handler: runCmd
}
Expand Down
3 changes: 3 additions & 0 deletions packages/blast-cmd/run/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ const {
getContractFactory,
getContractFromAddress
} = require('../../blast-utilities/contract-utils')
const { setClient } = require('../../blast-utilities/client.js')
const BlastError = require('../../blast-utilities/blast-error')

global.getContractFactory = getContractFactory
global.getContractFromAddress = getContractFromAddress

async function runCmd(argv) {
await setClient(argv.account, argv.network)

if (!fs.existsSync(`${path.resolve('.')}/${argv.scriptFilePath}`)) {
throw new BlastError(`Script at location ${path.resolve('.')}/${argv.scriptFilePath} does not exist.`)
}
Expand Down
9 changes: 6 additions & 3 deletions packages/blast-cmd/unittest/unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ const path = require('path')

const { executeRun } = require('../../blast-utilities/run-docker-commands')

function runUnitTests() {
function runUnitTests(argv) {
// TODO: the slimbuster img is 604 mb, can we reuse the rust-optimizer to call the test? - So far could not make it
// work on each test run the docker is downloading the packages again, how can we cache them?
const cmd = `-v "${path.resolve('.')}":/usr/src/cudos-blast -w /usr/src/cudos-blast ` +
let cmd = `-v "${path.resolve('.')}":/usr/src/cudos-blast -w /usr/src/cudos-blast ` +
'rust:1.55-slim-buster cargo test --lib'
if (argv.quiet) {
cmd += ' -q'
}
executeRun(cmd)
}

async function unitTestCmd(argv) {
console.log('Running contract unit tests...')
runUnitTests()
runUnitTests(argv)
}

module.exports = { unitTestCmd: unitTestCmd }
34 changes: 34 additions & 0 deletions packages/blast-utilities/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { SigningCosmWasmClient } = require('cudosjs')

const {
getEndpoint,
getNetwork,
getDefaultAccount
} = require('./config-utils.js')

const { getSigner } = require('./keypair.js')

let client

async function setClient(accountName, network) {
if (!accountName) {
accountName = await getDefaultAccount()
}
if (!network) {
network = await getNetwork()
}

const endpoint = await getEndpoint()
const wallet = await getSigner(accountName, network)
client = await SigningCosmWasmClient.connectWithSigner(endpoint, wallet)
client.name = accountName
}

function getClient() {
return client
}

module.exports = {
setClient: setClient,
getClient: getClient
}
24 changes: 23 additions & 1 deletion packages/blast-utilities/config-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,30 @@ function getGasPrice() {
return config.gasPrice
}

async function getNetwork() {
const { config } = await getConfig()

if (!config.network) {
throw new CudosError('Missing network in the config file.')
}

return config.network
}

async function getDefaultAccount() {
const { config } = await getConfig()

if (!config.defaultAccount) {
throw new CudosError('Missing defaultAccount in the config file.')
}

return config.defaultAccount
}

module.exports = {
getAccountByName: getAccountByName,
getEndpoint: getEndpoint,
getGasPrice: getGasPrice
getGasPrice: getGasPrice,
getNetwork: getNetwork,
getDefaultAccount: getDefaultAccount
}
26 changes: 7 additions & 19 deletions packages/blast-utilities/contract-utils.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
const { SigningCosmWasmClient } = require('cudosjs')
const {
GasPrice,
calculateFee
} = require('cudosjs')
const path = require('path')
const fs = require('fs')

const {
getEndpoint,
getGasPrice
} = require('./config-utils.js')
const {
getSigner,
getAccountAddress
} = require('./keypair.js')
const { getGasPrice } = require('./config-utils.js')
const { getAccountAddress } = require('./keypair.js')
const { getClient } = require('./client.js')
const BlastError = require('./blast-error')

async function getClient() {
const endpoint = getEndpoint()
// TODO: pass account and network as a param
const wallet = await getSigner('account1', 'cudos')
return await SigningCosmWasmClient.connectWithSigner(endpoint, wallet)
}

const Contract = class {
constructor(contractname, initMsg, label) {
this.contractname = contractname
this.initMsg = initMsg
this.label = label || contractname
this.client = {}
}

async init() {
this.client = getClient()

if (!this.deployed) {
this.wasmPath = path.join(process.cwd(), `artifacts/${this.contractname}.wasm`)
if (!fs.existsSync(this.wasmPath)) {
Expand All @@ -39,9 +29,7 @@ const Contract = class {
}

this.gasPrice = GasPrice.fromString(await getGasPrice())

this.client = await getClient()
this.defaultAccount = await getAccountAddress('account1')
this.defaultAccount = await getAccountAddress(this.client.name)

return this
}
Expand Down
54 changes: 54 additions & 0 deletions packages/cudos-tests/integration-tests/run-single-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
source ./packages/cudos-tests/integration-tests/vars.sh
compose='docker compose -f ./packages/cudos-config/docker-compose-start.yaml -f ./packages/cudos-config/docker-compose-init.yaml'
start_node() {
$compose up --build -d &> /dev/null
timer=45
sleep $timer
until [[ `$COMPOSE cudos-noded q block` =~ $VALID_BLOCK_STATUS ]]; do
sleep $timer
done;
}

if [[ ! `ls -a $TESTS_FOLDER` =~ $1 ]]; then
echo 'Invalid test file!'
exit 1
fi

if [[ $1 == 'node-start-status.test.sh' ]]; then
if [[ `docker ps` =~ $CONTAINER_NAME ]]; then
echo 'Node is started. Your node will be stopped and started again after the test is executed...'
$compose down &> /dev/null && sleep 5
node_stopped=true
fi
$TESTS_FOLDER/$1
exit_status=$?
if [[ $node_stopped == true && $exit_status == 1 ]]; then
echo 'Getting your node back up...'
start_node
fi
exit $exit_status
fi

if [[ ! `docker ps` =~ $CONTAINER_NAME && ($1 =~ 'keys' || $1 =~ 'node-stop') ]]; then
echo 'Node is not started. Your node will be started and stopped after the test is executed.'
start_node
node_started=true
fi
if [[ $1 =~ 'node-stop' && ! $node_started == true ]]; then
echo 'Node is started. Your node will be stopped and started after the test is executed'
node_stopped=true
fi
echo "Executing $1..."
$TESTS_FOLDER/$1
exit_status=$?

if [[ $node_started == true && `docker ps` =~ $CONTAINER_NAME ]]; then
echo 'Stopping the node...'
$compose down &> /dev/null && sleep 5
elif [[ $node_stopped == true && ! `docker ps` == $CONTAINER_NAME ]]; then
echo 'Getting your node back up...'
start_node
fi

exit $exit_status
55 changes: 55 additions & 0 deletions packages/cudos-tests/integration-tests/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
source ./packages/cudos-tests/integration-tests/vars.sh
compose='docker compose -f ./packages/cudos-config/docker-compose-start.yaml -f ./packages/cudos-config/docker-compose-init.yaml'
start_node() {
$compose up --build -d &> /dev/null
timer=45
sleep $timer
until [[ `$COMPOSE cudos-noded q block` =~ $VALID_BLOCK_STATUS ]]; do
sleep $timer
done;
}

if [[ $1 ]]; then
./packages/cudos-tests/integration-tests/run-single-test.sh $1
exit $?
fi

if [[ `docker ps` =~ $CONTAINER_NAME ]]; then
echo 'Node is started. Your node will be stopped and started again after the tests are executed...'
$compose down &> /dev/null && sleep 5
node_stopped=true
fi

echo 'Executing node-start-status.test.sh...'
$TESTS_FOLDER/node-start-status.test.sh
if [[ $? == 1 ]]; then
exit_status=1
start_node
fi

for test in $TESTS_FOLDER/*.test.sh; do
if [[ ! $test =~ 'node' ]]; then
split=(${test//// })
file_name=${split[5]}
echo "Executing $file_name..."
$test
if [[ $? == 1 ]]; then
exit_status=1
fi
fi
done

echo 'Executing node-stop-status.test.sh...'
$TESTS_FOLDER/node-stop-status.test.sh
if [[ $? == 1 ]]; then
exit_status=1
fi
if [[ ! $node_stopped == true && `docker ps` =~ $CONTAINER_NAME ]]; then
$compose down &> /dev/null && sleep 5
elif [[ $node_stopped == true && ! `docker ps` =~ $CONTAINER_NAME ]]; then
echo 'Getting your node back up...'
start_node
fi

exit $exit_status
Loading

0 comments on commit 7c25db8

Please sign in to comment.