Skip to content

Commit

Permalink
Tests for site ingestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio-Oliveira-Encora committed Apr 16, 2024
1 parent e48b7ee commit 5292662
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 79 deletions.
72 changes: 1 addition & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1 @@
# Tests
This directory contains integrations tests that can be run against the Diode Plugin


Here's what you'll need to do in order to run these tests:
- Start the Docker container for Diode Plugin
- Create the user and his token
- Configure the test settings
- Run behave


## Start the Docker container for Diode Plugin

To run the tests, you must have the diode plugin directory and execute the following command in diode/diode-server folder:

```bash
make docker-compose-up
```

## Create the user and his token

To create the user, execute:

```bash
docker exec -it diode-netbox-1 /opt/netbox/netbox/manage.py createsuperuser
```
Fill the username and password for the superuser as requested.

With this user, you can access the Netbox at http://0.0.0.0:8000/ and using the menu Admin -> API Token, you can create the token for this user.

## Test settings
Create the test config file from the template: `cp config.ini.tpl config.ini`.

Then fill in the correct values:

- **user_token**:
- Mandatory!
- string
- user token created in the previous step

- **api_root_path**:
- Mandatory!
- string
- netbox API URL, e.g. http://0.0.0.0:8000/api


## Run behave using parallel process

You can use [behavex](https://github.com/hrcorval/behavex) to run the scenarios using multiprocess by simply run:

Examples:

> behavex -t @\<TAG\> --parallel-processes=2 --parallel-schema=scenario
> behavex -t @\<TAG\> --parallel-processes=2 --parallel-schema=feature
Running smoke tests:

> behavex -t=@smoke --parallel-processes=2 --parallel-scheme=feature

## Test execution reports
[behavex](https://github.com/hrcorval/behavex) provides a friendly HTML test execution report that contains information related to test scenarios, execution status, execution evidence and metrics. A filters bar is also provided to filter scenarios by name, tag or status.

It should be available at the following path:

<output_folder>/report.html

## Clean your environment

The tests clean up the environment after running, you do not need any manual intervention to clean up the environment.
# diode
71 changes: 71 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Tests
This directory contains integrations tests that can be run against the Diode Plugin


Here's what you'll need to do in order to run these tests:
- Start the Docker container for Diode Plugin
- Create the user and his token
- Configure the test settings
- Run behave


## Start the Docker container for Diode Plugin

To run the tests, you must have the diode plugin directory and execute the following command in diode/diode-server folder:

```bash
make docker-compose-up
```

## Create the user and his token

To create the user, execute:

```bash
docker exec -it diode-netbox-1 /opt/netbox/netbox/manage.py createsuperuser
```
Fill the username and password for the superuser as requested.

With this user, you can access the Netbox at http://0.0.0.0:8000/ and using the menu Admin -> API Token, you can create the token for this user.

## Test settings
Create the test config file from the template: `cp config.ini.tpl config.ini`.

Then fill in the correct values:

- **user_token**:
- Mandatory!
- string
- user token created in the previous step

- **api_root_path**:
- Mandatory!
- string
- netbox API URL, e.g. http://0.0.0.0:8000/api


## Run behave using parallel process

You can use [behavex](https://github.com/hrcorval/behavex) to run the scenarios using multiprocess by simply run:

Examples:

> behavex -t @\<TAG\> --parallel-processes=2 --parallel-schema=scenario
> behavex -t @\<TAG\> --parallel-processes=2 --parallel-schema=feature
Running smoke tests:

> behavex -t=@smoke --parallel-processes=2 --parallel-scheme=feature

## Test execution reports
[behavex](https://github.com/hrcorval/behavex) provides a friendly HTML test execution report that contains information related to test scenarios, execution status, execution evidence and metrics. A filters bar is also provided to filter scenarios by name, tag or status.

It should be available at the following path:

<output_folder>/report.html

## Clean your environment

The tests clean up the environment after running, you do not need any manual intervention to clean up the environment.
7 changes: 7 additions & 0 deletions tests/features/cleanup.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Cleanup tests objects


@cleanup
Scenario: Cleanup of site object
Given the site object "Site A" is deleted
Then the site object is removed from the database
1 change: 1 addition & 0 deletions tests/features/configs.ini.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[tests_config]
user_token = USER_TOKEN_CREATED_ON_NETBOX
api_root_path = NETBOX API URL (e.g. http://localhost:8000/api)
api_key = INGESTION_API_KEY
14 changes: 6 additions & 8 deletions tests/features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ def before_tag(context, tag):


def after_tag(context, tag):
sites_names = []
if tag == "create.object":
sites_names = ["Site-Test"]
if tag == "update.object":
sites_names = ["Site-Test-2"]
if tag == "object.state":
sites_names = ["Site Z", "Site X"]
remove_sites_entry(sites_names)
switcher = {
"create.object": ["Site-Test"],
"update.object": ["Site-Test-2"],
"object.state": ["Site Z", "Site X"],
}
remove_sites_entry(switcher.get(tag, []))
26 changes: 26 additions & 0 deletions tests/features/ingestion_cases_site_objects.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Feature: Tests for ingestion of site objects
Validate the behavior of the ingestion of site objects

@smoke
@ingestion.site
Scenario: Ingestion of new site object
Given a new site "Site A" object
When the site object is ingested
Then the site object is created in the database

@smoke
@ingestion.site
Scenario: Ingestion of existing site object
Given site object already exists in the database
When the site object is ingested
Then do nothing


@smoke
@ingestion.site
Scenario: Ingestion of site object to change existing site object
Given site object with status "planned" and description "some string"
When the site object is ingested with the updates
Then the site object is updated in the database


18 changes: 18 additions & 0 deletions tests/features/steps/cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from behave import given, when, then

from steps.utils import get_site, send_delete_request


@given('the site object "{site_name}" is deleted')
def delete_site_object(context, site_name):
context.site_name = site_name
endpoint = "dcim/sites/"
site = get_site(context.site_name)
send_delete_request(endpoint, site.get("id"))


@then("the site object is removed from the database")
def check_site_object_deleted(context):
"""Check if the response status code is 200 and the result is success"""
site = get_site(context.site_name)
assert site is None
1 change: 1 addition & 0 deletions tests/features/steps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _read_configs():
configs["user_token"] = configs.get(
"user_token",
)
configs["api_key"] = configs.get("api_key")

return configs

Expand Down
98 changes: 98 additions & 0 deletions tests/features/steps/ingestion_site_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import time

from behave import given, when, then

from netboxlabs.diode.sdk import DiodeClient
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import Entity
from netboxlabs.diode.sdk.diode.v1.site_pb2 import Site

from steps.config import configs

from steps.utils import get_site, send_delete_request


api_key = str(configs["api_key"])


@given('a new site "{site_name}" object')
def step_create_new_site_object(context, site_name):
"""Set the body of the request to create a new site."""
context.site_name = site_name


@when("the site object is ingested")
def ingest_site_object(context):
"""Ingest the site object using the Diode SDK"""
with DiodeClient(
target="localhost:8081",
app_name="my-test-app",
app_version="0.0.1",
api_key=api_key,
) as client:
entities = [
Entity(site=Site(name=context.site_name)),
]

context.response = client.ingest(entities=entities)
return context.response


@then("the site object is created in the database")
@then("do nothing")
def check_site_object(context):
"""Check if the response status code is 200 and the result is success"""
assert context.response is not None
# Wait for the site object to be added to the cache
time.sleep(3)
site = get_site(context.site_name)
assert site.get("name") == context.site_name
assert site.get("status").get("value") == "active"


@given("site object already exists in the database")
def retrieve_existing_site_object(context):
"""Retrieve the site object from the database"""
context.site_name = "Site A"
context.site = get_site(context.site_name)
context.site_name = context.site.get("name")


@given('site object with status "{status}" and description "{description}"')
def create_site_object_to_update(context, status, description):
"""Create a site object with a status and description to update"""
context.site_name = "Site A"
context.status = status
context.description = description


@when("the site object is ingested with the updates")
def update_site_object(context):
"""Update the site object using the Diode SDK"""
with DiodeClient(
target="localhost:8081",
app_name="my-test-app",
app_version="0.0.1",
api_key=api_key,
) as client:
entities = [
Entity(
site=Site(
name=context.site_name,
status=context.status,
description=context.description,
)
),
]

context.response = client.ingest(entities=entities)
return context.response


@then("the site object is updated in the database")
def check_site_object_updated(context):
"""Check if the response status code is 200 and the result is success"""
assert context.response is not None
site = get_site(context.site_name)
assert site.get("name") == context.site_name
assert site.get("status").get("value") == context.status
assert site.get("description") == context.description
9 changes: 9 additions & 0 deletions tests/features/steps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from steps.config import TestConfig


configs = TestConfig.configs()
api_root_path = str(configs["api_root_path"])
token = str(configs["user_token"])
Expand Down Expand Up @@ -58,3 +59,11 @@ def get_site_id(site_name):
.get("id")
)
return site_id


def get_site(name):
endpoint = "dcim/sites/"
site = send_get_request(endpoint, {"name__ic": name}).json().get("results")
if site:
return site[0]
return site

0 comments on commit 5292662

Please sign in to comment.