-
Notifications
You must be signed in to change notification settings - Fork 201
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
Introduce netplan validate #335
Open
daniloegea
wants to merge
5
commits into
canonical:main
Choose a base branch
from
daniloegea:validate_command
base: main
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
084a554
netplan: add "validate" command
daniloegea dcba5b2
tests: add some tests for the new validate command
daniloegea 21c7cfc
misc: update the bash completion script
daniloegea 5b5ac6d
validate: implement support for --debug
daniloegea b90224b
validate: add the netplan-validate man page
daniloegea 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
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,48 @@ | ||
--- | ||
title: netplan-validate | ||
section: 8 | ||
author: | ||
- Danilo Egea Gondolfo ([email protected]) | ||
... | ||
|
||
## NAME | ||
|
||
netplan-validate - parse and validate your configuration without applying it | ||
|
||
## SYNOPSIS | ||
|
||
**netplan** [--debug] **validate** -h | --help | ||
|
||
**netplan** [--debug] **validate** [--root-dir=ROOT_DIR] | ||
|
||
## DESCRIPTION | ||
|
||
**netplan validate** reads and parses all YAML files from ``/{etc,lib,run}/netplan/*.yaml`` and shows any issues found with them. | ||
|
||
It doesn't generate nor apply the configuration to the running system. | ||
|
||
You can specify ``--debug`` to see what files are processed by Netplan in the order they are parsed. | ||
It will also show what files were shadowed, if any. | ||
|
||
For details of the configuration file format, see **netplan**(5). | ||
|
||
## OPTIONS | ||
|
||
-h, --help | ||
: Print basic help. | ||
|
||
--debug | ||
: Print debugging output during the process. | ||
|
||
--root-dir | ||
: Read YAML files from this root instead of / | ||
|
||
## RETURN VALUE | ||
|
||
On success, no issues were found, 0 is returned to the shell. | ||
|
||
On error, 1 is returned. | ||
|
||
## SEE ALSO | ||
|
||
**netplan**(5), **netplan-generate**(8), **netplan-apply**(8) |
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
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
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
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,93 @@ | ||
# Copyright (C) 2023 Canonical, Ltd. | ||
# Author: Danilo Egea Gondolfo <[email protected]> | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; version 3. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
'''netplan validate command line''' | ||
|
||
import glob | ||
import os | ||
|
||
from netplan.cli.utils import NetplanCommand | ||
import netplan.libnetplan as libnetplan | ||
|
||
|
||
class ValidationException(Exception): | ||
pass | ||
|
||
|
||
class NetplanValidate(NetplanCommand): | ||
|
||
def __init__(self): | ||
super().__init__(command_id='validate', | ||
description='Load, parse and validate your configuration without applying it', | ||
leaf=True) | ||
|
||
def run(self): | ||
self.parser.add_argument('--root-dir', default='/', | ||
help='Validate configuration files in this root directory instead of /') | ||
|
||
self.func = self.command_validate | ||
|
||
self.parse_args() | ||
self.run_command() | ||
|
||
def command_validate(self): | ||
|
||
if self.debug: | ||
# Replicates the behavior of src/util.c:netplan_parser_load_yaml_hierarchy() | ||
|
||
lib_glob = 'lib/netplan/*.yaml' | ||
etc_glob = 'etc/netplan/*.yaml' | ||
run_glob = 'run/netplan/*.yaml' | ||
|
||
lib_files = glob.glob(lib_glob, root_dir=self.root_dir) | ||
etc_files = glob.glob(etc_glob, root_dir=self.root_dir) | ||
run_files = glob.glob(run_glob, root_dir=self.root_dir) | ||
|
||
# Order of priority: lib -> etc -> run | ||
files = lib_files + etc_files + run_files | ||
files_dict = {} | ||
shadows = [] | ||
|
||
# Shadowing files with the same name and lower priority | ||
for file in files: | ||
basename = os.path.basename(file) | ||
filepath = os.path.join(self.root_dir, file) | ||
|
||
if key := files_dict.get(basename): | ||
shadows.append((key, filepath)) | ||
|
||
files_dict[basename] = filepath | ||
|
||
files = sorted(files_dict.keys()) | ||
if files: | ||
print('Order in which your files are parsed:') | ||
for file in files: | ||
print(files_dict.get(file)) | ||
|
||
if shadows: | ||
print('\nThe following files were shadowed:') | ||
for shadow in shadows: | ||
print(f'{shadow[0]} shadowed by {shadow[1]}') | ||
|
||
try: | ||
# Parse the full, existing YAML config hierarchy | ||
parser = libnetplan.Parser() | ||
parser.load_yaml_hierarchy(self.root_dir) | ||
|
||
# Validate the final parser state | ||
state = libnetplan.State() | ||
state.import_parser_results(parser) | ||
except Exception as e: | ||
raise ValidationException(e) |
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
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
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,162 @@ | ||
#!/usr/bin/python3 | ||
# Functional tests of netplan CLI. These are run during "make check" and don't | ||
# touch the system configuration at all. | ||
# | ||
# Copyright (C) 2023 Canonical, Ltd. | ||
# Author: Danilo Egea Gondolfo <[email protected]> | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; version 3. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
import unittest | ||
from unittest.mock import patch | ||
import tempfile | ||
import shutil | ||
import sys | ||
|
||
|
||
from netplan.cli.commands.validate import ValidationException | ||
from netplan.cli.core import Netplan | ||
|
||
from tests.test_utils import call_cli | ||
|
||
|
||
class TestValidate(unittest.TestCase): | ||
'''Test netplan set''' | ||
def setUp(self): | ||
self.workdir = tempfile.TemporaryDirectory(prefix='netplan_') | ||
self.file = '70-netplan-set.yaml' | ||
self.path = os.path.join(self.workdir.name, 'etc', 'netplan', self.file) | ||
os.makedirs(os.path.join(self.workdir.name, 'etc', 'netplan')) | ||
|
||
def tearDown(self): | ||
shutil.rmtree(self.workdir.name) | ||
|
||
def _validate(self, debug=False): | ||
args = ['validate', '--root-dir', self.workdir.name] | ||
if debug: | ||
args.append('--debug') | ||
return call_cli(args) | ||
|
||
def test_validate_raises_no_exceptions(self): | ||
with open(self.path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: | ||
dhcp4: false''') | ||
|
||
self._validate() | ||
|
||
def test_validate_raises_exception(self): | ||
with open(self.path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: | ||
dhcp4: nothanks''') | ||
|
||
with self.assertRaises(ValidationException) as e: | ||
self._validate() | ||
self.assertIn('invalid boolean value', str(e.exception)) | ||
|
||
def test_validate_raises_exception_main_function(self): | ||
with open(self.path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: | ||
dhcp4: nothanks''') | ||
|
||
with patch('logging.warning') as log, patch('sys.exit') as exit_mock: | ||
exit_mock.return_value = 1 | ||
|
||
old_argv = sys.argv | ||
args = ['validate', '--root-dir', self.workdir.name] | ||
sys.argv = [old_argv[0]] + args | ||
|
||
Netplan().main() | ||
|
||
# The idea was to capture stderr here but for some reason | ||
# any attempt to mock sys.stderr didn't work with pytest | ||
args = log.call_args.args | ||
self.assertIn('invalid boolean value', args[0]) | ||
|
||
sys.argv = old_argv | ||
|
||
def test_validate_debug_single_file(self): | ||
with open(self.path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: | ||
dhcp4: false''') | ||
|
||
output = self._validate(debug=True) | ||
self.assertIn('70-netplan-set.yaml', output) | ||
|
||
def test_validate_debug_with_shadow(self): | ||
with open(self.path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: | ||
dhcp4: false''') | ||
|
||
path = os.path.join(self.workdir.name, 'run', 'netplan', self.file) | ||
os.makedirs(os.path.join(self.workdir.name, 'run', 'netplan')) | ||
|
||
with open(path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: | ||
dhcp4: false''') | ||
|
||
output = self._validate(debug=True) | ||
|
||
lines = output.split('\n') | ||
highest_priority_file = lines[1] | ||
self.assertIn('run/netplan/70-netplan-set.yaml', highest_priority_file) | ||
|
||
self.assertIn('The following files were shadowed', output) | ||
|
||
def test_validate_debug_order(self): | ||
os.makedirs(os.path.join(self.workdir.name, 'run', 'netplan')) | ||
os.makedirs(os.path.join(self.workdir.name, 'lib', 'netplan')) | ||
|
||
path = os.path.join(self.workdir.name, 'etc', 'netplan', self.file) | ||
with open(self.path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: {}''') | ||
|
||
path = os.path.join(self.workdir.name, 'run', 'netplan', '90-config.yaml') | ||
with open(path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: {}''') | ||
|
||
path = os.path.join(self.workdir.name, 'lib', 'netplan', '99-zzz.yaml') | ||
with open(path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: {}''') | ||
|
||
path = os.path.join(self.workdir.name, 'run', 'netplan', '00-aaa.yaml') | ||
with open(path, 'w') as f: | ||
f.write('''network: | ||
ethernets: | ||
eth0: {}''') | ||
|
||
output = self._validate(debug=True) | ||
lines = output.split('\n') | ||
|
||
self.assertIn('run/netplan/00-aaa.yaml', lines[1]) | ||
self.assertIn('etc/netplan/70-netplan-set.yaml', lines[2]) | ||
self.assertIn('run/netplan/90-config.yaml', lines[3]) | ||
self.assertIn('lib/netplan/99-zzz.yaml', lines[4]) |
Oops, something went wrong.
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.
Is it worth growing generally a machine readable output so callers can invoke this with a
--format=json
or do we really prefer machine-readable consumers really to just use thelibnetplan.State.import_parser_results
directly and process the state errors?