Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 19 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,45 +350,27 @@ Destroying models may result in errors due to foreign key integrity. First delet

## Running tests

The tests in this repository are mainly integration tests, meaning you will need to run them using our preconfigured test server.

1. Ask a core developer for instructions on how to set up test server
credentials on your machine
2. `npm test`

If you wish to run the tests using your own test database instance,

__Set up the database__

1. Go to pgAdmin.
By default, the local database is one of the servers under Server Groups > Servers.
2. Under Login Roles, add a user called ```strongloop```.

__Change configuration for database connection__
## Running tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any reason we remove the info about how to run the tests locally without docker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI=true pre seeds the database. We do not need to do any additional configuration.


In ```test\init.js```, change the value of ```config``` to be pointing to the local database. For example,
### Own instance
If you have a local or remote PostgreSQL instance and would like to use that to run the test suite, use the following command:
- Linux
```bash
POSTGRESQL_HOST=<HOST> POSTGRESQL_USER=<USER> POSTGRESQL_PASSWORD=<PASSWORD> POSTGRESQL_PORT=<PORT> CI=true npm test
```
var config = {
host: 'localhost',
port: '5432',
database:'strongloop',
username: 'postgres',
password: 'postgres',
};
- Windows
```bash
SET POSTGRESQL_HOST=<HOST> SET POSTGRESQL_USER=<USER> SET POSTGRESQL_PASSWORD=<PASSWORD> SET POSTGRESQL_PORT=<PORT> SET CI=true npm test
```

2. (`Linux Only`) `CI=true PGHOST=localhost PGPORT=<pgport> PGDATABASE=<dbname> PGUSER=<username> PGPASSWORD=<password> npm test`

__Troubleshooting__

When running `npm test`, it runs the ```pretest.js``` which eventually runs ```schema.sql``` to set up the database and tables.
If there is problem, you can run the ```schema.sql``` manually. To do this:

1. Go to SQL Shell (psql)
2. Run:
### Docker
If you do not have a local PostgreSQL instance, you can also run the test suite with very minimal requirements.
- Assuming you have [Docker](https://docs.docker.com/engine/installation/) installed, run the following script which would spawn a PostgreSQL instance on your local:
```bash
source setup.sh <USER> <PASSWORD> <PORT>
```
where `<USER>`, `<PASSWORD>` and `<PORT>` are optional parameters. The default values are `root`, `pass` and `5432`.
- Run the test:
```bash
npm test
```
\i <<file path>>

For example on Windows,
\i c:\somepath\test\schema.sql
```
5 changes: 2 additions & 3 deletions pretest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
if (!process.env.TEST_POSTGRESQL_USER && !process.env.CI) {
console.log('not seeding DB with test db');
return;
if (!process.env.CI) {
return console.log('not seeding DB with test db');
}

var fs = require('fs');
Expand Down
100 changes: 100 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

### Shell script to spin up a docker container for postgresql.

## color codes
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
CYAN='\033[1;36m'
PLAIN='\033[0m'

## variables
POSTGRESQL_CONTAINER="postgresql_c"
USER="root"
PASSWORD="pass"
PORT=5432
if [ "$1" ]; then
USER=$1
fi
if [ "$2" ]; then
PASSWORD=$2
fi
if [ "$3" ]; then
PORT=$3
fi

## check if docker exists
printf "\n${RED}>> Checking for docker${PLAIN} ${GREEN}...${PLAIN}"
docker -v > /dev/null 2>&1
DOCKER_EXISTS=$?
if [ "$DOCKER_EXISTS" -ne 0 ]; then
printf "\n${CYAN}Status: ${PLAIN}${RED}Docker not found. Terminating setup.${PLAIN}\n"
exit 1
fi
printf "\n${CYAN}Found docker. Moving on with the setup.${PLAIN}\n"


## cleaning up previous builds
printf "\n${RED}>> Finding old builds and cleaning up${PLAIN} ${GREEN}...${PLAIN}"
docker rm -f $POSTGRESQL_CONTAINER > /dev/null 2>&1
printf "\n${CYAN}Clean up complete.${PLAIN}\n"

## pull latest postgresql image
printf "\n${RED}>> Pulling latest postgresql image${PLAIN} ${GREEN}...${PLAIN}"
docker pull postgres:latest > /dev/null 2>&1
printf "\n${CYAN}Image successfully built.${PLAIN}\n"

## run the postgresql container
printf "\n${RED}>> Starting the postgresql container${PLAIN} ${GREEN}...${PLAIN}"
CONTAINER_STATUS=$(docker run --name $POSTGRESQL_CONTAINER -e POSTGRES_USER=$USER -e POSTGRES_PASSWORD=$PASSWORD -p $PORT:5432 -d postgres:latest 2>&1)
if [[ "$CONTAINER_STATUS" == *"Error"* ]]; then
printf "\n${CYAN}Status: ${PLAIN}${RED}Error starting container. Terminating setup.${PLAIN}\n"
exit 1
fi
docker cp ./test/schema.sql $POSTGRESQL_CONTAINER:/home/ > /dev/null 2>&1
printf "\n${CYAN}Container is up and running.${PLAIN}\n"

## export the schema to the postgresql database
printf "\n${RED}>> Exporting schema to database${PLAIN} ${GREEN}...${PLAIN}\n"

## command to export schema
docker exec -it $POSTGRESQL_CONTAINER /bin/sh -c "psql -U $USER -f /home/schema.sql" > /dev/null 2>&1

## variables needed to health check export schema
OUTPUT=$?
TIMEOUT=120
TIME_PASSED=0
WAIT_STRING="."

printf "\n${GREEN}Waiting for database to respond with updated schema $WAIT_STRING${PLAIN}"
while [ "$OUTPUT" -ne 0 ] && [ "$TIMEOUT" -gt 0 ]
do
docker exec -it $POSTGRESQL_CONTAINER /bin/sh -c "psql -U $USER -f /home/schema.sql" > /dev/null 2>&1
OUTPUT=$?
sleep 1s
TIMEOUT=$((TIMEOUT - 1))
TIME_PASSED=$((TIME_PASSED + 1))

if [ "$TIME_PASSED" -eq 5 ]; then
printf "${GREEN}.${PLAIN}"
TIME_PASSED=0
fi
done

if [ "$TIMEOUT" -le 0 ]; then
printf "\n\n${CYAN}Status: ${PLAIN}${RED}Failed to export schema. Terminating setup.${PLAIN}\n"
exit 1
fi
printf "\n${CYAN}Successfully exported schema to database.${PLAIN}\n"

## set env variables for running test
printf "\n${RED}>> Setting env variables to run test${PLAIN} ${GREEN}...${PLAIN}"
export POSTGRESQL_HOST=0.0.0.0
export POSTGRESQL_PORT=$PORT
export POSTGRESQL_USER=$USER
export POSTGRESQL_PASSWORD=$PASSWORD
printf "\n${CYAN}Env variables set.${PLAIN}\n"

printf "\n${CYAN}Status: ${PLAIN}${GREEN}Set up completed successfully.${PLAIN}\n"
printf "\n${CYAN}To run the test suite:${PLAIN} ${YELLOW}npm test${PLAIN}\n\n"
24 changes: 11 additions & 13 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@ var DataSource = require('loopback-datasource-juggler').DataSource;

var config = require('rc')('loopback', {test: {postgresql: {}}}).test.postgresql;

if (process.env.CI) {
process.env.PGHOST = process.env.POSTGRESQL_HOST ||
process.env.PGHOST = process.env.POSTGRESQL_HOST ||
process.env.PGHOST ||
'localhost';
process.env.PGPORT = process.env.POSTGRESQL_PORT ||
process.env.PGPORT = process.env.POSTGRESQL_PORT ||
process.env.PGPORT ||
5432;
process.env.PGUSER = process.env.POSTGRESQL_USER ||
process.env.PGUSER = process.env.POSTGRESQL_USER ||
process.env.PGUSER ||
'test';
process.env.PGPASSWORD = process.env.POSTGRESQL_PASSWORD ||
process.env.PGPASSWORD = process.env.POSTGRESQL_PASSWORD ||
process.env.PGPASSWORD ||
'';
config = {
host: process.env.PGHOST,
port: process.env.PGPORT,
database: process.env.POSTGRESQL_DATABASE ||
config = {
host: process.env.PGHOST,
port: process.env.PGPORT,
database: process.env.POSTGRESQL_DATABASE ||
process.env.PGDATABASE ||
'emptytest',
username: process.env.PGUSER,
password: process.env.PGPASSWORD,
};
}
username: process.env.PGUSER,
password: process.env.PGPASSWORD,
};

var url = 'postgres://' + (config.username || config.user) + ':' +
config.password + '@' + (config.host || config.hostname) + ':' +
Expand Down