-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added generate_app_test_data management command. #809
Open
gsnider2195
wants to merge
7
commits into
develop
Choose a base branch
from
u/gas-generate-test-data
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
28aa2b5
Added generate_app_test_data management command.
gsnider2195 a01eb5e
Merge branch 'develop' into u/gas-generate-test-data
gsnider2195 5908d8a
docs and changelog
gsnider2195 1b43434
change annotation
gsnider2195 0fcc7e4
rename management command
gsnider2195 1826d1c
Merge remote-tracking branch 'origin/develop' into u/gas-generate-tes…
gsnider2195 0157327
Add GraphQLQuery and GoldenConfigSetting. Add --flush option.
gsnider2195 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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added management command `generate_app_test_data` to generate sample data for development environments. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
nautobot_golden_config/management/commands/generate_app_test_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
"""Generate test data for the Golden Config app.""" | ||
|
||
import random | ||
|
||
from django.core.management import call_command | ||
from django.core.management.base import BaseCommand | ||
from django.db import DEFAULT_DB_ALIAS | ||
from nautobot.core.factory import get_random_instances | ||
from nautobot.dcim.models import Platform | ||
from netutils.lib_mapper import NETUTILSPARSER_LIB_MAPPER_REVERSE | ||
|
||
from nautobot_golden_config.models import ( | ||
ComplianceFeature, | ||
ComplianceRule, | ||
ConfigCompliance, | ||
GoldenConfig, | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Populate the database with various data as a baseline for testing (automated or manual).""" | ||
|
||
help = __doc__ | ||
|
||
def add_arguments(self, parser): # noqa: D102 | ||
parser.add_argument( | ||
"--flush", | ||
action="store_true", | ||
help="Flush any existing data from the database before generating new data.", | ||
) | ||
parser.add_argument( | ||
"--database", | ||
default=DEFAULT_DB_ALIAS, | ||
help='The database to generate the test data in. Defaults to the "default" database.', | ||
) | ||
|
||
def _generate_static_data(self): | ||
platforms = get_random_instances( | ||
Platform.objects.filter(devices__isnull=False).distinct(), | ||
minimum=2, | ||
maximum=4, | ||
) | ||
devices = [p.devices.first() for p in platforms] | ||
|
||
# Ensure platform has a valid network_driver or compliance generation will fail | ||
for platform in platforms: | ||
if platform.network_driver not in NETUTILSPARSER_LIB_MAPPER_REVERSE: | ||
platform.network_driver = random.choice(list(NETUTILSPARSER_LIB_MAPPER_REVERSE.keys())) # noqa: S311 | ||
platform.save() | ||
|
||
# Create ComplianceFeatures | ||
compliance_features = [] | ||
message = "Creating 8 ComplianceFeatures..." | ||
self.stdout.write(message) | ||
for i in range(1, 9): | ||
name = f"ComplianceFeature{i}" | ||
compliance_features.append( | ||
ComplianceFeature.objects.create(name=name, slug=name, description=f"Test ComplianceFeature {i}") | ||
) | ||
|
||
# Create ComplianceRules | ||
count = len(compliance_features) * len(platforms) | ||
message = f"Creating {count} ComplianceRules..." | ||
self.stdout.write(message) | ||
for feature in compliance_features: | ||
for platform in platforms: | ||
ComplianceRule.objects.create( | ||
feature=feature, | ||
platform=platform, | ||
description=f"Test ComplianceRule for {feature.name} on {platform.name}", | ||
match_config=f"match {feature.name} on {platform.name}", | ||
) | ||
|
||
# Create ConfigCompliances | ||
count = len(devices) * len(compliance_features) | ||
message = f"Creating {count} ConfigCompliances..." | ||
self.stdout.write(message) | ||
for device in devices: | ||
for rule in ComplianceRule.objects.filter(platform=device.platform): | ||
is_compliant = random.choice([True, False]) # noqa: S311 | ||
ConfigCompliance.objects.create( | ||
device=device, | ||
rule=rule, | ||
compliance=is_compliant, | ||
compliance_int=int(is_compliant), | ||
intended=rule.match_config, | ||
actual=rule.match_config if is_compliant else f"mismatch {rule.feature.name}", | ||
) | ||
|
||
# Create GoldenConfigs | ||
message = f"Creating {len(devices)} GoldenConfigs..." | ||
self.stdout.write(message) | ||
for device in devices: | ||
GoldenConfig.objects.create( | ||
device=device, | ||
backup_config=f"backup config for {device.name}", | ||
intended_config=f"intended config for {device.name}", | ||
compliance_config=f"compliance config for {device.name}", | ||
) | ||
|
||
# TODO: Create ConfigRemoves | ||
# TODO: Create ConfigReplaces | ||
# TODO: Create RemediationSettings | ||
# TODO: Create ConfigPlans | ||
|
||
def handle(self, *args, **options): | ||
"""Entry point to the management command.""" | ||
# Call nautobot core's generate_test_data command to generate data for core models | ||
call_command("generate_test_data", flush=options["flush"]) | ||
|
||
self._generate_static_data() | ||
|
||
self.stdout.write(self.style.SUCCESS(f"Database {options['database']} populated with app data successfully!")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to call out that this name should be more specific to the particular app. If another app were to implement the same command for their own testing, they would conflict with each other. While Django handles this, this could cause a different generate_app_test_data run than what we may want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like for this to be called by core's
generate_test_data
so hopefully this would move into a separate module and be referenced byNautobotAppConfig
. I don't plan on rolling out this management command to every app.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to
nautobot-server generate_gc_test_data