# XiVO confd

XiVO CONFD is a web server that provides a [RESTful](http://en.wikipedia.org/wiki/Representational_state_transfer)
service for configuring and managing a XiVO server.

## Installing xivo-confd

The server is already provided as a part of [XiVO](http://documentation.xivo.solutions). Please refer
to [the documentation](https://documentation.xivo.solutions/en/stable/xivo/installation/installation.html) for further
details on installing one.

## Running unit tests

1. Run the latest version of the debian-builder container mounting the project directories (HOME by default)

```sh
debuilder pollux
```

1. Launch all of them with tox :

```sh
tox --recreate -e py3
```

### Running integration tests

- If your dev is linked to a branch in provd, db or rabbitmq, export the branch in an env variable (by default it will
   default to master) respectively:
  - DB_TEST_BRANCH
  - PROVD_TEST_BRANCH
  - RABBITMQ_TEST_BRANCH
  - CONFIG_TEST_BRANCH

  for example if you need branch *4436_user_advanced_search* for db:

   ```sh
   export DB_TEST_BRANCH=4436_user_advanced_search
   ```

- always first (only once)

```sh
cd integration_tests
make test-setup
```

- to run everything

```sh
make test
```

- to run specifics - it matters to rebuild xivo-confd-test if confd code changed

  - For Base Tests: in a terminal handle the assets: go to `integration_tests/assets/base`

    - run

      ```sh
      cd ../..; docker build --no-cache -t xivoxc/xivo-confd-test --build-arg VERSION=confd_integration_tests -f Dockerfile .. ; cd assets/base; docker compose down && docker compose run --rm sync; docker compose logs -tf confd
      ```

    - to rerun the tests, you should `ctrl+c` it and rerun this

  - meanwhile in another terminal, run the tests: go to `integration_tests`

    - run the builder

      ```sh
      make run-docker-builder
      ```

    - run

      ```sh
      python3 -m venv /tmp/venv3; \
        source /tmp/venv3/bin/activate; \
        pip install -r test-requirements.txt; \
        cd .. ; python setup.py egg_info ; cd - ;\
        cd suite
      ```

    - then run the integration tests you want (csv import/export tests needs a base one before) them

      ```sh
      python3 -m unittest -fcv base.test_devices base.test_user_import base.test_user_export
      ```

    - as said above, first cancel and restart the process of the assets terminal (recreate the db, etc...), then rerun the unittest command only

  - For Wizards Test: go to `integration_tests`

    - run the builder

      ```sh
      make run-docker-builder
      ```

    - set the environment

      ```sh
      python3 -m venv /tmp/venv3; \
        source /tmp/venv3/bin/activate; \
        pip install -r test-requirements.txt; \
        cd .. ; python setup.py egg_info ; cd - ;\
        cd suite
      ```

    - run the tests

    ```sh
    python3 -m unittest -fcv wizard.test_wizard
    ```

- Run the clean command:

   ```sh
   make distclean
   ```

### Note about integration test

- When migrating from nosetest to unittest we faced several issues.
- The main one was that unittest does not have a before all and after all setup/teardown method (it exists per class, per module (i.e. file) but not for the whole suite)
- Currently, to reproduce the "old way" we tricked unittest with
  - a file `test_aa_start_asset_for_all_tests` :
    - as it is named `test_aa_` we "ensure" it will be discovered first
    - and we have a dummy test case which subclasses the BaseIntegrationTestStart method which will start the asset
  - a file `test_zz_stop_asset_for_all_tests` :
    - as it is named `test_zz_` we "ensure" it will be discovered last
    - and we have a dummy test case which subclasses the BaseIntegrationTestStop method which will stop the asset

**Problem** currently this does not permit to launch only one test.
To launch only one test you must:

- launch the asset manually:
  - cd integration_tests/assets/base
  - docker compose run --rm sync
- then launch the test (via the docker):
  - cd integration_tests/suite
  - python3 -m unittest -fcv base.test_call_logs.TestCallLogs

## Development

### xivo-dao

In case you need to mount xivo_dao inside the xivo-confd container, add the following line in confd volumes in
integration_tests/assets/base/docker-compose.yml

```sh
- "/path/to/xivo_dao:/usr/local/lib/python2.7/site-packages/xivo_dao"
```

### Modified database

If you need to run tests against a modified database schema, run:

```sh
make update-db DB_DIR=../../xivo-db DB_VERSION=2019.12.latest
```
