2021-03-16 15:33:42 +00:00
# Testing & Local Development
2021-05-31 18:35:15 +00:00
- [License key ](#license-key )
2021-06-07 23:07:18 +00:00
- [Simulated hosts ](#hosts )
2021-03-21 23:05:11 +00:00
- [Test suite ](#test-suite )
2021-03-11 23:55:58 +00:00
- [End-to-end tests ](#end-to-end-tests )
2021-06-07 20:34:17 +00:00
- [Test hosts ](#test-hosts )
2021-03-11 23:55:58 +00:00
- [Email ](#email )
2021-03-16 15:33:42 +00:00
- [Database backup/restore ](#database-backuprestore )
2021-05-17 23:11:11 +00:00
- [Teams seed data ](#teams-seed-data )
2021-04-13 18:07:39 +00:00
- [MySQL shell ](#mysql-shell )
2021-03-29 20:50:39 +00:00
- [Testing SSO ](#testing-sso )
2020-12-24 22:33:03 +00:00
2021-05-31 18:35:15 +00:00
## License key
Need to test Fleet Basic features locally?
2021-06-23 01:02:40 +00:00
Use the `--dev_license` flag to use the default development license key.
2021-05-31 18:35:15 +00:00
For example:
```
2021-06-23 01:02:40 +00:00
./build/fleet serve --dev --dev_license
2021-05-31 18:35:15 +00:00
```
2021-06-07 23:07:18 +00:00
## Simulated hosts
It can be helpful to quickly populate the UI with simulated hosts when developing or testing features that require host information.
Check out [the instructions in the `/tools/osquery` directory ](../../tools/osquery/README.md#testing-with-containerized-osqueryd ) for starting up simulated hosts in your development environment.
2021-03-11 23:55:58 +00:00
## Test suite
2020-12-24 22:33:03 +00:00
2021-05-31 18:35:15 +00:00
To execute the basic unit and integration tests, run the following from the root of the repository:
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
MYSQL_TEST=1 REDIS_TEST=1 make test
2020-12-24 22:33:03 +00:00
```
It is a good idea to run `make test` before submitting a Pull Request.
#### Go unit tests
To run all Go unit tests, run the following:
```
make test-go
```
2021-03-11 23:55:58 +00:00
#### Go linters
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
To run all Go linters and static analyzers, run the following:
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
make lint-go
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
#### Javascript unit tests
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
To run all JS unit tests, run the following:
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
make test-js
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
or
```
yarn test
```
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
#### Javascript linters
To run all JS linters and static analyzers, run the following:
```
make lint-js
```
or
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
yarn lint
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
#### MySQL tests
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
To run MySQL integration tests set environment variables as follows:
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
MYSQL_TEST=1 make test-go
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
#### Redis tests
To run Redis integration tests set environment variables as follows:
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
REDIS_TEST=1 make test-go
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
#### Email tests
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
To run email related integration tests using MailHog set environment as follows:
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
MAIL_TEST=1 make test-go
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
### Viewing test coverage
2020-12-24 22:33:03 +00:00
When you run `make test` or `make test-go` from the root of the repository, test coverage reports are generated in every subpackage. For example, the `server` subpackage will have a coverage report generated in `./server/server.cover`
To explore a test coverage report on a line-by-line basis in the browser, run the following:
```bash
# substitute ./datastore/datastore.cover, etc
go tool cover -html=./server/server.cover
```
To view test a test coverage report in a terminal, run the following:
```bash
# substitute ./datastore/datastore.cover, etc
go tool cover -func=./server/server.cover
```
2021-03-11 23:55:58 +00:00
## End-to-end tests
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
E2E tests are run using Docker and Cypress.
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
#### Preparation
2020-12-24 22:33:03 +00:00
2021-03-11 23:55:58 +00:00
Make sure dependencies are up to date and the [Fleet binaries are built locally ](./1-Building-Fleet.md ).
2020-12-24 22:33:03 +00:00
2021-06-01 00:07:51 +00:00
For Fleet Core tests:
```
make e2e-reset-db
make e2e-serve-core
```
For Fleet Basic tests:
2020-12-24 22:33:03 +00:00
```
2021-03-11 23:55:58 +00:00
make e2e-reset-db
2021-06-01 00:07:51 +00:00
make e2e-serve-basic
2021-03-11 23:55:58 +00:00
```
This will start a local Fleet server connected to the E2E database. Leave this server running for the duration of end-to-end testing.
```
make e2e-setup
```
This will initialize the E2E instance with a user.
#### Run tests
Tests can be run in interactive mode, or from the command line.
#### Interactive
2021-06-01 00:07:51 +00:00
For Fleet Core tests:
```
2021-07-01 17:32:12 +00:00
yarn cypress:browser-core
2021-06-01 00:07:51 +00:00
```
For Fleet Basic tests:
2021-03-11 23:55:58 +00:00
```
2021-07-01 17:32:12 +00:00
yarn cypress:browser-basic
2021-03-11 23:55:58 +00:00
```
Use the graphical UI controls to run and view tests.
#### Command line
2021-06-01 00:07:51 +00:00
For Fleet Core tests:
```
2021-07-01 17:32:12 +00:00
yarn e2e-cli:core
2021-06-01 00:07:51 +00:00
```
For Fleet Basic tests:
2021-03-11 23:55:58 +00:00
```
2021-07-01 17:32:12 +00:00
yarn e2e-cli:basic
2021-03-11 23:55:58 +00:00
```
Tests will run automatically and results are reported to the shell.
2021-06-07 20:34:17 +00:00
## Test hosts
The Fleet repo includes tools to start test osquery hosts. Please see the documentation in [/tools/osquery ](../../tools/osquery ) for more information.
2021-03-11 23:55:58 +00:00
## Email
#### Manually testing email with MailHog
2021-06-22 21:31:26 +00:00
To intercept sent emails while running a Fleet development environment, first, in the Fleet UI, navigate to the Organization settings page under Admin.
2020-12-24 22:33:03 +00:00
2021-06-17 19:25:42 +00:00
Then, in the "SMTP Options" section, enter any email address in the "Sender Address" field, set the "SMTP Server" to `localhost` on port `1025` , and set "Authentication Type" to `None` . Note that you may use any active or inactive sender address.
Visit [locahost:8025 ](http://localhost:8025 ) to view Mailhog's admin interface which will display all emails sent using the simulated mail server.
2021-03-16 15:33:42 +00:00
## Database Backup/Restore
In the course of development (particularly when crafting database migrations), it may be useful to backup and restore the MySQL database. This can be achieved with the following commands:
Backup:
2021-05-31 18:35:15 +00:00
2021-03-16 15:33:42 +00:00
```
./tools/backup_db/backup.sh
```
The database dump is stored in `backup.sql.gz` .
Restore:
2021-05-31 18:35:15 +00:00
2021-03-16 15:33:42 +00:00
```
./tools/backup_db/restore.sh
```
Note that a "restore" will replace the state of the development database with the state from the backup.
2021-03-29 20:50:39 +00:00
2021-05-17 23:11:11 +00:00
## Teams seed data
2021-05-18 16:40:12 +00:00
When developing on both the `master` and `teams` branches, it may be useful to create seed data that includes users and teams.
Check out this Loom demo video that walks through creating teams seed data:
https://www.loom.com/share/1c41a1540e8f41328a7a6cfc56ad0a01
For a text-based walkthrough, check out the following steps:
2021-05-17 23:11:11 +00:00
First, create a `env` file with the following contents:
2021-05-31 18:35:15 +00:00
2021-05-17 23:11:11 +00:00
```
export SERVER_URL=https://localhost:8080 # your fleet server url and port
export CURL_FLAGS='-k -s' # set insecure flag
export TOKEN=eyJhbGciOi... # your login token
```
Next, set the `FLEET_ENV_PATH` to point to the `env` file. This will let the scripts in the `fleet/` folder source the env file.
```
export FLEET_ENV_PATH=/Users/victor/fleet_env
```
2021-06-07 20:34:17 +00:00
Finally run one of the bash scripts located in the [/tools/api ](../../tools/api/README.md ) directory.
2021-06-01 18:50:07 +00:00
The `fleet/create_core` script will generate an environment to roughly reflect an installation of Fleet Core. The script creates 3 users with different roles.
```
./tools/api/fleet/teams/create_core
```
The `fleet/create_basic` script will generate an environment to roughly reflect an installation of Fleet Basic. The script will create 2 teams 4 users with different roles.
```
./tools/api/fleet/teams/create_basic
```
The `fleet/create_figma` script will generate an environment to reflect the mockups in the Fleet EE (current) Figma file. The script creates 3 teams and 12 users with different roles.
2021-05-17 23:11:11 +00:00
```
2021-06-01 18:50:07 +00:00
./tools/api/fleet/teams/create_figma
2021-05-17 23:11:11 +00:00
```
2021-05-24 21:50:55 +00:00
Each user generated by the script has their password set to `user123#` .
2021-04-13 18:07:39 +00:00
## MySQL shell
Connect to the MySQL shell to view and interact directly with the contents of the development database.
To connect via Docker:
```
docker-compose exec mysql mysql -uroot -ptoor -Dfleet
```
2021-03-29 20:50:39 +00:00
## Testing SSO
Fleet's `docker-compose` file includes a SAML identity provider (IdP) for testing SAML-based SSO locally.
### Configuration
Configure SSO on the Organization Settings page with the following:
```
Identity Provider Name: SimpleSAML
Entity ID: https://localhost:8080
Issuer URI: http://localhost:8080/simplesaml/saml2/idp/SSOService.php
Metadata URL: http://localhost:9080/simplesaml/saml2/idp/metadata.php
```
2021-06-22 21:31:26 +00:00
The identity provider is configured with one user:
2021-03-29 20:50:39 +00:00
```
2021-06-22 21:31:26 +00:00
Username: sso_user
Email: sso_user@example.com
Password: user123#
2021-03-29 20:50:39 +00:00
```
Use the Fleet UI to invite one of these users with the associated email. Be sure the "Enable Single Sign On" box is checked for that user. Now after accepting the invitation, you should be able to log in as that user by clicking "Sign On with SimpleSAML" on the login page.
2021-06-22 21:31:26 +00:00
To add additional users, modify [tools/saml/users.php ](../../tools/saml/users.php ) and restart the `simplesaml` container.